def test_null_geometry(): pth = Path([[358.27203369, 3.56399965], [358.27203369, 3.56399965], [358.27203369, 3.56399965]]) geoms = cpatch.path_to_geos(pth) assert len(geoms) == 0
def test_empty_polyong(self): p = Path([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], codes=[1, 2, 2, 79, 1, 2, 2, 79]) geom = cpatch.path_to_geos(p) self.assertEqual(len(geom), 0)
def test_empty_polyon(self): p = Path( [[0, 0], [0, 0], [0, 0], [0, 0], [1, 2], [1, 2], [1, 2], [1, 2]], codes=[1, 2, 2, 79, 1, 2, 2, 79]) geoms = cpatch.path_to_geos(p) assert [type(geom) for geom in geoms] == [sgeom.Point, sgeom.Point] assert len(geoms) == 2
def test_empty_polygon(self): p = Path( [ [0, 0], [0, 0], [0, 0], [0, 0], [1, 2], [1, 2], [1, 2], [1, 2], # The vertex for CLOSEPOLY should be ignored. [2, 3], [2, 3], [2, 3], [42, 42], # Very close points should be treated the same. [193.75, -14.166664123535156], [193.75, -14.166664123535158], [193.75, -14.166664123535156], [193.75, -14.166664123535156], ], codes=[1, 2, 2, 79] * 4) geoms = cpatch.path_to_geos(p) assert [type(geom) for geom in geoms] == [sgeom.Point] * 4 assert len(geoms) == 4
def test_empty_polyon(self): p = Path([[0, 0], [0, 0], [0, 0], [0, 0], [1, 2], [1, 2], [1, 2], [1, 2]], codes=[1, 2, 2, 79, 1, 2, 2, 79]) geoms = cpatch.path_to_geos(p) assert [type(geom) for geom in geoms] == [sgeom.Point, sgeom.Point] assert len(geoms) == 2
def test_empty_polyon(self): p = Path( [[0, 0], [0, 0], [0, 0], [0, 0], [1, 2], [1, 2], [1, 2], [1, 2]], codes=[1, 2, 2, 79, 1, 2, 2, 79]) geoms = cpatch.path_to_geos(p) self.assertEqual(list(type(geom) for geom in geoms), [sgeom.Point, sgeom.Point]) self.assertEqual(len(geoms), 2)
def test_empty_polyon(self): p = Path([[0, 0], [0, 0], [0, 0], [0, 0], [1, 2], [1, 2], [1, 2], [1, 2]], codes=[1, 2, 2, 79, 1, 2, 2, 79]) geoms = cpatch.path_to_geos(p) self.assertEqual(list(type(geom) for geom in geoms), [sgeom.Point, sgeom.Point]) self.assertEqual(len(geoms), 2)
def test_polygon_with_interior_and_singularity(self): # A geometry with two interiors, one a single point. p = Path([[0, -90], [200, -40], [200, 40], [0, 40], [0, -90], [126, 26], [126, 26], [126, 26], [126, 26], [126, 26], [114, 5], [103, 8], [126, 12], [126, 0], [114, 5]], codes=[1, 2, 2, 2, 79, 1, 2, 2, 2, 79, 1, 2, 2, 2, 79]) geoms = cpatch.path_to_geos(p) assert [type(geom) for geom in geoms] == [sgeom.Polygon, sgeom.Point] assert len(geoms[0].interiors) == 1
def test_polygon_with_interior_and_singularity(self): # A geometry with two interiors, one a single point. p = Path([[0, -90], [200, -40], [200, 40], [0, 40], [0, -90], [126, 26], [126, 26], [126, 26], [126, 26], [126, 26], [114, 5], [103, 8], [126, 12], [126, 0], [114, 5]], codes=[1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2]) geoms = cpatch.path_to_geos(p) self.assertEqual(list(type(geom) for geom in geoms), [sgeom.Polygon, sgeom.Point]) self.assertEqual(len(geoms[0].interiors), 1)
def test_polygon_interiors(): ax = plt.subplot(211, projection=ccrs.PlateCarree()) ax.coastlines() ax.set_global() # XXX could be the default??? pth = Path([[0, -45], [60, -45], [60, 45], [0, 45], [0, 45], [10, -20], [10, 20], [40, 20], [40, -20], [10, 20]], [1, 2, 2, 2, 79, 1, 2, 2, 2, 79]) patches_native = [] patches = [] for geos in cpatch.path_to_geos(pth): for pth in cpatch.geos_to_path(geos): patches.append(mpatches.PathPatch(pth)) # buffer by 10 degrees (leaves a small hole in the middle) geos_buffered = geos.buffer(10) for pth in cpatch.geos_to_path(geos_buffered): patches_native.append(mpatches.PathPatch(pth)) # Set high zorder to ensure the polygons are drawn on top of coastlines. collection = PatchCollection(patches_native, facecolor='red', alpha=0.4, transform=ax.projection, zorder=10) ax.add_collection(collection) collection = PatchCollection(patches, facecolor='yellow', alpha=0.4, transform=ccrs.Geodetic(), zorder=10) ax.add_collection(collection) # test multiple interior polygons ax = plt.subplot(212, projection=ccrs.PlateCarree(), xlim=[-5, 15], ylim=[-5, 15]) ax.coastlines() exterior = np.array(sgeom.box(0, 0, 12, 12).exterior.coords) interiors = [np.array(sgeom.box(1, 1, 2, 2, ccw=False).exterior.coords), np.array(sgeom.box(1, 8, 2, 9, ccw=False).exterior.coords)] poly = sgeom.Polygon(exterior, interiors) patches = [] for pth in cpatch.geos_to_path(poly): patches.append(mpatches.PathPatch(pth)) collection = PatchCollection(patches, facecolor='yellow', alpha=0.4, transform=ccrs.Geodetic(), zorder=10) ax.add_collection(collection)
def test_polygon_interiors(): ax = plt.subplot(211, projection=ccrs.PlateCarree()) ax.coastlines() ax.set_global() pth = Path([[0, -45], [60, -45], [60, 45], [0, 45], [0, 45], [10, -20], [10, 20], [40, 20], [40, -20], [10, 20]], [1, 2, 2, 2, 79, 1, 2, 2, 2, 79]) patches_native = [] patches = [] for geos in cpatch.path_to_geos(pth): for pth in cpatch.geos_to_path(geos): patches.append(mpatches.PathPatch(pth)) # buffer by 10 degrees (leaves a small hole in the middle) geos_buffered = geos.buffer(10) for pth in cpatch.geos_to_path(geos_buffered): patches_native.append(mpatches.PathPatch(pth)) # Set high zorder to ensure the polygons are drawn on top of coastlines. collection = PatchCollection(patches_native, facecolor='red', alpha=0.4, transform=ax.projection, zorder=10) ax.add_collection(collection) collection = PatchCollection(patches, facecolor='yellow', alpha=0.4, transform=ccrs.Geodetic(), zorder=10) ax.add_collection(collection) # test multiple interior polygons ax = plt.subplot(212, projection=ccrs.PlateCarree(), xlim=[-5, 15], ylim=[-5, 15]) ax.coastlines() exterior = np.array(sgeom.box(0, 0, 12, 12).exterior.coords) interiors = [np.array(sgeom.box(1, 1, 2, 2, ccw=False).exterior.coords), np.array(sgeom.box(1, 8, 2, 9, ccw=False).exterior.coords)] poly = sgeom.Polygon(exterior, interiors) patches = [] for pth in cpatch.geos_to_path(poly): patches.append(mpatches.PathPatch(pth)) collection = PatchCollection(patches, facecolor='yellow', alpha=0.4, transform=ccrs.Geodetic(), zorder=10) ax.add_collection(collection)
def transform_path_non_affine(self, src_path): """ Transforms from source to target coordinates. Caches results, so subsequent calls with the same *src_path* argument (and the same source and target projections) are faster. Args: * src_path - A matplotlib :class:`~matplotlib.path.Path` object with vertices in source coordinates. Returns * A matplotlib :class:`~matplotlib.path.Path` with vertices in target coordinates. """ mapping = _PATH_TRANSFORM_CACHE.get(src_path) if mapping is not None: key = (self.source_projection, self.target_projection) result = mapping.get(key) if result is not None: return result # Allow the vertices to be quickly transformed, if # quick_vertices_transform allows it. new_vertices = self.target_projection.quick_vertices_transform( src_path.vertices, self.source_projection) if new_vertices is not None: if new_vertices is src_path.vertices: return src_path else: return mpath.Path(new_vertices, src_path.codes) if src_path.vertices.shape == (1, 2): return mpath.Path(self.transform(src_path.vertices)) transformed_geoms = [] for geom in patch.path_to_geos(src_path): transformed_geoms.append( self.target_projection.project_geometry(geom, self.source_projection) ) if not transformed_geoms: result = mpath.Path(np.empty([0, 2])) else: paths = patch.geos_to_path(transformed_geoms) if not paths: return mpath.Path(np.empty([0, 2])) points, codes = zip(*[patch.path_segments(path, curves=False, simplify=False) for path in paths]) result = mpath.Path(np.concatenate(points, 0), np.concatenate(codes)) # store the result in the cache for future performance boosts key = (self.source_projection, self.target_projection) if mapping is None: _PATH_TRANSFORM_CACHE[src_path] = {key: result} else: mapping[key] = result return result
def test_non_polygon_loop(self): p = Path([[0, 10], [170, 20], [-170, 30], [0, 10]], codes=[1, 2, 2, 2]) geoms = cpatch.path_to_geos(p) assert [type(geom) for geom in geoms] == [sgeom.MultiLineString] assert len(geoms) == 1
lons, lats, data = sample_data() plt.figure() ax = plt.axes(projection=ccrs.PlateCarree()) cs = plt.contourf( lons, lats, data, 5, # Choose approximately 5 sensible levels. transform=ccrs.PlateCarree()) ax.coastlines() plt.colorbar(orientation='horizontal') plt.show() paths=cs.collections[4].get_paths() geoms=[] for path in paths: geoms.extend(path_to_geos(path)) polygon=shapely.ops.unray_union(geoms) with open('contour.geojson', 'w') as fh: json.dump(sgeom.mapping(polygon), fh) atw80d = skimage.io.imread('640px-Around_the_World_in_Eighty_Days_map.png') yellowish=((atw80d[:, :, 0]>200)& (atw80d[:, :, 1]>200)& (atw80d[:, :, 2]<100)& (atw80d[:, :, 3]>250)) ind_y, ind_x=np.where(yellowish)