如何将 shapefile 中的信息从多边形转换为 lat/lon
How to convert the information from a shapefile from polygon to lat/lon
我需要在地图上绘制散点图。我有 shapefile 来绘制地图,但我需要将 shapefile(多边形)的信息转换为 lon 和 lat 坐标。这是我的代码:
borough = gpd.read_file('London_Borough_Excluding_MHW.shp')
borough.crs #{'init': 'epsg:27700'}
crs = {'init': 'epsg:27700'}
gdf = gpd.GeoDataFrame(
df2, crs=crs, geometry=gpd.points_from_xy(df2.lat, df2.lon))
ax = gdf.plot(marker='*', markersize=0.2)
borough.plot(ax=ax)
plt.show
现在我得到了这个情节:
我的 df2 看起来像这样:
$type additionalProperties children childrenUrls commonName id lat lon placeType url
0 Tfl.Api.Presentation.Entities.Place, Tfl.Api.P... [{'$type': 'Tfl.Api.Presentation.Entities.Addi... [] [] River Street , Clerkenwell BikePoints_1 51.529163 -0.109970 BikePoint /Place/BikePoints_1
1 Tfl.Api.Presentation.Entities.Place, Tfl.Api.P... [{'$type': 'Tfl.Api.Presentation.Entities.Addi... [] [] Phillimore Gardens, Kensington BikePoints_2 51.499606 -0.197574 BikePoint /Place/BikePoints_2
我的 shapefile 看起来像这样:
NAME GSS_CODE HECTARES NONLD_AREA ONS_INNER SUB_2009 SUB_2006 geometry
0 Kingston upon Thames E09000021 3726.117 0.000 F None None POLYGON ((516401.6 160201.8, 516407.3 160210.5...
1 Croydon E09000008 8649.441 0.000 F None None POLYGON ((535009.2 159504.7, 535005.5 159502, ...
2 Bromley E09000006 15013.487 0.000 F None None POLYGON ((540373.6 157530.4, 540361.2 157551.9...
我刚刚下载了 shpfile 和散点数据..你只需要正确转换 CRS
仅供参考,数据源指出散点数据采用 WGS1984 (EPSG 4326)
import geopandas as gp
import pandas as pd
s = gp.read_file('London_Borough_Excluding_MHW.shp')
s['geometry']=s['geometry'].to_crs({'init':'epsg:4326'})
df2 = gp.GeoDataFrame(df, crs = {'init':'epsg:4326'}, geometry = gp.points_from_xy(df['lon'],df['lat'])) #where df is your df with the scatter data
ax = s.plot()
df2.plot(ax=ax,color='red')
结果(我没有绘制所有点,需要一个 API 键,所以我只是复制了您的数据片段)
编辑 - 标记自治市镇(您必须格式化以使其更漂亮)
X = s['geometry'].representative_point().x
Y = s['geometry'].representative_point().y
L = s['NAME']
for x,y,l in zip(X,Y,L):
plt.text(x,y,l)
我需要在地图上绘制散点图。我有 shapefile 来绘制地图,但我需要将 shapefile(多边形)的信息转换为 lon 和 lat 坐标。这是我的代码:
borough = gpd.read_file('London_Borough_Excluding_MHW.shp')
borough.crs #{'init': 'epsg:27700'}
crs = {'init': 'epsg:27700'}
gdf = gpd.GeoDataFrame(
df2, crs=crs, geometry=gpd.points_from_xy(df2.lat, df2.lon))
ax = gdf.plot(marker='*', markersize=0.2)
borough.plot(ax=ax)
plt.show
现在我得到了这个情节:
我的 df2 看起来像这样: $type additionalProperties children childrenUrls commonName id lat lon placeType url
0 Tfl.Api.Presentation.Entities.Place, Tfl.Api.P... [{'$type': 'Tfl.Api.Presentation.Entities.Addi... [] [] River Street , Clerkenwell BikePoints_1 51.529163 -0.109970 BikePoint /Place/BikePoints_1
1 Tfl.Api.Presentation.Entities.Place, Tfl.Api.P... [{'$type': 'Tfl.Api.Presentation.Entities.Addi... [] [] Phillimore Gardens, Kensington BikePoints_2 51.499606 -0.197574 BikePoint /Place/BikePoints_2
我的 shapefile 看起来像这样:
NAME GSS_CODE HECTARES NONLD_AREA ONS_INNER SUB_2009 SUB_2006 geometry
0 Kingston upon Thames E09000021 3726.117 0.000 F None None POLYGON ((516401.6 160201.8, 516407.3 160210.5...
1 Croydon E09000008 8649.441 0.000 F None None POLYGON ((535009.2 159504.7, 535005.5 159502, ...
2 Bromley E09000006 15013.487 0.000 F None None POLYGON ((540373.6 157530.4, 540361.2 157551.9...
我刚刚下载了 shpfile 和散点数据..你只需要正确转换 CRS
仅供参考,数据源指出散点数据采用 WGS1984 (EPSG 4326)
import geopandas as gp
import pandas as pd
s = gp.read_file('London_Borough_Excluding_MHW.shp')
s['geometry']=s['geometry'].to_crs({'init':'epsg:4326'})
df2 = gp.GeoDataFrame(df, crs = {'init':'epsg:4326'}, geometry = gp.points_from_xy(df['lon'],df['lat'])) #where df is your df with the scatter data
ax = s.plot()
df2.plot(ax=ax,color='red')
结果(我没有绘制所有点,需要一个 API 键,所以我只是复制了您的数据片段)
编辑 - 标记自治市镇(您必须格式化以使其更漂亮)
X = s['geometry'].representative_point().x
Y = s['geometry'].representative_point().y
L = s['NAME']
for x,y,l in zip(X,Y,L):
plt.text(x,y,l)