【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 features
    import geopandas as gpd

    with rasterio.open(source) as src:
        band = src.read()

        mask = band != 0
        shapes = 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)
要将 NetCDF 或 R-NC 格式的数据换为 GeoTIFF 格式,可以使用 Java 或 R 中的 GDAL 库。下面是一个 Java 示例代码,可以将 NetCDF 文件换为 GeoTIFF 文件: ```java import org.gdal.gdal.gdal; import org.gdal.gdalconst.gdalconst; import org.gdal.osr.SpatialReference; import org.gdal.osr.osr; public class NetcdfToGeoTiff { public static void main(String[] args) { // Register GDAL drivers gdal.AllRegister(); // Open NetCDF file String inputFilePath = "path/to/netcdf/file.nc"; gdal.Dataset netcdfDataset = gdal.Open(inputFilePath, gdalconst.GA_ReadOnly); // Get number of bands and dimensions int numBands = netcdfDataset.getRasterCount(); int numDims = netcdfDataset.getRasterXSize() * netcdfDataset.getRasterYSize(); // Read data from NetCDF file float[] data = new float[numDims * numBands]; netcdfDataset.readRaster(0, 0, netcdfDataset.getRasterXSize(), netcdfDataset.getRasterYSize(), data); // Create output GeoTIFF file String outputFilePath = "path/to/geotiff/file.tiff"; gdal.Driver driver = gdal.GetDriverByName("GTiff"); gdal.Dataset geotiffDataset = driver.Create(outputFilePath, netcdfDataset.getRasterXSize(), netcdfDataset.getRasterYSize(), numBands, gdalconst.GDT_Float32); // Set projection and geotransform SpatialReference srs = new SpatialReference(netcdfDataset.GetProjectionRef()); geotiffDataset.SetProjection(srs.ExportToWkt()); double[] geoTransform = new double[6]; netcdfDataset.GetGeoTransform(geoTransform); geotiffDataset.SetGeoTransform(geoTransform); // Write data to GeoTIFF file for (int i = 0; i < numBands; i++) { gdal.Band band = geotiffDataset.GetRasterBand(i + 1); band.WriteRaster(0, 0, netcdfDataset.getRasterXSize(), netcdfDataset.getRasterYSize(), data, netcdfDataset.getRasterXSize(), netcdfDataset.getRasterYSize(), gdalconst.GDT_Float32, 0, 0); band.FlushCache(); } // Close datasets geotiffDataset.delete(); netcdfDataset.delete(); } } ``` 请注意,此示例代码需要 GDAL 和 JavaGDAL 库。你需要将这些库添加到你的项目中。 同样,你也可以使用 R 中的 GDAL 包来换 R-NC 数据。下面是一个简单的 R 示例代码: ```R library(gdal) # Open R-NC file inputFilePath <- "path/to/r-nc/file.nc" rncDataset <- gdalUtils::gdal_open(inputFilePath) # Get number of bands and dimensions numBands <- rncDataset@nbands numDims <- rncDataset@nrows * rncDataset@ncols # Read data from R-NC file data <- gdalUtils::gdal_read(rncDataset) # Create output GeoTIFF file outputFilePath <- "path/to/geotiff/file.tiff" geotiffDataset <- gdalUtils::gdal_translate(data, dst_dataset=outputFilePath, format="GTiff") # Set projection and geotransform geotiffDataset@proj4string <- rncDataset@proj4string geotiffDataset@extent <- rncDataset@extent # Close datasets gdalUtils::gdal_close(rncDataset) gdalUtils::gdal_close(geotiffDataset) ``` 请注意,此示例代码需要 GDAL 和 gdalUtils 包。你需要将这些库添加到你的 R 环境中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值