Skip to content

Commit

Permalink
Add NTv2 Globogis vector transformation for Italy
Browse files Browse the repository at this point in the history
  • Loading branch information
agiudiceandrea committed Nov 14, 2019
1 parent 2eb4eb4 commit 58c6eed
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 7 deletions.
2 changes: 2 additions & 0 deletions DETransformProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from ntv2_transformations.RasterES_ED50ERTS89DirInv import RasterES_ED50ERTS89DirInv
from ntv2_transformations.VectorIT_RER_ETRS89DirInv import VectorIT_RER_ETRS89DirInv
from ntv2_transformations.RasterIT_RER_ETRS89DirInv import RasterIT_RER_ETRS89DirInv
from ntv2_transformations.VectorIT_ITALY_ETRS89DirInv import VectorIT_ITALY_ETRS89DirInv
from ntv2_transformations.VectorCH_LV95ETRS89DirInv import VectorCH_LV95ETRS89DirInv
from ntv2_transformations.RasterCH_LV95ETRS89DirInv import RasterCH_LV95ETRS89DirInv
from ntv2_transformations.VectorUK_OSGB36ETRS89DirInv import VectorUK_OSGB36ETRS89DirInv
Expand Down Expand Up @@ -106,6 +107,7 @@ def getAlgs(self):
RasterES_ED50ERTS89DirInv(),
VectorIT_RER_ETRS89DirInv(),
RasterIT_RER_ETRS89DirInv(),
VectorIT_ITALY_ETRS89DirInv(),
VectorCH_LV95ETRS89DirInv(),
RasterCH_LV95ETRS89DirInv(),
VectorUK_OSGB36ETRS89DirInv(),
Expand Down
6 changes: 3 additions & 3 deletions RasterIT_RER_ETRS89DirInv.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def name(self):
return 'itrastertransform'

def displayName(self):
return '[IT] Direct and inverse Raster Transformation'
return '[IT] (Emilia Romagna) Raster - Direct and inverse Transformation'

def group(self):
return '[IT] Italy (Emilia-Romagna)'
return '[IT] Italy'

def groupId(self):
return 'italy'
Expand All @@ -86,7 +86,7 @@ def initAlgorithm(self, config=None):
('UTM - ED50 [EPSG:23032]', 23032),
)

self.grids = (('Grigliati NTv2 RER 2013 la trasformazione di coordinate in Emilia-Romagna', 'RER_ETRS89'),
self.grids = (('Grigliati NTv2 RER 2013 per la trasformazione di coordinate in Emilia-Romagna', 'RER_ETRS89'),
)

self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT,
Expand Down
169 changes: 169 additions & 0 deletions VectorIT_ITALY_ETRS89DirInv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
VectorIT_ITALY_ETRS89DirInv.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 (QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterVectorDestination
)

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils

from ntv2_transformations.transformations import it_transformation

pluginPath = os.path.dirname(__file__)


class VectorIT_ITALY_ETRS89DirInv(GdalAlgorithm):

INPUT = 'INPUT'
TRANSF = 'TRANSF'
CRS = 'CRS'
GRID = 'GRID'
OUTPUT = 'OUTPUT'

def __init__(self):
super().__init__()

def name(self):
return 'itvectortransform'

def displayName(self):
return '[IT] (Italy) Vector - Direct and inverse Transformation'

def group(self):
return '[IT] Italy'

def groupId(self):
return 'italy'

def tags(self):
return 'vector,grid,ntv2,direct,inverse,italy'.split(',')

def shortHelpString(self):
return 'Direct and inverse vector transformations using Globogis NTv2 grids.'

def icon(self):
return QIcon(os.path.join(pluginPath, 'icons', 'it.png'))

def initAlgorithm(self, config=None):
self.directions = ['Direct: Old Data -> ETRS89 [EPSG:4258]',
'Inverse: ETRS89 [EPSG:4258] -> Old Data'
]

self.datums = (('Monte Mario - GBO Italy 1 [EPSG:3003]', 3003),
('Monte Mario - GBO Italy 2 [EPSG:3004]', 3004),
('UTM - ED50 32N [EPSG:23033]', 23032),
('UTM - ED50 33N [EPSG:23033]', 23033),
)

self.grids = (('Grigliati NTv2 Globogis per la trasformazione di coordinate in Italia', 'ITALY_ETRS89'),
)

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
'Input vector'))
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(QgsProcessingParameterVectorDestination(self.OUTPUT,
'Output'))

