如何将空间参考从 shapefile 复制到 geotiff 栅格?
How to copy the spatial reference from a shapefile to a geotiff raster?
我构建了一个 python 脚本,该脚本从 shapefile 构建 geotiff 栅格。目前生成的 geotiff 不包含 shapefile 的空间参考。如何将空间参考从 shapefile 复制到 geotiff?我尝试通过以下命令将 shapefile 的空间参考复制到 geotiff:target_ds.SetProjection(source_layer.GetSpatialRef())
我认为与 shapefile 关联的空间参考对象与 geotiff 不同,但是,不知道如何从一个到另一个。
# This code creates a raster from a shapefile.
# Every feature in the shapefile is included in the raster.
import os
import gdal
import ogr
os.chdir(r'C:\Users\pipi\Documents\Rogaine\Tarlo\gpx') #folder containing gpx files
vector_fn = 'gpxcollection.shp' #filename of input shapefile
pixel_size = 25 #same unit as coordinates
raster_fn = 'test.tif' # Filename of the raster Tiff that will be created
#______Open's the data source and reads the extent________
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer() #returns the first layer in the data source
x_min, x_max, y_min, y_max = source_layer.GetExtent()
#______Create the destination raster file__________
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
# create the target raster file with 1 band
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
#______Populates the raster file with the data from the shapefile____
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[1])
del target_ds #flushes data from memory. Without this you often get an empty raster.
GDAL dataset.SetProjection()
方法需要众所周知的文本 (WKT) 或 PROJ4 字符串,根据 the docs:"The string should be in OGC WKT or PROJ.4 format"。
您可以通过执行以下操作之一实现此目的:
source_layer.GetSpatialRef().ExportToWkt()
source_layer.GetSpatialRef().ExportToProj4()
我构建了一个 python 脚本,该脚本从 shapefile 构建 geotiff 栅格。目前生成的 geotiff 不包含 shapefile 的空间参考。如何将空间参考从 shapefile 复制到 geotiff?我尝试通过以下命令将 shapefile 的空间参考复制到 geotiff:target_ds.SetProjection(source_layer.GetSpatialRef())
我认为与 shapefile 关联的空间参考对象与 geotiff 不同,但是,不知道如何从一个到另一个。
# This code creates a raster from a shapefile.
# Every feature in the shapefile is included in the raster.
import os
import gdal
import ogr
os.chdir(r'C:\Users\pipi\Documents\Rogaine\Tarlo\gpx') #folder containing gpx files
vector_fn = 'gpxcollection.shp' #filename of input shapefile
pixel_size = 25 #same unit as coordinates
raster_fn = 'test.tif' # Filename of the raster Tiff that will be created
#______Open's the data source and reads the extent________
source_ds = ogr.Open(vector_fn)
source_layer = source_ds.GetLayer() #returns the first layer in the data source
x_min, x_max, y_min, y_max = source_layer.GetExtent()
#______Create the destination raster file__________
x_res = int((x_max - x_min) / pixel_size)
y_res = int((y_max - y_min) / pixel_size)
# create the target raster file with 1 band
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
band = target_ds.GetRasterBand(1)
#______Populates the raster file with the data from the shapefile____
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[1])
del target_ds #flushes data from memory. Without this you often get an empty raster.
GDAL dataset.SetProjection()
方法需要众所周知的文本 (WKT) 或 PROJ4 字符串,根据 the docs:"The string should be in OGC WKT or PROJ.4 format"。
您可以通过执行以下操作之一实现此目的:
source_layer.GetSpatialRef().ExportToWkt()
source_layer.GetSpatialRef().ExportToProj4()