看SAM-Geo库源码时,看到了TIFF影像转矢量数据的方法,这就触动了我的专业基因,必须得保存下来,以防后续用的到.手动doge
def raster_to_vector(source, output, simplify_tolerance=None, dst_crs=None, **kwargs):"""Vectorize a raster dataset.Args:source (str): The path to the tiff file.output (str): The path to the vector file.simplify_tolerance (float, optional): The maximum allowed geometry displacement.The higher this value, the smaller the number of vertices in the resulting geometry."""from rasterio import featuresimport geopandas as gpdwith rasterio.open(source) as src:band = src.read()mask = band != 0shapes = features.shapes(band, mask=mask, transform=src.transform)fc = [{"geometry": shapely.geometry.shape(shape), "properties": {"value": value}}for shape, value in shapes]if simplify_tolerance is not None:for i in fc:i["geometry"] = i["geometry"].simplify(tolerance=simplify_tolerance)gdf = gpd.GeoDataFrame.from_features(fc)if src.crs is not None:gdf.set_crs(crs=src.crs, inplace=True)if dst_crs is not None:gdf = gdf.to_crs(dst_crs)gdf.to_file(output, **kwargs)