]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(shapely): add SRID support
authorKamil Monicz <kamil@monicz.dev>
Sun, 23 Mar 2025 10:30:11 +0000 (10:30 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 26 Mar 2025 18:26:14 +0000 (19:26 +0100)
psycopg/psycopg/types/shapely.py
tests/types/test_shapely.py

index 78260e687bf80db6915c1f9e92f0d3841c7d08e8..bce1c24907cec577449d676ed688f28795102d8a 100644 (file)
@@ -12,7 +12,7 @@ from .._compat import cache
 from .._typeinfo import TypeInfo
 
 try:
-    from shapely.wkb import dumps, loads
+    from shapely import from_wkb, to_wkb
     from shapely.geometry.base import BaseGeometry
 
 except ImportError:
@@ -26,29 +26,25 @@ class GeometryBinaryLoader(Loader):
     format = Format.BINARY
 
     def load(self, data: Buffer) -> BaseGeometry:
-        if not isinstance(data, bytes):
-            data = bytes(data)
-        return loads(data)
+        return from_wkb(bytes(data))
 
 
 class GeometryLoader(Loader):
     def load(self, data: Buffer) -> BaseGeometry:
         # it's a hex string in binary
-        if isinstance(data, memoryview):
-            data = bytes(data)
-        return loads(data.decode(), hex=True)
+        return from_wkb(bytes(data))
 
 
 class BaseGeometryBinaryDumper(Dumper):
     format = Format.BINARY
 
     def dump(self, obj: BaseGeometry) -> Buffer | None:
-        return dumps(obj)  # type: ignore
+        return to_wkb(obj, include_srid=True)
 
 
 class BaseGeometryDumper(Dumper):
     def dump(self, obj: BaseGeometry) -> Buffer | None:
-        return dumps(obj, hex=True).encode()  # type: ignore
+        return to_wkb(obj, True, include_srid=True).encode()
 
 
 def register_shapely(info: TypeInfo, context: AdaptContext | None = None) -> None:
index 98fbc2052d888ee8d0d081b5869cbc8e0049ba0d..ab5d2536ee0d9c897c5416971cc21372cfbd9999 100644 (file)
@@ -7,6 +7,7 @@ from psycopg.types import TypeInfo
 
 pytest.importorskip("shapely")
 
+from shapely import get_srid, set_srid
 from shapely.geometry import MultiPolygon, Point, Polygon
 
 from psycopg.types.shapely import register_shapely
@@ -105,7 +106,8 @@ def test_with_adapter(shapely_conn):
 @pytest.mark.parametrize("fmt_out", Format)
 def test_write_read_shape(shapely_conn, fmt_in, fmt_out):
     SAMPLE_POINT = Point(1.2, 3.4)
-    SAMPLE_POLYGON = Polygon([(0, 0), (1, 1), (1, 0)])
+    SAMPLE_POLYGON_4326 = Polygon([(0, 0), (1, 1), (1, 0)])
+    set_srid(SAMPLE_POLYGON_4326, 4326)
 
     with shapely_conn.cursor(binary=fmt_out) as cur:
         cur.execute(
@@ -122,16 +124,18 @@ def test_write_read_shape(shapely_conn, fmt_in, fmt_out):
         )
         cur.execute(
             f"insert into sample_geoms(id, geom) VALUES(2, %{fmt_in})",
-            (SAMPLE_POLYGON,),
+            (SAMPLE_POLYGON_4326,),
         )
 
         cur.execute("select geom from sample_geoms where id=1")
         result = cur.fetchone()[0]
         assert result == SAMPLE_POINT
+        assert get_srid(result) == 0
 
         cur.execute("select geom from sample_geoms where id=2")
         result = cur.fetchone()[0]
-        assert result == SAMPLE_POLYGON
+        assert result == SAMPLE_POLYGON_4326
+        assert get_srid(result) == 4326
 
 
 @pytest.mark.parametrize("fmt_out", Format)