Skip to content

Instantly share code, notes, and snippets.

@sgibbes
Created April 9, 2018 17:20
Show Gist options
  • Save sgibbes/48023e4dbd72176dc08629eafa218e5a to your computer and use it in GitHub Desktop.
Save sgibbes/48023e4dbd72176dc08629eafa218e5a to your computer and use it in GitHub Desktop.
from utilities import util, tile_geom_util
import json
import sys
from shapely.geometry import shape
# big aoi
aoi = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[25.576171875,7.841615185204699],[15.864257812499998,-1.098565496040652],[18.45703125,-9.44906182688142],[31.6845703125,-9.535748998133615],[35.859375,-0.4394488164139641],[25.576171875,7.841615185204699]]]}}]}
aoi = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[14.501953124999998,5.703447982149503],[2.4609375,1.142502403706165],[17.841796875,-2.3723687086440504],[21.708984375,-12.297068292853805],[27.773437499999996,-0.4394488164139641],[38.056640625,-1.4939713066293112],[32.16796875,6.489983332670651],[35.947265625,13.068776734357694],[24.78515625,12.64033830684679],[20.126953125,18.562947442888312],[14.501953124999998,5.703447982149503]]]}}]}
#
# with open('geom.geojson', 'w') as thefile:
# json.dump(aoi, thefile)
event = {
'body': json.dumps({'geojson': aoi}),
#'body': {'fire_data': fire_data},
'queryStringParameters': {'aggregate_by':'day', 'layer': 'glad', 'aggregate_values': 'true', 'tile_id': '00N_116E', 'fire-type': 'all', 'period': '2017-04-01,2018-02-02'}
}
geom = util.get_shapely_geom(event)
geom_tiled = json.loads(tile_geom_util.tile_geom(geom))
#
# with open('first_tile.geojson', 'w') as thefile:
# json.dump(geom_tiled, thefile)
features_list = geom_tiled['features']
result = []
tile_invocations = 0
while features_list:
recheck_items = []
for tile in features_list:
tile_id = tile['properties']['id']
print "id: {}".format(tile_id)
tile_area = shape(tile['geometry']).area
if tile_area < .0625:
# print "appending {}".format(tile_id)
result.append(tile)
else:
tile_invocations +=1
tile = shape(tile['geometry'])
# print "tiling {}".format(tile_id)
item_list = tile_geom_util.tile_geom(tile)
item_list = json.loads(item_list)
item_list_geom = item_list['features']
# print "items to append: {}".format(len(item_list_geom))
recheck_items += item_list_geom
# print "len of recheck items: {}".format(len(recheck_items))
# feature_col = {"type": 'FeatureCollection', 'features':recheck_items}
# with open('test.geojson', 'w') as thefile:
# json.dump(feature_col, thefile)
features_list = recheck_items # Let the loop restart with these, if any
# print "length of feature list: {}".format(len(features_list))
print len(result)
print tile_invocations
feature_col = {"type": 'FeatureCollection', 'features':result}
with open('geom_tiled.geojson', 'w') as thefile:
json.dump(feature_col, thefile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment