def assert_geometries_equal(actual, expected): actual = np.asarray(actual) expected = np.broadcast_to(expected, actual.shape) mask = pygeos.is_geometry(expected) if np.any(mask): assert pygeos.equals(actual[mask], expected[mask]).all() if np.any(~mask): assert pygeos.is_missing(actual[~mask])
def _cast(collection): """ Cast a collection to a pygeos geometry array. """ if isinstance(collection, (geopandas.GeoSeries, geopandas.GeoDataFrame)): return collection.geometry.values.data.squeeze() elif pygeos.is_geometry(collection).all(): if isinstance(collection, (numpy.ndarray, list)): return numpy.asarray(collection) else: return numpy.array([collection]) elif isinstance(collection, (numpy.ndarray, list)): return pygeos.from_shapely(collection).squeeze() else: return numpy.array([pygeos.from_shapely(collection)])
def _cast(collection): """ Cast a collection to a pygeos geometry array. """ try: import pygeos, geopandas except (ImportError, ModuleNotFoundError) as exception: raise type(exception)( "pygeos and geopandas are required for map comparison statistics.") if isinstance(collection, (geopandas.GeoSeries, geopandas.GeoDataFrame)): return collection.geometry.values.data.squeeze() elif pygeos.is_geometry(collection).all(): if isinstance(collection, (numpy.ndarray, list)): return numpy.asarray(collection) else: return numpy.array([collection]) elif isinstance(collection, (numpy.ndarray, list)): return pygeos.from_shapely(collection).squeeze() else: return numpy.array([pygeos.from_shapely(collection)])
def test_subclass_is_geometry(with_point_in_registry): assert pygeos.is_geometry(Point("POINT (1 1)"))
def test_from_wkt_empty(wkt): geom = pygeos.from_wkt(wkt) assert pygeos.is_geometry(geom).all() assert pygeos.is_empty(geom).all() assert pygeos.to_wkt(geom) == wkt
def test_from_wkb_empty(wkt): wkb = pygeos.to_wkb(pygeos.Geometry(wkt)) geom = pygeos.from_wkb(wkb) assert pygeos.is_geometry(geom).all() assert pygeos.is_empty(geom).all() assert pygeos.to_wkb(geom) == wkb
if not filename.exists(): print(f"WARNING: {filename} not found") continue # Extract and merge lakes and wetlands df = read_dataframe( f"/vsizip/{filename}/HU8_{huc8}_Watershed/HU8_{huc8}_Wetlands.shp", columns=["ATTRIBUTE", "WETLAND_TY"], where="WETLAND_TY in ('Lake', 'Pond', 'Riverine')", ).rename(columns={ "ATTRIBUTE": "nwi_code", "WETLAND_TY": "nwi_type" }) # some geometries are invalid, filter them out df = df.loc[pg.is_geometry(df.geometry.values.data)].copy() if not len(df): continue df = df.to_crs(CRS) # Mark structurally altered types where # codes with x (excavated), d (ditched), r (artificial substrate), h (diked) # strip any terminal numbers then take last character df["modifier"] = df.nwi_code.str.rstrip("123456789").str[-1:] df["altered"] = df.modifier.isin(MODIFIERS) waterbodies = append(waterbodies, df.loc[df.nwi_type.isin(["Lake", "Pond"])])