Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored sqlalchemy-related parsing of geometry #88

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pygeofilter/backends/sqlalchemy/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from datetime import timedelta
from functools import reduce
from inspect import signature
Expand All @@ -7,7 +6,6 @@
from pygeoif import shape
from sqlalchemy import and_, func, not_, or_


def parse_bbox(box, srid: int = None):
minx, miny, maxx, maxy = box
return func.ST_GeomFromEWKT(
Expand All @@ -18,11 +16,15 @@ def parse_bbox(box, srid: int = None):
)


def parse_geometry(geom):
def parse_geometry(geom: dict):
crs_identifier = geom.get(
"crs", {}
).get(
"properties", {}
).get("name", "urn:ogc:def:crs:EPSG::4326")
srid = crs_identifier.rpartition("::")[-1]
wkt = shape(geom).wkt
search = re.search(r"SRID=(\d+);", wkt)
sridtxt = "" if search else "SRID=4326;"
return func.ST_GeomFromEWKT(f"{sridtxt}{wkt}")
return func.ST_GeomFromEWKT(f"SRID={srid};{wkt}")


# ------------------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions tests/backends/sqlalchemy/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import cast
import pytest

from pygeofilter.backends.sqlalchemy import filters


@pytest.mark.parametrize("geom, expected", [
pytest.param(
{
"type": "Point",
"coordinates": [10, 12]
},
"ST_GeomFromEWKT('SRID=4326;POINT (10 12)')",
id="without-crs"
),
pytest.param(
{
"type": "Point",
"coordinates": [1, 2],
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::3004"
}
}
},
"ST_GeomFromEWKT('SRID=3004;POINT (1 2)')",
id="with-crs"
),
])
def test_parse_geometry(geom, expected):
parsed = filters.parse_geometry(cast(dict, geom))
result = str(parsed.compile(compile_kwargs={"literal_binds": True}))
assert result == expected
Loading