def test_edge_length(self): for i in range(0, 15): self.assertTrue(isinstance(h3.edge_length(i), float)) self.assertTrue(isinstance(h3.edge_length(i, 'm'), float)) with pytest.raises(ValueError) as e_info: h3.edge_length(5, 'ft') self.assertTrue(isinstance(e_info.value, ValueError))
global gdict gdict = {} global cdict cdict = {} svalue = str(datetime.datetime(2020, 3, 16, 0)) evalue = str(datetime.datetime(2020, 3, 16, 6)) list_hex_edge_km = [] list_hex_edge_m = [] list_hex_perimeter_km = [] list_hex_perimeter_m = [] list_hex_area_sqkm = [] list_hex_area_sqm = [] for i in range(0, max_res + 1): ekm = h3.edge_length(resolution=i, unit='km') em = h3.edge_length(resolution=i, unit='m') list_hex_edge_km.append(round(ekm, 3)) list_hex_edge_m.append(round(em, 3)) list_hex_perimeter_km.append(round(6 * ekm, 3)) list_hex_perimeter_m.append(round(6 * em, 3)) akm = h3.hex_area(resolution=i, unit='km^2') am = h3.hex_area(resolution=i, unit='m^2') list_hex_area_sqkm.append(round(akm, 3)) list_hex_area_sqm.append(round(am, 3)) df_meta = pd.DataFrame({ "edge_length_km": list_hex_edge_km, "perimeter_km": list_hex_perimeter_km, "area_sqkm": list_hex_area_sqkm,
def raster_to_h3(raster_path, value_name, cell_min_res, cell_max_res, extent=None, pix_size_factor=3): """Load raster values into h3 dggs cells Parameters: raster (string): path to raster file for uploading resolutions (string): srs epsg code of raster's territory coordinate system table (string): name of a value to be uploaded into dggs cells cell_min_res (integer): min h3 resolution to look for based on raster cell size cell_max_res (integer): max h3 resolution to look for based on raster cell size extent (list): Extent as array of 2 lon lat pairs to get raster values for pix_size_factor (pinteger): how times smaller h3 hex size should be comparing with raster cell size Returns: Pandas dataframe """ # Open raster rs = rasterio.open(raster_path) # Get extent to fill with h3 hexes if extent: extent = gpd.GeoSeries(box(extent[0], extent[1], extent[2], extent[3])).__geo_interface__ else: extent = gpd.GeoSeries(box(rs.bounds.left, rs.bounds.bottom, rs.bounds.right, rs.bounds.top)).__geo_interface__ # Get resolution:edge lenght in m dict resolutions = {} for i in range(cell_min_res, cell_max_res, 1): resolutions[i] = h3.edge_length(resolution=i, unit='m') # Get two points on borders of neighbour pixels in raster x1 = rs.transform[2] y1 = rs.transform[5] x2 = rs.transform[2] + rs.transform[0] y2 = rs.transform[5] - rs.transform[4] # Get pixel size from projected src # transformer = Transformer.from_crs("epsg:4326", 'proj=isea') # size = Point(transformer.transform(y1, x1)).distance(Point(transformer.transform(y2, x1))) # print(f'Projected size{size}') # Get pixel size from haversine formula size = __haversine(x1, y1, x1, y2) print(f"Raster pixel size {size}") # Get raster band as np array raster_band_array = rs.read(1) # Get h3 resolution for raster pixel size for key, value in resolutions.items(): if value < size / pix_size_factor: resolution = key break print(resolution) # Create dataframe with cell_ids from extent with given resolution print(f"Start filling raster extent with h3 indexes at resolution {resolution}") df = pd.DataFrame({'cell_id': list(h3.polyfill_geojson(extent['features'][0]["geometry"], res=resolution))}) # Get raster values for each cell_id print(f"Start getting raster values for hexes at resolution {resolution}") df[value_name] = df['cell_id'].apply(lambda x: raster_band_array[rs.index(h3.h3_to_geo(x)[1], h3.h3_to_geo(x)[0])]) # Drop nodata df = df[df[value_name] != rs.nodata] return df
from dispersal_model.types_and_units import (meters, Iterator, Tuple) RESOLUTION = 5 AREA = h3.hex_area(RESOLUTION, "km^2") # The resolution of our hexes should be about 450 km² following Gavin (2017) assert h3.hex_area(RESOLUTION - 1, "km^2") > 450 > AREA GEODESIC = geodesic.Geodesic() # Instead of going through the h3 module, we can copy the relevant code from # there directly and reduce type conversions. ring_size = int(52413 / h3.edge_length(RESOLUTION, 'm') * 1.5 + 1) array_len = h3.libh3.maxKringSize(ring_size) KringArray = h3.H3Index * array_len DistanceArray = h3.c_int * array_len krings = KringArray() distances = DistanceArray() class BoundingBox: """A bounding box with geo-coordinates west, east, south, and north""" w: cython.float e: cython.float s: cython.float n: cython.float def __init__(self, w: cython.float, e: cython.float, s: cython.float,