【发布时间】:2017-02-16 11:31:16
【问题描述】:
在 Python 脚本中使用 OGR 库或 GDAL 库时,是否可以在不实际添加新数据点的情况下增加矢量图层的范围?在我的具体情况下,我想增加与 gpx 文件关联的矢量图层的范围,以便当我将它们转换为栅格时,它们都具有相同的像素矩阵。
编辑:我尝试使用gdal.Rasterize 不会产生“tiff”文件,也不会导致报告错误:
import os
import gdal
import ogr
import math
os.chdir(r'C:\Users\pipi\Documents\Rogaine\Tarlo\gpx') #folder containing gpx files
vector_fn = '6_hour_Autumngaine_w_Tom_Elle.gpx' #filename of input gpxfile
pixel_size = 20 #units are in m if gpx file is left in wgs84
raster_fn = '0011a.tif' # Filename of the raster Tiff that will be created
driver = ogr.GetDriverByName('GPX')
source_ds = driver.Open(vector_fn, 0)
source_layer = source_ds.GetLayer('track_points') #returns the 'track points' layer of the data source
SR = source_layer.GetSpatialRef().ExportToWkt()
#_______USING VALUES FROM THE FILE___________
x_min1, x_max1, y_min1, y_max1 = source_layer.GetExtent()
pixel_sizey = pixel_size/(111.2*math.pow(10,3)) #determines an approximate x and y size because of geographic coordinates.
pixel_sizex = pixel_size/(math.cos(((y_max1 + y_min1)/2)*(math.pi/180))*111.2*math.pow(10,3))
print (pixel_sizey, pixel_sizex)
x_res = int((x_max1 - x_min1) / pixel_sizex)
y_res = int((y_max1 - y_min1) / pixel_sizey)
print (x_res, y_res)
layer_list = ['track_points']
gdal.Rasterize(raster_fn, vector_fn, format='GTiff', outputBounds=[x_min1, y_min1, x_max1, y_max1], outputSRS=SR, xRes=x_res, yRes=y_res, burnValues=[1], layers=layer_list)
target_ds = None
vector_fn = None
source_layer = None
source_ds = None
【问题讨论】:
-
你在使用
gdal.Rasterize()吗? -
不,我使用的是
gdal.RasterizeLayer()。 -
我会很感激帮助,或者使用 gdal.Rasterize() 工作的 python 代码的副本,即使它用于其他目的。我使用 gdal.Rasterize() 的努力并没有导致不产生 tiff。
标签: python geospatial gdal ogr extent