Reputation: 377
I tried to edit geographic coordinates in shape file using .loc to the proper cell, but everytime I was getting the same error: TypeError: Value should be either a BaseGeometry or None
I even tried to paste into the cell totally the same geographic coordinates, but still had the same error. Where is the problem here?
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()
Upvotes: 1
Views: 1303
Reputation: 7814
Geometry in geopandas in stored as shapely.geometry
objects. You are trying to pass a string (WKT) representation instead, that is why it causes the above mentioned error. You have to first convert your string to shapely geometry.
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
If you try to assign multi-part geometry, it may in some cases lead to ValueError: Must have equal len keys and value when setting with an iterable
which is know bug in pandas (https://github.com/geopandas/geopandas/issues/992). The workaround would be passing it through GeoSeries.
geom = loads(string)
df.loc[145, 'geometry'] = geopandas.GeoSeries([geom]).values
Upvotes: 3