Skip to content

Commit f89f17c

Browse files
authored
Merge pull request #2110 from jorisvandenbossche/compat-shapely-2-for-0.21
Compatibility with shapely 2.0 (remove usage of lgeos)
2 parents 9a4d894 + e8a77a2 commit f89f17c

File tree

9 files changed

+32
-15
lines changed

9 files changed

+32
-15
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ deps-run: &deps-install
4545
pyshp \
4646
scipy \
4747
setuptools_scm \
48-
'shapely<2' \
48+
shapely \
4949
$EXTRA_PACKAGES \
5050
--file docs/doc-requirements.txt
5151
conda list -n test-environment

.github/workflows/ci-testing.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ jobs:
1212
matrix:
1313
os: [ubuntu-latest, macos-latest]
1414
python-version: [3.8, 3.9, '3.10']
15+
shapely-dev: [false]
16+
include:
17+
- os: ubuntu-latest
18+
python-version: '3.10'
19+
shapely-dev: true
1520
defaults:
1621
run:
1722
shell: bash -l {0}
@@ -33,9 +38,14 @@ jobs:
3338
echo "PACKAGES=cython=0.29.15 matplotlib-base=3.2.1 numpy=1.19 owslib=0.19.1 pyproj=3.0 scipy=1.4.0 shapely=1.6.4" >> $GITHUB_ENV
3439
3540
- name: Latest packages
36-
if: steps.minimum-packages.conclusion == 'skipped'
41+
if: steps.minimum-packages.conclusion == 'skipped' && !matrix.shapely-dev
3742
run: |
38-
echo "PACKAGES=cython fiona matplotlib-base numpy pyproj pykdtree scipy shapely<2" >> $GITHUB_ENV
43+
echo "PACKAGES=cython fiona matplotlib-base numpy pyproj pykdtree scipy shapely" >> $GITHUB_ENV
44+
45+
- name: Latest packages with Shapely dev
46+
if: steps.minimum-packages.conclusion == 'skipped' && matrix.shapely-dev
47+
run: |
48+
echo "PACKAGES=cython fiona matplotlib-base numpy pyproj pykdtree scipy geos" >> $GITHUB_ENV
3949
4050
- name: Coverage packages
4151
id: coverage
@@ -49,11 +59,16 @@ jobs:
4959
- name: Install dependencies
5060
run: |
5161
PACKAGES="$PACKAGES owslib pep8 pillow pyshp pytest pytest-mpl!=0.16.0"
52-
PACKAGES="$PACKAGES pytest-xdist setuptools_scm shapely<2"
62+
PACKAGES="$PACKAGES pytest-xdist setuptools_scm"
5363
conda install $PACKAGES
5464
conda info -a
5565
conda list
5666
67+
- name: Install Shapely dev
68+
if: matrix.shapely-dev
69+
run: |
70+
python -m pip install git+https://github.com/shapely/shapely.git@main
71+
5772
- name: Install Cartopy
5873
id: install
5974
run: |

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: |
2727
PACKAGES="cython fiona matplotlib-base numpy pyproj pykdtree scipy"
2828
PACKAGES="$PACKAGES owslib pep8 pillow pyshp pytest"
29-
PACKAGES="$PACKAGES pytest-xdist setuptools_scm shapely<2"
29+
PACKAGES="$PACKAGES pytest-xdist setuptools_scm shapely"
3030
conda install $PACKAGES
3131
3232
- name: Create sdist

INSTALL

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ For macOS, the required dependencies can be installed in the following way::
8585
pip3 install --upgrade pyshp
8686
# shapely needs to be built from source to link to geos. If it is already
8787
# installed, uninstall it by: pip3 uninstall shapely
88-
pip3 install "shapely<2" --no-binary shapely
88+
pip3 install shapely --no-binary shapely
8989

9090
Still on macOS, make sure you have installed pkg-config and set the
9191
`PKG_CONFIG_PATH` environment variable as follows::
@@ -117,7 +117,7 @@ Further information about the required dependencies can be found here:
117117
GEOS is an API of spatial predicates and functions for processing geometry
118118
written in C++.
119119

120-
**Shapely** between 1.6.4 and 1.8.4 (https://github.com/Toblerity/Shapely)
120+
**Shapely** 1.6.4 or later (https://github.com/Toblerity/Shapely)
121121
Python package for the manipulation and analysis of planar geometric
122122
objects.
123123

environment.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
# conda activate cartopy-dev
55
# pip install -e .
66
#
7-
name: cartopy-dev2
7+
name: cartopy-dev
88
channels:
99
- conda-forge
1010
dependencies:
1111
- cython>=0.28.5
1212
- numpy>=1.18
13-
- shapely>=1.6.4,<2
13+
- shapely>=1.6.4
1414
- geos>=3.7.2
1515
- pyshp>=2.1
1616
- pyproj>=3.0.0

lib/cartopy/feature/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def intersecting_geometries(self, extent):
100100
geometries for this dataset.
101101
102102
"""
103-
if extent is not None:
103+
# shapely 2.0 returns tuple of NaNs instead of None for empty geometry
104+
# -> check for both
105+
if extent is not None and not np.isnan(extent[0]):
104106
extent_geom = sgeom.box(extent[0], extent[2],
105107
extent[1], extent[3])
106108
return (geom for geom in self.geometries() if

lib/cartopy/mpl/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def path_to_geos(path, force_ccw=False):
208208

209209
# Remove any zero area Polygons
210210
def not_zero_poly(geom):
211-
return ((isinstance(geom, sgeom.Polygon) and not geom._is_empty and
211+
return ((isinstance(geom, sgeom.Polygon) and not geom.is_empty and
212212
geom.area != 0) or
213213
not isinstance(geom, sgeom.Polygon))
214214

lib/cartopy/trace.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ cdef extern from "geos_c.h":
3131
pass
3232
ctypedef struct GEOSCoordSequence
3333
ctypedef struct GEOSPreparedGeometry
34+
GEOSContextHandle_t GEOS_init_r() nogil
3435
GEOSCoordSequence *GEOSCoordSeq_create_r(GEOSContextHandle_t, unsigned int, unsigned int) nogil
3536
GEOSGeometry *GEOSGeom_createPoint_r(GEOSContextHandle_t, GEOSCoordSequence *) nogil
3637
GEOSGeometry *GEOSGeom_createLineString_r(GEOSContextHandle_t, GEOSCoordSequence *) nogil
@@ -53,14 +54,13 @@ import re
5354
import warnings
5455

5556
import shapely.geometry as sgeom
56-
from shapely.geos import lgeos
5757
from pyproj import Geod, Transformer
5858
from pyproj.exceptions import ProjError
5959

6060

6161
cdef GEOSContextHandle_t get_geos_context_handle():
62-
cdef ptr handle = lgeos.geos_handle
63-
return <GEOSContextHandle_t>handle
62+
cdef GEOSContextHandle_t handle = GEOS_init_r()
63+
return handle
6464

6565

6666
cdef GEOSGeometry *geos_from_shapely(shapely_geom) except *:

requirements/default.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
numpy>=1.18
22
matplotlib>=3.1
3-
shapely>=1.6.4,<2
3+
shapely>=1.6.4
44
pyshp>=2.1
55
pyproj>=3.0.0

0 commit comments

Comments
 (0)