Python geopandas - 编辑地理数据

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

我试着用.loc编辑shape文件中的地理坐标到合适的单元格中,但每次我都得到同样的错误。TypeError: Value should be either a BaseGeometry or None

我甚至尝试将完全相同的地理坐标粘贴到单元格中,但还是出现了同样的错误。问题出在哪里?

import geopandas as gpd
fp = 'http://gis-lab.info/data/mos-adm/mo.geojson'
map_df = gpd.read_file(fp)
map_df.loc[[145],['geometry']]= 'MULTIPOLYGON (((37.2905 55.80199, 37.29542 55.803, 37.29663 55.8032, 37.29777 55.80335, 37.29864 55.80345, 37.29969 55.80352, 37.30356 55.80356, 37.30327 55.80318, 37.30292 55.80248, 37.30278 55.80127, 37.30235 55.79863, 37.29822 55.79763, 37.29447 55.79672, 37.29441 55.79679, 37.29412 55.79671, 37.29417 55.79663, 37.29321 55.79641, 37.29326 55.79806, 37.2905 55.80199)))'
map_df.plot()
python geospatial edit geopandas
1个回答
1
投票

几何图形在geopandas中存储为:......。shapely.geometry 对象。你试图传递一个字符串(WKT)表示,这就是为什么它会导致上述错误。您必须先将字符串转换为形状的几何体。

from shapely.wkt import loads

string = 'MULTIPOLYGON (((37.2905 55.80199, 37.29542 55.803, 37.29663 55.8032, 37.29777 55.80335, 37.29864 55.80345, 37.29969 55.80352, 37.30356 55.80356, 37.30327 55.80318, 37.30292 55.80248, 37.30278 55.80127, 37.30235 55.79863, 37.29822 55.79763, 37.29447 55.79672, 37.29441 55.79679, 37.29412 55.79671, 37.29417 55.79663, 37.29321 55.79641, 37.29326 55.79806, 37.2905 55.80199)))'
geom = loads(string)
df.loc[145, 'geometry'] = geom

如果您尝试分配多部分几何体,在某些情况下可能会导致 ValueError: Must have equal len keys and value when setting with an iterable 这是熊猫中的一个已知的错误(https:/github.comgeopandasgeopandasissues992。). 变通的办法是通过GeoSeries。

geom = loads(string)
df.loc[145, 'geometry'] = geopandas.GeoSeries([geom]).values
© www.soinside.com 2019 - 2024. All rights reserved.