【问题标题】:How to convert MULTIPOLYGON xy geometry to latitude and longitude?如何将 MULTIPOLYGON xy 几何转换为纬度和经度?
【发布时间】:2021-10-19 08:31:46
【问题描述】:

我正在使用 python 模块 arcgis 下载 shapefile,位于:https://www.arcgis.com/home/item.html?id=2d5c785555aa4b0b946f1aa61c56274f

我已经设法按照文档将其提取到 pandas 数据框中:

from arcgis import GIS
import pandas as pd
gis = GIS(verify_cert=False,api_key=your_key)

# search for file by name which is National_LHO
search_result = gis.content.search(query="title:National_LHO", item_type="Feature Layer")

# get layer
layer = search_result[0].layers[0]

# dataframe from layer
df= pd.DataFrame.spatial.from_layer(layer)

# check it out
print(df.head())


   FID              LHO   Shape__Area  Shape__Length  \
0    1  Carlow/Kilkenny  7.053876e+09   5.924032e+05   
1    2   Cavan/Monaghan  8.858580e+09   7.801971e+05   
2    3            Clare  9.055446e+09   8.005301e+05   
3    4          Donegal  1.467971e+10   2.135710e+06   
4    5     Dublin North  1.076876e+09   3.327819e+05   

                                               SHAPE  
0  {"rings": [[[-747212.35980769, 6967909.5066712...  
1  {"rings": [[[-781459.713316924, 7249668.124932...  
2  {"rings": [[[-1083308.07544972, 6918940.329570...  
3  {"rings": [[[-912809.697847961, 7265617.367554...  
4  {"rings": [[[-674539.041086896, 7057323.867987...  

然后我将其转换为 geopandas 数据框:

import geopandas as gpd

gdf = gpd.GeoDataFrame(dc_df)


但是 CRS 信息看起来很奇怪。它说边界是(-180.0, -90.0, 180.0, 90.0),但几何坐标要高得多。

print(gdf.crs)

<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
print(gdf.head())

   FID              LHO   Shape__Area  Shape__Length  \
0    1  Carlow/Kilkenny  7.053876e+09   5.924032e+05   
1    2   Cavan/Monaghan  8.858580e+09   7.801971e+05   
2    3            Clare  9.055446e+09   8.005301e+05   
3    4          Donegal  1.467971e+10   2.135710e+06   
4    5     Dublin North  1.076876e+09   3.327819e+05   

                                            geometry  
0  MULTIPOLYGON (((-747212.360 6967909.507, -7459...  
1  MULTIPOLYGON (((-781459.713 7249668.125, -7813...  
2  MULTIPOLYGON (((-1083308.075 6918940.330, -108...  
3  MULTIPOLYGON (((-912809.698 7265617.368, -9127...  
4  MULTIPOLYGON (((-674539.041 7057323.868, -6748...  

有人告诉我,只需使用 to_crs('EPSG:4326') 即可将所有内容转换为纬度和经度,但这不起作用。

gdf.to_crs('EPSG:4326').head()

如您所见,它们仍在 xy 坐标中,而不是长/纬度。

我想不通。我只想要经度和纬度多边形。

有没有人知道这样做?我正在失去理智,为此花了几天时间。我不是地理熊猫专家。

也许是一些有用的信息。我下载的数据有这个空间参考:Spatial Reference: 102100 (3857)

我在转换为地理数据框时尝试使用3857,但出现错误。

gdf = gpd.GeoDataFrame(dc_df,crs='3857')

FutureWarning: CRS mismatch between CRS of the passed geometries and 'crs'. Use 'GeoDataFrame.set_crs(crs, allow_override=True)' to overwrite CRS or 'GeoDataFrame.to_crs(crs)' to reproject geometries. CRS mismatch will raise an error in the future versions of GeoPandas.

【问题讨论】:

    标签: python geopandas


    【解决方案1】:

    它没有转换,因为当您将普通的 pandas 数据帧读入 geopandas 数据帧时,crs 信息丢失了

    from arcgis import GIS
    import pandas as pd
    gis = GIS(verify_cert=False)
    
    # search for file by name which is National_LHO
    search_result = gis.content.search(query="title:National_LHO", item_type="Feature Layer")
    
    # get layer
    layer = search_result[0].layers[0]
    
    # dataframe from layer
    df= pd.DataFrame.spatial.from_layer(layer)
    
    # check it out
    # print(df.head())
    
    import geopandas as gpd
    
    gdf = gpd.GeoDataFrame(df)
    

    如果您执行gdf.crs,则返回空。

    所以crs信息必须手动设置。正如你所提到的,将其设置为 epsg 3857

    gdf = gdf.set_geometry('SHAPE')
    gdf = gdf.set_crs(epsg='3857')
    

    现在你可以做

    gdf.to_crs(epsg='4326').head()
    

    【讨论】:

    • 哇。非常感谢您的快速回复。我尝试了to_crsepsg 的许多变体。我不知道我做错了什么。可能犯了一个新手错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    相关资源
    最近更新 更多