From c137932770cfba7f6ba0cc94bbcd290ec6fdccd4 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Thu, 26 Oct 2017 17:57:31 +0200 Subject: [PATCH] Geospatial now group too close markers --- server.py | 36 ++++++++++++++++++++++++++++++++++-- templates/geo.html | 29 +++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index a89e39a..3ff5b0b 100755 --- a/server.py +++ b/server.py @@ -149,8 +149,15 @@ def getHitMap(): data = getZrange(keyCateg, dayNum, topNum) return jsonify(data) +def isCloseTo(coord1, coord2): + if abs(float(coord1[0]) - float(coord2[0])) <= 0.0001: + if abs(float(coord1[1]) - float(coord2[1])) <= 0.0001: + return True + return False + @app.route("/_getCoordsByRadius") def getCoordsByRadius(): + dico_coord = {} to_return = [] try: dateStart = datetime.datetime.fromtimestamp(float(request.args.get('dateStart'))) @@ -168,8 +175,33 @@ def getCoordsByRadius(): keyCateg = 'GEO_RAD' keyname = "{}:{}".format(keyCateg, date_str) res = serv_redis_db.georadius(keyname, centerLon, centerLat, radius, unit='km', withcoord=True) - res = [ [json.loads(data), coord] for data, coord in res ] #correctly send the json - to_return.append(res) + + #sum up really close coord + for data, coord in res: + flag_added = False + coord = [coord[0], coord[1]] + #list all coord + for dicoCoordStr in dico_coord.keys(): + dicoCoord = json.loads(dicoCoordStr) + #if curCoord close to coord + if isCloseTo(dicoCoord, coord): + #add data to dico coord + dico_coord[dicoCoordStr].append(data) + flag_added = True + break + # coord not in dic + if not flag_added: + dico_coord[str(coord)] = [data] + + for dicoCoord, array in dico_coord.items(): + print(array) + dicoCoord = json.loads(dicoCoord) + to_return.append([array, dicoCoord]) + + #res = [ [json.loads(data), coord] for data, coord in res ] #correctly send the json + #to_return.append(res) + import pprint + pprint.pprint(to_return) return jsonify(to_return) diff --git a/templates/geo.html b/templates/geo.html index c4f05e6..5bbf1c5 100644 --- a/templates/geo.html +++ b/templates/geo.html @@ -329,21 +329,34 @@ small { var dateStart = datePickersRadiusWidgetFrom.datepicker("getDate").getTime() / 1000; var dateEnd = datePickersRadiusWidgetTo.datepicker("getDate").getTime() / 1000; $.getJSON("{{ url_for('getCoordsByRadius') }}?dateStart="+dateStart+"&dateEnd="+dateEnd+"¢erLat="+coord.lat+"¢erLon="+coord.lng+"&radius="+radius_km, function(allList){ + console.log(allList); // remove old markers for (var i in savedMarkerRadius) { savedMarkerRadius[i].remove(); // remove marker } - for (var listIndex in allList) { - var list = allList[listIndex]; - for (var dayListIndex in list) { - var jsonData = list[dayListIndex][0]; - var coordJson = list[dayListIndex][1]; + //for (var listIndex in allList) { + // var list = allList[listIndex]; + // for (var dayListIndex in list) { + // var jsonData = list[dayListIndex][0]; + // var coordJson = list[dayListIndex][1]; - var marker = L.marker([coordJson[1], coordJson[0]]).addTo(radiusOpenStreetMap); - savedMarkerRadius.push(marker); - marker.bindPopup(''+jsonData.categ+'
'+jsonData.value, {autoClose:false}).openPopup(); + // var marker = L.marker([coordJson[1], coordJson[0]]).addTo(radiusOpenStreetMap); + // savedMarkerRadius.push(marker); + // marker.bindPopup(''+jsonData.categ+': '+jsonData.value, {autoClose:false}).openPopup(); + // } + //} + for (var listIndex in allList) { + var curMarker = allList[listIndex]; + var dataText = ""; + var coordJson = curMarker[1]; + for (var dataI in curMarker[0]) { + var jsonData = JSON.parse(curMarker[0][dataI]) + dataText += ''+jsonData.categ+': '+jsonData.value + "
" } + var marker = L.marker([coordJson[1], coordJson[0]]).addTo(radiusOpenStreetMap); + savedMarkerRadius.push(marker); + marker.bindPopup(dataText, {autoClose:false}).openPopup(); } }); }