如何通过geoplot正确投影绘制多个几何图形?

问题描述 投票:0回答:1

我使用如下代码来绘制多个几何图形:

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的投影,我怎样才能让两条线都正确投影?

enter image description here

python matplotlib map-projections geoplot
1个回答
0
投票

对此的回答将是完整的博客文章,解释投影、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)

PyProj PyPi 链接

© www.soinside.com 2019 - 2024. All rights reserved.