From 7f825f7c95ce3953f47538b0d83f8af0af600f24 Mon Sep 17 00:00:00 2001 From: Waldo Jaquith Date: Thu, 11 May 2017 16:42:20 -0400 Subject: [PATCH] Turn GeoJSON into individual JSON files Closes #33. --- frostline.py | 97 +++++++++++++++------------------------------------- 1 file changed, 28 insertions(+), 69 deletions(-) diff --git a/frostline.py b/frostline.py index f35dd4ed..86e4877c 100755 --- a/frostline.py +++ b/frostline.py @@ -3,6 +3,7 @@ import csvkit import os +import re import errno import glob import sys @@ -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. @@ -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')