From 0aec916d92e050bb3802288975b3adbebad43882 Mon Sep 17 00:00:00 2001 From: Lauren Gulland Date: Sat, 5 Mar 2016 21:23:42 -0500 Subject: [PATCH 01/30] Added Oliver Steeles example code on interactive maps --- pygame_draw_state.py | 66 ++++++++++++++++++++++++++++++++++++++ us_map.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 pygame_draw_state.py create mode 100644 us_map.py diff --git a/pygame_draw_state.py b/pygame_draw_state.py new file mode 100644 index 0000000..ace8eca --- /dev/null +++ b/pygame_draw_state.py @@ -0,0 +1,66 @@ +"""Sample code for `us_map.py`. + +Author: Oliver Steele +License: MIT + +Requirements: + + sudo pip install BeautifulSoup + sudo pip install matplotlib + sudo pip install svg.path +""" + +import pygame +import sys +import matplotlib.path +import us_map + +# Colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +BLUE = (0, 0, 255) +GREEN = (0, 255, 0) +GRAY = (127, 127, 127) +LIGHT_GRAY = (191, 191, 191) + +STATE = 'CO' + +width, height = 640, 480 + +pygame.init() +screen = pygame.display.set_mode((width, height)) +screen.fill(WHITE) + + +def point_in_polygon(pt, polygon): + """Returns True iff `pt` is inside `polygon`. + `polygon` is a list of tuples `(x, y)`.""" + + return matplotlib.path.Path(polygon).contains_point(pt) + +# Draw the polygons for the state. +for polygon in us_map.states[STATE]: + # `polygon` points are tuples `(float, float)`. PyGame requires `(int, int)`. + points = [(int(x), int(y)) for x, y in polygon] + # Draw the interior + pygame.draw.polygon(screen, BLUE, points) + # Draw the boundary + pygame.draw.polygon(screen, BLACK, points, 1) + +pygame.display.flip() + +last_mouse_in_state = False + +while True: + if any(event.type == pygame.QUIT for event in pygame.event.get()): + sys.exit() + + # Is the mouse inside the state? + mouse_in_state = any(point_in_polygon(pygame.mouse.get_pos(), polygon) for polygon in us_map.states[STATE]) + # Only print a message if the mouse moved from the inside to the outside, or vice versa + if mouse_in_state != last_mouse_in_state: + last_mouse_in_state = mouse_in_state + if mouse_in_state: + print 'mouse in state' + else: + print 'mouse not in state' diff --git a/us_map.py b/us_map.py new file mode 100644 index 0000000..ee16b00 --- /dev/null +++ b/us_map.py @@ -0,0 +1,76 @@ +"""Exports a dict `states` that maps state codes to lists of polygons. + +Author: Oliver Steele +License: MIT + +Requirements: + + sudo pip install BeautifulSoup + sudo pip install svg.path +""" + +from BeautifulSoup import BeautifulSoup +from collections import OrderedDict +from svg.path import parse_path + +states = None +"""A `dict` of state abbreviations (e.g. `"MA"`) to lists of polygons. Each polygon is a list of points. +Each point is a tuple of floats `(x, y)`.""" + +props = ['start', 'control', 'control1', 'control2', 'end'] + + +def get_segment_control_points(segment): + """Given an `svg.path` segment, return its list of control points. + Each control point is a complex number.""" + + # segment_type = segment.__class__.__name__ + # assert segment_type in ['Line', 'CubicBezier'], segment_type + return [getattr(segment, prop) for prop in props if hasattr(segment, prop)] + + +def path_to_points(path): + """Given an `svg.path` Path, return its list of control points. + Each control point is a pair of float `(x, y)`.""" + + return [(pt.real, pt.imag) + for segment in path + for pt in get_segment_control_points(segment)] + + +def initialize(svg_filename='Blank_US_Map.svg'): + """Initialize the `states` global variable.""" + + with open(svg_filename, 'r') as svg: + soup = BeautifulSoup(svg.read(), selfClosingTags=['defs']) + paths = soup.findAll('path') + + global states + states = {} + for p in paths: + state_name = p.get('id', None) + path_string = p.get('d', None) + if not state_name or not path_string: + continue + # `svg.path` treats the Move command as though it were Line. + # Split the path data, in order to collect one Path per contour. + path_strings = [s for s in path_string.split('m') if s] + polygons = [] + path_prefix = 'm' + for path_string in path_strings: + if path_string[0] not in 'M': + path_string = path_prefix + path_string + path = parse_path(path_string) + polygons.append(path_to_points(path)) + end_pt = path[-1].end + end_pt = path[0].start + path_prefix = 'M %f,%f m' % (end_pt.real, end_pt.imag) + states[state_name] = polygons + + states = OrderedDict(sorted(states.items())) + +initialize() + +if __name__ == '__main__': + print states['CA'] + From 0ca3301276e5aaac5551cd54ebade9bfb7c213e2 Mon Sep 17 00:00:00 2001 From: margaretmcrawf Date: Sat, 5 Mar 2016 21:31:09 -0500 Subject: [PATCH 02/30] testing some shit --- bokehtest.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 bokehtest.py diff --git a/bokehtest.py b/bokehtest.py new file mode 100644 index 0000000..aae7652 --- /dev/null +++ b/bokehtest.py @@ -0,0 +1,23 @@ +from bokeh.models import BoxSelectTool, BoxZoomTool, LassoSelectTool +from bokeh.plotting import figure, output_file, show + +# output to static HTML file +output_file("bokehlinetest.html") + +p = figure(plot_width=400, plot_height=400) + +# add a circle renderer with a size, color, and alpha +selected_room = p.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.8) +unselected_room = p.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.2) + +p.segment(x0=[1, 1, 1, 6], y0=[1, 6, 1, 1], x1=[6, 6, 1, 6], + y1=[1, 6, 6, 6], color="#F4A582", line_width=3, legend="West Hall") + +p.select_one(BoxZoomTool).overlay.line_color = "olive" +p.select_one(BoxZoomTool).overlay.line_width = 8 +p.select_one(BoxZoomTool).overlay.line_dash = "solid" +p.select_one(BoxZoomTool).overlay.fill_color = None + + +# show the results +show(p) \ No newline at end of file From 292e4e4c09deafdaa36eb069c60f3af97cfd17da Mon Sep 17 00:00:00 2001 From: Lauren Gulland Date: Sat, 5 Mar 2016 22:18:32 -0500 Subject: [PATCH 03/30] Messed around with tabs, crosshair, and hovering functionality --- bokehtestLauren.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bokehtestLauren.py diff --git a/bokehtestLauren.py b/bokehtestLauren.py new file mode 100644 index 0000000..3518fbc --- /dev/null +++ b/bokehtestLauren.py @@ -0,0 +1,45 @@ +from bokeh.models import BoxSelectTool, BoxZoomTool, LassoSelectTool, HoverTool +from bokeh.plotting import figure, output_file, show, gridplot +from bokeh.models.widgets import Dropdown, Panel, Tabs, CheckboxButtonGroup +from bokeh.io import output_file, show, vform + +# output to static HTML file +output_file("bokehlinetest.html") +TOOLS = 'box_zoom,box_select,crosshair,resize,reset,hover' + +p1 = figure(plot_width=400, plot_height=400, tools=TOOLS,title="West Hall First Floor") +selected_room = p1.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.8) +unselected_room = p1.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.2) +p1.segment(x0=[1, 1, 1, 6], y0=[1, 6, 1, 1], x1=[6, 6, 1, 6], + y1=[1, 6, 6, 6], color="#F4A582", line_width=3, legend="West Hall") +p1.select_one(BoxZoomTool).overlay.line_color = "olive" +p1.select_one(BoxZoomTool).overlay.line_width = 8 +p1.select_one(BoxZoomTool).overlay.line_dash = "solid" +p1.select_one(BoxZoomTool).overlay.fill_color = None +tab1 = Panel(child=p1, title="WH1") + +p2 = figure(plot_width=400, plot_height=400, tools=TOOLS,title="West Hall Second Floor") +selected_room = p2.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.8) +unselected_room = p2.quad(top=[2],bottom=[1], left=[1], right=[2], color="navy", alpha=0.2) +p2.segment(x0=[1, 1, 1, 6], y0=[1, 6, 1, 1], x1=[6, 6, 1, 6], + y1=[1, 6, 6, 6], color="#F4A582", line_width=3, legend="West Hall") +p2.select_one(BoxZoomTool).overlay.line_color = "olive" +p2.select_one(BoxZoomTool).overlay.line_width = 8 +p2.select_one(BoxZoomTool).overlay.line_dash = "solid" +p2.select_one(BoxZoomTool).overlay.fill_color = None +tab2 = Panel(child=p2, title="WH2") + + +checkbox_button_group = CheckboxButtonGroup( + labels=["Gender", "Same Roommates", "Other Selectable Shit"], active=[0, 1]) +dropdown_choices = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")] +dropdown = Dropdown(label="Dropdown button", type="warning", menu=dropdown_choices) + + + +tabs = Tabs(tabs=[ tab1, tab2 ]) + +# show the results +show(tabs) +#show(vform(dropdown)) +#show(vform(checkbox_button_group)) \ No newline at end of file From c9ffa2b55990236426733c5c096ceea5a1c71d06 Mon Sep 17 00:00:00 2001 From: margaretmcrawf Date: Sun, 6 Mar 2016 13:22:34 -0500 Subject: [PATCH 04/30] all of the tabby things in one place --- bokehlinetest.html | 30 ++++++++++++++++++++++ bokehtest3.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 bokehlinetest.html create mode 100644 bokehtest3.py diff --git a/bokehlinetest.html b/bokehlinetest.html new file mode 100644 index 0000000..b5f867f --- /dev/null +++ b/bokehlinetest.html @@ -0,0 +1,30 @@ + + + + + + Bokeh Plot + + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/bokehtest3.py b/bokehtest3.py new file mode 100644 index 0000000..d4c9b16 --- /dev/null +++ b/bokehtest3.py @@ -0,0 +1,64 @@ +from bokeh.models import BoxSelectTool, BoxZoomTool, LassoSelectTool, HoverTool, Quad +from bokeh.plotting import figure, output_file, show, gridplot +from bokeh.models.widgets import Dropdown, Panel, Tabs, CheckboxButtonGroup, RadioGroup +from bokeh.io import output_file, show, vform + +# output to static HTML file +output_file("bokehlinetest.html") +TOOLS = "box_zoom,box_select,crosshair,resize,reset,hover,tap" + +p1 = figure(plot_width=400, plot_height=400, tools=TOOLS,title="West Hall First Floor") +rooms1 = p1.quad(top=[2],bottom=[1], left=[1], right=[2], + + nonselection_fill_alpha=0.01, + nonselection_fill_color=None, + nonselection_line_color="firebrick", + nonselection_line_alpha=1.0, + + selection_color="blue", + selection_fill_alpha=.5) + +p1.segment(x0=[1, 1, 1, 6], y0=[1, 6, 1, 1], x1=[6, 6, 1, 6], + y1=[1, 6, 6, 6], color="#F4A582", line_width=3, legend="West Hall") +p1.select_one(BoxZoomTool).overlay.line_color = "olive" +p1.select_one(BoxZoomTool).overlay.line_width = 8 +p1.select_one(BoxZoomTool).overlay.line_dash = "solid" +p1.select_one(BoxZoomTool).overlay.fill_color = None +tab1 = Panel(child=p1, title="WH1") + +p2 = figure(plot_width=400, plot_height=400, tools=TOOLS,title="West Hall Second Floor") + +rooms2 = p2.quad(top=[2],bottom=[1], left=[1], right=[2], + + nonselection_fill_alpha=0.01, + nonselection_fill_color=None, + nonselection_line_color="firebrick", + nonselection_line_alpha=1.0, + + selection_color="blue", + selection_fill_alpha=.5) + +p2.segment(x0=[1, 1, 1, 6], y0=[1, 6, 1, 1], x1=[6, 6, 1, 6], + y1=[1, 6, 6, 6], color="#F4A582", line_width=3, legend="West Hall") + +p2.select_one(BoxZoomTool).overlay.line_color = "olive" +p2.select_one(BoxZoomTool).overlay.line_width = 8 +p2.select_one(BoxZoomTool).overlay.line_dash = "solid" +p2.select_one(BoxZoomTool).overlay.fill_color = None +tab2 = Panel(child=p2, title="WH2") + + +radio_group = RadioGroup( + labels=["Option 1", "Option 2", "Option 3"], active=0) +dropdown_choices = [("Item 1", "item_1"), ("Item 2", "item_2"), None, ("Item 3", "item_3")] +dropdown = Dropdown(label="Dropdown button", type="warning", menu=dropdown_choices) + + + +tabs = Tabs(tabs=[ tab1, tab2 ]) +layout = vform(dropdown, radio_group, tabs) + +# show the results +show(tabs) +show(layout) +#show(vform(checkbox_button_group)) \ No newline at end of file From 377c59a77dbcff1862e3b05d1ffba89b44008092 Mon Sep 17 00:00:00 2001 From: margaretmcrawf Date: Sun, 6 Mar 2016 14:27:24 -0500 Subject: [PATCH 05/30] update --- bokehlinetest.html | 6 +++--- bokehtest3.py | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bokehlinetest.html b/bokehlinetest.html index b5f867f..8cd2f29 100644 --- a/bokehlinetest.html +++ b/bokehlinetest.html @@ -16,12 +16,12 @@ -
+