This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
/
map_index.py
410 lines (350 loc) · 14.4 KB
/
map_index.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""
/***************************************************************************
LoadByClass
A QGIS plugin
Load database classes.
-------------------
begin : 2014-12-18
git sha : $Format:%H$
copyright : (C) 2014 by Luiz Andrade - Cartographic Engineer @ Brazilian Army
email : luiz.claudio@dsg.eb.mil.br
mod history : 2014-12-17 by Leonardo Lourenço - Computing Engineer @ Brazilian Army
mod history : 2014-12-17 by Maurício de Paulo - Cartographic Engineer @ Brazilian Army
***************************************************************************/
/***************************************************************************
* *
* 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 PyQt4.QtCore import QRunnable, QObject, pyqtSignal, pyqtSlot, QVariant
from qgis.core import QgsPoint, QgsGeometry, QgsFeature, QgsVectorLayer, QgsField, QgsCoordinateReferenceSystem, QgsCoordinateTransform
from qgis.core import QgsMapLayerRegistry, QgsVectorFileWriter
import string, os
class Aux(QObject):
rangeCalculated = pyqtSignal(int)
processFinished = pyqtSignal()
stepProcessed = pyqtSignal()
errorOccurred = pyqtSignal(str)
userCanceled = pyqtSignal()
def __init__(self, thread):
super(Aux, self).__init__()
self.thread = thread
def getMemoryLayerErrorMessage(self):
return self.tr('Problem loading memory layer!')
def getIndexFieldName(self):
return self.tr('map_index')
@pyqtSlot()
def cancel(self):
self.thread.stop()
class UtmGrid(QRunnable):
def __init__(self):
"""Constructor."""
super(UtmGrid, self).__init__()
self.scales = [1000,500,250,100,50,25,10,5,2,1]
nomen1000 = ['Nao Recorta']
nomen500 = [['V','X'],['Y','Z']]
nomen250 = [['A','B'],['C','D']]
nomen100 = [['I','II','III'],['IV','V','VI']]
nomen50 = [['1','2'],['3','4']]
nomen25 = [['NO','NE'],['SO','SE']]
nomen10 = [['A','B'],['C','D'],['E','F']]
nomen5 = [['I','II'],['III','IV']]
nomen2 = [['1', '2', '3'], ['4', '5', '6']]
nomen1 = [['A','B'],['C','D']]
self.scaleText = [nomen1000,nomen500,nomen250,nomen100,nomen50,nomen25,nomen10,nomen5,nomen2,nomen1]
self.matrizRecorte = []
self.spacingX = []
self.spacingY = []
self.stepsDone = 0
self.stepsTotal = 0
self.featureBuffer = []
self.MIdict = []
self.MIRdict = []
self.aux = Aux(self)
self.stopped = False
def stop(self):
self.stopped = True
def findScaleText(self, scaleText, scaleId):
"""Get the scale matrix for the given scaleText and scaleId
"""
j = -1
for j, row in enumerate(self.scaleText[scaleId]):
if scaleText in row:
i=row.index(scaleText)
break
return (i, len(self.scaleText[scaleId])-j-1)
def getScale(self, inomen):
"""Get scale for the given map index
"""
return self.scales[ self.getScaleIdFromiNomen(inomen) ]
def getScaleIdFromiNomen(self, inomen):
"""Get scale index in self.scales object for the given map index
"""
id = len(inomen.split('-')) - 2
return id
def getScaleIdFromScale(self, scale):
"""Get scale if for the given scale (e.g. 1, 2, 25, 250)
"""
return self.scales.index(scale)
def getSpacingX(self, scale):
"""Get X spacing fot the given scale
"""
scaleId = self.scales.index(scale)
if scaleId < 0: return 0
if len(self.spacingX) == 0:
dx = 6
self.spacingX = [dx]
for i in range(1, len(self.scaleText)):
subdivisions = len(self.scaleText[i][0])
dx /= float(subdivisions)
self.spacingX.append(dx)
return self.spacingX[scaleId]
def getSpacingY(self, scale):
"""Get Y spacing fot the given scale
"""
scaleId = self.scales.index(scale)
if scaleId < 0: return 0
if len(self.spacingY) == 0:
dy = 4
self.spacingY = [dy]
for i in range(1, len(self.scaleText)):
subdivisions = len(self.scaleText[i])
dy /= float(subdivisions)
self.spacingY.append(dy)
return self.spacingY[scaleId]
def makeQgsPolygon(self, xmin, ymin, xmax, ymax):
"""Creating a polygon for the given coordinates
"""
dx = (xmax - xmin)/3
dy = (ymax - ymin)/3
polyline = []
point = QgsPoint(xmin, ymin)
polyline.append(point)
point = QgsPoint(xmin+dx, ymin)
polyline.append(point)
point = QgsPoint(xmax-dx, ymin)
polyline.append(point)
point = QgsPoint(xmax, ymin)
polyline.append(point)
point = QgsPoint(xmax, ymin+dy)
polyline.append(point)
point = QgsPoint(xmax, ymax-dy)
polyline.append(point)
point = QgsPoint(xmax, ymax)
polyline.append(point)
point = QgsPoint(xmax-dx, ymax)
polyline.append(point)
point = QgsPoint(xmin+dx, ymax)
polyline.append(point)
point = QgsPoint(xmin, ymax)
polyline.append(point)
point = QgsPoint(xmin, ymax-dy)
polyline.append(point)
point = QgsPoint(xmin, ymin+dy)
polyline.append(point)
point = QgsPoint(xmin, ymin)
polyline.append(point)
qgsPolygon = QgsGeometry.fromMultiPolygon([[polyline]])
return qgsPolygon
def getHemisphereMultiplier(self,inomen):
"""Check the hemisphere
"""
if len(inomen) > 1:
h = inomen[0].upper()
if h == 'S':
return -1
else:
return 1
def getLLCornerLatitude1kk(self,inomen):
"""Get lower left Latitude for 1:1.000.000 scale
"""
l = inomen[1].upper()
y = 0.0;
operator = self.getHemisphereMultiplier(inomen)
verticalPosition = string.uppercase.index(l)
y = (y+4*verticalPosition)*operator
if (operator<0): y-=4
return y
def getLLCornerLongitude1kk(self,inomen):
"""Get lower left Longitude for 1:1.000.000 scale
"""
fuso = int(inomen[3:5])
x = 0
if((fuso > 0) and (fuso <= 60)):
x = (((fuso - 30)*6.0)-6.0)
return x
def getLLCorner(self,inomen):
"""Get lower left coordinates for scale determined by the given map index
"""
x = self.getLLCornerLongitude1kk(inomen)
y = self.getLLCornerLatitude1kk(inomen)
inomenParts = inomen.upper().split('-')
#Escala de 500.00
for partId in range(2,len(inomenParts)):
scaleId = partId-1
dx = self.getSpacingX(self.scales[scaleId])
dy = self.getSpacingY(self.scales[scaleId])
scaleText = inomenParts[partId]
i,j = self.findScaleText(scaleText, partId-1)
x += i*dx
y += j*dy
return (x,y)
def computeNumberOfSteps(self,startScaleId,stopScaleId):
"""Compute the number of steps to build a progress
"""
steps=1
for i in range(startScaleId+1,stopScaleId+1):
steps *= len(self.scaleText[i])*len(self.scaleText[i][0])
return steps
def getQgsPolygonFrame(self, map_index):
"""Particular case used to create frame polygon for the given
map_index
"""
scale = self.getScale(map_index)
(x, y) = self.getLLCorner(map_index)
dx = self.getSpacingX(scale)
dy = self.getSpacingY(scale)
poly = self.makeQgsPolygon(x, y, x + dx, y + dy)
return poly
def populateQgsLayer(self, iNomen, stopScale, layer, mi = ''):
"""Generic recursive method to create frame polygon for the given
stopScale within the given map index (iNomen)
"""
if self.stopped:
del layer
layer = None
self.aux.userCanceled.emit()
return
scale = self.getScale(iNomen)
#first run
if (self.stepsTotal==0):
self.stepsTotal=self.computeNumberOfSteps(self.getScaleIdFromScale(scale), self.getScaleIdFromScale(stopScale))
self.aux.rangeCalculated.emit(self.stepsTotal*2)
self.stepsDone = 0
if scale == stopScale:
(x, y) = self.getLLCorner(iNomen)
dx = self.getSpacingX(stopScale)
dy = self.getSpacingY(stopScale)
poly = self.makeQgsPolygon(x, y, x + dx, y + dy)
self.insertFrameIntoQgsLayer(layer, poly, iNomen, mi)
self.stepsDone += 1
self.aux.stepProcessed.emit()
else:
scaleId = self.getScaleIdFromiNomen(iNomen)
matrix = self.scaleText[ scaleId+1 ]
for i in range(len(matrix)):
line = matrix[i]
for j in range(len(line)):
inomen2 = iNomen + '-' + line[j]
mi2 = mi + '-' + line[j]
self.populateQgsLayer(inomen2, stopScale, layer, mi2)
def insertFrameIntoQgsLayer(self, layer, poly, map_index, mi):
"""Inserts the poly into layer
"""
provider = layer.dataProvider()
#Creating the feature
feature = QgsFeature()
feature.setGeometry(poly)
#Fixing the problem to get MI when the base scale is not 100k
mi_index = '-'.join(map_index.split('-')[0:5])
mi = self.getMIFromINomen(mi_index)
#------------------------------------------------------------
feature.setAttributes([map_index, mi])
# Adding the feature into the file
provider.addFeatures([feature])
def getMIdict(self):
if not self.MIdict:
self.MIdict = self.getDict("MI100.csv")
return self.MIdict
def getMIRdict(self):
if not self.MIRdict:
self.MIRdict = self.getDict("MIR250.csv")
return self.MIRdict
def getDict(self, file_name):
csvFile = open(os.path.join(os.path.dirname(__file__),file_name))
data = csvFile.readlines()
csvFile.close()
l1 = map(lambda x: (x.strip()).split(';'),data)
dicionario = dict((a[1].lstrip('0'),a[0]) for a in l1)
return dicionario
def getINomenFromMI(self,mi):
return self.getINomen(self.getMIdict(), mi)
def getINomenFromMIR(self,mir):
return self.getINomen(self.getMIRdict(), mir)
def getINomen(self, dict, index):
key = index.split('-')[0]
otherParts = index.split('-')[1:]
if (dict.has_key(key)):
return dict[key]+'-'+string.join(otherParts,'-')
else:
return ''
def getMIFromINomen(self,inomen):
return self.getMI(self.getMIdict(), inomen)
def getMI(self, dict, inomen):
if inomen in dict.values():
return dict.keys()[dict.values().index(inomen)].lstrip('0')
else:
return ''
def setParameters(self, index, stopScale, mi, crs, output, layer = None):
self.index = index
self.stopScale = stopScale
self.mi = mi
self.crs = crs
self.output = output
self.layer = layer
def createGridLayer(self, name, layerType, crsAuthId):
layer = QgsVectorLayer('%s?crs=%s'% (layerType, crsAuthId), name, 'memory')
if not layer.isValid():
self.aux.errorOccurred.emit(self.aux.getMemoryLayerErrorMessage())
return
provider = layer.dataProvider()
provider.addAttributes([QgsField(self.aux.getIndexFieldName(), QVariant.String), QgsField('mi', QVariant.String)])
layer.updateFields()
return layer
def run(self):
tempLayer = self.createGridLayer('temp', 'Multipolygon', self.crs.geographicCRSAuthId())
self.populateQgsLayer(self.index, self.stopScale, tempLayer, self.mi)
useMemory = True
if not self.layer:
useMemory = False
self.layer = self.createGridLayer('Grid Zones', 'Multipolygon', self.crs.authid())
for feature in tempLayer.getFeatures():
if self.stopped:
del tempLayer
tempLayer = None
self.aux.userCanceled.emit()
return
geom = feature.geometry()
reprojected = self.reprojectGridZone(geom)
self.insertGridZoneIntoQgsLayer(self.layer, reprojected, feature.attributes())
self.aux.stepProcessed.emit()
del tempLayer
tempLayer = None
self.aux.stepProcessed.emit()
if not useMemory:
QgsVectorFileWriter.writeAsVectorFormat(self.layer, self.output, "utf-8", None, "ESRI Shapefile")
self.aux.processFinished.emit()
def reprojectGridZone(self, multipoly):
crsSrc = QgsCoordinateReferenceSystem(self.crs.geographicCRSAuthId())
coordinateTransformer = QgsCoordinateTransform(crsSrc, self.crs)
polyline = multipoly.asMultiPolygon()[0][0]
newPolyline = []
for point in polyline:
newPolyline.append(coordinateTransformer.transform(point))
qgsMultiPolygon = QgsGeometry.fromMultiPolygon([[newPolyline]])
return qgsMultiPolygon
def insertGridZoneIntoQgsLayer(self, layer, multipoly, attributes):
"""Inserts the poly into layer
"""
provider = layer.dataProvider()
#Creating the feature
feature = QgsFeature()
feature.setGeometry(multipoly)
feature.setAttributes(attributes)
# Adding the feature into the file
provider.addFeatures([feature])