Skip to content

Commit

Permalink
Merge pull request #1755 from CartoDB/chore/sc-279134/cartoframes-not…
Browse files Browse the repository at this point in the history
…-working-with-shapely-2-0

Add compatibility with shapely>=1.7
  • Loading branch information
Shylpx authored Feb 8, 2023
2 parents 7c7392b + db76d4d commit 1f56b75
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 23 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/cartoframes-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ on:
jobs:
test:
strategy:
fail-fast: false
matrix:
python-version: [3.5, 3.6, 3.7, 3.8]
include:
- os: ubuntu-20.04
python-version: 3.5
- os: ubuntu-20.04
python-version: 3.6
- os: ubuntu-latest
python-version: 3.7
- os: ubuntu-latest
python-version: 3.8

name: Run tests on Python ${{ matrix.python-version }}

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
init:
pip install -e .
pip install -U pip
pip install -e .[tests]

lint:
flake8 cartoframes tests

test:
pytest tests/unit/
Expand Down
5 changes: 3 additions & 2 deletions cartoframes/data/observatory/catalog/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ def subscribe(self, credentials=None):

@check_do_enabled
def subscription_info(self, credentials=None):
"""Get the subscription information of a Dataset, which includes the license, Terms of Service, rights, price, and
estimated time of delivery, among other metadata of interest during the :py:attr:`Dataset.subscription` process.
"""Get the subscription information of a Dataset, which includes the license, Terms of Service, rights, price,
and estimated time of delivery, among other metadata of interest during the :py:attr:`Dataset.subscription`
process.
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
Expand Down
2 changes: 1 addition & 1 deletion cartoframes/io/managers/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def has_table(self, table_name, schema=None):
def delete_table(self, table_name):
query = _drop_table_query(table_name)
output = self.execute_query(query)
return not('notices' in output and 'does not exist' in output['notices'][0])
return not ('notices' in output and 'does not exist' in output['notices'][0])

def _delete_function(self, function_name):
query = _drop_function_query(function_name)
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: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_version():
REQUIRES = [
'appdirs>=1.4.3,<2.0',
'carto>=1.11.3,<2.0',
'markupsafe<=2.0.1',
'jinja2>=2.10.1,<3.0',
'pandas>=0.25.0',
'geopandas>=0.6.0,<1.0',
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/io/test_carto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pandas import Index
from geopandas import GeoDataFrame
from shapely.geometry import Point
from shapely.geometry.base import BaseGeometry
from shapely import wkt

from carto.exceptions import CartoException
Expand Down Expand Up @@ -83,17 +82,18 @@ def test_read_carto_basegeometry_as_null_geom_value(mocker):
})

# When
gdf = read_carto('__source__', CREDENTIALS, null_geom_value=BaseGeometry())
gdf = read_carto('__source__', CREDENTIALS, null_geom_value=None)

# Then
expected = GeoDataFrame({
'cartodb_id': [1],
'the_geom': [
BaseGeometry()
None
]
}, geometry='the_geom')

cm_mock.assert_called_once_with('__source__', None, None, 3)
print(expected, gdf)
assert expected.equals(gdf)
assert gdf.crs == 'epsg:4326'

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 1f56b75

Please sign in to comment.