Skip to content
Snippets Groups Projects
Commit 2e6813fc authored by tolgayurt's avatar tolgayurt
Browse files

added polygon edit tool and java script for the test html

parent d8ae27ed
No related branches found
No related tags found
No related merge requests found
......@@ -856,9 +856,38 @@ class End_Container(object):
else:
return fig
list2=[[(233.50398313507685, 1720.9265753781722), (787.0422555505176, 1491.3173835514308), (0.0, 1680.8654987569212), (233.50398313507685, 1720.9265753781722)], [(1124.8805630148158, 1708.6895362501566), (532.1290575497333, 1491.3173835514308), (233.50398313507685, 1519.6028026015497), (1124.8805630148158, 1708.6895362501566)], [(1498.1911834971443, 1765.660563957103), (1832.2885709298307, 1593.0223998373076), (1124.8805630148158, 1491.3173835514308), (1498.1911834971443, 1765.660563957103)]]
list3=[]
for poly in list2:
list3.append((Polygon2(poly)))
high_classes = height_classes(list3)
test = building_containers(high_classes)
\ No newline at end of file
def plot_polygons_in_single_plot(polygon_list, title="", render=True, border=None):
polygon_number = len(polygon_list)
# x_data = self.x_values
# y_data = self.y_values
TOOLTIPS = [("index", "$index"), ("(x,y)", "($x, $y)"), ]
# if title=="":
# title= 'height:{} slope:{}'.format(height,slope)
# else:
# title= '{} height:{} slope:{}'.format(title,height,slope)
fig = figure(title=title, x_axis_label='x', y_axis_label='y', tooltips=TOOLTIPS, toolbar_location="below")
colors = itertools.cycle(palette)
legend_items = []
legend_polygons = []
for counter, polygon in enumerate(polygon_list):
color = next(colors)
x = polygon.x_values
y = polygon.y_values
legend_label = "Polygon {}".format(counter)
poly_fig = fig.line(x, y, color=color, line_width=2, muted_alpha=0.2)
circle_fig = fig.circle(x, y, color=color, line_width=2, muted_alpha=0.2, size=8)
legend_polygons.append((legend_label, [poly_fig, circle_fig]))
# spine_x_values = [x[0] for x in self.spine]
# spine_y_values = [x[1] for x in self.spine]
if border != None:
fig_border = fig.line(border[0], border[1], line_color="red", line_width=2, muted_alpha=0.2)
legend_items.append(("border", [fig_border]))
legend_items = legend_items + legend_polygons
legend = Legend(items=legend_items)
legend.click_policy = "mute"
fig.add_layout(legend, 'right')
fig.legend.click_policy = "mute"
if render:
return show(fig)
else:
return fig
\ No newline at end of file
......@@ -11,6 +11,7 @@
<h1>Polygons</h1>
{{polygons_plot}}
{{polygons_plot.}}
<hr>
<h1> Highclasses</h1>
{{hc_plot}}
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style>
html {
width: 100%;
height: 100%;
}
body {
width: 90%;
height: 100%;
margin: auto;
}
</style>
<script>
function change_ds_value(name, idx, value) {
var ds = Bokeh.documents[0].get_model_by_name('my-data-source');
ds.data[name][idx] = value;
ds.change.emit();
}
</script>
</head>
<body>
<form action="/hello/" target="_blank" method="post">
{% csrf_token %}
<label for="your_name">Your name: </label>
<input id="your_name" type="hidden" name="your_name" value="test">
<input type="submit" onclick="myFunction()" value="OK">
</form>
<p id="p1">Hello World!</p>
<div>
{{ response|safe }}
</div>
</body>
<script type="text/javascript">
var element = document.getElementById("your_name");
element.value = "{{response}}";
</script>
<script>
function myFunction() {
var ds = Bokeh.documents[0].get_model_by_name('my-data-source');
var dict1 = {x:ds.data["x"], y:ds.data["y"]};
document.getElementById("your_name").value =JSON.stringify(dict1);
}
</script>
</html>
\ No newline at end of file
......@@ -5,4 +5,6 @@ from . import views
urlpatterns = [
path('plot/', views.index, name='index'),
path('json/', views.get_json_view, name='index2'),
path('test/', views.test, name='index3'),
path('hello/', views.hello, name='index4'),
]
\ No newline at end of file
from django.shortcuts import render
# Create your views here.
import json
from django.shortcuts import render
from plotly.offline import plot
import plots.polygon as poly
from bokeh.embed import file_html
from django.views.generic import TemplateView
import matplotlib
from django.http import JsonResponse
from plotly.graph_objs import Scatter
......@@ -23,14 +26,22 @@ from bokeh.layouts import layout
from bokeh.models.widgets import Tabs, Panel
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.embed import components
from bokeh.embed import json_item
from bokeh.events import ButtonClick
import numpy as np
from bokeh.models import PolyDrawTool, PolyEditTool
from bokeh.models.callbacks import CustomJS
from bokeh.embed import json_item
import json
def index(request):
convex_polygons = poly.create_multiple_convex_polygons(20,9)
convex_polygons = poly.create_multiple_convex_polygons(50,9)
polygons_plot= poly.plot_polygons(convex_polygons,plot_width=300,plot_height=300,render=False)
html_polygons = file_html(polygons_plot, CDN, "my plot")
......@@ -137,4 +148,86 @@ def get_json_view(request):
# tabs = Tabs(tabs=[tab1, tab2])
# response =file_html(tabs, CDN, "my plot")
#show(tabs)
return render(request, 'plots/get.html', context={'containers': response,'containers2':mini_plots})
\ No newline at end of file
return render(request, 'plots/get.html', context={'containers': response,'containers2':mini_plots})
def test(request):
# source = ColumnDataSource(data=dict(x=[1, 2, 3],
# y=[3, 2, 1]),
# name='my-data-source')
#
# p = figure()
# l1 = p.line("x", "y", source=source)
# response = file_html(p, CDN, "my plot")
# list =[1,2,3,4]
source = ColumnDataSource(data=dict(x=[],
y=[]),
name='my-data-source')
p = figure(x_range=(0, 10), y_range=(0, 10), width=400, height=400,
title='Poly Edit Tool')
p1 = p.patches("x", "y", fill_alpha=0.4,source=source, fill_color="red")
c1 = p.circle([], [], size=10, color='red')
draw_tool = PolyDrawTool(renderers=[p1])
edit_tool = PolyEditTool(renderers=[p1], vertex_renderer=c1)
p.add_tools(draw_tool, edit_tool)
p.toolbar.active_drag = edit_tool
response = file_html(p, CDN, "my plot")
return render(request, 'plots/test.html', context={"response":response, "list":list})
def hello(request):
if request.method == 'POST':
test = request.POST["your_name"]
dict_poly = json.loads(test)
print(dict_poly["x"])
print(dict_poly["y"])
polygon_list=[]
polygons_x_y_tuple = list(zip(dict_poly["x"],dict_poly["y"]))
# polygon_x_list = []
# polygon_y_list = []
for x,y in polygons_x_y_tuple:
polygon_list.append(list(zip(x,y)))
# for x,y in polygons_x_y_tuple:
# polygon_x_list.append(x)
# polygon_y_list.append(y)
print(polygon_list)
real_polygons=[]
for polygon in polygon_list:
if len(polygon)<3:
continue
if polygon[0]!= polygon[-1]:
polygon.append(polygon[0])
polygon2 = poly.Polygon(polygon)
print("konkav",list(polygon2.exterior.coords))
polygon_parent_class = polygon2.convex_hull
polygon2 = poly.Polygon2(list(polygon_parent_class.exterior.coords))
print("convex",polygon2.shell)
real_polygons.append(polygon2)
# print(polygon_list)
# q = map(list, zip(*polygon_list))
# s= json.loads(test)
# x = s["x"]
# y = s["y"]
# source = ColumnDataSource(data=dict(x=polygon_x_list, y=polygon_y_list),name='my-data-source')
# print(test)
plot = poly.plot_polygons_in_single_plot(real_polygons,render=False)
# plot = poly.plot_polygons(real_polygons,render=False)
# print(type(test))
# p = figure()
# p.multi_line("x", "y", source=source)
response = file_html(plot, CDN, "my plot")
# polygon = Polygon(polygon_points)
# polygon_parent_class = polygon.convex_hull
# polygon2 = Polygon2(list(polygon_parent_class.exterior.coords))
return HttpResponse(response)
\ No newline at end of file
......@@ -5,4 +5,5 @@ numpy==1.19.2
mplcursors==0.3
mpld3
bokeh
django-jquery
\ No newline at end of file
django-jquery
django.js==0.8.1
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment