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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2013-09-26 21:03:10 +03:00
|
|
|
|
|
|
|
__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
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2013-09-26 21:03:10 +03:00
|
|
|
from osgeo import gdal, osr
|
|
|
|
|
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
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
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
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'extractprojection'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Extract projection')
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'projection-export.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Raster projections')
|
|
|
|
|
2013-09-26 21:03:10 +03:00
|
|
|
def defineCharacteristics(self):
|
2015-01-13 10:49:08 +02:00
|
|
|
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input file')))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterBoolean(self.PRJ_FILE,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Create also .prj file'), False))
|
2013-09-26 21:03:10 +03:00
|
|
|
|
2015-05-07 13:14:01 +02:00
|
|
|
def getConsoleCommands(self):
|
2016-01-08 08:27:22 +01:00
|
|
|
return ["extractprojection"]
|
2015-05-07 13:14:01 +02:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2013-09-26 21:03:10 +03:00
|
|
|
rasterPath = self.getParameterValue(self.INPUT)
|
|
|
|
createPrj = self.getParameterValue(self.PRJ_FILE)
|
|
|
|
|
2016-09-21 18:24:26 +02:00
|
|
|
raster = gdal.Open(str(rasterPath))
|
2013-09-26 21:03:10 +03:00
|
|
|
crs = raster.GetProjection()
|
|
|
|
geotransform = raster.GetGeoTransform()
|
|
|
|
raster = None
|
|
|
|
|
2016-09-21 18:24:26 +02:00
|
|
|
outFileName = os.path.splitext(str(rasterPath))[0]
|
2013-09-26 21:03:10 +03:00
|
|
|
|
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
|
|
|
|
|
2016-11-07 10:35:15 +10:00
|
|
|
with open(outFileName + '.prj', 'wt') as prj:
|
|
|
|
prj.write(crs)
|
|
|
|
|
|
|
|
with open(outFileName + '.wld', 'wt') as wld:
|
|
|
|
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])
|
2017-03-04 19:41:23 +01:00
|
|
|
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]))
|