示例#1
0
 def test_from_dict_holds_the_lack_of_geocoords(self):
     dict1 = {"station": {"coord": {}}}
     dict2 = {"coord": {}}
     result1 = Location.from_dict(dict1)
     self.assertTrue(isinstance(result1, Location))
     self.assertEqual(result1.lat, 0.0)
     self.assertEqual(result1.lon, 0.0)
     self.assertTrue(result1.country is None)
     self.assertTrue(result1.name is None)
     self.assertTrue(result1.id is None)
     result2 = Location.from_dict(dict2)
     self.assertTrue(isinstance(result2, Location))
     self.assertEqual(result2.lat, 0.0)
     self.assertEqual(result2.lon, 0.0)
     self.assertTrue(result2.country is None)
     self.assertTrue(result2.name is None)
     self.assertTrue(result2.id is None)
示例#2
0
    def reverse_geocode(self, lat, lon, limit=None):
        geo.assert_is_lon(lon)
        geo.assert_is_lat(lat)
        if limit is not None:
            assert isinstance(limit, int)
            assert limit > 0

        params = {'lat': lat, 'lon': lon}
        if limit is not None:
            params['limit'] = limit

        _, json_data = self.http_client.get_json(REVERSE_GEOCODING_URI,
                                                 params=params)
        return [Location.from_dict(item) for item in json_data]
示例#3
0
    def test_from_dict(self):
        try:
            Location.from_dict(None)
            self.fail()
        except exceptions.ParseAPIResponseError:
            pass
        dict1 = {
            "coord": {
                "lon": -0.12574,
                "lat": 51.50853
            },
            "id": 2643743,
            "name": "London",
            "cnt": 9
        }
        dict2 = {
            "city": {
                "coord": {
                    "lat": 51.50853,
                    "lon": -0.125739
                },
                "country": "GB",
                "id": 2643743,
                "name": "London",
                "population": 1000000
            }
        }
        dict3 = {"station": {"coord": {"lon": -90.47, "lat": 39.38}}}
        dict4 = {"station": {"coord": {"lng": -90.47, "lat": 39.38}}}
        dict5 = {"station": {}}
        result1 = Location.from_dict(dict1)
        result2 = Location.from_dict(dict2)
        result3 = Location.from_dict(dict3)
        result4 = Location.from_dict(dict4)
        self.assertTrue(isinstance(result1, Location))
        self.assertTrue(isinstance(result2, Location))
        self.assertFalse(result1.country is not None)
        self.assertTrue(result1.id is not None)
        self.assertTrue(result1.lat is not None)
        self.assertTrue(result1.lon is not None)
        self.assertTrue(result1.name is not None)
        self.assertTrue(result2.country is not None)
        self.assertTrue(result2.id is not None)
        self.assertTrue(result2.lat is not None)
        self.assertTrue(result2.lon is not None)
        self.assertTrue(result2.name is not None)
        self.assertTrue(result3.lat is not None)
        self.assertTrue(result3.lon is not None)
        self.assertTrue(result3.country is None)
        self.assertTrue(result3.name is None)
        self.assertTrue(result3.id is None)
        self.assertIsInstance(result4, Location)

        self.assertRaises(KeyError, Location.from_dict, dict5)
示例#4
0
    def geocode(self, toponym, country=None, state_code=None, limit=None):
        """
        Invokes the direct geocoding API endpoint

        :param toponym: the name of the location
        :type toponym: `str`
        :param country: the 2-chars ISO symbol of the country
        :type country: `str` or `None`
        :param state_code: the 2-chars ISO symbol of state (only useful in case the country is US)
        :type state_code: `str` or `None`
        :param limit: the max number of results to be returned in case of multiple matchings (no limits by default)
        :type limit: `int` or `None`
        :returns: a list of *Location* instances
        :raises: *AssertionError*, *ValueError*, *APIRequestError*

        """
        assert toponym, 'Toponym must be specified'
        if country is not None and len(country) != 2:
            raise ValueError("Country must be a 2-char string")
        if state_code is not None and len(state_code) != 2:
            raise ValueError("State Code must be a 2-char string")
        if limit is not None:
            assert isinstance(limit, int)
            assert limit > 0

        query = toponym
        if state_code is not None:
            query += ',' + state_code
        if country is not None:
            query += ',' + country

        params = {'q': query}

        if limit is not None:
            params['limit'] = limit

        _, json_data = self.http_client.get_json(DIRECT_GEOCODING_URI,
                                                 params=params)
        return [Location.from_dict(item) for item in json_data]