【问题标题】:A Shapely MultiLinestring shows up as a MultiPolygon in Pyplot一个 Shapely MultiLinestring 在 Pyplot 中显示为一个 MultiPolygon
【发布时间】: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


【解决方案1】:

设置 facecolor='none' 将阻止 matplotlib 填充生成的底层路径。一个快速重现的案例:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature


ax = plt.axes(projection=ccrs.PlateCarree())

# Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
states_provinces = cfeature.NaturalEarthFeature(
    category='physical',
    name='rivers_lake_centerlines',
    scale='110m',
    )

ax.add_feature(states_provinces, edgecolor='gray', linewidth=5)
ax.coastlines()

保持其他一切不变,但将特征构造更改为不设置面部颜色会产生所需的结果:

states_provinces = cfeature.NaturalEarthFeature(
    category='physical',
    name='rivers_lake_centerlines',
    scale='110m',
    facecolor='none'
    )

【讨论】:

  • 我设法通过赋予河流多边形与土地相同的面色来获得正确的外观。就我而言,我同时打开了'white' 并且河流多边形没有显示。我认为您的解决方案'none'(而不是我之前尝试过的None)效果更好,因为它是自适应的。但是,我担心在检查点是否在河流线串上方或陆地多边形内部时会遇到一些麻烦,因为河流顶部仍然有自己的多边形。稍后我会谈到这个。
猜你喜欢
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 2016-03-28
相关资源
最近更新 更多