0

I'm new to geospatial data and need a way to get data out of a CSV in this format:

Latitude, Longitude, Altitude, Timestamp, Trip Identifier

and into a geojson suitable for kepler.gl with their specified format:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": { "vendor": "A",
      "vol":20},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [-74.20986, 40.81773, 0, 1564184363],
          [-74.20987, 40.81765, 0, 1564184396],
          [-74.20998, 40.81746, 0, 1564184409]
        ]
      }
    }
  ]
}

My attempts in Python (heavily based on code from ewcz) have not been successful; this returns a ValueError, and I can't see a way to incorporate MultiLineString as the number of coordinate pairs changes between records.

import csv, json
from geojson import Feature, FeatureCollection, Point, LineString

features = []
with open('Trips.csv', newline='', encoding='utf-16') as csvfile:
    reader = csv.reader(csvfile, delimiter='    ')
    for Latitude, Longitude, Altitude, Timestamp, ID in reader:
        Latitude, Longitude = map(float, (Latitude, Longitude))
        features.append(
            Feature(
                geometry = LineString([Latitude,Longitude,Altitude,Timestamp]),
                properties = {
                    'ID': ID,
                }
            )
        )

collection = FeatureCollection(features)
with open("Trips.json", "w") as f:
    f.write('%s' % collection)

Error given:

ValueError                                Traceback (most recent call last)
<ipython-input-1-5dadf758869b> in <module>
      9         features.append(
     10             Feature(
---> 11                 geometry = LineString([Latitude,Longitude,Altitude,Timestamp]),
     12                 properties = {
     13                     'ID': ID

~/anaconda3/anaconda3/lib/python3.7/site-packages/geojson/geometry.py in __init__(self, coordinates, validate, precision, **extra)
     30         super(Geometry, self).__init__(**extra)
     31         self["coordinates"] = self.clean_coordinates(
---> 32             coordinates or [], precision)
     33 
     34         if validate:

~/anaconda3/anaconda3/lib/python3.7/site-packages/geojson/geometry.py in clean_coordinates(cls, coords, precision)
     53                 new_coords.append(round(coord, precision))
     54             else:
---> 55                 raise ValueError("%r is not a JSON compliant number" % coord)
     56         return new_coords
     57 

ValueError: '0' is not a JSON compliant number
JamesE
  • 1
  • 2
  • Can you share the stack trace and point to the line that throw the error? Can you share the csv as well? – balderman Oct 23 '19 at 12:28
  • Updated question with errors. An extract of the CSV is [here](https://github.com/jamang1999/hello-world/blob/master/Trips.csv). The last line of the error message rejects any number in the third column of the CSV (Altitude). – JamesE Oct 25 '19 at 09:41
  • the link to the csv does not work. Upload it to pastebin or try another link. – balderman Oct 25 '19 at 09:43
  • See my answer - I think it should work now. – balderman Oct 25 '19 at 09:53

1 Answers1

0

It looks like the issue is that you are passing strings to API that needs numbers.

for Latitude, Longitude, Altitude, Timestamp, ID in reader

should be replaced with code that will transform the strings into numbers. something like:

for float(Latitude), float(Longitude), int(Altitude), int(Timestamp), ID in reader

Data example:

51.467525   -0.445004   0   1569324754  EIN159

So it looks like that the first 2 fields are float numbers , fields 3,4 are ints and field 5 is a string.

balderman
  • 22,927
  • 7
  • 34
  • 52