Skip to content

Commit

Permalink
use python API to get raster extent, instead of relying on gdalinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Jan 30, 2014
1 parent 1d812b3 commit 4b9e181
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions python/plugins/GdalTools/tools/GdalTools_utils.py
Expand Up @@ -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()

if arr == '':
return

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
ds = gdal.Open(fileName)
if ds is None:
return

x = ds.RasterXSize
y = ds.RasterYSize

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 )

Expand Down

0 comments on commit 4b9e181

Please sign in to comment.