-
Notifications
You must be signed in to change notification settings - Fork 4
/
osgrLayer.py
348 lines (270 loc) · 11 KB
/
osgrLayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# -*- coding: utf-8 -*-
"""
/***************************************************************************
osgrLayer
A class for handling grid squares generated 'on the fly'
FSC QGIS plugin for biological recorders
-------------------
begin : 2014-02-17
copyright : (C) 2014 by Rich Burkmar, Field Studies Council
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.core import *
from qgis.gui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtNetwork import *
#from osgr import *
#from envmanager import *
from . import osgr
from . import envmanager
class osgrLayer(QObject):
progChange = pyqtSignal(int)
progMax = pyqtSignal(int)
gridComplete = pyqtSignal()
def __init__(self, iface):
super(osgrLayer,self).__init__()
self.canvas = iface.mapCanvas()
self.iface = iface
# Get a reference to an osgr object
self.osgr = osgr.osgr()
self.vl = None
def getCRS(self):
if self.vl is None:
return None
else:
try:
#Vector layer Could have been removed
return self.vl.crs()
except:
return None
def infoMessage(self, strMessage):
self.iface.messageBar().pushMessage("Info", strMessage, level=Qgis.Info)
def logMessage(self, strMessage, level=Qgis.Info):
QgsMessageLog.logMessage(strMessage, "OSGR Tool", level)
def createLayer(self):
# Create layer with the same CRS as the canvas.
# Need to find a way to create a layer with specified CRS other than by EPSG string
# because this is retained in general layer properties even after CRS is changed.
# If layer created without CRS, the user is prompted to select one so that's not an option.
#self.vl = QgsVectorLayer("Polygon?epsg:27700", "OSGR grid squares", "memory")
#self.vl.setCrs(self.canvas.mapSettings().destinationCrs())
QgsMessageLog.logMessage(self.canvas.mapSettings().destinationCrs().authid(), "OSGR Tool")
self.vl = QgsVectorLayer("Polygon?crs=" + self.canvas.mapSettings().destinationCrs().authid() + "&field=GridType:string(22)&field=GridRef:string(12)", "OSGR grid squares", "memory")
self.pr = self.vl.dataProvider()
# Symbology
props = { 'color_border' : '0,0,0,200', 'style' : 'no', 'style_border' : 'solid' }
s = QgsFillSymbol.createSimple(props)
self.vl.setRenderer( QgsSingleSymbolRenderer( s ) )
# Labeling
palyr = QgsPalLayerSettings()
palyr.fieldName = 'GridRef'
palyr.placement = Qgis.LabelPlacement.OverPoint
l = QgsVectorLayerSimpleLabeling(palyr)
self.vl.setLabeling(l)
if self.showLabels:
self.vl.setLabelsEnabled(True)
else:
self.vl.setLabelsEnabled(False)
# Add to map layer registry
QgsProject.instance().addMapLayer(self.vl)
def clear(self):
try:
QgsProject.instance().removeMapLayer(self.vl.id())
except:
pass
def setGrType(self, grType):
self.grType = grType
def setPrecision(self, precision):
self.precision = precision
def setPrecisionText(self, precisionText):
self.precisionText = precisionText
def setShowLabels(self, boolShowLabels):
self.showLabels = boolShowLabels
if not self.vl is None:
QgsMessageLog.logMessage("self.vl IS set", 'OSGRLayer', Qgis.Info)
if self.showLabels:
self.vl.setLabelsEnabled(True)
else:
self.vl.setLabelsEnabled(False)
self.vl.triggerRepaint()
else:
QgsMessageLog.logMessage("No self.vl set", 'OSGRLayer', Qgis.Info)
try:
pass
except:
pass
def cancelGrid(self):
self.cancel = True
def addSquare(self, gr, grType, precisionText, square):
#Transform square to output layer CRS if required
if grType == "os" and self.vl.crs().authid() != "EPSG:27700":
transGrid = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:27700"), self.vl.crs(), QgsProject.instance())
elif grType == "irish" and self.vl.crs().authid() != "EPSG:29903":
transGrid = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:29903"), self.vl.crs(), QgsProject.instance())
elif grType == "other" and self.vl.crs().authid() != self.canvas.mapSettings().destinationCrs().authid():
transGrid = QgsCoordinateTransform(self.canvas.mapSettings().destinationCrs(), self.vl.crs(), QgsProject.instance())
else:
transGrid = None
if transGrid is not None:
square.transform(transGrid)
fet = QgsFeature()
fet.setGeometry(square)
self.iface.messageBar().pushMessage("Info", gr, level=Qgis.Info)
if gr != "na" and gr!="":
fet.setAttributes([precisionText, gr])
else:
fet.setAttributes(["", ""])
self.vl.addFeatures([fet])
def GenerateSquares(self, xMin, yMin, xMax, yMax, selectedGeometries=[], selectedFeaturesCrs=None):
self.canvas.setRenderFlag(False)
# If layer is not present, create it
try:
self.vl.startEditing()
except:
# Create grid layer
self.createLayer()
self.vl.startEditing()
# Apply any offsets specified in environment file
self.env = envmanager.envManager()
try:
offsetX = float(self.env.getEnvValue("biorec.xGridOffset"))
except:
offsetX = 0
try:
offsetY = float(self.env.getEnvValue("biorec.yGridOffset"))
except:
offsetY = 0
# Build the grid
llx = ((xMin - offsetX) // self.precision) * self.precision + offsetX
lly = ((yMin - offsetY) // self.precision) * self.precision + offsetY
x = llx
y = lly
self.cancel = False
iCount = 0
xStep = (xMax - llx) // self.precision + 1
yStep = (yMax - lly) // self.precision + 1
iMax = int(xStep * yStep)
self.progMax.emit(iMax)
self.progChange.emit(0)
while x < xMax:
while y < yMax:
# add a feature for the grid square (if it has a valid GR)
if self.grType == "os" or self.grType == "irish":
# This may return string 'na' if the precision
# is not a valid one for the grid.
gr = self.osgr.grFromEN(x,y,self.precision, self.grType)
else:
gr = ""
# If grid type os 'other' then always make a square (subject to overlap where appropriate).
# If grid type is 'os' or 'irish' only make a square if valid GR
if self.grType == "other" or (gr != "" and gr != "na"):
points = [[QgsPointXY(x,y), QgsPointXY(x,y + self.precision), QgsPointXY(x + self.precision,y + self.precision), QgsPointXY(x + self.precision,y)]]
square = QgsGeometry.fromPolygonXY(points)
#At this point we have a square generated in the correct CRS - British, Irish or other (map canvas CRS)
#and we can check to see if it overlaps with passed in geometries which have been passed in with
#the correct CRS.
include = False
if len(selectedGeometries) > 0:
self.logMessage("testing oerlap")
for geom in selectedGeometries:
if geom.intersects(square):
self.logMessage("overlap found")
include = True
break
else:
include = True
if include:
self.addSquare(gr, self.grType, self.precisionText, square)
y += self.precision
# Process other events so if user clicks cancel button, this operation is cancelled.
iCount += 1
self.progChange.emit(iCount)
if iCount % 100 == 0:
QCoreApplication.processEvents()
if self.cancel:
break
y = lly
x += self.precision
if self.cancel:
break
self.cancel = False
if self.cancel:
self.vl.rollBack(True)
else:
# Commit changes
self.vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
self.vl.updateExtents()
self.vl.removeSelection()
self.canvas.setRenderFlag(True)
self.progChange.emit(0)
self.gridComplete.emit()
def GenerateSquaresFromArray(self, grs):
self.canvas.setRenderFlag(False)
# If layer is not present, create it
try:
self.vl.startEditing()
except:
# Create grid layer
self.createLayer()
self.vl.startEditing()
# Apply any offsets specified in environment file
self.env = envmanager.envManager()
try:
offsetX = float(self.env.getEnvValue("biorec.xGridOffset"))
except:
offsetX = 0
try:
offsetY = float(self.env.getEnvValue("biorec.yGridOffset"))
except:
offsetY = 0
self.cancel = False
iCount = 0
iMax = len(grs)
self.progMax.emit(iMax)
self.progChange.emit(0)
for gr in grs:
ret = self.osgr.checkGR(gr)
precisionText = ret[1]
grType = ret[2]
if grType == "os":
crs = "EPSG:27700"
elif grType == "irish":
crs = "EPSG:29903"
else:
crs = None
if crs is not None:
self.logMessage("processing " + gr + " " + crs)
square = self.osgr.geomFromGR(gr, "square", crs)
self.addSquare(gr, grType, precisionText, square)
# Process other events so if user clicks cancel button, this operation is cancelled.
iCount += 1
self.progChange.emit(iCount)
if iCount % 100 == 0:
QCoreApplication.processEvents()
if self.cancel:
break
self.cancel = False
if self.cancel:
self.vl.rollBack(True)
else:
# Commit changes
self.vl.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
self.vl.updateExtents()
self.vl.removeSelection()
self.canvas.setRenderFlag(True)
self.progChange.emit(0)
self.gridComplete.emit()