hood selection is now done by db

This commit is contained in:
Dominik Heidler 2015-09-08 21:23:22 +02:00
parent 4b693bdd49
commit 75cb0960b5
2 changed files with 2 additions and 41 deletions

View File

@ -1,16 +1,13 @@
#!/usr/bin/python
from math import sin, cos, sqrt, atan2, radians
from pymongo import MongoClient
client = MongoClient()
db = client.freifunk
CONFIG = {"default_hood_id": 1}
"""
# create db indexes
db.hoods.create_index([("position", "2dsphere")])
db.hoods.insert_many([
{
"keyxchange_id": 1,
@ -65,35 +62,3 @@ db.hoods.insert_many([
"net": "10.50.60.0/22",
"position": {"type": "Point", "coordinates": [10.568013390003, 50.08]}
}])
"""
def km_distance(lng1, lat1, lng2, lat2):
lng1 = radians(lng1)
lat1 = radians(lat1)
lng2 = radians(lng2)
lat2 = radians(lat2)
dlng = lng2 - lng1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlng / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
# approximate radius of earth in km
R = 6373.0
distance = R * c
return distance
def hood_by_pos(lng, lat):
current_hood_dist = 99999999
current_hood = db.hoods.find({"keyxchange_id": CONFIG["default_hood_id"]})
for hood in db.hoods.find({"position": {"$exists": True}}):
distance = km_distance(hood["position"]["lng"], hood["position"]["lat"], lng, lat)
if distance <= current_hood_dist:
current_hood_dist = distance
current_hood = hood
return current_hood

View File

@ -2,7 +2,6 @@
import lxml.etree
import requests
import hoods
from pymongo import MongoClient
client = MongoClient()
@ -43,9 +42,6 @@ for r in tree.xpath("/netmon_response/routerlist/router"):
lat = float(r.xpath("latitude/text()")[0])
assert lng != 0
assert lat != 0
#router["position"]["lng"] = lng
#router["position"]["lat"] = lat
#router["loc"] = { "type": "Point", "coordinates": [ -73.88, 40.78 ], "comment": "foobar" }
router["position"] = {
"type": "Point",
@ -53,7 +49,7 @@ for r in tree.xpath("/netmon_response/routerlist/router"):
}
# define hood
router["hood"] = hoods.hood_by_pos(*router["position"]["coordinates"])["name"]
router["hood"] = db.hoods.find_one({"position": {"$near": {"$geometry": router["position"]}}})["name"]
# try to get comment
router["position"]["comment"] = r.xpath("location/text()")[0]