def query_cell(self, event, context): cities = [] resolution = h3.h3_get_resolution(event['h3_address']) base_cell = str(h3.h3_get_base_cell(event['h3_address'])) if resolution < max_res: max_query_res = resolution else: max_query_res = max_res range_query = "#".join([ h3.h3_to_parent(event['h3_address'], x) for x in range(min_res, max_query_res + 1) ]) key_condition_expression = "ParentCell = :parentcell AND begins_with(CellLocationIndex, :index)" expression_values = { ":parentcell": { "S": base_cell }, ":index": { "S": range_query } } resp = self.query_db_table(key_condition_expression, expression_values) for item in resp['Items']: city = item['CityName']['S'] if city not in cities: cities.append(city) return cities
def load_cities(self, event, context): with open('usa_cities.geojson', 'r') as geoj: cities = json.load(geoj)['features'] _db_table = dynamodb_resource.Table(db_table.name) print("Loading database table") with _db_table.batch_writer() as db_batch: for idx, city in enumerate(cities): city_id = str(idx) if city['properties']['NAME']: hexagons = h3.polyfill(city['geometry'], max_res, geo_json_conformant=True) if len(hexagons) > 0: for hex in hexagons: parents = [ h3.h3_to_parent(hex, x) for x in range(min_res, max_res + 1) ] range_key = "#".join(parents) + "#{}".format( city_id) db_item = { 'ParentCell': "{}".format(h3.h3_get_base_cell(hex)), 'CellLocationIndex': range_key, 'CityName': city['properties']['NAME'], 'CityID': city_id } db_batch.put_item(Item=db_item) if idx % 1000 == 0: print("Processed {} cities".format(idx))
def test_h3_get_base_cell(self): self.assertTrue(isinstance(h3.h3_get_base_cell('8928308280fffff'), int))