forked from dlt-hub/dlt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Spatial Types for PostGIS (dlt-hub#1927)
* Add dependencies Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add shapely dependency Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Move sample geodata to correct folder Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Make smaller Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Enhance PostgresTableBuilder test suite with geometry type handling. Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add tests Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add geometry columns with default SRID 4326. Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * resource can't serialize shapely objects Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Expand geom test Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Comments Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Update lock file Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * schema Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * [fix](database): remove unused hex validation method Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Create custom insert job for geom types Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove hanging client parameter Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add a TODO comment to address the issue in the splitting logic Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove unnecessary init override Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add debugging points Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * [test](database): add tests for geometry parsing in Postgres Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Correct row parsing in Postgres destination Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Yield from supermethod Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add control flow for geom Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add test * refactor geo parsing Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * [fix](test): correct schema name in PostGIS geometry test Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove stale test Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove geopandas test until resolution Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add docs and raise on malformed values Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add postgis dependency to ci Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * fix postgis image repo Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add postgis to dbt runner Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Change snippet to py instead of python Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * add postgis Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove unused geodata file * Remove unnecessary INSERT class Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Add WKB format handling Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Packaging Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Move import to local Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Comment Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * postgis docs Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Update lockfile Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * fix(deps): remove shapely dependency from postgis extra Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * format Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * feat(postgres): add support for CSV loading of geometry columns Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * Remove wkb examples in docs Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> * format Signed-off-by: Marcel Coetzee <marcel@mooncoon.com> --------- Signed-off-by: Marcel Coetzee <marcel@mooncoon.com>
- Loading branch information
Showing
13 changed files
with
547 additions
and
74 deletions.
There are no files selected for viewing
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
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
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
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,63 @@ | ||
from typing import Any, Optional | ||
|
||
from dlt.common.schema.typing import TColumnNames, TTableSchemaColumns | ||
from dlt.destinations.utils import get_resource_for_adapter | ||
from dlt.extract import DltResource | ||
|
||
GEOMETRY_HINT = "x-postgres-geometry" | ||
SRID_HINT = "x-postgres-srid" | ||
|
||
|
||
def postgres_adapter( | ||
data: Any, | ||
geometry: TColumnNames = None, | ||
srid: Optional[int] = 4326, | ||
) -> DltResource: | ||
"""Prepares data for the postgres destination by specifying which columns should | ||
be cast to PostGIS geometry types. | ||
Args: | ||
data (Any): The data to be transformed. It can be raw data or an instance | ||
of DltResource. If raw data, the function wraps it into a DltResource | ||
object. | ||
geometry (TColumnNames, optional): Specify columns to cast to geometries. | ||
It can be a single column name as a string, or a list of column names. | ||
srid (int, optional): The Spatial Reference System Identifier (SRID) to be | ||
used for the geometry columns. If not provided, SRID 4326 will be used. | ||
Returns: | ||
DltResource: A resource with applied postgres-specific hints. | ||
Raises: | ||
ValueError: If input for `geometry` is invalid, or if no geometry columns are specified. | ||
Examples: | ||
>>> data = [{"town": "Null Island", "loc": "POINT(0 0)"}] | ||
>>> postgres_adapter(data, geometry="loc", srid=4326) | ||
[DltResource with hints applied] | ||
""" | ||
resource = get_resource_for_adapter(data) | ||
|
||
column_hints: TTableSchemaColumns = {} | ||
|
||
if geometry: | ||
if isinstance(geometry, str): | ||
geometry = [geometry] | ||
if not isinstance(geometry, list): | ||
raise ValueError( | ||
"'geometry' must be a list of column names or a single column name as a string." | ||
) | ||
|
||
for column_name in geometry: | ||
column_hints[column_name] = { | ||
"name": column_name, | ||
GEOMETRY_HINT: True, # type: ignore[misc] | ||
} | ||
if srid is not None: | ||
column_hints[column_name][SRID_HINT] = srid # type: ignore | ||
|
||
if not column_hints: | ||
raise ValueError("A value for 'geometry' must be specified.") | ||
else: | ||
resource.apply_hints(columns=column_hints) | ||
return resource |
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
Oops, something went wrong.