Skip to content

Commit

Permalink
Add axios and update polygons from mouse tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemfp committed Sep 6, 2017
1 parent 65c2b59 commit 266ebdf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
32 changes: 23 additions & 9 deletions geoguide/client/static/src/pageEnvironment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ import clusterMaker from 'clusters'
import randomColor from 'randomcolor'
import quickHull from 'quick-hull-2d'
import * as modifiers from './modifiers'
import axios from 'axios'

let pointChoice = -1
let iugaLastId = -1
Expand Down Expand Up @@ -570,16 571,29 @@ const showClustersFromMouseTrackingAfterIuga = () => {
lng: point[1]
}))

var pol = new google.maps.Polygon({
paths: path,
strokeColor: color,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: color,
fillOpacity: 0.35
axios.post(`/environment/${datasetOptions.filename}/points`, {
polygon: hull.map(point => point.join(':')).join(',')
}).then(({ data }) => {
if (data.count > 2) {
let hull = quickHull(data.points)
let path = hull.map((point) => ({
lat: point[0],
lng: point[1]
}))

var pol = new google.maps.Polygon({
paths: path,
strokeColor: color,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: color,
fillOpacity: 0.35
})
pol.setMap(map)
mousePolygons.push(pol)
}
})
pol.setMap(map)
mousePolygons.push(pol)

});
}

Expand Down
2 changes: 1 addition & 1 deletion geoguide/client/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 56,7 @@
{% include 'footer.html' %}

<!-- scripts -->
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% block js %}{% endblock %}

Expand Down
34 changes: 32 additions & 2 deletions geoguide/server/geoguide/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 6,14 @@
import numpy as np
from uuid import uuid4
from flask import request, session, render_template, Blueprint, flash, redirect, url_for, jsonify, abort
from geoguide.server import app, db, datasets
from geoguide.server import app, db, datasets, logging
from flask_uploads import UploadNotAllowed
from geoguide.server.models import Dataset, Attribute, AttributeType
from geoguide.server.geoguide.helpers import save_as_hdf, path_to_hdf, save_as_sql
from geoguide.server.iuga import run_iuga
from sqlalchemy import create_engine

SQLALCHEMY_DATABASE_URI = app.config['SQLALCHEMY_DATABASE_URI']
DEBUG = app.config['DEBUG']
USE_SQL = app.config['USE_SQL']
geoguide_blueprint = Blueprint('geoguide', __name__,)
Expand Down Expand Up @@ -105,7 107,6 @@ def point_details(selected_dataset, index):
df = pd.read_csv(datasets.path(dataset.filename))
return df.loc[index].to_json(), 200, {'Content-Type': 'application/json'}


@geoguide_blueprint.route('/environment/<selected_dataset>/<int:index>/iuga', methods=['POST'])
def point_suggestions(selected_dataset, index):
k = int(request.args['k'])
Expand Down Expand Up @@ -136,3 137,32 @@ def point_suggestions(selected_dataset, index):
filtered_points=filtered_points,
clusters=clusters)
return jsonify(vm)

@geoguide_blueprint.route('/environment/<selected_dataset>/points', methods=['GET', 'POST'])
def point(selected_dataset):
dataset = Dataset.query.filter_by(filename=selected_dataset).first_or_404()

engine = create_engine(SQLALCHEMY_DATABASE_URI)
table_name = dataset.filename.rsplit('.', 1)[0]
polygon_path = request.get_json(True, True).get('polygon', '')
polygon_path = polygon_path.split(',') if polygon_path is not '' else []
polygon = ''

if len(polygon_path) > 2:
polygon_path.append(polygon_path[0])
polygon = 'POLYGON(({}))'.format(','.join(['{} {}'.format(*p.split(':')[::-1]) for p in polygon_path]))
elif len(polygon_path) != 0:
return jsonify(dict(points=[], count=0)), 400

cursor = engine.execute('''
select {}, {}
from "{}"
{}
'''.format(
dataset.latitude_attr,
dataset.longitude_attr,
table_name,
("where ST_Contains('{}', geom)".format(polygon) if len(polygon) > 0 else '')))

points = [list(x) for x in cursor]
return jsonify(dict(points=points, count=len(points)))
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 26,7 @@
"webpack": "^2.3.2"
},
"dependencies": {
"axios": "^0.16.2",
"clusters": "0.0.4",
"crossfilter": "^1.3.12",
"d3": "^3.5.17",
Expand Down

0 comments on commit 266ebdf

Please sign in to comment.