From 91eea30e7defce0e7761e675006d630dfdb78412 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Fri, 10 Feb 2017 13:40:13 +1000 Subject: [PATCH] [FEATURE][processing] Algorithm to find an unknown layer's projection If you have a layer with an unknown CRS, this algorithm gives a list of possible candidate CRSes which the layer could be in. It allows users to set the area (and corresponding CRS) which they know the layer should be located near. The algorithm then tests every CRS in the database to see what candidate CRSes would cause the layer to be located at that preset area. It's much faster than it sounds!! (just a couple of seconds) Sponsored by SMEC/Surbana Jurong --- python/plugins/processing/algs/help/qgis.yaml | 7 ++ .../processing/algs/qgis/FindProjection.py | 116 ++++++++++++++++++ .../algs/qgis/QGISAlgorithmProvider.py | 3 +- .../tests/testdata/custom/find_projection.gfs | 16 +++ .../tests/testdata/custom/find_projection.gml | 34 +++++ .../testdata/expected/find_projection.html | 11 ++ .../tests/testdata/qgis_algorithm_tests.yaml | 14 +++ 7 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 python/plugins/processing/algs/qgis/FindProjection.py create mode 100644 python/plugins/processing/tests/testdata/custom/find_projection.gfs create mode 100644 python/plugins/processing/tests/testdata/custom/find_projection.gml create mode 100644 python/plugins/processing/tests/testdata/expected/find_projection.html diff --git a/python/plugins/processing/algs/help/qgis.yaml b/python/plugins/processing/algs/help/qgis.yaml index e7ab0eae5d8..a3bb736820d 100644 --- a/python/plugins/processing/algs/help/qgis.yaml +++ b/python/plugins/processing/algs/help/qgis.yaml @@ -200,6 +200,13 @@ qgis:extractspecificnodes: > qgis:fieldcalculator: > This algorithm computes a new vector layer with the same features of the input layer, but with an additional attribute. The values of this new attribute are computed from each feature using a mathematical formula, based on te properties and attributes of the feature. +qgis:findprojection: > + This algorithm allows creation of a shortlist of possible candidate coordinate reference systems for a layer with an unknown projection. + + The expected area which the layer should reside in must be specified via the target area parameter. Additionally, the coordinate reference system for this target area must also be set. + + The algorithm operates by testing the layer's extent in every known reference system and listing any in which the bounds would fall near the target area if the layer was in this projection. + qgis:fixeddistancebuffer: > This algorithm computes a buffer area for all the features in an input layer, using a fixed distance. diff --git a/python/plugins/processing/algs/qgis/FindProjection.py b/python/plugins/processing/algs/qgis/FindProjection.py new file mode 100644 index 00000000000..6a50786bb4b --- /dev/null +++ b/python/plugins/processing/algs/qgis/FindProjection.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + FindProjection.py + ----------------- + Date : February 2017 + Copyright : (C) 2017 by Nyall Dawson + Email : nyall dot dawson 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__ = 'Nyall Dawson' +__date__ = 'February 2017' +__copyright__ = '(C) 2017, Nyall Dawson' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + +import os +import codecs + +from qgis.core import (QgsGeometry, + QgsRectangle, + QgsCoordinateReferenceSystem, + QgsCoordinateTransform) + +from processing.core.GeoAlgorithm import GeoAlgorithm +from processing.core.parameters import ParameterVector +from processing.core.parameters import ParameterCrs +from processing.core.parameters import ParameterExtent +from processing.core.outputs import OutputHTML +from processing.tools import dataobjects + + +pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] + + +class FindProjection(GeoAlgorithm): + + INPUT_LAYER = 'INPUT_LAYER' + TARGET_AREA = 'TARGET_AREA' + TARGET_AREA_CRS = 'TARGET_AREA_CRS' + OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE' + + def defineCharacteristics(self): + self.name, self.i18n_name = self.trAlgorithm('Find projection') + self.group, self.i18n_group = self.trAlgorithm('Vector general tools') + self.tags = self.tr('crs,srs,coordinate,reference,system,guess,estimate,finder,determine') + + self.addParameter(ParameterVector(self.INPUT_LAYER, + self.tr('Input layer'))) + extent_parameter = ParameterExtent(self.TARGET_AREA, + self.tr('Target area for layer'), + self.INPUT_LAYER) + extent_parameter.skip_crs_check = True + self.addParameter(extent_parameter) + self.addParameter(ParameterCrs(self.TARGET_AREA_CRS, 'Target area CRS')) + + self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE, + self.tr('Candidates'))) + + def processAlgorithm(self, feedback): + layer = dataobjects.getObjectFromUri( + self.getParameterValue(self.INPUT_LAYER)) + + extent = self.getParameterValue(self.TARGET_AREA).split(',') + target_crs = QgsCoordinateReferenceSystem(self.getParameterValue(self.TARGET_AREA_CRS)) + + target_geom = QgsGeometry.fromRect(QgsRectangle(float(extent[0]), float(extent[2]), + float(extent[1]), float(extent[3]))) + + output_file = self.getOutputValue(self.OUTPUT_HTML_FILE) + + # make intersection tests nice and fast + engine = QgsGeometry.createGeometryEngine(target_geom.geometry()) + engine.prepareGeometry() + + layer_bounds = QgsGeometry.fromRect(layer.extent()) + + results = [] + + for srs_id in QgsCoordinateReferenceSystem.validSrsIds(): + candidate_crs = QgsCoordinateReferenceSystem.fromSrsId(srs_id) + if not candidate_crs.isValid(): + continue + + transform_candidate = QgsCoordinateTransform(candidate_crs, target_crs) + transformed_bounds = QgsGeometry(layer_bounds) + try: + if not transformed_bounds.transform(transform_candidate) == 0: + continue + except: + continue + + if engine.intersects(transformed_bounds.geometry()): + results.append(candidate_crs.authid()) + + self.createHTML(output_file, results) + + def createHTML(self, outputFile, candidates): + with codecs.open(outputFile, 'w', encoding='utf-8') as f: + f.write('\n') + f.write('\n') + for c in candidates: + f.write('

