Search code examples
pythonloopsgeojsoncoordinate

How to write a Python loop to convert a large amount of coordinates into the GeoJSON LineString format?


I reconstructed my dataset in pandas DataFrame by using a multi-index, and it is now in the following format.

In [1]: df.head(12)
Out [1]:

enter image description here

In order to put it into a GeoJSON LineString format and visualize it on a map, I need to write a Python loop over each point and each line through millions of satellite observational points. For reference, the following example specifies a GeoJSON LineString.

{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

However, not always as shown in the figure that a line consists of 4 points for the first three lines, the number of points for a specific line in this dataset is totally random, ranging from 4 to hundreds.

I am so confused how to write a Python loop that could help me put my coordinates into GeoJSON LineString type by using a multi-index, e.g.

In [2]: df.Longitude[1][4]
Out [2]: 128

Thanks for your time!


Solution

  • A combination of groupby and to_json seems to work well.

    import pandas as pd
    import numpy as np
    import pprint
    
    arrays = [np.array([1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4]),
              np.array([1, 2, 3, 1, 2, 1, 2, 3, 4, 5, 1, 2])]
    
    df = pd.DataFrame(np.arange(24).reshape(12,2), 
                      index=arrays, columns=['Longitude', 'Lattitude'])
    
    dd = {"type":"Feature", 
          "geometry":{"type":"Linestring",
                      "coordinates":None
                     },
          "properties":{"prop0":'red',
                        "prop1":'dashed'
                       }
         }
    
    for _, group in df.groupby(level=0):
        dd["geometry"]["coordinates"] = group.to_json(orient='values')
        pprint.pprint(dd)
    

    output:

    {'geometry': {'coordinates': '[[0,1],[2,3],[4,5]]',
                  'type': 'Linestring'},
     'properties': {'prop0': 'red',
                    'prop1': 'dashed'},
     'type': 'Feature'}
    {'geometry': {'coordinates': '[[6,7],[8,9]]',
                  'type': 'Linestring'},
     'properties': {'prop0': 'red',
                    'prop1': 'dashed'},
     'type': 'Feature'}
    {'geometry': {'coordinates': '[[10,11],[12,13],[14,15],[16,17],[18,19]]',
                  'type': 'Linestring'},
     'properties': {'prop0': 'red',
                    'prop1': 'dashed'},
     'type': 'Feature'}
    {'geometry': {'coordinates': '[[20,21],[22,23]]',
                  'type': 'Linestring'},
     'properties': {'prop0': 'red',
                    'prop1': 'dashed'},
     'type': 'Feature'}