TransWikia.com

Trying to export shapefile attributetable to JSON using GeoPandas without the geometry

Geographic Information Systems Asked by lord corwin on November 16, 2020

I am using GeoPandas’ read_file function to read a shapefile
and to_file function to save to GeoJSON.
This is working fast and good.

But I need to save only the attribute table without the geometry
or with empty geometry.

How can I do that?

import geopandas as gpd
import pandas as pd
import fiona


def main():
filepath = "D:DATAlayers1TTL_TRANSPORT.shp"
destination = "D:DATAjsonfilessample.json"

gdf = gpd.read_file(filepath)

df = pd.DataFrame(gdf)
columns_without_geom = gdf.columns

print( columns_without_geom)

if 'geometry' in columns_without_geom:
   columns_without_geom.drop('geometry')

gdf = gdf[columns_without_geom]
df = pd.DataFrame(gdf)
df.to_json(destination)
df.to_json(destination)

2 Answers

If needed, you can convert your dataframe to pandas dataframe and save it with:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html

so something like:

df = pd.DataFrame(gdf)
df.to_json(output)

You might have to select columns without geometry.

columns_without_geom = gdf.columns
if 'geometry' in columns_without_geom: 
     columns_without_geom.remove('geometry')

gdf = gdf[columns_without_geom]
df = pd.DataFrame(gdf)
df.to_json(output)

Answered by Jan Doležal on November 16, 2020

GeoPandas is an addon to Pandas where a geometry column has special meaning and writing output writes geospatial file formats. If you want to export just the non-spatial data, the easiest way would be to create a plain Pandas DataFrame from your GeoDataframe, so the geometry column loses its special meaning, and then remove it.

To do so in one "step":

df = pd.DataFrame(gdf).drop(columns="geometry")

Then you can use the resulting DataFrame's to_json() as intended.

df.to_json(destination)

Answered by bugmenot123 on November 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP