Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active January 2, 2016 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clhenrick/8273016 to your computer and use it in GitHub Desktop.
Save clhenrick/8273016 to your computer and use it in GitHub Desktop.
Demo showing that Python is effective at processing GeoJson data, as the format is similar to Python's dictionary object type.
#! usr/bin/env/python
# loop through input data and add one feature per week to output data
# create a while loop with a counter that adds 7 days from start day and
# maxes out at an end date
from sys import argv
import simplejson as json
from datetime import datetime, timedelta
script, infile, outfile = argv
data = json.load(open(infile, 'r'))
output = open(outfile, 'w')
geojson = {
"type": "FeatureCollection",
"features": []
}
def push_data(input):
"""
adds features from input file incrementing by week
"""
for feature in input['features']:
# if feature.geometry != null
if feature['geometry'] != None:
date = datetime.strptime(feature['properties']['date'], '%m/%d/%Y')
# print stuff to debug...
print "date: ", date
print "geometry x: ", feature['geometry']['coordinates'][0]
print "geometry y: ", feature['geometry']['coordinates'][1]
#create variables for while loop
start_date = datetime.strptime('5/6/2013', '%m/%d/%Y')
end_date = datetime.strptime('9/20/2013', '%m/%d/%Y')
match_date = start_date
#create variables for input data to append to output data
mileage = feature['properties']['interval']
total_miles = feature['properties']['total']
x = feature['geometry']['coordinates'][0]
y = feature["geometry"]["coordinates"][1]
# loop over dates and add one feature per week to output
while match_date < end_date:
if date == match_date:
geojson["features"].append(
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [x,y]
},
"properties": {
"date": date.strftime('%m/%d/%Y'),
"mileage": mileage,
"total miles": total_miles
}
})
# increase the match_date variable by one week
match_date += timedelta(days=7)
push_data(data)
json.dump(geojson, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment