【发布时间】:2018-07-11 15:55:05
【问题描述】:
目标: 在同一个 pyplot 图上绘制 MultiPolygon(土地)和 MultiLinestring(河流)。将大地染成白色。
问题: 似乎 MultiLinestring 显示为通过自动关闭其所有 Linestring 以使其成为多边形而构建的 MultiPolygon
Telltale:在将 MultiPolygon 着色为白色时,它不会为由 MultiLinestring 中的 Linestrings 制成的多边形着色
这是可复制的代码:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature
# creates a map
map_projection = ccrs.PlateCarree(central_longitude=0.0, globe=None)
map_figure = plt.figure()
map_subplot = plt.axes(projection=map_projection)
# limits the display bounds of the map
map_subplot.set_extent((5.2, 31.4, 35, 54.3), crs=map_projection)
# adding land from a local shp (source : Natural Earth website)
# facecolor = white
landshpfilename = "Central Europe _ lands minus lakes.shp"
landshapereader = shpreader.Reader(landshpfilename)
landshape_feature = ShapelyFeature(landshapereader.geometries(), map_projection, facecolor='white',edgecolor='black')
map_subplot.add_feature(landshape_feature)
# adding large river from a local shp (source : Natural Earth website)
# edgecolor = blue
largeriversshpfilename = "Central Europe _ large rivers minus lakes.shp"
largeriversshapereader = shpreader.Reader(largeriversshpfilename)
largeriversshape_feature = ShapelyFeature(largeriversshapereader.geometries(), map_projection,edgecolor='blue')
map_subplot.add_feature(largeriversshape_feature)
# verifying the geom_type of the first objects in the shapefiles
# putting it as a title
land_geom_type_text = ' '.join(['lands geom_type :',next(landshape_feature.geometries()).geom_type])
river_geom_type_text = ' '.join(['rivers geom_type :',next(largeriversshape_feature.geometries()).geom_type])
map_figure.suptitle('\n'.join([land_geom_type_text,river_geom_type_text]))
plt.show()
结果如下: rendered map
问题:如何解决?
【问题讨论】:
-
我们无权访问您在代码中引用的 .shp 文件。您可以将它们更改为简单的 geojson 参考吗?您可以使用fiona 读取 shapefile(并将其转换为 json 格式)
-
this question 的回答表明,cartopy 可能会以某种方式将几何图形变成多边形,然后对其进行适当的着色。
-
@Micks Ketches 我不认为 shapefile 在这里是相关的,但为了记录,它们是 naturalearthdata.com/downloads/10m-physical-vectors 的“陆地”和“河流 + 湖泊中心线”的剪辑。另外,看到 gepcel 的回答,我想我必须找到另一种绘制它们的方法,例如从 geojson 或 wkt。
-
@gepcel 感谢您的回答。我从pelson的回答中了解到,我的问题暂时无法解决。您的问题及其答案可以追溯到 2014 年。自那以后您是否向 Cartopy 项目团队提交了问题单?
-
@Tehem 不,我没有提出任何问题。那时,我正在寻找一种绘制点的方法,最终我使用了
plt.scatter。
标签: python matplotlib shapely cartopy