skip to Main Content

I’m working with a MongoDB database containing city polygon data. I’ve successfully connected to it from Python and downloaded the data. However, when I convert it into a GeoDataFrame, the geometry column doesn’t seem to be recognized correctly. How can I ensure GeoPandas can utilize the geometry data?

The following code demonstrates the process:


query = {
    'properties.PROVINCIA': 'Ciudad'
}
consult = collection.find(query)
df_result = pd.DataFrame(consult)

df = pd.DataFrame.from_records(df_result['properties'].tolist())
df['geometry'] = df_result['geometry']
df.head()


enter image description here

Thanks!!

2

Answers


  1. I think you need to use a geopandas.GeoDataFrame()

    Something like this:

    df = pd.DataFrame.from_records(df_result['properties'].tolist())
    gdf = geopandas.GeoDataFrame(data=df, geometry=df_result['geometry'])
    
    Login or Signup to reply.
  2. I have no experience in converting data from MongoDB to GeoPandas … but I would try to replace pd.DataFrame.from_records() with geopandas.GeoDataFrame.from_records().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search