本文主要是介绍【Geo-AI】Tiff影像转vector方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看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)
这篇关于【Geo-AI】Tiff影像转vector方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!