Exemplo n.º 1
0
    def test7_resolve_zip_by_lat_lon(self):
        loc = ZIPCode.getByLatLon(34.0452, -118.284, self.redis)

        self.assertIsNotNone(loc)
        self.assertEqual(loc.name, "90006")
        self.assertEqual(loc.country, "United States")
        self.assertIn(loc.state, ("CA", "California"))
Exemplo n.º 2
0
    def test7_resolve_zip_by_lat_lon(self):
        loc = ZIPCode.getByLatLon(34.0452, -118.284, self.redis)

        self.assertIsNotNone(loc)
        self.assertEqual(loc.name, '90006')
        self.assertEqual(loc.country, 'United States')
        self.assertIn(loc.state, ('CA', 'California'))
Exemplo n.º 3
0
    def lookup(self, req):
        '''
        Handles a Gearman string req. 
        Method interface required by python-gearman
        
        Resolves provided geographic coordinates to closest location. 
        The lookup service is provided by the geodis library and default data set.
        
        req should be a string of format of '<lat>,<lon>' format,
        where lat and lon are ASCII string representations of float.
        
        The job response upon successful lookup is a JSON string of format:
        {'city': '<city>', 'zip': '<zip>', 'country': '<country>', 
        'lat': '<lat>', 'lon': '<lon>', 'state': '<state>'}
        
        Fields:
        city - City name.
        zip - ZIP code (optional)
        country - Country
        lat - Latitude of found location
        lon - Longitude of found location
        state - State (optional)
        
        An error response is a JSON string of format:
        {'error':'<error>'}
        where error is a string description.
        '''

        lat, lon = req.split(",")
        lat = float(lat)
        lon = float(lon)

        try:
            loc = geodis.City.getByLatLon(lat, lon, self.redisClient)

            if self.hasZIPCodeData(loc.country):
                '''
                We only lookup location's zip after determining it 
                is in a supported country. Otherwise, geodis' ZIPCode
                lookup will return a location within a country that has a
                zip code. So a lookup for zip in unsupported UK would 
                return a ZIP somewhere in Maine, US.
                '''
                loc = ZIPCode.getByLatLon(lat, lon, self.redisClient)
                resp = formatZipResponse(loc)
            else:
                resp = formatCityResponse(loc)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            traceback.print_exc()
            resp = formatErrResponse(str(sys.exc_info()[0]))

        return resp
    def lookup(self, req):
        """
        Handles a Gearman string req. 
        Method interface required by python-gearman
        
        Resolves provided geographic coordinates to closest location. 
        The lookup service is provided by the geodis library and default data set.
        
        req should be a string of format of '<lat>,<lon>' format,
        where lat and lon are ASCII string representations of float.
        
        The job response upon successful lookup is a JSON string of format:
        {'city': '<city>', 'zip': '<zip>', 'country': '<country>', 
        'lat': '<lat>', 'lon': '<lon>', 'state': '<state>'}
        
        Fields:
        city - City name.
        zip - ZIP code (optional)
        country - Country
        lat - Latitude of found location
        lon - Longitude of found location
        state - State (optional)
        
        An error response is a JSON string of format:
        {'error':'<error>'}
        where error is a string description.
        """

        lat, lon = req.split(",")
        lat = float(lat)
        lon = float(lon)

        try:
            loc = geodis.City.getByLatLon(lat, lon, self.redisClient)

            if self.hasZIPCodeData(loc.country):
                """
                We only lookup location's zip after determining it 
                is in a supported country. Otherwise, geodis' ZIPCode
                lookup will return a location within a country that has a
                zip code. So a lookup for zip in unsupported UK would 
                return a ZIP somewhere in Maine, US.
                """
                loc = ZIPCode.getByLatLon(lat, lon, self.redisClient)
                resp = formatZipResponse(loc)
            else:
                resp = formatCityResponse(loc)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            traceback.print_exc()
            resp = formatErrResponse(str(sys.exc_info()[0]))

        return resp