From cea2a2e4042bcb01b69c76aca6e87b31d89ea220 Mon Sep 17 00:00:00 2001 From: comsultia Date: Sun, 28 Aug 2016 21:26:34 +0200 Subject: [PATCH 01/12] get map data in squares --- pogom/models.py | 5 ++++ static/map.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/pogom/models.py b/pogom/models.py index 86d00a04..a41acbfd 100644 --- a/pogom/models.py +++ b/pogom/models.py @@ -4,6 +4,7 @@ import logging import random import math +from flask import request from peewee import Model, SqliteDatabase, InsertQuery, IntegerField, \ CharField, FloatField, BooleanField, DateTimeField, fn, SQL from datetime import datetime @@ -53,6 +54,10 @@ def get_active(cls): query = (Pokemon .select() .where(Pokemon.disappear_time > datetime.utcnow()) + .where(Pokemon.latitude < request.values.get('boundary-north', type=float)) + .where(Pokemon.latitude > request.values.get('boundary-south', type=float)) + .where(Pokemon.longitude > request.values.get('boundary-west', type=float)) + .where(Pokemon.longitude < request.values.get('boundary-east', type=float)) .dicts()) pokemons = [] diff --git a/static/map.js b/static/map.js index 47bde4f9..208eeb27 100644 --- a/static/map.js +++ b/static/map.js @@ -10,6 +10,7 @@ var $selectExclude = $("#exclude-pokemon"); var excludedPokemon = []; var map; +var rectangle; var scanLocations = new Map(); var coverCircles = []; var newLocationMarker; @@ -159,16 +160,21 @@ function initMap() { map = new google.maps.Map(document.getElementById('map'), { // Change this to geolocation? center: {lat: initLat, lng: initLng}, - zoom: 13, + zoom: 16, mapTypeControl: false, streetViewControl: false, disableAutoPan: true }); + rectangle = new google.maps.Rectangle(); updateScanLocations(initialScanLocations); updateMap(); updateHeatMap(); + google.maps.event.addListener(map, 'idle', function(event) { + updateMap(); + }); + if(is_logged_in()) { // on click listener for google.maps.event.addListener(map, 'click', function(event) { @@ -459,11 +465,71 @@ function updateScanLocations(updatedScanLocations) { // 'pokestops': document.getElementById('pokestops-checkbox').checked, // 'pokestops-lured': document.getElementById('pokestops-lured-checkbox').checked, function updateMap() { + if (!map){return} + if (!map.getBounds()){return} + var boundary_north = map.getBounds().getNorthEast().lat(); // y + var boundary_west = map.getBounds().getSouthWest().lng(); // x + var boundary_south = map.getBounds().getSouthWest().lat(); // y + var boundary_east = map.getBounds().getNorthEast().lng(); // x + var zoom = map.getZoom(); + + var boundary_round = 0.005; // 0.002 + + if (zoom >= 18){boundary_round = 0.05;} + else if (zoom >= 16){boundary_round = 0.03;} + else if (zoom >= 10){boundary_round = 0.2;} + else {boundary_round = 0.5} + + boundary_north = Math.floor((boundary_north+boundary_round) / boundary_round) * boundary_round; + boundary_south = Math.floor((boundary_south-boundary_round) / boundary_round) * boundary_round; + boundary_west = Math.floor((boundary_west-boundary_round) / boundary_round) * boundary_round; + boundary_east = Math.floor((boundary_east+boundary_round) / boundary_round) * boundary_round; + + rectangle.setOptions({ + strokeColor: '#FF0000', + strokeOpacity: 0.8, + strokeWeight: 2, +// fillColor: '#FF0000', + fillOpacity: 0, + map: map, + bounds: { + north: boundary_north, + south: boundary_south, + east: boundary_east, + west: boundary_west + } + }); + + console.log('map-data-round zoom=' + zoom + ' lat=' + boundary_north + '-' + boundary_south + ', lng=' + boundary_west + '-' + boundary_east); + + var squares=0; + for (var i_y = boundary_south; i_y < boundary_north; i_y += boundary_round) + { +// i_y=i_y.toFixed(3); + for (var i_x = boundary_west; i_x < boundary_east; i_x += boundary_round) + { +// i_x=i_x.toFixed(3); + squares = squares + 1; + var i_x_=i_x.toFixed(3); + var i_x_to_ = (i_x_ + boundary_round); +// i_x_to_ = i_x_to_.toFixed(3); + var i_y_=i_y.toFixed(3); + var i_y_to_ = (i_y_ + boundary_round); +// i_y_to_ = i_y_to_.toFixed(3); + console.log(' square[' + squares + '] x=' + i_x_ + ' y=' + i_y_); + // square start + $.ajax({ url: "map-data", type: 'GET', - data: {'pokemon': localStorage.displayPokemons, - 'gyms': localStorage.displayGyms}, + data: { + 'boundary-north': i_y_to_, + 'boundary-west': i_x_, + 'boundary-south': i_y_, + 'boundary-east': i_x_to_, + 'pokemon': localStorage.displayPokemons, + 'gyms': localStorage.displayGyms + }, dataType: "json" }).done(function(result) { statusLabels(result["server_status"]); @@ -521,6 +587,11 @@ function updateMap() { $lastRequestLabel.addClass('label-danger'); $lastRequestLabel.html("Disconnected from Server") }); + + // square end + } + } + } window.setInterval(updateMap, 10000); From ff3de65a0d9adb5eac2aa80baeac84a9e27af034 Mon Sep 17 00:00:00 2001 From: comsultia Date: Mon, 29 Aug 2016 12:44:54 +0200 Subject: [PATCH 02/12] hotfix --- static/map.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/static/map.js b/static/map.js index 208eeb27..6931c430 100644 --- a/static/map.js +++ b/static/map.js @@ -511,12 +511,12 @@ function updateMap() { // i_x=i_x.toFixed(3); squares = squares + 1; var i_x_=i_x.toFixed(3); - var i_x_to_ = (i_x_ + boundary_round); -// i_x_to_ = i_x_to_.toFixed(3); + var i_x_to_ = (i_x + boundary_round); + i_x_to_ = i_x_to_.toFixed(3); var i_y_=i_y.toFixed(3); - var i_y_to_ = (i_y_ + boundary_round); -// i_y_to_ = i_y_to_.toFixed(3); - console.log(' square[' + squares + '] x=' + i_x_ + ' y=' + i_y_); + var i_y_to_ = (i_y + boundary_round); + i_y_to_ = i_y_to_.toFixed(3); + console.log(' square[' + squares + '] x=' + i_x_ + '-' + i_x_to_ + ' y=' + i_y_ + '-' + i_y_to_); // square start $.ajax({ From 5447eedbe83792f84bf7d0dd99e2f6c1fa0833e9 Mon Sep 17 00:00:00 2001 From: comsultia Date: Mon, 29 Aug 2016 12:45:52 +0200 Subject: [PATCH 03/12] longer interval --- static/map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/map.js b/static/map.js index 6931c430..2c5cbd1f 100644 --- a/static/map.js +++ b/static/map.js @@ -747,4 +747,4 @@ var updateLabelDiffTime = function() { }); }; -window.setInterval(updateLabelDiffTime, 1000); +window.setInterval(updateLabelDiffTime, 3000); From 8cc86e115de96cc69dd3cf18c811cfa72e3cfc54 Mon Sep 17 00:00:00 2001 From: comsultia Date: Mon, 29 Aug 2016 13:20:51 +0200 Subject: [PATCH 04/12] 30s interval --- static/map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/map.js b/static/map.js index 2c5cbd1f..1ee948ea 100644 --- a/static/map.js +++ b/static/map.js @@ -747,4 +747,4 @@ var updateLabelDiffTime = function() { }); }; -window.setInterval(updateLabelDiffTime, 3000); +window.setInterval(updateLabelDiffTime, 30000); From f37ab628fb1319693409a68d79cdd116f812969c Mon Sep 17 00:00:00 2001 From: comsultia Date: Mon, 29 Aug 2016 13:27:12 +0200 Subject: [PATCH 05/12] only gyms from square --- pogom/app.py | 2 +- pogom/models.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pogom/app.py b/pogom/app.py index 9d470d7c..8543dfc2 100644 --- a/pogom/app.py +++ b/pogom/app.py @@ -176,7 +176,7 @@ def map_data(self): # TODO: Lured pokestops if request.args.get('gyms', 'true') == 'true': - d['gyms'] = Gym.get_all() + d['gyms'] = Gym.get_active() return jsonify(d) diff --git a/pogom/models.py b/pogom/models.py index a41acbfd..52b2bd35 100644 --- a/pogom/models.py +++ b/pogom/models.py @@ -127,7 +127,22 @@ class Gym(BaseModel): latitude = FloatField() longitude = FloatField() last_modified = DateTimeField() + + @classmethod + def get_active(cls): + query = (Gym + .select() + .where(Gym.latitude < request.values.get('boundary-north', type=float)) + .where(Gym.latitude > request.values.get('boundary-south', type=float)) + .where(Gym.longitude > request.values.get('boundary-west', type=float)) + .where(Gym.longitude < request.values.get('boundary-east', type=float)) + .dicts()) + + gyms = [] + for p in query: + gyms.append(p) + return gyms def parse_map(map_dict): pokemons = {} From a1f918c7963995fd7cbe8712dfe6786dd02cb550 Mon Sep 17 00:00:00 2001 From: comsultia Date: Mon, 29 Aug 2016 13:32:11 +0200 Subject: [PATCH 06/12] changed interval --- static/map.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/map.js b/static/map.js index 1ee948ea..b5e60c8d 100644 --- a/static/map.js +++ b/static/map.js @@ -594,7 +594,7 @@ function updateMap() { } -window.setInterval(updateMap, 10000); +window.setInterval(updateMap, 30000); $('#gyms-checkbox').change(function() { localStorage.displayGyms = this.checked; @@ -747,4 +747,4 @@ var updateLabelDiffTime = function() { }); }; -window.setInterval(updateLabelDiffTime, 30000); +window.setInterval(updateLabelDiffTime, 5000); From 671b2917347968c7a1075da3e1ae41a5d4705dc3 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 17:58:18 +0200 Subject: [PATCH 07/12] more precise calculations of squares --- static/map.js | 79 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 12 deletions(-) diff --git a/static/map.js b/static/map.js index b5e60c8d..7f34d3c9 100644 --- a/static/map.js +++ b/static/map.js @@ -11,6 +11,9 @@ var excludedPokemon = []; var map; var rectangle; +var rectangles=[]; +var rectangles_debug=0; +var rectangle_markers=[]; var scanLocations = new Map(); var coverCircles = []; var newLocationMarker; @@ -475,16 +478,18 @@ function updateMap() { var boundary_round = 0.005; // 0.002 - if (zoom >= 18){boundary_round = 0.05;} - else if (zoom >= 16){boundary_round = 0.03;} - else if (zoom >= 10){boundary_round = 0.2;} + if (zoom >= 15){boundary_round = 0.02;} + else if (zoom >= 10){boundary_round = 0.1;} else {boundary_round = 0.5} - boundary_north = Math.floor((boundary_north+boundary_round) / boundary_round) * boundary_round; - boundary_south = Math.floor((boundary_south-boundary_round) / boundary_round) * boundary_round; - boundary_west = Math.floor((boundary_west-boundary_round) / boundary_round) * boundary_round; - boundary_east = Math.floor((boundary_east+boundary_round) / boundary_round) * boundary_round; +// console.log('map-data zoom=' + zoom + ' lat=' + boundary_north.toFixed(3) + '-' + boundary_south.toFixed(3) + ', lng=' + boundary_west.toFixed(3) + '-' + boundary_east.toFixed(3)); + boundary_north = parseFloat((Math.ceil(boundary_north / boundary_round) * boundary_round).toFixed(3)); + boundary_south = parseFloat((Math.floor(boundary_south / boundary_round) * boundary_round).toFixed(3)); + boundary_west = parseFloat((Math.floor(boundary_west / boundary_round) * boundary_round).toFixed(3)); + boundary_east = parseFloat((Math.ceil(boundary_east / boundary_round) * boundary_round).toFixed(3)); + +/* rectangle.setOptions({ strokeColor: '#FF0000', strokeOpacity: 0.8, @@ -499,16 +504,36 @@ function updateMap() { west: boundary_west } }); +*/ - console.log('map-data-round zoom=' + zoom + ' lat=' + boundary_north + '-' + boundary_south + ', lng=' + boundary_west + '-' + boundary_east); + for (var i = 1; i<20; i+=1) + { + if (rectangles[i]){rectangles[i].setMap(null);} + if (rectangle_markers[i]){rectangle_markers[i].setMap(null);} + } + + if (zoom <= 11) + { + return + } var squares=0; for (var i_y = boundary_south; i_y < boundary_north; i_y += boundary_round) { -// i_y=i_y.toFixed(3); + i_y = parseFloat(i_y.toFixed(3)); + if (i_y >= boundary_north){ + break + }; +// console.log('i_y=' + i_y); + for (var i_x = boundary_west; i_x < boundary_east; i_x += boundary_round) { -// i_x=i_x.toFixed(3); + i_x = parseFloat(i_x.toFixed(3)); + if (i_x >= boundary_east){ + break + }; +// console.log('i_x=' + i_x); + squares = squares + 1; var i_x_=i_x.toFixed(3); var i_x_to_ = (i_x + boundary_round); @@ -516,7 +541,37 @@ function updateMap() { var i_y_=i_y.toFixed(3); var i_y_to_ = (i_y + boundary_round); i_y_to_ = i_y_to_.toFixed(3); - console.log(' square[' + squares + '] x=' + i_x_ + '-' + i_x_to_ + ' y=' + i_y_ + '-' + i_y_to_); + + if (squares > 12){break}; + + if (rectangles_debug) + { + if (!rectangles[squares]){rectangles[squares] = new google.maps.Rectangle();} + if (!rectangle_markers[squares]){rectangle_markers[squares] = new google.maps.Marker();} + rectangles[squares].setOptions({ + strokeColor: '#FF0000', + strokeOpacity: 0.8, + strokeWeight: 2, + // fillColor: '#FF0000', + fillOpacity: 0, + map: map, + bounds: { + north: parseFloat(i_y_to_), + south: parseFloat(i_y_), + east: parseFloat(i_x_to_), + west: parseFloat(i_x_) + } + }); + rectangle_markers[squares].setOptions({ + title: 'Square #' + squares, + map: map, + position: { + lat: parseFloat(i_y_) + (boundary_round/2), + lng: parseFloat(i_x_) + (boundary_round/2) + } + }); + console.log(' square[' + squares + '] x=' + i_x_ + '-' + i_x_to_ + ' y=' + i_y_ + '-' + i_y_to_); + } // square start $.ajax({ @@ -591,7 +646,7 @@ function updateMap() { // square end } } - + console.log('get map-data zoom=' + zoom + ' sqr size=' + boundary_round + ' cnt=' + squares + ' lat=' + boundary_north.toFixed(3) + '-' + boundary_south.toFixed(3) + ', lng=' + boundary_west.toFixed(3) + '-' + boundary_east.toFixed(3)); } window.setInterval(updateMap, 30000); From a4c8cfaf2306f827c05ff01929c7d4a1eb5bc8d5 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 18:19:29 +0200 Subject: [PATCH 08/12] difftime hotfix --- static/map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/map.js b/static/map.js index 7f34d3c9..4be9c043 100644 --- a/static/map.js +++ b/static/map.js @@ -802,4 +802,4 @@ var updateLabelDiffTime = function() { }); }; -window.setInterval(updateLabelDiffTime, 5000); +window.setInterval(updateLabelDiffTime, 1000); From ee8c3ad83a2a768996b28c504fafff97b88226b6 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 20:22:05 +0200 Subject: [PATCH 09/12] GA of maploading if available --- static/map.js | 1 + 1 file changed, 1 insertion(+) diff --git a/static/map.js b/static/map.js index 4be9c043..c3b72018 100644 --- a/static/map.js +++ b/static/map.js @@ -574,6 +574,7 @@ function updateMap() { } // square start + if (ga){ga('send', 'event', 'map', 'load', i_y_to_ + ':' + i_y_ + ',' + i_x_ + ':' + i_x_to_);} $.ajax({ url: "map-data", type: 'GET', From bac1ecfe6ea67dd13809c8cf6aec196f808908a2 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 21:01:18 +0200 Subject: [PATCH 10/12] avoid parallel requests to same square --- static/map.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/static/map.js b/static/map.js index c3b72018..994b1f42 100644 --- a/static/map.js +++ b/static/map.js @@ -11,9 +11,10 @@ var excludedPokemon = []; var map; var rectangle; -var rectangles=[]; -var rectangles_debug=0; -var rectangle_markers=[]; +var rectangles = []; +var rectangles_debug = 0; +var rectangles_load = {}; +var rectangle_markers = []; var scanLocations = new Map(); var coverCircles = []; var newLocationMarker; @@ -573,9 +574,16 @@ function updateMap() { console.log(' square[' + squares + '] x=' + i_x_ + '-' + i_x_to_ + ' y=' + i_y_ + '-' + i_y_to_); } // square start - - if (ga){ga('send', 'event', 'map', 'load', i_y_to_ + ':' + i_y_ + ',' + i_x_ + ':' + i_x_to_);} - $.ajax({ + + var square_str = i_y_to_ + ':' + i_y_ + ',' + i_x_ + ':' + i_x_to_; + if (rectangles_load[square_str] && rectangles_load[square_str].readyState == 1) + { +// console.log('still loading'); + continue; + } + + if (ga){ga('send', 'event', 'map', 'load', square_str);} + rectangles_load[square_str]=$.ajax({ url: "map-data", type: 'GET', data: { From f76becef3bb9d3552229e72a2eba76ea3e5b2f31 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 22:29:23 +0200 Subject: [PATCH 11/12] squares tuning --- static/map.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/static/map.js b/static/map.js index 994b1f42..01c53bdf 100644 --- a/static/map.js +++ b/static/map.js @@ -479,9 +479,10 @@ function updateMap() { var boundary_round = 0.005; // 0.002 - if (zoom >= 15){boundary_round = 0.02;} - else if (zoom >= 10){boundary_round = 0.1;} - else {boundary_round = 0.5} + if (zoom >= 13){boundary_round = 0.05;} + else if (zoom >= 11){boundary_round = 0.2;} + else if (zoom >= 9){boundary_round = 1;} + else {boundary_round = 2} // console.log('map-data zoom=' + zoom + ' lat=' + boundary_north.toFixed(3) + '-' + boundary_south.toFixed(3) + ', lng=' + boundary_west.toFixed(3) + '-' + boundary_east.toFixed(3)); @@ -513,7 +514,7 @@ function updateMap() { if (rectangle_markers[i]){rectangle_markers[i].setMap(null);} } - if (zoom <= 11) + if (zoom <= 8) { return } @@ -543,7 +544,7 @@ function updateMap() { var i_y_to_ = (i_y + boundary_round); i_y_to_ = i_y_to_.toFixed(3); - if (squares > 12){break}; + if (squares > 20){break}; if (rectangles_debug) { From ea4bea072964c9af0a607a7652666dd8d448bd91 Mon Sep 17 00:00:00 2001 From: comsultia Date: Tue, 30 Aug 2016 23:32:39 +0200 Subject: [PATCH 12/12] don't add dissapeared pokemons from map-data --- static/map.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/static/map.js b/static/map.js index 01c53bdf..fd0e3b4e 100644 --- a/static/map.js +++ b/static/map.js @@ -600,12 +600,16 @@ function updateMap() { statusLabels(result["server_status"]); updateScanLocations(result['scan_locations']); - + $.each(result.pokemons, function(i, item){ if (!document.getElementById('pokemon-checkbox').checked) { return false; // in case the checkbox was unchecked in the meantime. } - + if (item.disappear_time < new Date().getTime()) // outdated json with old data (proxy?) + { + + } + else if (!(item.encounter_id in map_pokemons) && excludedPokemon.indexOf(item.pokemon_id) < 0) { // add marker to map and item to dict