Geographic Information Systems Asked on April 27, 2021
I have a dataframe with a column called geo_shape
, for each row in this column data is in the format str
with the following structure:
{"type":"MultiPolygon","coordinates":[[[[-99.1634043186724,19.3929362021363],[-99.1633935829153,19.3929814150842],[-99.1633779741792,19.3930471892216],[-99.1633676666966,19.3930905871479],[-99.1633539424123,19.3931484149288],[-99.1633536713463,19.393149556657],[-99.1633326516698,19.3932380913299],[-99.1633273408772,19.3932604765675],[-99.1633258846922,19.3932665988711],[-99.1632503982955,19.393250045508],[-99.1631580388255,19.3932297985256],[-99.1631022520036,19.3932175708424],[-99.1630096356198,19.3931972605697],[-99.1629243185542,19.3931785487074],[-99.1628253831388,19.3931568656532],[-99.1627721849032,19.3931451977826],[-99.1626860494573,19.3931263142213],[-99.1626865729325,19.3931240386763],[-99.162711364006,19.393124012126],[-99.1627524427473,19.3929332906781],[-99.1627565829766,19.3929140659265],[-99.1627814909565,19.3927984103957],[-99.1627980880081,19.3928020859532],[-99.1628764196311,19.3928194161424],[-99.1629163896067,19.3928282573258],[-99.1629931414886,19.3928452352628],[-99.1630458733238,19.3928569030601],[-99.1631201794492,19.3928733390959],[-99.1631648506849,19.3928832187645],[-99.1632414789013,19.3929001785344],[-99.1632705142618,19.3929065993979],[-99.1633527478204,19.3929247873036],[-99.1633697636313,19.3929285531163],[-99.1634043186724,19.3929362021363]]]]}
I want to convert each one of these rows to a MultiPolygon in a GeoDataframe. I have tried passing each str
to a dict
, then to a tuple
and from there try to create the GeoDataframe, but my guess is that there is a quicker way of doing this.
Firstly, you have to create a shapely geometry by using shape function1
Secondly, you must use the geopandas library, specifying your dataframe and geometry column.
import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
# First step
df['geo_shape'] = shape({"type":"MultiPolygon","coordinates":[[[[-99.1634043186724,19.3929362021363],[-99.1633935829153,19.3929814150842],[-99.1633779741792,19.3930471892216],[-99.1633676666966,19.3930905871479],[-99.1633539424123,19.3931484149288],[-99.1633536713463,19.393149556657],[-99.1633326516698,19.3932380913299],[-99.1633273408772,19.3932604765675],[-99.1633258846922,19.3932665988711],[-99.1632503982955,19.393250045508],[-99.1631580388255,19.3932297985256],[-99.1631022520036,19.3932175708424],[-99.1630096356198,19.3931972605697],[-99.1629243185542,19.3931785487074],[-99.1628253831388,19.3931568656532],[-99.1627721849032,19.3931451977826],[-99.1626860494573,19.3931263142213],[-99.1626865729325,19.3931240386763],[-99.162711364006,19.393124012126],[-99.1627524427473,19.3929332906781],[-99.1627565829766,19.3929140659265],[-99.1627814909565,19.3927984103957],[-99.1627980880081,19.3928020859532],[-99.1628764196311,19.3928194161424],[-99.1629163896067,19.3928282573258],[-99.1629931414886,19.3928452352628],[-99.1630458733238,19.3928569030601],[-99.1631201794492,19.3928733390959],[-99.1631648506849,19.3928832187645],[-99.1632414789013,19.3929001785344],[-99.1632705142618,19.3929065993979],[-99.1633527478204,19.3929247873036],[-99.1633697636313,19.3929285531163],[-99.1634043186724,19.3929362021363]]]]})
print(type(df)) # <class 'pandas.core.frame.DataFrame'>
# Second step
gdf = gpd.GeoDataFrame(df, geometry='geo_shape')
print(type(gdf)) # <class 'geopandas.geodataframe.GeoDataFrame'>
Correct answer by Vincent Bré on April 27, 2021
To create a shapely geometry, you can use shape
function
from shapely.geometry import shape
shape({"type":"MultiPolygon","coordinates":[[[[-99.1634043186724,19.3929362021363],[-99.1633935829153,19.3929814150842],[-99.1633779741792,19.3930471892216],[-99.1633676666966,19.3930905871479],[-99.1633539424123,19.3931484149288],[-99.1633536713463,19.393149556657],[-99.1633326516698,19.3932380913299],[-99.1633273408772,19.3932604765675],[-99.1633258846922,19.3932665988711],[-99.1632503982955,19.393250045508],[-99.1631580388255,19.3932297985256],[-99.1631022520036,19.3932175708424],[-99.1630096356198,19.3931972605697],[-99.1629243185542,19.3931785487074],[-99.1628253831388,19.3931568656532],[-99.1627721849032,19.3931451977826],[-99.1626860494573,19.3931263142213],[-99.1626865729325,19.3931240386763],[-99.162711364006,19.393124012126],[-99.1627524427473,19.3929332906781],[-99.1627565829766,19.3929140659265],[-99.1627814909565,19.3927984103957],[-99.1627980880081,19.3928020859532],[-99.1628764196311,19.3928194161424],[-99.1629163896067,19.3928282573258],[-99.1629931414886,19.3928452352628],[-99.1630458733238,19.3928569030601],[-99.1631201794492,19.3928733390959],[-99.1631648506849,19.3928832187645],[-99.1632414789013,19.3929001785344],[-99.1632705142618,19.3929065993979],[-99.1633527478204,19.3929247873036],[-99.1633697636313,19.3929285531163],[-99.1634043186724,19.3929362021363]]]]})
For the whole DataFrame, you pass it to apply.
geometry = df['your_column'].apply(shape)
Answered by martinfleis on April 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP