-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import platform | ||
from pathlib import Path | ||
|
||
import duckdb | ||
import pyarrow as pa | ||
import pyarrow.parquet as pq | ||
from h3ronpy.arrow.vector import ContainmentMode, wkb_to_cells | ||
from pooch import Decompress, retrieve | ||
from shapely.geometry.base import BaseGeometry | ||
|
||
|
||
def _transform_geometry_filter_to_h3( | ||
geometry: BaseGeometry, working_directory: Path, h3_resolution: int = 11 | ||
) -> Path: | ||
"""Fill geometry filter with H3 polygons and save them in a dedicated parquet file.""" | ||
result_file_path = working_directory / "geometry_filter_h3_indexes.parquet" | ||
h3_indexes = wkb_to_cells( | ||
[sub_geometry.wkb for sub_geometry in geometry.geoms], | ||
resolution=h3_resolution, | ||
containment_mode=ContainmentMode.Covers, | ||
flatten=True, | ||
).unique() | ||
pq.write_table(pa.table(dict(h3=h3_indexes)), result_file_path) | ||
return result_file_path | ||
|
||
|
||
# Based on https://github.com/fusedio/udfs/blob/main/public/DuckDB_H3_Example/utils.py | ||
# Will be changed after H3 extension becomes available in the official repository | ||
def _load_h3_duckdb_extension(con: duckdb.DuckDBPyConnection) -> None: | ||
"""Load H3 DuckDB extension for current system.""" | ||
system = platform.system() | ||
arch = platform.machine() | ||
arch = "amd64" if arch == "x86_64" else arch | ||
|
||
if system == "Windows": | ||
detected_os = "windows_amd64" | ||
elif system == "Darwin": | ||
detected_os = f"osx_{arch}" | ||
else: | ||
detected_os = f"linux_{arch}" | ||
if detected_os == "linux_amd64": | ||
detected_os = "linux_amd64_gcc4" | ||
|
||
url = f"https://pub-cc26a6fd5d8240078bd0c2e0623393a5.r2.dev/v{duckdb.__version__}/{detected_os}/h3ext.duckdb_extension.gz" | ||
# Note this is not the correct file name, it will be fixed later in this function | ||
# This workaround of downloading in Python is needed because DuckDB cannot load extensions | ||
# from https (ssl, secure) URLs. | ||
ungzip_path = retrieve( | ||
url=url, | ||
processor=Decompress(name="h3ext.duckdb_extension"), | ||
known_hash=None, | ||
path="cache/h3_ext", | ||
) | ||
|
||
con.sql(f"INSTALL '{ungzip_path}';") | ||
con.sql("LOAD h3ext;") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters