-
Notifications
You must be signed in to change notification settings - Fork 4
/
legendlayer.py
210 lines (181 loc) · 7.17 KB
/
legendlayer.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : Legend Layer
Description : Classes for add legend in layer
Date : July, 2015
copyright : (C) 2015 by Luiz Motta
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 PyQt4 import QtCore, QtGui, QtXml
from qgis import core as QgsCore, gui as QgsGui, utils as QgsUtils
class PolygonEffectsCanvas():
def __init__(self):
self.canvas = QgsUtils.iface.mapCanvas()
self.crs = None # setCRS if need
self.color = QtGui.QColor(255,0,0)
def setCRS(self, crs):
self.crs = crs
def zoom(self, extent):
extentTransform = extent
crsCanvas = self.canvas.mapSettings().destinationCrs()
if not self.crs == crsCanvas:
ct = QgsCore.QgsCoordinateTransform( self.crs, crsCanvas )
extentTransform = ct.transform( extent )
self.canvas.setExtent( extentTransform )
self.canvas.zoomByFactor(1.05)
self.canvas.refresh()
def highlight(self, geom, seconds=2):
def removeRB():
rb.reset( True )
self.canvas.scene().removeItem( rb )
geomTransform = geom
crsCanvas = self.canvas.mapSettings().destinationCrs()
if not self.crs == crsCanvas:
ct = QgsCore.QgsCoordinateTransform( self.crs, crsCanvas )
geomTransform.transform( ct )
rb = QgsGui.QgsRubberBand( self.canvas, QgsCore.QGis.Polygon)
rb.setBorderColor( self.color )
rb.setWidth(2)
rb.setToGeometry( geomTransform, None )
QtCore.QTimer.singleShot( seconds*1000, removeRB )
class LegendRaster(object):
def __init__(self, pluginName):
def initLegendLayer():
self.legendLayer = [
{
'menu': u"Highlight",
'id': "idHighlight",
'slot': self.highlight,
'action': None
},
{
'menu': u"Zoom to",
'id': "idZoom",
'slot': self.zoom,
'action': None
},
{
'menu': u"Open form(table)",
'id': "idForm",
'slot': self.openForm,
'action': None
}
]
for item in self.legendLayer:
item['action'] = QtGui.QAction( item['menu'], None )
item['action'].triggered.connect( item['slot'] )
self.legendInterface.addLegendLayerAction( item['action'], self.pluginName, item['id'], QgsCore.QgsMapLayer.RasterLayer, False )
self.pluginName = pluginName
self.msgBar = QgsUtils.iface.messageBar()
self.legendInterface = QgsUtils.iface.legendInterface()
initLegendLayer() # Set self.legendLayer
self.polygonEC = PolygonEffectsCanvas()
def __del__(self):
for item in self.legendLayer:
self.legendInterface.removeLegendLayerAction( item['action'] )
def setLayer(self, layer):
for item in self.legendLayer:
self.legendInterface.addLegendLayerActionForLayer( item['action'], layer )
@QtCore.pyqtSlot()
def zoom(self):
layer = self.legendInterface.currentLayer()
extent = layer.extent()
self.polygonEC.setCRS( layer.crs() )
self.polygonEC.zoom( extent )
geom = QgsCore.QgsGeometry.fromRect( extent )
self.polygonEC.highlight( geom )
@QtCore.pyqtSlot()
def highlight(self):
layer = self.legendInterface.currentLayer()
geom = QgsCore.QgsGeometry.fromRect( layer.extent() )
self.polygonEC.highlight( geom )
@QtCore.pyqtSlot()
def openForm(self):
pass
class LegendTMSXml(LegendRaster):
def __init__(self, pluginName):
super(LegendRasterGeom, self).__init__( pluginName )
def _getExtent(self, layer):
def getTargetWindow():
nodes = doc.elementsByTagName('TargetWindow')
node = nodes.item( 0 )
targetWindow = { 'ulX': None, 'ulY': None, 'lrX': None, 'lrY': None }
labels = { 'UpperLeftX': 'ulX', 'UpperLeftY': 'ulY', 'LowerRightX': 'lrX', 'LowerRightY': 'lrY' }
for key, value in labels.iteritems():
text = node.firstChildElement( key ).text()
if len( text ) == 0:
continue
targetWindow[ value ] = float( text )
return targetWindow
doc = QtXml.QDomDocument()
file = QtCore.QFile( layer.source() )
doc.setContent( file )
file.close()
tw = getTargetWindow()
return QgsCore.QgsRectangle( tw['ulX'], tw['lrY'], tw['lrX'], tw['ulY'] )
@QtCore.pyqtSlot()
def zoom(self):
layer = self.legendInterface.currentLayer()
extent = self._getExtent( layer )
self.polygonEC.setCRS( layer.crs() )
self.polygonEC.zoom( extent )
geom = QgsCore.QgsGeometry.fromRect( extent )
self.polygonEC.highlight( geom )
@QtCore.pyqtSlot()
def highlight(self):
layer = self.legendInterface.currentLayer()
extent = self.self._getExtent( layer )
geom = QgsCore.QgsGeometry.fromRect( extent )
self.polygonEC.setCRS( layer.crs() )
self.polygonEC.highlight( geom )
class LegendRasterGeom(LegendRaster):
def __init__(self, pluginName):
super(LegendRasterGeom, self).__init__( pluginName )
def _getGeometry(self, layer):
wkt_geom = layer.customProperty('wkt_geom')
return QgsCore.QgsGeometry.fromWkt( wkt_geom )
@QtCore.pyqtSlot()
def zoom(self):
layer = self.legendInterface.currentLayer()
geom = self._getGeometry( layer )
self.polygonEC.setCRS( layer.crs() )
self.polygonEC.zoom( geom.boundingBox() )
self.polygonEC.highlight( geom )
@QtCore.pyqtSlot()
def highlight(self):
layer = self.legendInterface.currentLayer()
geom = self._getGeometry( layer )
self.polygonEC.setCRS( layer.crs() )
self.polygonEC.highlight( geom )
@QtCore.pyqtSlot()
def openForm(self):
layer = self.legendInterface.currentLayer()
id_table = layer.customProperty('id_table')
layers_table = [ l for l in self.legendInterface.layers() if id_table == l.id() ]
if len( layers_table ) == 0:
msg = "Layer used for create this image not found."
arg = ( self.pluginName, msg, QgsGui.QgsMessageBar.WARNING, 4 )
self.msgBar.pushMessage( *arg )
return
table = layers_table[0]
id_image = layer.customProperty('id_image')
request = QgsCore.QgsFeatureRequest().setFilterExpression( u'"id" = \'{0}\''.format( id_image ) )
request.setFlags( QgsCore.QgsFeatureRequest.NoGeometry )
feats = [ f for f in table.getFeatures(request) ]
if len( feats ) == 0:
msg = "Image '{}' not found in '{}'.".format( id_image, table.name() )
arg = ( self.pluginName, msg, QgsGui.QgsMessageBar.WARNING, 4 )
self.msgBar.pushMessage( *arg )
return
form = QgsUtils.iface.getFeatureForm( table, feats[0] )
form.show()