Skip to content

Commit

Permalink
Make srid functions compatible with Shapely versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus89 committed Feb 6, 2023
1 parent bb9c3b5 commit db76d4d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
init:
pip install -U pip
pip install -e .[tests]

lint:
flake8 cartoframes tests

test:
pytest tests/unit/

Expand Down
19 changes: 17 additions & 2 deletions cartoframes/utils/geom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _load_ewkt(egeom):
srid, geom = _extract_srid(egeom)
ogeom = _load_wkt(geom)
if srid:
shapely.geos.lgeos.GEOSSetSRID(ogeom._geom, int(srid))
ogeom = set_srid(ogeom, int(srid))
return ogeom


Expand All @@ -241,7 +241,7 @@ def encode_geometry_ewkt(geom, srid=4326):

def encode_geometry_ewkb(geom, srid=4326):
if isinstance(geom, shapely.geometry.base.BaseGeometry):
shapely.geos.lgeos.GEOSSetSRID(geom._geom, srid)
geom = set_srid(geom, srid)
return shapely.wkb.dumps(geom, hex=True, include_srid=True)


Expand Down Expand Up @@ -272,3 +272,18 @@ def get_crs(gdf):
return gdf.crs['init']
else:
return str(gdf.crs)


def get_srid(geom):
if shapely.__version__ < '2.0':
return shapely.geos.lgeos.GEOSGetSRID(geom._geom)
else:
return shapely.get_srid(geom)


def set_srid(geom, srid):
if shapely.__version__ < '2.0':
shapely.geos.lgeos.GEOSSetSRID(geom._geom, int(srid))
return geom
else:
return shapely.set_srid(geom, int(srid))
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def get_version():
'markupsafe<=2.0.1',
'jinja2>=2.10.1,<3.0',
'pandas>=0.25.0',
'shapely>=1.7,<2.0',
'geopandas>=0.6.0,<1.0',
'unidecode>=1.1.0,<2.0',
'semantic_version>=2.8.0,<3'
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/utils/test_geom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import pandas as pd
import geopandas as gpd

from shapely.geos import lgeos
from shapely.geometry import Point

from cartoframes.utils.geom_utils import (ENC_EWKT, ENC_SHAPELY, ENC_WKB,
ENC_WKB_BHEX, ENC_WKB_HEX, ENC_WKT,
decode_geometry, decode_geometry_item, detect_encoding_type)
decode_geometry, decode_geometry_item,
detect_encoding_type, get_srid)


class TestGeomUtils(object):
Expand Down Expand Up @@ -92,38 +92,38 @@ def test_decode_geometry_shapely(self):
def test_decode_geometry_wkb(self):
geom = decode_geometry_item(
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@', ENC_WKB)
assert lgeos.GEOSGetSRID(geom._geom) == 0
assert get_srid(geom) == 0
assert geom.wkb == b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@'

geom = decode_geometry_item(
b'\x01\x01\x00\x00 \xe6\x10\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@', ENC_WKB) # ext
assert lgeos.GEOSGetSRID(geom._geom) == 4326
assert get_srid(geom) == 4326
assert geom.wkb == b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@'

def test_decode_geometry_wkb_hex(self):
geom = decode_geometry_item('0101000000000000000048934000000000009DB640', ENC_WKB_HEX)
assert lgeos.GEOSGetSRID(geom._geom) == 0
assert get_srid(geom) == 0
assert geom.wkb_hex == '0101000000000000000048934000000000009DB640'

geom = decode_geometry_item('0101000020E6100000000000000048934000000000009DB640', ENC_WKB_HEX) # ext
assert lgeos.GEOSGetSRID(geom._geom) == 4326
assert get_srid(geom) == 4326
assert geom.wkb_hex == '0101000000000000000048934000000000009DB640'

def test_decode_geometry_wkb_bhex(self):
geom = decode_geometry_item(b'0101000000000000000048934000000000009DB640', ENC_WKB_BHEX)
assert lgeos.GEOSGetSRID(geom._geom) == 0
assert get_srid(geom) == 0
assert geom.wkb == b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@'

geom = decode_geometry_item(b'0101000020E6100000000000000048934000000000009DB640', ENC_WKB_BHEX) # ext
assert lgeos.GEOSGetSRID(geom._geom) == 4326
assert get_srid(geom) == 4326
assert geom.wkb == b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00H\x93@\x00\x00\x00\x00\x00\x9d\xb6@'

def test_decode_geometry_wkt(self):
geom = decode_geometry_item('POINT (1234 5789)', ENC_WKT)
assert lgeos.GEOSGetSRID(geom._geom) == 0
assert get_srid(geom) == 0
assert geom.wkt == 'POINT (1234 5789)'

def test_decode_geometry_ewkt(self):
geom = decode_geometry_item('SRID=4326;POINT (1234 5789)', ENC_EWKT) # ext
assert lgeos.GEOSGetSRID(geom._geom) == 4326
assert get_srid(geom) == 4326
assert geom.wkt == 'POINT (1234 5789)'

0 comments on commit db76d4d

Please sign in to comment.