Exemplo n.º 1
0
 def nearest_location(self, ip_addr):
     """Return nearest supported location for this IP Address"""
     try:
         _log.debug("Geo IP mapping %s", ip_addr)
         if ',' in ip_addr:
             ip_addr = ip_addr.split(',')[:1][0]
         dist, loc = 10000, settings.LOCATION_DEFAULT
         gi = pygeoip.GeoIP(settings.LOCATION_GEOIP_PATH, pygeoip.MEMORY_CACHE)
         rec = gi.record_by_addr(ip_addr)
         if not rec:
             return loc
         cn = rec.get('country_code', '')
         if cn == 'US':
             # Find nearest supported city by lat, lng
             lat, lng = rec['latitude'], rec['longitude']                
             for lc, params in settings.LOCATION_DATA.iteritems():
                 llat, llng = params[:2]
                 d = gislib.getDistance((lat, lng), (llat, llng))
                 # print ">>>", lc, d
                 if d < dist:
                     dist, loc = d, lc
             # print "Nearest:", loc, dist
         return loc
     except Exception, e:
         _log.error("GeoIP error on %s", ip_addr)
         _log.exception(e)
         return settings.LOCATION_DEFAULT
Exemplo n.º 2
0
 def distance(self):
     """
     :returns: Distance between first and last 
     :rtype: float
     """
     self.sort()
     return gislib.getDistance(self.start().location,self.end().location)
Exemplo n.º 3
0
 def nearest_location(self, ip_addr):
     """Return nearest supported location for this IP Address"""
     try:
         _log.debug("Geo IP mapping %s", ip_addr)
         if ',' in ip_addr:
             ip_addr = ip_addr.split(',')[:1][0]
         dist, loc = 10000, settings.LOCATION_DEFAULT
         gi = pygeoip.GeoIP(settings.LOCATION_GEOIP_PATH,
                            pygeoip.MEMORY_CACHE)
         rec = gi.record_by_addr(ip_addr)
         if not rec:
             return loc
         cn = rec.get('country_code', '')
         if cn == 'US':
             # Find nearest supported city by lat, lng
             lat, lng = rec['latitude'], rec['longitude']
             for lc, params in settings.LOCATION_DATA.iteritems():
                 llat, llng = params[:2]
                 d = gislib.getDistance((lat, lng), (llat, llng))
                 # print ">>>", lc, d
                 if d < dist:
                     dist, loc = d, lc
             # print "Nearest:", loc, dist
         return loc
     except Exception, e:
         _log.error("GeoIP error on %s", ip_addr)
         _log.exception(e)
         return settings.LOCATION_DEFAULT
Exemplo n.º 4
0
    def distanceTraveled(self):
        """
        :returns: Total distance traveled along path in kilometers
        :rtype: float

        .. note:
            This requires additional work to handle noise from indoor readings.
        """
        self.sort()
        total = 0
        for idx, event in enumerate(self.events[:-1]):
            total += gislib.getDistance(event.location, self.events[idx+1].location)
        return total
Exemplo n.º 5
0
    def speed(self,relative=-1):
        """
        Calculates the speed of the current event by comparing 
        its position with its relative.  Defaults to looking at the previous event.

        :returns: Kilometers per hour
        :rtype: float
        """

        previous = self.neighbor(relative)
        deltaTime = self.epoch - previous.epoch
        divisor = deltaTime/3600.0
        distance = gislib.getDistance(previous.location,self.location)
        if divisor == 0:
            return 0
        else:
            return distance / divisor
Exemplo n.º 6
0
 def testCornerIsFurtherThanSide(self):
     "make sure the corner of bounding box is further than side (circle radius)"
     loc1 = (self.oxlat, self.oxlon + self.lon_extra)
     loc2 = (self.oxlat + self.lat_extra, self.oxlon)
     d = gislib.getDistance(loc1, loc2)
     assert round(d, 1) > self.distance
Exemplo n.º 7
0
 def testCalculateDistanceToNorth(self):
     "make sure we can calculate northbound distances ok"
     loc1 = (self.oxlat, self.oxlon)
     loc2 = (self.oxlat + self.lat_extra, self.oxlon)
     d = gislib.getDistance(loc1, loc2)
     self.assertEqual(round(d, 1), self.distance)
Exemplo n.º 8
0
 def testCalculateDistanceToWest(self):
     "make sure we can calculate westbound distances ok"
     loc1 = (self.oxlat, self.oxlon)
     loc2 = (self.oxlat, self.oxlon - self.lon_extra)
     d = gislib.getDistance(loc1, loc2)
     self.assertEqual(round(d, 1), self.distance)
Exemplo n.º 9
0
"""Pulls live Serial Stream"""

import serial
from geolocator import gislib

ser = serial.Serial('/dev/tty.Qstarz818XT-SPPslave', 19200, timeout=1)

last_point = (0.0, 0.0)

for i in range(1, 10):
    line = (ser.readline())  # read a '\n' terminated line
    print line
    data = line.split(',')
    if len(data) == 15:
        lat = data[2]
        lat_dir = data[3]
        long = data[4]
        long_dir = data[5]
        print "lat: {0} {1} long: {2} {3}".format(lat, lat_dir, long, long_dir)
        point = (float(lat), float(long) * -1.0)
        print gislib.getDistance(point, last_point)
        last_point = point

ser.close()
Exemplo n.º 10
0
 def is_within_radius(self, coords, radius):
     return self.coords is not None and \
         gislib.getDistance(self.coords, coords) <= radius
Exemplo n.º 11
0
 def is_within_radius(self, coords, radius):
     return self.coords is not None and \
         gislib.getDistance(self.coords, coords) <= radius
Exemplo n.º 12
0
 def testCornerIsFurtherThanSide(self):
    "make sure the corner of bounding box is further than side (circle radius)"
    loc1 = (self.oxlat, self.oxlon + self.lon_extra)
    loc2 = (self.oxlat + self.lat_extra, self.oxlon)
    d = gislib.getDistance(loc1, loc2)
    assert round(d,1) > self.distance
Exemplo n.º 13
0
 def testCalculateDistanceToNorth(self):
    "make sure we can calculate northbound distances ok"
    loc1 = (self.oxlat, self.oxlon)
    loc2 = (self.oxlat + self.lat_extra, self.oxlon)
    d = gislib.getDistance(loc1, loc2)
    self.assertEqual(round(d,1), self.distance)
Exemplo n.º 14
0
 def testCalculateDistanceToWest(self):
    "make sure we can calculate westbound distances ok"
    loc1 = (self.oxlat, self.oxlon)
    loc2 = (self.oxlat, self.oxlon - self. lon_extra)
    d = gislib.getDistance(loc1, loc2)
    self.assertEqual(round(d,1), self.distance)
Exemplo n.º 15
0
 def distanceTo(self, event):
     return gislib.getDistance(event.location, self.location)
Exemplo n.º 16
0
 def distance_to(self, location):
   """
   Returns the distance from this meetup to the passed point. The point is
   tuple, (lat, lng)
   """
   return gislib.getDistance((self.latitude, self.longitude), location)
Exemplo n.º 17
0
 def distance_to(self, location):
     """
 Returns the distance from this meetup to the passed point. The point is
 tuple, (lat, lng)
 """
     return gislib.getDistance((self.latitude, self.longitude), location)