def getConsoleCommands(self, parameters, context, feedback, executing=True):
ogrLayer, layerName = self.getOgrCompatibleSource(self.INPUT, parameters, context, feedback, executing)
outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
self.setOutputValue(self.OUTPUT, outFile)

output, outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
if outputFormat in ('SQLite', 'GPKG') and os.path.isfile(output):
raise QgsProcessingException('Output file "{}" already exists.'.format(output))

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 = it_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')

arguments.append('-f {}'.format(outputFormat))
arguments.append('-lco')
arguments.append('ENCODING=UTF-8')

arguments.append(output)
arguments.append(ogrLayer)
arguments.append(layerName)
else:
# Inverse transformation
arguments.append('-s_srs')
arguments.append('EPSG:4258')
arguments.append('-t_srs')
arguments.append(text)

arguments.append('-f')
arguments.append('Geojson')
arguments.append('/vsistdout/')
arguments.append(ogrLayer)
arguments.append(layerName)
arguments.append('-lco')
arguments.append('ENCODING=UTF-8')
arguments.append('|')
arguments.append('ogr2ogr')
arguments.append('-f {}'.format(outputFormat))
arguments.append('-a_srs')
arguments.append('EPSG:{}'.format(epsg))
arguments.append(output)
arguments.append('/vsistdin/')

if not os.path.isfile(os.path.join(pluginPath, 'grids', 'NadRoma40.gsb')):
urlretrieve('http://www.naturalgis.pt/downloads/ntv2grids/it_globo/NadRoma40.gsb', os.path.join(pluginPath, 'grids', 'NadRoma40.gsb'))
urlretrieve('http://www.naturalgis.pt/downloads/ntv2grids/it_globo/NadED50.gsb', os.path.join(pluginPath, 'grids', 'NadED50.gsb'))

return ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)]
8 changes: 4 additions & 4 deletions VectorIT_RER_ETRS89DirInv.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def __init__(self):
super().__init__()

def name(self):
return 'itvectortransform'
return 'itrervectortransform'

def displayName(self):
return '[IT] Direct and inverse Vector Transformation'
return '[IT] (Emilia-Romagna) Vector - Direct and inverse Transformation'

def group(self):
return '[IT] Italy (Emilia-Romagna)'
return '[IT] Italy'

def groupId(self):
return 'italy'
Expand All @@ -85,7 +85,7 @@ def initAlgorithm(self, config=None):
('UTM - ED50 [EPSG:23032]', 23032),
)

self.grids = (('Grigliati NTv2 RER 2013 la trasformazione di coordinate in Emilia-Romagna', 'RER_ETRS89'),
self.grids = (('Grigliati NTv2 RER 2013 per la trasformazione di coordinate in Emilia-Romagna', 'RER_ETRS89'),
)

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
Expand Down
13 changes: 13 additions & 0 deletions transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ def it_transformation(epsg, grid):
elif epsg == 23032:
gridFile = os.path.join(pluginPath, 'grids', 'RER_ED50_ETRS89_GPS7_K2.GSB')
return True, '+proj=utm +zone=32 +ellps=intl +nadgrids={} +wktext +units=m +no_defs'.format(gridFile)
elif grid == 'ITALY_ETRS89':
if epsg == 3003:
gridFile = os.path.join(pluginPath, 'grids', 'NadRoma40.gsb')
return True, '+proj=tmerc +lat_0=0 +lon_0=9 +k=0.9996 +x_0=1500000 +y_0=0 +ellps=intl +nadgrids={} +wktext +units=m +no_defs'.format(gridFile)
elif epsg == 3004:
gridFile = os.path.join(pluginPath, 'grids', 'NadRoma40.gsb')
return True, '+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9996 +x_0=2520000 +y_0=0 +ellps=intl +nadgrids={} +wktext +units=m +no_defs'.format(gridFile)
elif epsg == 23032:
gridFile = os.path.join(pluginPath, 'grids', 'NadED50.gsb')
return True, '+proj=utm +zone=32 +ellps=intl +nadgrids={} +wktext +units=m +no_defs'.format(gridFile)
elif epsg == 23033:
gridFile = os.path.join(pluginPath, 'grids', 'NadED50.gsb')
return True, '+proj=utm +zone=33 +ellps=intl +nadgrids={} +wktext +units=m +no_defs'.format(gridFile)

return False, NO_TRANSFORMATION

Expand Down

0 comments on commit 58c6eed

Please sign in to comment.