' + c + '

\n') + f.write('\n') diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 506eb409efa..14cdbfc4a08 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -186,6 +186,7 @@ from .TruncateTable import TruncateTable from .Polygonize import Polygonize from .FixGeometry import FixGeometry from .ExecuteSQL import ExecuteSQL +from .FindProjection import FindProjection pluginPath = os.path.normpath(os.path.join( os.path.split(os.path.dirname(__file__))[0], os.pardir)) @@ -255,7 +256,7 @@ class QGISAlgorithmProvider(AlgorithmProvider): ShortestPathPointToPoint(), ShortestPathPointToLayer(), ShortestPathLayerToPoint(), ServiceAreaFromPoint(), ServiceAreaFromLayer(), TruncateTable(), Polygonize(), - FixGeometry(), ExecuteSQL() + FixGeometry(), ExecuteSQL(), FindProjection() ] if hasMatplotlib: diff --git a/python/plugins/processing/tests/testdata/custom/find_projection.gfs b/python/plugins/processing/tests/testdata/custom/find_projection.gfs new file mode 100644 index 00000000000..da10d235bfe --- /dev/null +++ b/python/plugins/processing/tests/testdata/custom/find_projection.gfs @@ -0,0 +1,16 @@ + + + find_projection + find_projection + + 1 + EPSG:28356 + + 4 + 326064.87345 + 328710.85361 + 6245538.80829 + 6247808.84283 + + + diff --git a/python/plugins/processing/tests/testdata/custom/find_projection.gml b/python/plugins/processing/tests/testdata/custom/find_projection.gml new file mode 100644 index 00000000000..ff0226fbca9 --- /dev/null +++ b/python/plugins/processing/tests/testdata/custom/find_projection.gml @@ -0,0 +1,34 @@ + + + + + 326064.87345387256245538.808291084 + 328710.85360640866247808.842832778 + + + + + + 326597.10210384,6247528.48897066 + + + + + 327449.712845641,6246290.065952 + + + + + 328710.853606409,6247808.84283278 + + + + + 326064.873453873,6245538.80829108 + + + diff --git a/python/plugins/processing/tests/testdata/expected/find_projection.html b/python/plugins/processing/tests/testdata/expected/find_projection.html new file mode 100644 index 00000000000..79820c159c6 --- /dev/null +++ b/python/plugins/processing/tests/testdata/expected/find_projection.html @@ -0,0 +1,11 @@ + + +

EPSG:20256

+

EPSG:20356

+

EPSG:28356

+

EPSG:32356

+

EPSG:32556

+

EPSG:32756

+

IGNF:UTM56SW84

+

EPSG:5552

+ diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index 9c2d957da6b..8967c0d7477 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -2347,3 +2347,17 @@ tests: OUTPUT: name: expected/voronoi_buffer.gml type: vector + + - algorithm: qgis:findprojection + name: Find projection + params: + INPUT_LAYER: + name: custom/find_projection.gml + type: vector + TARGET_AREA: 151.1198,151.1368,-33.9118,-33.9003 + TARGET_AREA_CRS: EPSG:4326 + results: + OUTPUT_HTML_FILE: + name: expected/find_projection.html + type: file +