2013-09-26 21:03:10 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
extractprojection.py
|
|
|
|
---------------------
|
|
|
|
Date : September 2013
|
|
|
|
Copyright : (C) 2013 by Alexander Bruy
|
|
|
|
Email : alexander dot bruy at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Alexander Bruy'
|
|
|
|
__date__ = 'September 2013'
|
|
|
|
__copyright__ = '(C) 2013, Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-26 21:03:10 +03:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-26 21:03:10 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from osgeo import gdal, osr
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterRaster
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
2013-09-26 21:03:10 +03:00
|
|
|
|
|
|
|
|
2014-06-02 00:35:49 +02:00
|
|
|
class ExtractProjection(GdalAlgorithm):
|
2013-09-26 21:03:10 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
PRJ_FILE = 'PRJ_FILE'
|
2013-09-26 21:03:10 +03:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Extract projection'
|
|
|
|
self.group = '[GDAL] Projections'
|
|
|
|
self.addParameter(ParameterRaster(self.INPUT, 'Input file'))
|
|
|
|
self.addParameter(ParameterBoolean(self.PRJ_FILE,
|
|
|
|
'Create also .prj file', False))
|
2013-09-26 21:03:10 +03:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
rasterPath = self.getParameterValue(self.INPUT)
|
|
|
|
createPrj = self.getParameterValue(self.PRJ_FILE)
|
|
|
|
|
|
|
|
raster = gdal.Open(unicode(rasterPath))
|
|
|
|
crs = raster.GetProjection()
|
|
|
|
geotransform = raster.GetGeoTransform()
|
|
|
|
raster = None
|
|
|
|
|
|
|
|
outFileName = os.path.splitext(unicode(rasterPath))[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if crs != '' and createPrj:
|
2013-09-26 21:03:10 +03:00
|
|
|
tmp = osr.SpatialReference()
|
2013-10-01 20:52:22 +03:00
|
|
|
tmp.ImportFromWkt(crs)
|
2013-09-26 21:03:10 +03:00
|
|
|
tmp.MorphToESRI()
|
|
|
|
crs = tmp.ExportToWkt()
|
|
|
|
tmp = None
|
|
|
|
|
|
|
|
prj = open(outFileName + '.prj', 'wt')
|
2013-10-01 20:52:22 +03:00
|
|
|
prj.write(crs)
|
2013-09-26 21:03:10 +03:00
|
|
|
prj.close()
|
|
|
|
|
|
|
|
wld = open(outFileName + '.wld', 'wt')
|
2013-10-01 20:52:22 +03:00
|
|
|
wld.write('%0.8f\n' % geotransform[1])
|
|
|
|
wld.write('%0.8f\n' % geotransform[4])
|
|
|
|
wld.write('%0.8f\n' % geotransform[2])
|
|
|
|
wld.write('%0.8f\n' % geotransform[5])
|
|
|
|
wld.write('%0.8f\n' % (geotransform[0] + 0.5 * geotransform[1] + 0.5
|
|
|
|
* geotransform[2]))
|
|
|
|
wld.write('%0.8f\n' % (geotransform[3] + 0.5 * geotransform[4] + 0.5
|
|
|
|
* geotransform[5]))
|
2013-09-26 21:03:10 +03:00
|
|
|
wld.close()
|