2017-02-10 13:40:13 +10:00
|
|
|
# -*- 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
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsGeometry,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-02-10 13:40:13 +10:00
|
|
|
QgsRectangle,
|
|
|
|
QgsCoordinateReferenceSystem,
|
2017-05-02 14:36:23 +10:00
|
|
|
QgsCoordinateTransform,
|
|
|
|
QgsProcessingUtils)
|
2017-02-10 13:40:13 +10:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-02-10 13:40:13 +10:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterCrs
|
|
|
|
from processing.core.parameters import ParameterExtent
|
|
|
|
from processing.core.outputs import OutputHTML
|
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class FindProjection(QgisAlgorithm):
|
2017-02-10 13:40:13 +10:00
|
|
|
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
TARGET_AREA = 'TARGET_AREA'
|
|
|
|
TARGET_AREA_CRS = 'TARGET_AREA_CRS'
|
|
|
|
OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE'
|
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('crs,srs,coordinate,reference,system,guess,estimate,finder,determine').split(',')
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-02-10 13:40:13 +10:00
|
|
|
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')))
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'findprojection'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Find projection')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
|
2017-02-10 13:40:13 +10:00
|
|
|
|
|
|
|
extent = self.getParameterValue(self.TARGET_AREA).split(',')
|
2017-05-17 07:41:15 +10:00
|
|
|
if not extent:
|
|
|
|
extent = QgsProcessingUtils.combineLayerExtents([layer])
|
2017-02-10 13:40:13 +10:00
|
|
|
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('<html><head>\n')
|
|
|
|
f.write('<meta http-equiv="Content-Type" content="text/html; \
|
|
|
|
charset=utf-8" /></head><body>\n')
|
|
|
|
for c in candidates:
|
|
|
|
f.write('<p>' + c + '</p>\n')
|
|
|
|
f.write('</body></html>\n')
|