Skip to content

Commit 4b9e181

Browse files
committedJan 30, 2014
use python API to get raster extent, instead of relying on gdalinfo
1 parent 1d812b3 commit 4b9e181

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed
 

‎python/plugins/GdalTools/tools/GdalTools_utils.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,35 +306,26 @@ def getRasterSRS( parent, fileName ):
306306

307307
return ''
308308

309+
# get raster extent using python API - replaces old method which parsed gdalinfo output
309310
def getRasterExtent(parent, fileName):
310-
processSRS = QProcess( parent )
311-
processSRS.start( "gdalinfo", [fileName], QIODevice.ReadOnly )
312-
arr = ''
313-
if processSRS.waitForFinished():
314-
arr = str(processSRS.readAllStandardOutput())
315-
processSRS.close()
316-
317-
if arr == '':
318-
return
319-
320-
ulCoord = lrCoord = ''
321-
xUL = yLR = xLR = yUL = 0
322-
info = arr.splitlines()
323-
for elem in info:
324-
m = re.match("^Upper\sLeft.*", elem)
325-
if m:
326-
ulCoord = m.group(0).strip()
327-
ulCoord = ulCoord[string.find(ulCoord,"(") + 1 : string.find(ulCoord,")") - 1].split( "," )
328-
xUL = float(ulCoord[0])
329-
yUL = float(ulCoord[1])
330-
continue
331-
m = re.match("^Lower\sRight.*", elem)
332-
if m:
333-
lrCoord = m.group(0).strip()
334-
lrCoord = lrCoord[string.find(lrCoord,"(") + 1 : string.find(lrCoord,")") - 1].split( "," )
335-
xLR = float(lrCoord[0])
336-
yLR = float(lrCoord[1])
337-
continue
311+
ds = gdal.Open(fileName)
312+
if ds is None:
313+
return
314+
315+
x = ds.RasterXSize
316+
y = ds.RasterYSize
317+
318+
gt = ds.GetGeoTransform()
319+
if gt is None:
320+
xUL = 0
321+
yUL = 0
322+
xLR = x
323+
yLR = y
324+
else:
325+
xUL = gt[0]
326+
yUL = gt[3]
327+
xLR = gt[0] + gt[1]*x + gt[2]*y
328+
yLR = gt[3] + gt[4]*x + gt[5]*y
338329

339330
return QgsRectangle( xUL, yLR, xLR, yUL )
340331

0 commit comments

Comments
 (0)
Please sign in to comment.