@@ -306,35 +306,26 @@ def getRasterSRS( parent, fileName ):
306
306
307
307
return ''
308
308
309
+ # get raster extent using python API - replaces old method which parsed gdalinfo output
309
310
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
338
329
339
330
return QgsRectangle ( xUL , yLR , xLR , yUL )
340
331
0 commit comments