mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-15 00:07:25 -05:00
use python API to get raster extent, instead of relying on gdalinfo
This commit is contained in:
parent
1d812b3187
commit
4b9e181e23
@ -306,35 +306,26 @@ def getRasterSRS( parent, fileName ):
|
||||
|
||||
return ''
|
||||
|
||||
# get raster extent using python API - replaces old method which parsed gdalinfo output
|
||||
def getRasterExtent(parent, fileName):
|
||||
processSRS = QProcess( parent )
|
||||
processSRS.start( "gdalinfo", [fileName], QIODevice.ReadOnly )
|
||||
arr = ''
|
||||
if processSRS.waitForFinished():
|
||||
arr = str(processSRS.readAllStandardOutput())
|
||||
processSRS.close()
|
||||
ds = gdal.Open(fileName)
|
||||
if ds is None:
|
||||
return
|
||||
|
||||
if arr == '':
|
||||
return
|
||||
x = ds.RasterXSize
|
||||
y = ds.RasterYSize
|
||||
|
||||
ulCoord = lrCoord = ''
|
||||
xUL = yLR = xLR = yUL = 0
|
||||
info = arr.splitlines()
|
||||
for elem in info:
|
||||
m = re.match("^Upper\sLeft.*", elem)
|
||||
if m:
|
||||
ulCoord = m.group(0).strip()
|
||||
ulCoord = ulCoord[string.find(ulCoord,"(") + 1 : string.find(ulCoord,")") - 1].split( "," )
|
||||
xUL = float(ulCoord[0])
|
||||
yUL = float(ulCoord[1])
|
||||
continue
|
||||
m = re.match("^Lower\sRight.*", elem)
|
||||
if m:
|
||||
lrCoord = m.group(0).strip()
|
||||
lrCoord = lrCoord[string.find(lrCoord,"(") + 1 : string.find(lrCoord,")") - 1].split( "," )
|
||||
xLR = float(lrCoord[0])
|
||||
yLR = float(lrCoord[1])
|
||||
continue
|
||||
gt = ds.GetGeoTransform()
|
||||
if gt is None:
|
||||
xUL = 0
|
||||
yUL = 0
|
||||
xLR = x
|
||||
yLR = y
|
||||
else:
|
||||
xUL = gt[0]
|
||||
yUL = gt[3]
|
||||
xLR = gt[0] + gt[1]*x + gt[2]*y
|
||||
yLR = gt[3] + gt[4]*x + gt[5]*y
|
||||
|
||||
return QgsRectangle( xUL, yLR, xLR, yUL )
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user