34 lines
926 B
Plaintext
34 lines
926 B
Plaintext
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_disk(cell, distance=1):
|
|
try:
|
|
return sorted(h3.grid_disk(cell, distance))
|
|
except (ValueError, h3.H3Error):
|
|
return []
|