Skip to content

Commit fdd8f50

Browse files
committed
Fix unhandled GeometryCollection in BaseGeometry.intersection
Solves issue #566.
1 parent d85cff4 commit fdd8f50

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: packages/basemap/src/_geoslib.pyx

+24
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,30 @@ cdef class BaseGeometry:
318318
b = _get_coords(gout)
319319
p = LineString(b)
320320
pout.append(p)
321+
elif typeid == GEOS_GEOMETRYCOLLECTION:
322+
numgeoms = GEOSGetNumGeometries(g3)
323+
pout = []
324+
for i from 0 <= i < numgeoms:
325+
gout = GEOSGetGeometryN(g3, i)
326+
typeid = GEOSGeomTypeId(gout)
327+
if typeid == GEOS_POLYGON:
328+
b = _get_coords(gout)
329+
p = Polygon(b)
330+
pout.append(p)
331+
elif typeid == GEOS_LINESTRING:
332+
b = _get_coords(gout)
333+
p = LineString(b)
334+
pout.append(p)
335+
else:
336+
# More cases might need to be handled here:
337+
# - GEOS_MULTILINESTRING
338+
# - GEOS_MULTIPOLYGON
339+
# - GEOS_GEOMETRYCOLLECTION
340+
# The underlying problem is the need of a generic
341+
# converter from GEOSGeom pointers to `_geoslib`
342+
# objects, since postprocessing `GeometryCollections`
343+
# might need recursiveness.
344+
pass
321345
else:
322346
#type = PyBytes_FromString(GEOSGeomType(g3))
323347
#raise NotImplementedError("intersections of type '%s' not yet implemented" % (type))

Diff for: packages/basemap/test/mpl_toolkits/basemap/test_Basemap.py

+41
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99
import matplotlib as mpl
1010
import matplotlib.pyplot as plt
11+
from matplotlib.collections import LineCollection
1112
from matplotlib.image import AxesImage
1213
from matplotlib.patches import Polygon
1314
from mpl_toolkits.basemap import Basemap
@@ -85,6 +86,46 @@ def test_init_with_optional_casting(self):
8586
bmap2 = Basemap(lat_1=bmap2_lat_1, **kwds)
8687
self.assertEqual(bmap1.proj4string, bmap2.proj4string)
8788

89+
def test_drawcoastlines(self, axs=None, axslen0=10):
90+
"""Test that no lines are missing when drawing coastlines."""
91+
92+
axs_obj = plt.gca() if axs is None else axs
93+
axs_children = axs_obj.get_children()
94+
self.assertEqual(len(axs_children), axslen0)
95+
96+
bmap = Basemap(projection="merc", resolution="i", lat_ts=20,
97+
llcrnrlat=36.0, llcrnrlon=6.0,
98+
urcrnrlat=47.7, urcrnrlon=19.0)
99+
100+
collection = bmap.drawcoastlines(linewidth=1, color="red")
101+
self.assertIsInstance(collection, LineCollection)
102+
103+
axs_children = axs_obj.get_children()
104+
self.assertEqual(len(axs_children), axslen0 + 1)
105+
106+
lines = collection.get_paths()
107+
self.assertEqual(len(lines), 27)
108+
109+
def test_drawcountries(self, axs=None, axslen0=10):
110+
"""Test that no lines are missing when drawing country boundaries."""
111+
112+
axs_obj = plt.gca() if axs is None else axs
113+
axs_children = axs_obj.get_children()
114+
self.assertEqual(len(axs_children), axslen0)
115+
116+
bmap = Basemap(projection="merc", resolution="i", lat_ts=20,
117+
llcrnrlat=36.0, llcrnrlon=6.0,
118+
urcrnrlat=47.7, urcrnrlon=19.0)
119+
120+
collection = bmap.drawcountries(linewidth=1, color="blue")
121+
self.assertIsInstance(collection, LineCollection)
122+
123+
axs_children = axs_obj.get_children()
124+
self.assertEqual(len(axs_children), axslen0 + 1)
125+
126+
lines = collection.get_paths()
127+
self.assertEqual(len(lines), 29)
128+
88129
@unittest.skipIf(PIL is None, reason="pillow unavailable")
89130
def test_arcgisimage_with_cyl(self, axs=None, axslen0=10):
90131
"""Test showing an ArcGIS image as background."""

0 commit comments

Comments
 (0)