Geographic Information Systems Asked on December 29, 2021
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:\DATAlayers1\TTL_TRANSPORT.shp"
destination = "D:\DATA\jsonfilessample.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)
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 December 29, 2021
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 December 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP