Navigation Menu

Skip to content

Commit

Permalink
use python API to get raster SRS, 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 415b467 commit 1d812b3
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions python/plugins/GdalTools/tools/GdalTools_utils.py
Expand Up @@ -35,7 +35,7 @@
from qgis.core import *
from qgis.gui import *

from osgeo import gdal, ogr
from osgeo import gdal, ogr, osr
from osgeo.gdalconst import *

import os
Expand Down Expand Up @@ -287,27 +287,22 @@ def getVectorFields(vectorFile):

# get raster SRS if possible
def getRasterSRS( parent, fileName ):
processSRS = QProcess( parent )
if ( GdalConfig.versionNum() >= 1900 ):
processSRS.start( "gdalsrsinfo", ["-o", "wkt", "-p", fileName], QIODevice.ReadOnly )
else:
processSRS.start( "gdalinfo", [fileName], QIODevice.ReadOnly )
arr = ''
if processSRS.waitForFinished():
arr = str(processSRS.readAllStandardOutput())
processSRS.close()

if arr == '':
return ''
ds = gdal.Open(fileName)
if ds is None:
return ''

info = arr.splitlines()
if len(info) == 0:
proj = ds.GetProjectionRef()
if proj is None:
return ''

sr = osr.SpatialReference()
if sr.ImportFromWkt(proj) != gdal.CE_None:
return ''

for elem in reversed(info):
m = re.match("^\s*AUTHORITY\[\"([a-z]*[A-Z]*)\",\"(\d*)\"\]", elem)
if m and len(m.groups()) == 2:
return '%s:%s' % (m.group(1), m.group(2))
name = sr.GetAuthorityName(None)
code = sr.GetAuthorityCode(None)
if name is not None and code is not None:
return '%s:%s' % (name,code)

return ''

Expand Down

0 comments on commit 1d812b3

Please sign in to comment.