Select to view content in your preferred language

Convert JSON to GeoDataFrame and append both geometry and filename

1095
0
02-03-2022 01:54 AM
TamnaSidhart
New Contributor

I am currently running an experiment on automating a task by merging information from various files types and doing a number of operation to achieve this.

Currently, I am reading a a list of GeoJSONs and trying to build a GeoDataFrame that contains both the geometry and the filename.

Example of file name structure in folder is: event1.json

The desired geodataframe should look like this:

    index     geometry                      event_name
    0         LINESTRING1(coordinates...)   event1
    1         LINESTRING2(coordinates...)   event2
    2         LINESTRING3(coordinates...)   event3

I am able to read the JSONs and create the geometries, but I am not able to assign the matching filename.

     import os, geopandas as gpd

     event_tracks = gpd.GeoDataFrame(columns=['geometry', 'eventname'])

     directory = os.getcwd()

     
     for dir_root, dir_dir, js_file  in os.walk(directory):
       for f in js_file:
         if f.endswith(".json"):
         fname = os.path.basename(f)
         fname, fext = fname.split('.')
         print (fname)
         print ('Reading Track Path JSON ' + f)
         with open(f, 'r') as path_f:
           event_tracks = gpd.read_file(path_f).append(event_tracks)

The code above works for geometries and generates a geodataframe with valid geometries. However, I cannot assign the event name into the column. So far, I've tried to:

  • Create list of names and append list elements
  • Reindex and concatenate two different dataframes

However, it either returns a NaN-filled column or an error such as: TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid.

Is there any workaround to do this?

0 Kudos
0 Replies