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
It looks like the issue is that you are passing strings to API that needs numbers.
should be replaced with code that will transform the strings into numbers. something like:
Data example:
So it looks like that the first 2 fields are float numbers , fields 3,4 are ints and field 5 is a string.