Как скопировать пространственную привязку из шейп-файла в растр geotiff?

Я создал скрипт Python, который создает растровый файл geotiff из шейп-файла. В настоящее время созданный геотиф не содержит пространственной привязки шейп-файла. Как скопировать пространственную привязку из шейп-файла в геотиф? Я попытался скопировать пространственную привязку шейп-файла в геотифф с помощью команды: target_ds.SetProjection(source_layer.GetSpatialRef())
Я думаю, что объект пространственной привязки, связанный с шейп-файлом, отличается от геотиффа, но не не знаю, как перейти от одного к другому.

# 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.

person Philip Whitten    schedule 23.06.2016    source источник


Ответы (1)