Geospatial now group too close markers

pull/18/head
Sami Mokaddem 2017-10-26 17:57:31 +02:00
parent a6dd63548d
commit c137932770
2 changed files with 55 additions and 10 deletions

View File

@ -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)

View File

@ -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+"&centerLat="+coord.lat+"&centerLon="+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('<strong>'+jsonData.categ+'</strong><br>'+jsonData.value, {autoClose:false}).openPopup();
// var marker = L.marker([coordJson[1], coordJson[0]]).addTo(radiusOpenStreetMap);
// savedMarkerRadius.push(marker);
// marker.bindPopup('<strong>'+jsonData.categ+': </strong> '+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 += '<strong>'+jsonData.categ+': </strong> '+jsonData.value + "<br>"
}
var marker = L.marker([coordJson[1], coordJson[0]]).addTo(radiusOpenStreetMap);
savedMarkerRadius.push(marker);
marker.bindPopup(dataText, {autoClose:false}).openPopup();
}
});
}