26
26
__revision__ = '$Format:%H$'
27
27
28
28
import os
29
- import codecs
30
29
31
- from qgis .core import (QgsApplication ,
32
- QgsGeometry ,
30
+ from qgis .core import (QgsGeometry ,
31
+ QgsFeature ,
33
32
QgsFeatureSink ,
34
- QgsRectangle ,
33
+ QgsField ,
34
+ QgsFields ,
35
35
QgsCoordinateReferenceSystem ,
36
36
QgsCoordinateTransform ,
37
- QgsProcessingUtils )
37
+ QgsWkbTypes ,
38
+ QgsProcessingParameterFeatureSource ,
39
+ QgsProcessingParameterExtent ,
40
+ QgsProcessingParameterCrs ,
41
+ QgsProcessingParameterFeatureSink )
42
+ from qgis .PyQt .QtCore import QVariant
38
43
39
44
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
40
- from processing .core .parameters import ParameterVector
41
- from processing .core .parameters import ParameterCrs
42
- from processing .core .parameters import ParameterExtent
43
- from processing .core .outputs import OutputHTML
44
45
45
46
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
46
47
47
48
48
49
class FindProjection (QgisAlgorithm ):
49
50
50
- INPUT_LAYER = 'INPUT_LAYER '
51
+ INPUT = 'INPUT '
51
52
TARGET_AREA = 'TARGET_AREA'
52
53
TARGET_AREA_CRS = 'TARGET_AREA_CRS'
53
- OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE '
54
+ OUTPUT = 'OUTPUT '
54
55
55
56
def tags (self ):
56
57
return self .tr ('crs,srs,coordinate,reference,system,guess,estimate,finder,determine' ).split (',' )
@@ -62,17 +63,16 @@ def __init__(self):
62
63
super ().__init__ ()
63
64
64
65
def initAlgorithm (self , config = None ):
65
- self .addParameter (ParameterVector (self .INPUT_LAYER ,
66
- self .tr ('Input layer' )))
67
- extent_parameter = ParameterExtent (self .TARGET_AREA ,
68
- self .tr ('Target area for layer' ),
69
- self .INPUT_LAYER )
70
- extent_parameter .skip_crs_check = True
66
+ self .addParameter (QgsProcessingParameterFeatureSource (self .INPUT ,
67
+ self .tr ('Input layer' )))
68
+ extent_parameter = QgsProcessingParameterExtent (self .TARGET_AREA ,
69
+ self .tr ('Target area for layer' ))
70
+ #extent_parameter.skip_crs_check = True
71
71
self .addParameter (extent_parameter )
72
- self .addParameter (ParameterCrs (self .TARGET_AREA_CRS , 'Target area CRS' ))
72
+ self .addParameter (QgsProcessingParameterCrs (self .TARGET_AREA_CRS , 'Target area CRS' ))
73
73
74
- self .addOutput ( OutputHTML (self .OUTPUT_HTML_FILE ,
75
- self .tr ('Candidates ' )))
74
+ self .addParameter ( QgsProcessingParameterFeatureSink (self .OUTPUT ,
75
+ self .tr ('CRS candidates ' )))
76
76
77
77
def name (self ):
78
78
return 'findprojection'
@@ -81,27 +81,34 @@ def displayName(self):
81
81
return self .tr ('Find projection' )
82
82
83
83
def processAlgorithm (self , parameters , context , feedback ):
84
- layer = QgsProcessingUtils . mapLayerFromString ( self .getParameterValue ( self .INPUT_LAYER ) , context )
84
+ source = self .parameterAsSource ( parameters , self .INPUT , context )
85
85
86
- extent = self .getParameterValue (self .TARGET_AREA ).split (',' )
87
- if not extent :
88
- extent = QgsProcessingUtils .combineLayerExtents ([layer ])
89
- target_crs = QgsCoordinateReferenceSystem (self .getParameterValue (self .TARGET_AREA_CRS ))
86
+ extent = self .parameterAsExtent (parameters , self .TARGET_AREA , context )
87
+ target_crs = self .parameterAsCrs (parameters , self .TARGET_AREA_CRS , context )
90
88
91
- target_geom = QgsGeometry .fromRect (QgsRectangle (float (extent [0 ]), float (extent [2 ]),
92
- float (extent [1 ]), float (extent [3 ])))
89
+ target_geom = QgsGeometry .fromRect (extent )
93
90
94
- output_file = self .getOutputValue (self .OUTPUT_HTML_FILE )
91
+ fields = QgsFields ()
92
+ fields .append (QgsField ('auth_id' , QVariant .String , '' , 20 ))
93
+
94
+ (sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
95
+ fields , QgsWkbTypes .NoGeometry , QgsCoordinateReferenceSystem ())
95
96
96
97
# make intersection tests nice and fast
97
98
engine = QgsGeometry .createGeometryEngine (target_geom .geometry ())
98
99
engine .prepareGeometry ()
99
100
100
- layer_bounds = QgsGeometry .fromRect (layer .extent ())
101
+ layer_bounds = QgsGeometry .fromRect (source .sourceExtent ())
102
+
103
+ crses_to_check = QgsCoordinateReferenceSystem .validSrsIds ()
104
+ total = 100.0 / len (crses_to_check )
105
+
106
+ found_results = 0
101
107
102
- results = []
108
+ for current , srs_id in enumerate (crses_to_check ):
109
+ if feedback .isCanceled ():
110
+ break
103
111
104
- for srs_id in QgsCoordinateReferenceSystem .validSrsIds ():
105
112
candidate_crs = QgsCoordinateReferenceSystem .fromSrsId (srs_id )
106
113
if not candidate_crs .isValid ():
107
114
continue
@@ -115,15 +122,15 @@ def processAlgorithm(self, parameters, context, feedback):
115
122
continue
116
123
117
124
if engine .intersects (transformed_bounds .geometry ()):
118
- results . append ( candidate_crs .authid ())
119
-
120
- self . createHTML ( output_file , results )
121
-
122
- def createHTML ( self , outputFile , candidates ):
123
- with codecs . open ( outputFile , 'w' , encoding = 'utf-8' ) as f :
124
- f . write ( '<html><head> \n ' )
125
- f . write ( '<meta http-equiv="Content-Type" content="text/html; \
126
- charset=utf-8" /></head><body> \n ' )
127
- for c in candidates :
128
- f . write ( '<p>' + c + '</p> \n ' )
129
- f . write ( '</body></html> \n ' )
125
+ feedback . pushInfo ( self . tr ( 'Found candidate CRS: {}' ). format ( candidate_crs .authid () ))
126
+ f = QgsFeature ( fields )
127
+ f . setAttributes ([ candidate_crs . authid ()] )
128
+ sink . addFeature ( f , QgsFeatureSink . FastInsert )
129
+ found_results += 1
130
+
131
+ feedback . setProgress ( int ( current * total ) )
132
+
133
+ if found_results == 0 :
134
+ feedback . reportError ( self . tr ( 'No matching projections found' ))
135
+
136
+ return { self . OUTPUT : dest_id }
0 commit comments