Skip to content

Commit 91eea30

Browse files
committed
[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
1 parent 3ecafb3 commit 91eea30

File tree

7 files changed

+200
-1
lines changed

7 files changed

+200
-1
lines changed

python/plugins/processing/algs/help/qgis.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ qgis:extractspecificnodes: >
200200
qgis:fieldcalculator: >
201201
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.
202202

203+
qgis:findprojection: >
204+
This algorithm allows creation of a shortlist of possible candidate coordinate reference systems for a layer with an unknown projection.
205+
206+
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.
207+
208+
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.
209+
203210
qgis:fixeddistancebuffer: >
204211
This algorithm computes a buffer area for all the features in an input layer, using a fixed distance.
205212

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
FindProjection.py
6+
-----------------
7+
Date : February 2017
8+
Copyright : (C) 2017 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'February 2017'
22+
__copyright__ = '(C) 2017, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
import codecs
30+
31+
from qgis.core import (QgsGeometry,
32+
QgsRectangle,
33+
QgsCoordinateReferenceSystem,
34+
QgsCoordinateTransform)
35+
36+
from processing.core.GeoAlgorithm import GeoAlgorithm
37+
from processing.core.parameters import ParameterVector
38+
from processing.core.parameters import ParameterCrs
39+
from processing.core.parameters import ParameterExtent
40+
from processing.core.outputs import OutputHTML
41+
from processing.tools import dataobjects
42+
43+
44+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
45+
46+
47+
class FindProjection(GeoAlgorithm):
48+
49+
INPUT_LAYER = 'INPUT_LAYER'
50+
TARGET_AREA = 'TARGET_AREA'
51+
TARGET_AREA_CRS = 'TARGET_AREA_CRS'
52+
OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE'
53+
54+
def defineCharacteristics(self):
55+
self.name, self.i18n_name = self.trAlgorithm('Find projection')
56+
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')
57+
self.tags = self.tr('crs,srs,coordinate,reference,system,guess,estimate,finder,determine')
58+
59+
self.addParameter(ParameterVector(self.INPUT_LAYER,
60+
self.tr('Input layer')))
61+
extent_parameter = ParameterExtent(self.TARGET_AREA,
62+
self.tr('Target area for layer'),
63+
self.INPUT_LAYER)
64+
extent_parameter.skip_crs_check = True
65+
self.addParameter(extent_parameter)
66+
self.addParameter(ParameterCrs(self.TARGET_AREA_CRS, 'Target area CRS'))
67+
68+
self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE,
69+
self.tr('Candidates')))
70+
71+
def processAlgorithm(self, feedback):
72+
layer = dataobjects.getObjectFromUri(
73+
self.getParameterValue(self.INPUT_LAYER))
74+
75+
extent = self.getParameterValue(self.TARGET_AREA).split(',')
76+
target_crs = QgsCoordinateReferenceSystem(self.getParameterValue(self.TARGET_AREA_CRS))
77+
78+
target_geom = QgsGeometry.fromRect(QgsRectangle(float(extent[0]), float(extent[2]),
79+
float(extent[1]), float(extent[3])))
80+
81+
output_file = self.getOutputValue(self.OUTPUT_HTML_FILE)
82+
83+
# make intersection tests nice and fast
84+
engine = QgsGeometry.createGeometryEngine(target_geom.geometry())
85+
engine.prepareGeometry()
86+
87+
layer_bounds = QgsGeometry.fromRect(layer.extent())
88+
89+
results = []
90+
91+
for srs_id in QgsCoordinateReferenceSystem.validSrsIds():
92+
candidate_crs = QgsCoordinateReferenceSystem.fromSrsId(srs_id)
93+
if not candidate_crs.isValid():
94+
continue
95+
96+
transform_candidate = QgsCoordinateTransform(candidate_crs, target_crs)
97+
transformed_bounds = QgsGeometry(layer_bounds)
98+
try:
99+
if not transformed_bounds.transform(transform_candidate) == 0:
100+
continue
101+
except:
102+
continue
103+
104+
if engine.intersects(transformed_bounds.geometry()):
105+
results.append(candidate_crs.authid())
106+
107+
self.createHTML(output_file, results)
108+
109+
def createHTML(self, outputFile, candidates):
110+
with codecs.open(outputFile, 'w', encoding='utf-8') as f:
111+
f.write('<html><head>\n')
112+
f.write('<meta http-equiv="Content-Type" content="text/html; \
113+
charset=utf-8" /></head><body>\n')
114+
for c in candidates:
115+
f.write('<p>' + c + '</p>\n')
116+
f.write('</body></html>\n')

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
from .Polygonize import Polygonize
187187
from .FixGeometry import FixGeometry
188188
from .ExecuteSQL import ExecuteSQL
189+
from .FindProjection import FindProjection
189190

190191
pluginPath = os.path.normpath(os.path.join(
191192
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -255,7 +256,7 @@ def __init__(self):
255256
ShortestPathPointToPoint(), ShortestPathPointToLayer(),
256257
ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
257258
ServiceAreaFromLayer(), TruncateTable(), Polygonize(),
258-
FixGeometry(), ExecuteSQL()
259+
FixGeometry(), ExecuteSQL(), FindProjection()
259260
]
260261

261262
if hasMatplotlib:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>find_projection</Name>
4+
<ElementPath>find_projection</ElementPath>
5+
<!--POINT-->
6+
<GeometryType>1</GeometryType>
7+
<SRSName>EPSG:28356</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>4</FeatureCount>
10+
<ExtentXMin>326064.87345</ExtentXMin>
11+
<ExtentXMax>328710.85361</ExtentXMax>
12+
<ExtentYMin>6245538.80829</ExtentYMin>
13+
<ExtentYMax>6247808.84283</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
</GMLFeatureClass>
16+
</GMLFeatureClassList>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>326064.8734538725</gml:X><gml:Y>6245538.808291084</gml:Y></gml:coord>
10+
<gml:coord><gml:X>328710.8536064086</gml:X><gml:Y>6247808.842832778</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:find_projection fid="find_projection.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:28356"><gml:coordinates>326597.10210384,6247528.48897066</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
</ogr:find_projection>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:find_projection fid="find_projection.1">
21+
<ogr:geometryProperty><gml:Point srsName="EPSG:28356"><gml:coordinates>327449.712845641,6246290.065952</gml:coordinates></gml:Point></ogr:geometryProperty>
22+
</ogr:find_projection>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:find_projection fid="find_projection.2">
26+
<ogr:geometryProperty><gml:Point srsName="EPSG:28356"><gml:coordinates>328710.853606409,6247808.84283278</gml:coordinates></gml:Point></ogr:geometryProperty>
27+
</ogr:find_projection>
28+
</gml:featureMember>
29+
<gml:featureMember>
30+
<ogr:find_projection fid="find_projection.3">
31+
<ogr:geometryProperty><gml:Point srsName="EPSG:28356"><gml:coordinates>326064.873453873,6245538.80829108</gml:coordinates></gml:Point></ogr:geometryProperty>
32+
</ogr:find_projection>
33+
</gml:featureMember>
34+
</ogr:FeatureCollection>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html><head>
2+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
3+
<p>EPSG:20256</p>
4+
<p>EPSG:20356</p>
5+
<p>EPSG:28356</p>
6+
<p>EPSG:32356</p>
7+
<p>EPSG:32556</p>
8+
<p>EPSG:32756</p>
9+
<p>IGNF:UTM56SW84</p>
10+
<p>EPSG:5552</p>
11+
</body></html>

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,3 +2347,17 @@ tests:
23472347
OUTPUT:
23482348
name: expected/voronoi_buffer.gml
23492349
type: vector
2350+
2351+
- algorithm: qgis:findprojection
2352+
name: Find projection
2353+
params:
2354+
INPUT_LAYER:
2355+
name: custom/find_projection.gml
2356+
type: vector
2357+
TARGET_AREA: 151.1198,151.1368,-33.9118,-33.9003
2358+
TARGET_AREA_CRS: EPSG:4326
2359+
results:
2360+
OUTPUT_HTML_FILE:
2361+
name: expected/find_projection.html
2362+
type: file
2363+

0 commit comments

Comments
 (0)