我有一个光栅文件和一个WGS84纬度/经度点。
我想知道栅格中的哪个值与该点相对应。
我的感觉是我应该在栅格对象或其中一个波段上使用GetSpatialRef()
,然后将ogr.osr.CoordinateTransformation()
应用于该点以将其映射到栅格的空间。
我希望我可以简单地问一下歌手的乐队当时的情况。
但是,栅格对象似乎没有GetSpatialRef()
或访问地理位置点的方法,所以我对如何执行此操作感到有些不知所措。
有什么想法吗?
答案 0 :(得分:2)
说我有一个geotiff文件test.tif。然后跟随代码应该在像素附近的某处查找值。我对查找单元格的部分没有信心,并且会修复错误。该页面应该有帮助,"GDAL Data Model"
另外,如果你没有,你可以去gis.stackexchange.com寻找专家。
import gdal, osr
class looker(object):
"""let you look up pixel value"""
def __init__(self, tifname='test.tif'):
"""Give name of tif file (or other raster data?)"""
# open the raster and its spatial reference
self.ds = gdal.Open(tifname)
srRaster = osr.SpatialReference(self.ds.GetProjection())
# get the WGS84 spatial reference
srPoint = osr.SpatialReference()
srPoint.ImportFromEPSG(4326) # WGS84
# coordinate transformation
self.ct = osr.CoordinateTransformation(srPoint, srRaster)
# geotranformation and its inverse
gt = self.ds.GetGeoTransform()
dev = (gt[1]*gt[5] - gt[2]*gt[4])
gtinv = ( gt[0] , gt[5]/dev, -gt[2]/dev,
gt[3], -gt[4]/dev, gt[1]/dev)
self.gt = gt
self.gtinv = gtinv
# band as array
b = self.ds.GetRasterBand(1)
self.arr = b.ReadAsArray()
def lookup(self, lon, lat):
"""look up value at lon, lat"""
# get coordinate of the raster
xgeo,ygeo,zgeo = self.ct.TransformPoint(lon, lat, 0)
# convert it to pixel/line on band
u = xgeo - self.gtinv[0]
v = ygeo - self.gtinv[3]
# FIXME this int() is probably bad idea, there should be
# half cell size thing needed
xpix = int(self.gtinv[1] * u + self.gtinv[2] * v)
ylin = int(self.gtinv[4] * u + self.gtinv[5] * v)
# look the value up
return self.arr[ylin,xpix]
# test
l = looker('test.tif')
lon,lat = -100,30
print l.lookup(lon,lat)
lat,lon =28.816944, -96.993333
print l.lookup(lon,lat)
答案 1 :(得分:0)
是的,API不一致。栅格(数据源)改为使用GetProjection()
方法(返回WKT)。
这是一个完成你想要的功能(从here中提取):
def extract_point_from_raster(point, data_source, band_number=1):
"""Return floating-point value that corresponds to given point."""
# Convert point co-ordinates so that they are in same projection as raster
point_sr = point.GetSpatialReference()
raster_sr = osr.SpatialReference()
raster_sr.ImportFromWkt(data_source.GetProjection())
transform = osr.CoordinateTransformation(point_sr, raster_sr)
point.Transform(transform)
# Convert geographic co-ordinates to pixel co-ordinates
x, y = point.GetX(), point.GetY()
forward_transform = Affine.from_gdal(*data_source.GetGeoTransform())
reverse_transform = ~forward_transform
px, py = reverse_transform * (x, y)
px, py = int(px + 0.5), int(py + 0.5)
# Extract pixel value
band = data_source.GetRasterBand(band_number)
structval = band.ReadRaster(px, py, 1, 1, buf_type=gdal.GDT_Float32)
result = struct.unpack('f', structval)[0]
if result == band.GetNoDataValue():
result = float('nan')
return result
其文件如下(摘自here):
spatial.extract_point_from_raster(point, data_source, band_number=1)
data_source是GDAL栅格,point是OGR点对象。该 function返回指定band的像素值 最靠近point的data_source。
point和data_source不需要在同一个参考系统中,但是 他们必须都定义了适当的空间参考。
如果该点未落入栅格中,则会引发RuntimeError。
答案 2 :(得分:-1)
project = self.ds.GetProjection()
srPoint = osr.SpatialReference(wkt=project)
完成...因此,矢量文件采用了输入光栅文件的投影