Exemplo n.º 1
0
    def geocode(self,
                address,
                sensor='false',
                bounds='',
                region='',
                language='',
                components=''):
        """
        Given a string address, return a dictionary of information about
        that location, including its latitude and longitude.

        :param address: Address of location to be geocoded.
        :type address: string
        :param sensor: ``'true'`` if the address is coming from, say, a GPS device.
        :type sensor: string
        :param bounds: The bounding box of the viewport within which to bias geocode results more prominently.
        :type bounds: string
        :param region: The region code, specified as a ccTLD ("top-level domain") two-character value for biasing
        :type region: string
        :param components: The components to use when restricting the search results.
        :type components: string
        :param language: The language in which to return results.
        :type language: string
        :returns: `geocoder return value`_ dictionary
        :rtype: dict
        :raises GeocoderError: if there is something wrong with the query.

        For details on the input parameters, visit
        http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

        For details on the output, visit
        http://code.google.com/apis/maps/documentation/geocoding/#GeocodingResponses

        """

        params = {
            'address': address,
            'sensor': sensor,
            'bounds': bounds,
            'region': region,
            'language': language,
            'components': components,
        }

        if self is not None:
            return GeocoderResult(self.get_data(params=params))
        else:
            return GeocoderResult(Geocoder.get_data(params=params))
Exemplo n.º 2
0
    def reverse_geocode(self,
                        lat,
                        lng,
                        sensor='false',
                        bounds='',
                        region='',
                        language=''):
        """
        Converts a (latitude, longitude) pair to an address.

        :param lat: latitude
        :type lat: float
        :param lng: longitude
        :type lng: float
        :return: `Reverse geocoder return value`_ dictionary giving closest
            address(es) to `(lat, lng)`
        :rtype: dict
        :raises GeocoderError: If the coordinates could not be reverse geocoded.

        Keyword arguments and return value are identical to those of :meth:`geocode()`.

        For details on the input parameters, visit
        http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

        For details on the output, visit
        http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

        """
        params = {
            'latlng': "%f,%f" % (lat, lng),
            'sensor': sensor,
            'bounds': bounds,
            'region': region,
            'language': language,
        }

        if self is not None:
            return GeocoderResult(self.get_data(params=params))
        else:
            return GeocoderResult(Geocoder.get_data(params=params))
Exemplo n.º 3
0
    def test_geocoder_results(self):
        """Test GeocoderResult's indexing and iteration access"""
        results = GeocoderResult(json.loads(MOCK_DATA))

        self.assertEqual(results[1].neighborhood, "East Colorado Springs")
        self.assertEqual(results.establishment, "DMV")
        for index, result in enumerate(results):
            if index == 0:
                self.assertEqual(result.neighborhood, "Winnetka")
            elif index == 1:
                self.assertEqual(result.street_number, "2447")
            else:
                self.fail()
Exemplo n.º 4
0
def geocode_record(record):
    result = {}
    reverse = False
    sitename = record.get("location-sitename", [ False ])[0]
    city = record.get("location-city", [ False ])[0]
    state = record.get("location-state", [ False ])[0]
    code = record.get("location-code", [ False ])[0]
    country = record.get("location-country", [ False ])[0]
    latitude = record.get("location-latitude", [ False ])[0]
    longitude = record.get("location-longitude", [ False ])[0]
    if latitude and longitude:
        reverse = True
        result = reverse_geocode(latitude, longitude)
    else:
        query = ""
        if sitename:
            query += sitename + ", "
        if city:
            query += city + ", "
        if state:
            query += state
            if code:
                query += " " + code
            query += ", "
        elif code:
            query += code + ", "
        if country:
            query += country
        if query:
            result = geocode(query)
    if result:
        result = GeocoderResult(result)
        record["location-sitename"] = [ sitename or result.establishment or "" ]
        record["location-city"] = [ city or result.city or "" ]
        record["location-state"] = [ state or result.state or "" ]
        record["location-code"] = [ code or result.postal_code or "" ]
        record["location-country"] = [ country or result.country__short_name or "" ]
        if not reverse:
            record["location-latitude"] = [ str(result.coordinates[0]) ]
            record["location-longitude"] = [ str(result.coordinates[1]) ]