如何将形状文件中的信息从多边形转换为lat/lon

2024-06-25 06:09:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在地图上画一个散点图。我有图形文件来绘制地图,但我需要将图形文件(多边形)的信息转换为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

现在我得到这个情节: plot

我的df2看起来像这样: $type additionalProperties子级childrenurl 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

我的形状文件如下所示:

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...

Tags: noneapitypeplaceaxpresentationentitieslon
1条回答
网友
1楼 · 发布于 2024-06-25 06:09:36

我刚下载了shp文件和散乱数据。你只需要正确地转换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键,所以我只是复制了您的数据片段)

enter image description here

编辑标签行政区(你必须格式化使其更漂亮)

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)

相关问题 更多 >