我使用如下代码来绘制多个几何图形:
newGdf = gpd.read_file(os.path.abspath(newFile), driver='KML')
oldGdf = gpd.read_file(os.path.abspath(oldFile), driver='KML')
lonMean, LagMean = newGdf.centroid.get_coordinates().mean()
proj = geoplot.crs.Orthographic(central_longitude=lonMean, central_latitude=LagMean)
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(18, 10.5), subplot_kw={"projection": proj})
ax.set_aspect('equal')
newPolyGdf = newGdf[newGdf['geometry'].apply(lambda x: isinstance(x, Polygon))]
newLineGdf = newGdf[newGdf.geometry.type=='LineString']
geoplot.polyplot(newPolyGdf, ax=ax, facecolor=color, zorder=2)
geoplot.polyplot(newLineGdf, ax=ax, facecolor='none', color='blue', zorder=3)
oldPolyGdf = oldGdf[oldGdf['geometry'].apply(lambda x: isinstance(x, Polygon))]
oldLineGdf = oldGdf[oldGdf.geometry.type=='LineString']
geoplot.polyplot(oldPolyGdf, ax=ax, facecolor=color, zorder=2)
geoplot.polyplot(oldLineGdf, ax=ax, facecolor='none', color='blue', zorder=3)
你可以看到postplot oldGdf LineString不正确,我认为原因是使用了newGdf的投影,我怎样才能让两条线都正确投影?
对此的回答将是完整的博客文章,解释投影、CRS 等。简而言之,在使用投影时,使用 PyProj 进行投影转换。 Shapely 进行转换的代码示例:
# change to metric system
project_to_3857 = pyproj.Transformer.from_proj(
pyproj.Proj(init='epsg:4326'), # source coordinate system
pyproj.Proj(init='epsg:3857')) # destination coordinate system
polygon = shapely.ops.transform(project_to_4326.transform, polygon)