-- coding: utf-8 --
“”"
Created on Mon Jul 19 11:54:57 2015
@author: avanetten
“”"
import os
from flask import Flask, json, Response
import pickle
from bokeh.server.crossdomain import crossdomain
from bokeh.models.sources import (ColumnDataSource, AjaxDataSource)
from bokeh.models import (Callback, ColumnDataSource, BoxSelectTool, Range1d,
Rect, Patch, WheelZoomTool, PanTool, PolySelectTool,
Circle, ResetTool, LassoSelectTool, HBox, VBoxForm,
GMapPlot, GMapOptions, LinearAxis, Grid,
Paragraph, PreviewSaveTool)
from bokeh.plotting import figure, show, output_server, output_file, vplot
import bokeh.plotting as bk
import numpy as np
cd to appopriate directory
gmap_background = True
plot_width = 800
plot_height = int(0.75*plot_width)
text = ‘’
define ajax data source
source = AjaxDataSource(data_url=‘http://localhost:5000/data’, polling_interval=1000)
attempt to print coords to screen…
#source_t = AjaxDataSource(data_url=‘http://localhost:5000/text’, polling_interval=1000)
#################
Box Select
rect_data = dict(x=, y=, width=, height=)
source_rect_sel = ColumnDataSource(data=rect_data)
rect_callback = Callback(args=dict(source=source_rect_sel), code=“”"
// get data source from Callback args
var data = source.get(‘data’);
/// get BoxSelectTool dimensions from cb_data parameter of Callback
var geometry = cb_data['geometry'];
/// calculate Rect attributes
var width = geometry['x1'] - geometry['x0'];
var height = geometry['y1'] - geometry['y0'];
var x = geometry['x0'] + width/2;
var y = geometry['y0'] + height/2;
/// replace values
data['x'] = [x];
data['y'] = [y];
data['width'] = [width];
data['height'] = [height];
// update ajax
var url = "http://127.0.0.1:5000/subsample/"+data.x+"/"+data.y+"/"+data.width+"/"+data.height;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.send();
// trigger update of data source
source.trigger('change');
“”")
box_select = BoxSelectTool(callback=rect_callback)
#################
#################
Poly Select (no callback feature yet)
#source_poly_sel = ColumnDataSource(data=dict(x=, y=, width=, height=))
#poly_callback = Callback(args=dict(source=source_poly_sel), code=“”"
// get data source from Callback args
var data = source.get(‘data’);
/// get BoxSelectTool dimensions from cb_data parameter of Callback
var geometry = cb_data[‘geometry’];
/// calculate poly attributes
var x = geometry[‘x’];
var y = geometry[‘y’];
/// replace values
data[‘x’] = ;
data[‘y’] = [y];
// trigger update of data source
source.trigger(‘change’);
#“”")
#poly_select = PolySelectTool(callback=poly_callback)
#################
##################
if not gmap_background:
## Figure
p = bk.figure(plot_width=plot_width,
plot_height=plot_height,
tools=[box_select, WheelZoomTool(), PanTool(), ResetTool(),
LassoSelectTool()],
title=“Select Below”,
x_range=(0,1),#Range1d(start=0.0, end=1.0),
y_range=(0,1))#Range1d(start=0.0, end=1.0))
else:
# gmap
mapzoom = 12
x_mid, y_mid = -77.046998, 38.888214 # Washington DC
map_options = GMapOptions(lat=y_mid, lng=x_mid, zoom=mapzoom)
x_range = Range1d()
y_range = Range1d()
p = GMapPlot(x_range=x_range, y_range=y_range,
map_options=map_options,
plot_height=plot_height, plot_width=plot_width,
title='Select Below')
p.map_options.map_type='hybrid'
# !! Reset Tool goes to lat, lon = 0,0 !!
p.add_tools(box_select, WheelZoomTool(), PanTool(), PreviewSaveTool())
#,#PolySelectTool(), LassoSelectTool(), ResetTool())
# add axes
xaxis = LinearAxis(axis_label='Longitude')
p.add_layout(xaxis, 'below')
yaxis = LinearAxis(axis_label='Latitude')
p.add_layout(yaxis, 'left')
# add grid
xgrid = Grid(plot=p, dimension=0, ticker=xaxis.ticker, \
grid_line_dash="dashed", grid_line_color="gray")
ygrid = Grid(plot=p, dimension=1, ticker=yaxis.ticker, \
grid_line_dash="dashed", grid_line_color="gray")
p.renderers.extend([xgrid, ygrid])
define glyphs
rect = Rect(x=‘x’,
y=‘y’,
width=‘width’,
height=‘height’,
fill_alpha=0.3,
fill_color=‘#009933’,
line_color=‘black’)
poly = Patch(x=‘x’,
y=‘y’,
fill_alpha=0.3,
fill_color=‘#009933’)
add glyphs to plot
p.add_glyph(source_rect_sel, rect, selection_glyph=rect,
nonselection_glyph=rect)
#p.add_glyph(source_poly_sel, poly, selection_glyph=poly,
nonselection_glyph=poly)
This resets the plot, not sure why
from bokeh.models.widgets import Button
button = Button(label=“Reset”, type=“success”)
#############
paragraph = Paragraph(text="Coordinates: ")
create plot
vbox = VBoxForm(children=[button], width=50)
vbox2 = VBoxForm(children=[p, paragraph])
hbox = HBox(children=[vbox, vbox2])
bk.output_file(“remotedata.html”)
bk.save(obj=hbox)
bk.show(hbox
##############
app = Flask(name)
@app.route(‘/subsample////’, methods=[‘GET’, ‘OPTIONS’])
@crossdomain(origin=“*”, methods=[‘GET’, ‘POST’], headers=None)
def subsample(x, y, width, height):
global rect_data, text, paragraph
rect_data = dict(x=float(x), y=float(y),
width=float(width), height=float(height))
# define text for title
text = ‘’
for key in rect_data:
title_add = key + ': ’ + str(round(rect_data[key], 2)) + ’ ’
text = text + title_add
paragraph.text = text
print "rect_data", rect_data
#print "title", text
# save to file
pickle.dump(rect_data, open('coords.pkl', 'wb'))
return json.jsonify(rect_data)
@app.route(‘/data’, methods=[‘GET’, ‘OPTIONS’])
@crossdomain(origin=“*”, methods=[‘GET’, ‘POST’], headers=None)
def get_data():
return json.jsonify(rect_data)
try to update text in plot
@app.route(‘/text’, methods=[‘GET’, ‘OPTIONS’])
@crossdomain(origin=“*”, methods=[‘GET’, ‘POST’], headers=None)
def get_text():
print “text”
return json.jsonify(text)
if name == ‘main’: