-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRasterNL_RDNAPETRS89DirInv.py
150 lines (116 loc) · 5.89 KB
/
RasterNL_RDNAPETRS89DirInv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
***************************************************************************
RasterNL_RDNAPETRS89DirInv.py
---------------------
Date : August 2019
Copyright : (C) 2019 by Giovanni Manghi
Email : giovanni dot manghi at naturalgis dot pt
***************************************************************************
* *
* 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, Giovanni Manghi'
__date__ = 'August 2019'
__copyright__ = '(C) 2019, Giovanni Manghi'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
from urllib.request import urlretrieve
from qgis.PyQt.QtGui import QIcon
from qgis.core import (QgsRasterFileWriter,
QgsProcessingException,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterEnum,
QgsProcessingParameterRasterDestination
)
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
from ntv2_transformations.transformations import nl_transformation
pluginPath = os.path.dirname(__file__)
class RasterNL_RDNAPETRS89DirInv(GdalAlgorithm):
INPUT = 'INPUT'
TRANSF = 'TRANSF'
CRS = 'CRS'
GRID = 'GRID'
OUTPUT = 'OUTPUT'
def __init__(self):
super().__init__()
def name(self):
return 'nlrastertransform'
def displayName(self):
return '[NL] Direct and inverse Raster Transformation'
def group(self):
return '[NL] Netherlands'
def groupId(self):
return 'netherlands'
def tags(self):
return 'raster,grid,ntv2,direct,inverse,netherlands'.split(',')
def shortHelpString(self):
return 'Direct and inverse raster transformations using Netherlands NTv2 grids.'
def icon(self):
return QIcon(os.path.join(pluginPath, 'icons', 'nl.png'))
def initAlgorithm(self, config=None):
self.directions = ['Direct: Old Data -> ETRS89 [EPSG:4258]',
'Inverse: ETRS89 [EPSG:4258] -> Old Data'
]
self.datums = (('Amersfoort/RD [EPSG:28992]', 28992),
)
self.grids = (('RDNAPTRANS [NTv2 + VDatum]', 'naptrans2008'),
('RDNAPTRANS [NTv2 only]', 'rdtrans2008')
)
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
'Input raster'))
self.addParameter(QgsProcessingParameterEnum(self.TRANSF,
'Transformation',
options=self.directions,
defaultValue=0))
self.addParameter(QgsProcessingParameterEnum(self.CRS,
'Old Datum',
options=[i[0] for i in self.datums],
defaultValue=0))
self.addParameter(QgsProcessingParameterEnum(self.GRID,
'NTv2 Grid',
options=[i[0] for i in self.grids],
defaultValue=0))
self.addParameter(QgsProcessingParameterRasterDestination(self.OUTPUT,
'Output'))
def getConsoleCommands(self, parameters, context, feedback, executing=True):
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
if inLayer is None:
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, outFile)
direction = self.parameterAsEnum(parameters, self.TRANSF, context)
epsg = self.datums[self.parameterAsEnum(parameters, self.CRS, context)][1]
grid = self.grids[self.parameterAsEnum(parameters, self.GRID, context)][1]
found, text = nl_transformation(epsg, grid)
if not found:
raise QgsProcessingException(text)
arguments = []
if direction == 0:
# Direct transformation
arguments.append('-s_srs')
arguments.append(text)
arguments.append('-t_srs')
arguments.append('EPSG:4258')
else:
# Inverse transformation
arguments = ['-s_srs']
arguments.append('EPSG:4258')
arguments.append('-t_srs')
arguments.append(text)
arguments.append('-multi')
arguments.append('-of')
arguments.append(QgsRasterFileWriter.driverForExtension(os.path.splitext(outFile)[1]))
arguments.append(inLayer.source())
arguments.append(outFile)
if not os.path.isfile(os.path.join(pluginPath, 'grids', 'rdtrans2008.gsb')):
urlretrieve('http://www.naturalgis.pt/downloads/ntv2grids/nl/rdtrans2008.gsb', os.path.join(pluginPath, 'grids', 'rdtrans2008.gsb'))
urlretrieve('http://www.naturalgis.pt/downloads/ntv2grids/nl/naptrans2008.gtx', os.path.join(pluginPath, 'grids', 'naptrans2008.gtx'))
return ['gdalwarp', GdalUtils.escapeAndJoin(arguments)]