2020-12-04 00:14:31 +01:00
#usage: $ python3 convert-json-to-geojson.py 1 (where 1 is the first scooter id)
import os , sys , json , geojson
from collections import OrderedDict
arg = int ( sys . argv [ 1 ] )
#read json file
with open ( " historicalData.json " , " r " ) as read_file :
data = json . load ( read_file )
#for each individual scooter, per day, format coordinates
#print(data[0][0]['lastPos'] + data[0][0]['name'])
#print("/n")
#print(data[0][2]['lastPos'] + data[0][0]['name'])
#for entry in data:
#for key in data[5747]:
# print(key['lastPos']+' '+key['name'])
l = [ ] # temp list of positions (including duplicates)
t = [ ] # list containing last location update times
p = { } # dictionairy containing times and positions
nest = [ ]
2020-12-04 21:45:05 +01:00
#print(data[20][0]) # = data[data point (0-5746?)][scooter id (0-10)]
#for j in range(1): # if 1; print only first scooter (aram)
2020-12-04 00:14:31 +01:00
#print(j)
2020-12-04 21:45:05 +01:00
for i in range ( 5747 ) : # all entries (data points)
2020-12-04 00:14:31 +01:00
#l.append(data[i][j]['lastPos']+'|'+data[i][j]['lastLocationUpdate'])
#l.append(data[i][j]['lastLocationUpdate'])
#p.update(i:{data[i][j]['lastPos'],data[i][j]['lastLocationUpdate']})
#l.append(data[i][j]['lastPos'])
#nest.append(data[i][j]['lastLocationUpdate'])
2020-12-04 21:45:05 +01:00
l . append ( [ data [ i ] [ arg ] [ ' lastPos ' ] , data [ i ] [ arg ] [ ' lastLocationUpdate ' ] ] ) #make 2d list
2020-12-04 00:14:31 +01:00
2020-12-04 21:45:05 +01:00
2020-12-04 00:14:31 +01:00
# if entry / pos is the same/double..remove? mmm
#uniquePos = set(l) # remove duplicates from all entries
#print(l[0])
2020-12-04 21:45:05 +01:00
2020-12-04 00:14:31 +01:00
# construct GeoJSON
2020-12-04 21:45:05 +01:00
#list format: l[list index][0=lat,long,1=timestamp]
2020-12-04 00:14:31 +01:00
string1 = ( """
{ " type " : " FeatureCollection " ,
" features " : [
""" )
string2 = " "
for j in range ( 5747 ) : # if 1; print only first scooter (aram)
#for pos, time in p.items(): #make dictionairy if you want unique points
string2 + = ( """
{ " type " : " Feature " ,
" geometry " : { " type " : " Point " , " coordinates " : [ """ + ' , ' .join(l[j][0].split( ' , ' )[::-1])+ """ ] } ,
\" properties \" : { " time " : \" " " " + l [ j ] [ 1 ] + """ \" }
} ,
""" )
#print(', '.join(item.split(',')[::-1])) #reverse
string3 = ( """
]
}
""" )
#print(string2[:-6])
print ( " Writing to file: ' " + str ( arg ) + " .json ' " ) #remove trailing commma in string2
#write to file
f = open ( str ( arg ) + " .json " , " w " )
f . write ( string1 + string2 [ : - 6 ] + string3 )
f . close ( )
print ( " Converting to CSV file: ' " + str ( arg ) + " .csv ' " ) #remove trailing commma in string2
os . system ( " python geojson2csv.py " + str ( arg ) + " .json " + " " + str ( arg ) + " .csv " )
print ( " Successfully written: ' " + str ( arg ) + " .json ' and ' " + str ( arg ) + " .csv ' " )
#remove
#print(len(data)) # total 26143 entries ('days') holy...0-5747 = all scooters, 5748-26143 = only speedy+scooty
#more on time:
#Time when the event occurred. Times are reported in milliseconds since the epoch ( 1970-01-01T00:00:00.000Z), and do not include leap seconds. In certain output formats, the date is formatted for readability.
#
#
#https://earthquake.usgs.gov/data/comcat/data-eventterms.php#time