0

我正在尝试在法国地图上绘制一次旅行,但我不能只绘制法国地图(见图),有人可以帮我吗?我相信这可能是上下文库中的 crs 的问题,但我没有在网上找到任何信息这是一个可重现的示例...

import pandas as pd
import geopandas as gpd

from shapely.geometry import Point
import shapely

import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns

import contextily as ctx

def main():
    gdf = pd.DataFrame() 
    gdf["longitudes"] = [5.8127891, 5.2250324]
    gdf["latitudes"] = [46.1965678, 46.2051192]

    gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])
    
    gdf = gpd.GeoDataFrame(gdf, crs= {"init": "epsg:4326"})
        
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    
    france = world.query('name == "France"')

    # Make the plot
    fig, ax = plt.subplots(figsize=(4,5))

    #ax = gdf.plot(figsize=(10, 5))

    # Plot the state outlines
    france.boundary.plot(color='black', linewidth=0.5, ax=ax)


    # Modify projection to match what contextily uses
    gdf = gdf.to_crs(epsg=3857)

    part1 = shapely.geometry.LineString(
        gdf['geometry'].values)
    

    linegdf = gpd.GeoDataFrame(
    {'geometry': [part1]}
    )


    c = to_hex(sns.light_palette('green')[0])

    linegdf.plot(
        color=c,
        linewidth=3,
        ax=ax
    )

    print(len(gdf))

    # Plot points colored by day
    gdf.plot(
        cmap=sns.light_palette('green', as_cmap=True),
        ax=ax,
        markersize=50,
        edgecolor='black', linewidth=0.5,
        zorder=1000 # force the points to be the top layer of the plot
    )

    # Add basemap
    ctx.add_basemap(ax=ax)

    # Remove axes
    ax.set_axis_off()

    plt.show()

main()

我剩下的

任何帮助深表感谢 !

4

1 回答 1

1

两点

  • 法国是一个MutliPolygon,其中一部分在南极洲。已将其从几何中排除
  • 需要与CRS非常一致。另外最好不要使用 legacy 来引用 CRS{"init": "epsg:4326"}
import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns
import pandas as pd
import geopandas as gpd
import shapely.geometry
import contextily as ctx


def main():
    gdf = pd.DataFrame()
    gdf["longitudes"] = [5.8127891, 5.2250324]
    gdf["latitudes"] = [46.1965678, 46.2051192]

    gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])

    # lets project CRS early...
    gdf = gpd.GeoDataFrame(gdf, crs="epsg:4326").to_crs("EPSG:3857")

    world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

    france = world.query('name == "France"')
    # just mainland france, not antartica... plus CRS
    france = (
        france["geometry"]
        .apply(
            lambda mp: shapely.geometry.MultiPolygon(
                [p for p in mp.geoms if p.bounds[1] > 20]
            )
        )
        .to_crs("EPSG:3857")
    )

    # Make the plot
    fig, ax = plt.subplots(figsize=(4, 5))

    # Plot the state outlines
    ax = france.boundary.plot(color="black", linewidth=0.5, ax=ax)

    part1 = shapely.geometry.LineString(gdf["geometry"].values)

    linegdf = gpd.GeoDataFrame({"geometry": [part1]})

    c = to_hex(sns.light_palette("green")[0])

    linegdf.plot(color=c, linewidth=3, ax=ax)

    # Plot points colored by day
    gdf.plot(
        cmap=sns.light_palette("green", as_cmap=True),
        ax=ax,
        markersize=50,
        edgecolor="black",
        linewidth=0.5,
        zorder=1000,  # force the points to be the top layer of the plot
    )

    # Add basemap
    ctx.add_basemap(ax=ax)

    # Remove axes
    ax.set_axis_off()

    plt.show()


main()

在此处输入图像描述

于 2022-01-05T09:57:27.390 回答