我有两个 shapefile。一个是点要素 shapefile,名为“point.shp”,另一个是名为“polygon.shp”的多边形 shapefile。我想使用 cartopy 添加到 map 中。
我设法添加了“polygon.shp”,但失败了“point.shp”。
这是我的代码:
import matplotlib.pyplot as plt
from cartopy import crs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
ax = plt.axes(projection=crs.PlateCarree())
# add the polygon file, worked
ax.add_geometries(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='w')
# or(also worked):
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))
# but these two ways both failed with the "point.shp"
ax.add_geometries(Reader("point.shp").geometries(), crs.PlateCarree())
# or, this doesn't work neither:
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))
有没有人知道如何做到这一点,或者为什么不检索所有点的 x、y 坐标然后绘制它们?
并使用坐标(x,y 值),
ax.plot()
有效,但 ax.scatter()
失败,为什么?谢谢
最佳答案
add_geometries 当前将几何图形转换为多边形,然后对其进行适当的着色,这当然意味着当您传递 add_geometries 点时,多边形是不可见的。 future ,cartopy 可能会在这方面做得更好,但与此同时,听起来您只想使用 scatter 之类的东西来可视化您的数据。
您可以通过从几何体中获取 x 和 y 坐标值并直接传递这些值以使用适当的变换进行散射来实现这一点:
import cartopy.crs as ccrs
import cartopy.io
import matplotlib.pyplot as plt
fname = cartopy.io.shapereader.natural_earth(resolution='10m',
category='cultural',
name='populated_places_simple')
plt.figure(figsize=(12, 6))
ax = plt.axes(projection=ccrs.Robinson())
ax.set_title('Populated places of the world.')
ax.coastlines()
points = list(cartopy.io.shapereader.Reader(fname).geometries())
ax.scatter([point.x for point in points],
[point.y for point in points],
transform=ccrs.Geodetic())
plt.show()
HTH
关于cartopy - 如何使用cartopy将点特征shapefile添加到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340427/