mirror of
https://github.com/qgis/QGIS.git
synced 2025-06-18 00:04:02 -04:00
Merge pull request #8378 from havatv/patch-1
Reintroduce GDAL extract projection and update extractprojection.py to QGIS 3
This commit is contained in:
commit
530cd5c12c
@ -71,7 +71,7 @@ from .tpi import tpi
|
||||
from .tri import tri
|
||||
from .warp import warp
|
||||
|
||||
# from .extractprojection import ExtractProjection
|
||||
from .extractprojection import ExtractProjection
|
||||
# from .rasterize_over import rasterize_over
|
||||
|
||||
from .Buffer import Buffer
|
||||
@ -181,7 +181,7 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
|
||||
tri(),
|
||||
warp(),
|
||||
# rasterize(),
|
||||
# ExtractProjection(),
|
||||
ExtractProjection(),
|
||||
# rasterize_over(),
|
||||
# ----- OGR tools -----
|
||||
Buffer(),
|
||||
|
@ -32,8 +32,10 @@ from qgis.PyQt.QtGui import QIcon
|
||||
from osgeo import gdal, osr
|
||||
|
||||
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
|
||||
from processing.core.parameters import ParameterRaster
|
||||
from processing.core.parameters import ParameterBoolean
|
||||
from qgis.core import QgsProcessingException
|
||||
from qgis.core import (QgsProcessingParameterRasterLayer,
|
||||
QgsProcessingParameterBoolean,
|
||||
QgsProcessingOutputFile)
|
||||
|
||||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
||||
|
||||
@ -41,15 +43,26 @@ pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
||||
class ExtractProjection(GdalAlgorithm):
|
||||
|
||||
INPUT = 'INPUT'
|
||||
PRJ_FILE_CREATE = 'PRJ_FILE_CREATE'
|
||||
WORLD_FILE = 'WORLD_FILE'
|
||||
PRJ_FILE = 'PRJ_FILE'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input file')))
|
||||
self.addParameter(ParameterBoolean(self.PRJ_FILE,
|
||||
self.tr('Create also .prj file'), False))
|
||||
self.addParameter(QgsProcessingParameterRasterLayer(
|
||||
self.INPUT,
|
||||
self.tr('Input file')))
|
||||
self.addParameter(QgsProcessingParameterBoolean(
|
||||
self.PRJ_FILE_CREATE,
|
||||
self.tr('Create also .prj file'), False))
|
||||
self.addOutput(QgsProcessingOutputFile(
|
||||
self.WORLD_FILE,
|
||||
self.tr('World file')))
|
||||
self.addOutput(QgsProcessingOutputFile(
|
||||
self.PRJ_FILE,
|
||||
self.tr('ESRI Shapefile prj file')))
|
||||
|
||||
def name(self):
|
||||
return 'extractprojection'
|
||||
@ -58,7 +71,8 @@ class ExtractProjection(GdalAlgorithm):
|
||||
return self.tr('Extract projection')
|
||||
|
||||
def icon(self):
|
||||
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools', 'projection-export.png'))
|
||||
return QIcon(os.path.join(pluginPath, 'images', 'gdaltools',
|
||||
'projection-export.png'))
|
||||
|
||||
def group(self):
|
||||
return self.tr('Raster projections')
|
||||
@ -69,20 +83,31 @@ class ExtractProjection(GdalAlgorithm):
|
||||
def commandName(self):
|
||||
return 'extractprojection'
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
||||
def getConsoleCommands(self, parameters, context, feedback,
|
||||
executing=True):
|
||||
return [self.commandName()]
|
||||
|
||||
def processAlgorithm(self, parameters, context, feedback):
|
||||
rasterPath = self.getParameterValue(self.INPUT)
|
||||
createPrj = self.getParameterValue(self.PRJ_FILE)
|
||||
|
||||
raster = gdal.Open(str(rasterPath))
|
||||
crs = raster.GetProjection()
|
||||
geotransform = raster.GetGeoTransform()
|
||||
createPrj = self.parameterAsBool(parameters,
|
||||
self.PRJ_FILE_CREATE,
|
||||
context)
|
||||
raster = self.parameterAsRasterLayer(parameters, self.INPUT,
|
||||
context)
|
||||
if not raster.dataProvider().name() == 'gdal':
|
||||
raise QgsProcessingException(self.tr('This algorithm can '
|
||||
'only be used with '
|
||||
'GDAL raster layers'))
|
||||
rasterPath = raster.source()
|
||||
rasterDS = gdal.Open(rasterPath, gdal.GA_ReadOnly)
|
||||
geotransform = rasterDS.GetGeoTransform()
|
||||
inputcrs = raster.crs()
|
||||
crs = inputcrs.toWkt()
|
||||
raster = None
|
||||
rasterDS = None
|
||||
|
||||
outFileName = os.path.splitext(str(rasterPath))[0]
|
||||
|
||||
results = {}
|
||||
if crs != '' and createPrj:
|
||||
tmp = osr.SpatialReference()
|
||||
tmp.ImportFromWkt(crs)
|
||||
@ -92,15 +117,21 @@ class ExtractProjection(GdalAlgorithm):
|
||||
|
||||
with open(outFileName + '.prj', 'wt') as prj:
|
||||
prj.write(crs)
|
||||
results[self.PRJ_FILE] = outFileName + '.prj'
|
||||
else:
|
||||
results[self.PRJ_FILE] = None
|
||||
|
||||
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])
|
||||
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]))
|
||||
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]))
|
||||
results[self.WORLD_FILE] = outFileName + '.wld'
|
||||
|
||||
return results
|
||||
|
1
python/plugins/processing/tests/testdata/expected/gdal/dem.prj
vendored
Normal file
1
python/plugins/processing/tests/testdata/expected/gdal/dem.prj
vendored
Normal file
@ -0,0 +1 @@
|
||||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
|
6
python/plugins/processing/tests/testdata/expected/gdal/dem.wld
vendored
Normal file
6
python/plugins/processing/tests/testdata/expected/gdal/dem.wld
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
0.00010000
|
||||
0.00000000
|
||||
0.00000000
|
||||
-0.00010000
|
||||
18.66634794
|
||||
45.81165144
|
@ -122,6 +122,33 @@ tests:
|
||||
name: expected/gdal/contour.gml
|
||||
type: vector
|
||||
|
||||
- algorithm: gdal:extractprojection
|
||||
name: Extract Projection (extractprojection)
|
||||
params:
|
||||
INPUT:
|
||||
name: dem.tif
|
||||
type: raster
|
||||
PRJ_FILE_CREATE: True
|
||||
results:
|
||||
WORLD_FILE:
|
||||
name: expected/gdal/dem.wld
|
||||
type: file
|
||||
PRJ_FILE:
|
||||
name: expected/gdal/dem.prj
|
||||
type: file
|
||||
|
||||
- algorithm: gdal:extractprojection
|
||||
name: Extract Projection (extractprojection)
|
||||
params:
|
||||
INPUT:
|
||||
name: dem.tif
|
||||
type: raster
|
||||
PRJ_FILE_CREATE: False
|
||||
results:
|
||||
WORLD_FILE:
|
||||
name: expected/gdal/dem.wld
|
||||
type: file
|
||||
|
||||
- algorithm: gdal:gdalinfo
|
||||
name: gdalinfo
|
||||
params:
|
||||
|
Loading…
x
Reference in New Issue
Block a user