def test_validation(): h = '8a28308280fffff' # invalid hex with pytest.raises(H3CellError): h3.h3_get_base_cell(h) with pytest.raises(H3CellError): h3.h3_get_resolution(h) with pytest.raises(H3CellError): h3.h3_to_parent(h, 9) with pytest.raises(H3CellError): h3.h3_distance(h, h) with pytest.raises(H3CellError): h3.k_ring(h, 1) with pytest.raises(H3CellError): h3.hex_ring(h, 1) with pytest.raises(H3CellError): h3.h3_to_children(h, 11) with pytest.raises(H3CellError): h3.compact({h}) with pytest.raises(H3CellError): h3.uncompact({h}, 10)
def test_distance(): h = '8a28308280c7fff' assert h3.h3_distance(h, h) == 0 n = h3.hex_ring(h, 1).pop() assert h3.h3_distance(h, n) == 1 n = h3.hex_ring(h, 2).pop() assert h3.h3_distance(h, n) == 2
def on_post(self, req, resp): resp.status = falcon.HTTP_200 credentials = json.loads(req.stream.read()) body = json.loads(credentials["body"]) wind_power = body["windPower"]; offset = body["offset"]; resolution = body["resolution"]; trees = body["trees"]; tic = time.perf_counter() wind = Wind(); trees_out = []; for tree in trees: longitude = tree["longitude"]; latitude = tree["latitude"]; color = tree["color"]; polygon = wind.getWindLayerCoordinates(longitude, latitude, wind_power, offset) geoJson = { 'type': 'Polygon', 'coordinates': [polygon] } hexagons = h3.polyfill(geoJson, resolution) h3_index = h3.geo_to_h3(latitude, longitude, resolution); for hex in hexagons: direction = h3.h3_distance(h3_index, hex); opacity = 255 - direction * (80 / wind_power); if (opacity > 0): trees_out.append({ 'opacity': opacity, 'hex': hex, 'color': color }); toc = time.perf_counter() print(f"Downloaded the tutorial in {(toc - tic)*1000:4f} ms") content = { 'hex': json.dumps(trees_out, default=serialize_sets) } resp.body = json.dumps(content)
def on_event(self): passenger_pre = (taxiOD04_RLsample001 >> filter_by(X.O_time_block == self.current_time, X.h3_lv7_O == self.current_loc, X.h3_lv7_D.isin(cell_base_map.h3_index))) fare = 0 if passenger_pre.shape[0] > 0: passenger = (passenger_pre >> sample(1)).iloc[0,].to_dict() if passenger['freq'] * 2 >= random.randint(1, 10): self.current_loc = passenger['h3_lv7_D'] self.current_time = passenger['D_time_block'] fare = 3000 + h3.h3_distance(passenger['h3_lv7_O'], passenger['h3_lv7_D']) * 1000 return fare
def test_distance_error(): h1 = '8353b0fffffffff' h2 = '835804fffffffff' with pytest.raises(H3ValueError): h3.h3_distance(h1, h2)
def test_h3_distance(): h = '89283082993ffff' assert 0 == h3.h3_distance(h, h) assert 1 == h3.h3_distance(h, '8928308299bffff') assert 5 == h3.h3_distance(h, '89283082827ffff')