-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTest1_bokeh.py
68 lines (55 loc) · 1.92 KB
/
Test1_bokeh.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
##Plot_Layer=vector
##x_axis=field Plot_Layer
##x_type=selection Automatic;Linear;Logarithmic;DateTime
##y_axis=field Plot_Layer
##y_type=selection Automatic;Linear;Logarithmic;DateTime
##PopUp=field Plot_Layer
##Bokeh=output file
##Color=string red
##Size=number 10
from bokeh.io import output_file
from bokeh.plotting import figure, show, save, ColumnDataSource
from bokeh.models import HoverTool, PanTool, WheelZoomTool, ResetTool, BoxZoomTool, SaveTool
from qgis.core import QgsVectorLayer
# Set axis type
axis_type = ["auto", "linear", "log", "datetime"]
xtype = axis_type[x_type]
ytype = axis_type[y_type]
# Set Output file (the mode parameter can be cdn or inline, and cdn is default)
output_file(Bokeh, title='QGIS Vector Layer Plot by Klas Karlsson', mode='cdn')
# Create QGIS vector layer object
lyr = QgsVectorLayer(Plot_Layer, "Lager", "ogr")
# Get field index number for selected fields
idx = lyr.fieldNameIndex(x_axis)
idy = lyr.fieldNameIndex(y_axis)
idp = lyr.fieldNameIndex(PopUp)
# Create Bokeh ColumnDataSource
source = ColumnDataSource(data=dict(
x = [i.attributes()[idx] for i in lyr.getFeatures()],
y = [i.attributes()[idy] for i in lyr.getFeatures()],
pop = [i.attributes()[idp] for i in lyr.getFeatures()]
))
# Create Hoover Tooltip
hover = HoverTool(tooltips='@pop')
# Variables for labels in plot
title = 'Plotting ' + x_axis + ' vs ' + y_axis
x_label = x_axis
y_label = y_axis
# Create the Bokeh plot canvas
p = figure(
title=title,
plot_width=1000,
plot_height=1000,
x_axis_label=x_label,
x_axis_type=xtype,
y_axis_label=y_label,
y_axis_type=ytype,
tools=[hover]
)
# Add suitable tools for the plot
p.add_tools(PanTool(), BoxZoomTool(), WheelZoomTool(), ResetTool(), SaveTool())
# Add the data series with style to the plot
p.circle('x', 'y', size=Size, color=Color, alpha=0.6, source=source)
# Save the generated Bokeh plot as file
save(p)
progress.setText("File saved: " + Bokeh)