From cb753733269aa54a37714113607aa924fdab436f Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 10 Jul 2024 17:07:27 +0200 Subject: [PATCH] Fix: Can handle negative coordinates with Spatialite --- geoalchemy2/elements.py | 2 +- tests/test_functional.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/geoalchemy2/elements.py b/geoalchemy2/elements.py index d836a77e..dffaa389 100644 --- a/geoalchemy2/elements.py +++ b/geoalchemy2/elements.py @@ -117,7 +117,7 @@ class WKTElement(_SpatialElement): """ _REMOVE_SRID = re.compile("(SRID=([0-9]+); ?)?(.*)") - SPLIT_WKT_PATTERN = re.compile(r"((SRID=\d+) *; *)?([\w ]+) *(\([\d\. ,\(\)]+\))") + SPLIT_WKT_PATTERN = re.compile(r"((SRID=\d+) *; *)?([\w ]+) *(\([-\d\. ,\(\)]+\))") geom_from: str = "ST_GeomFromText" geom_from_extended_version: str = "ST_GeomFromEWKT" diff --git a/tests/test_functional.py b/tests/test_functional.py index c9d7b143..4702cbdb 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -445,6 +445,32 @@ def test_insert_geom_poi(self, conn, Poi, setup_tables): assert srid == 4326 assert row[1] == from_shape(Point(1, 1), srid=4326, extended=True) + def test_insert_negative_coords(self, conn, Poi, setup_tables, dialect_name): + conn.execute( + Poi.__table__.insert(), + [ + {"geom": "SRID=4326;POINT(-1 1)"}, + {"geom": WKTElement("POINT(-1 1)", srid=4326)}, + {"geom": WKTElement("SRID=4326;POINT(-1 1)", extended=True)}, + {"geom": from_shape(Point(-1, 1), srid=4326)}, + {"geom": from_shape(Point(-1, 1), srid=4326, extended=True)}, + ], + ) + + results = conn.execute(Poi.__table__.select()) + rows = results.fetchall() + + for row in rows: + assert isinstance(row[1], WKBElement) + wkt = conn.execute(row[1].ST_AsText()).scalar() + assert format_wkt(wkt) == "POINT(-1 1)" + srid = conn.execute(row[1].ST_SRID()).scalar() + assert srid == 4326 + if dialect_name == "mysql": + assert row[1] == from_shape(Point(-1, 1), srid=4326) + else: + assert row[1] == from_shape(Point(-1, 1), srid=4326, extended=True) + class TestSelectBindParam: @pytest.fixture