Files
FireworksApp/fireworks_detect/h3_utils.py
T

43 lines
1.2 KiB
Python

import h3
from django.conf import settings
H3_RESOLUTION_SETTING = "FIREWORKS_H3_RESOLUTION"
def h3_resolution():
resolution = getattr(settings, H3_RESOLUTION_SETTING, 9)
if isinstance(resolution, bool) or not isinstance(resolution, int) or not 0 <= resolution <= 15:
raise ValueError(f"{H3_RESOLUTION_SETTING} must be an integer between 0 and 15.")
return resolution
def cell_from_coordinates(latitude, longitude):
if latitude is None or longitude is None:
return None
return h3.latlng_to_cell(float(latitude), float(longitude), h3_resolution())
def cell_boundary(cell):
try:
return [[latitude, longitude] for latitude, longitude in h3.cell_to_boundary(cell)]
except (ValueError, h3.H3Error):
return None
def cell_center(cell):
"""Return the centre of an H3 cell without exposing its source coordinate."""
try:
latitude, longitude = h3.cell_to_latlng(cell)
return [latitude, longitude]
except (ValueError, h3.H3Error):
return None
def cell_disk(cell, distance=1):
try:
return sorted(h3.grid_disk(cell, distance))
except (ValueError, h3.H3Error):
return []