forked from Oslandia/albion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis_layer.py
75 lines (58 loc) · 2.17 KB
/
axis_layer.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
# coding: utf-8
from qgis.core import (QgsPluginLayerType,
QgsPluginLayer,
QgsRectangle)
from PyQt4.QtCore import pyqtSignal
import traceback
import logging
class AxisLayerType(QgsPluginLayerType):
def __init__(self):
QgsPluginLayerType.__init__(self, AxisLayer.LAYER_TYPE)
def createLayer(self):
return AxisLayer()
return True
def showLayerProperties(self, layer):
return False
class AxisLayer(QgsPluginLayer):
LAYER_TYPE = 'axis'
__msg = pyqtSignal(str)
__drawException = pyqtSignal(str)
def __init__(self, crs):
QgsPluginLayer.__init__(
self,
AxisLayer.LAYER_TYPE,
'axis plugin layer')
self.__msg.connect(self.__print)
self.__drawException.connect(self.__raise)
self.setCrs(crs)
self.setValid(True)
def extent(self):
return QgsRectangle(-1, -1, 1, 1)
def __print(self, msg):
logging.info(msg)
def __raise(self, err):
logging.error(err)
raise Exception(err)
def draw(self, rendererContext):
try:
painter = rendererContext.painter()
ext = rendererContext.extent()
map_unit_per_pixel = rendererContext.mapToPixel().\
mapUnitsPerPixel()
width, height = \
int((ext.xMaximum()-ext.xMinimum())/map_unit_per_pixel),\
int((ext.yMaximum()-ext.yMinimum())/map_unit_per_pixel)
nb_div = 10
dw, dh = width/nb_div, height/nb_div
dx, dy = ((ext.xMaximum()-ext.xMinimum())/nb_div,
(ext.yMaximum()-ext.yMinimum())/nb_div)
for i in range(nb_div+2):
painter.drawText(5, int(i*dh), "%.0f" % (ext.yMaximum()-i*dy))
painter.drawLine(50, int(i*dh), 60, int(i*dh))
for i in range(nb_div+2):
painter.drawText(int(i*dw), 20, "%.0f" % (ext.xMinimum()+i*dx))
painter.drawLine(int(i*dw), 20, int(i*dw), 25)
return True
except Exception as e:
self.__drawException.emit(traceback.format_exc(e))
return False