Skip to content

Commit

Permalink
Turn GeoJSON into individual JSON files
Browse files Browse the repository at this point in the history
Closes #33.
  • Loading branch information
waldoj committed May 11, 2017
1 parent b5b422d commit 7f825f7
Showing 1 changed file with 28 additions and 69 deletions.
97 changes: 28 additions & 69 deletions frostline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import csvkit
import os
import re
import errno
import glob
import sys
Expand Down Expand Up @@ -127,7 +128,7 @@ def create_zone_json():
if os.path.isfile(source_data):
zones = open(source_data, 'r')
else:
print source_data + "could not be found."
print source_data + " could not be found."
sys.exit(1)

# Create the /api/ directory, if it doesn't exist.
Expand All @@ -146,79 +147,37 @@ def create_zone_json():
file.close()

# Convert an ASC file to a seris of filesystem aliases (symbolic links).
def create_coord_api(asc_file):
def create_coord_api(geojson_file):

if os.path.isfile(asc_file) is False:
print asc_file + "could not be found."
if os.path.isfile(geojson_file) is False:
print geojson_file + "could not be found."
sys.exit(1)

# Read in PHZ data.
zones = {}
phz = open('zones.yml', 'r')
for zone, temps in yaml.load(phz).items():
zones[zone] = {}
temps = temps.split(' to ')
zones[zone]['low'] = temps[0]
zones[zone]['high'] = temps[1]

with open(asc_file) as f:

# Iterate through each row in the file.
row_num=0
map = {}
for row in enumerate(f):

# Remove cruft.
fields = row[1].rstrip()

# Extract header data about the file.
if row_num < 6:
value = fields.split(' ')[1]
if row_num == 2:
map['x_corner'] = float(value)
elif row_num == 3:
map['y_corner'] = float(value)
elif row_num == 4:
map['cell_size'] = float(value)
elif row_num == 5:
map['nodata_value'] = value
row_num+=1
continue

# Now parse actual data.
column_num=0
fields = fields.split(' ')
for field in fields:

# If this field has no value, skip it.
if field == map['nodata_value']:
continue

# Figure out what zone this field is in.
place = {}
place['temp'] = (int(field) / 100 * (9/5)) + 32
for zone, temps in zones.items():
if float(place['temp']) >= float(temps['low']) and float(place['temp']) <= float(temps['high']):
place['zone'] = zone
break

# Assign this field to a latitude and longitude.
place['lon'] = round(map['x_corner'] + map['cell_size'] * column_num, 2)
place['lat'] = round(map['y_corner'] + map['cell_size'] * row_num, 2)

# Create a symlink, naming the file for the coordinates.
filename = 'api/' + str(place['lon']) + ',' + str(place['lat']) + '.json'
try:
os.symlink(place['zone'] + '.json', filename)
except OSError:
pass

column_num+=1

row_num+=1
# Read in GeoJSON data.
places = json.loads(open(geojson_file).read())

# Iterate through every place
for place in places['features']:

output = {}
match = re.search('^([0-9][a,b]+)', place['properties']['ZONE'])
if match:
pass
else:
continue
output['zone'] = match.group()
output['lon'] = round(place['geometry']['coordinates'][0][0][0], 2)
output['lat'] = round(place['geometry']['coordinates'][0][0][1], 2)

# Create a symlink, naming the file for the coordinates.
filename = 'api/' + str(output['lon']) + ',' + str(output['lat']) + '.json'
try:
os.symlink(output['zone'] + '.json', filename)
except OSError:
pass

if __name__ == "__main__":
main()
create_zip_api()
create_zone_json()
create_coord_api('phm_us_grid/phm_us.asc')
create_coord_api('phm_us_shp/phm_us_shp.geojson')

0 comments on commit 7f825f7

Please sign in to comment.