def test_empty_query(self):
        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               side_effect=ZeroDivisionError):
            coordinates = geocoding.get_coordinates('')

        self.assertEqual([], coordinates)
    def test_get_coordinates_of_location_with_no_city(self):
        mock_get = self.mock_get('search-balltrapp.json')
        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               return_value=mock_get):
            coordinates = geocoding.get_coordinates("ball trapp")

        self.assertIn("city", coordinates[0])
    def test_duplicate_features(self):
        mock_get = self.mock_get('search-metz.json')

        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               return_value=mock_get):
            coordinates = geocoding.get_coordinates('metz', limit=10)

        self.assertNotEqual(coordinates[0]['label'], coordinates[1]['label'])
    def test_400_error(self):
        mock_get = mock.Mock(status_code=400,
                             json=mock.Mock(side_effect=ValueError))
        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               return_value=mock_get):
            coordinates = geocoding.get_coordinates(
                '22 Allée Darius Milhaud, 75019 Paris')

        self.assertEqual([], coordinates)
Beispiel #5
0
def get_location(request_args):
    """
    Parse request parameters to extract a desired location and location names.

    Returns:

        location (Location) or None
        named_location (NamedLocation) or None
    """

    # Parse location from latitude/longitude
    location = None
    zipcode = city_name = location_name = ''
    if 'lat' in request_args and 'lon' in request_args:
        try:
            latitude = float(request_args['lat'])
            longitude = float(request_args['lon'])
        except ValueError:
            pass
        else:
            location = Location(latitude, longitude)
            addresses = geocoding.get_address(latitude, longitude, limit=1)
            if addresses:
                zipcode = addresses[0]['zipcode']
                city_name = addresses[0]['city']
                location_name = addresses[0]['label']

    # Parse location from zipcode/city slug (slug is optional)
    if location is None and 'zipcode' in request_args:
        zipcode = request_args['zipcode']
        city_slug = request_args.get('city', '')
        city = CityLocation(zipcode, city_slug)

        location = city.location
        zipcode = city.zipcode
        city_name = city.name
        location_name = city.full_name

    # Autocompletion has probably not worked: do autocompletion here
    if location is None and 'l' in request_args:
        try:
            result = geocoding.get_coordinates(request_args['l'], limit=1)[0]
        except IndexError:
            pass
        else:
            location = Location(result['latitude'], result['longitude'])
            zipcode = result['zipcode']
            city_name = result['city']
            location_name = result['label']

    named_location = None
    if zipcode and city_name and location_name:
        named_location = NamedLocation(zipcode, city_name, location_name)

    return location, named_location
    def test_city_homonyms_can_be_distinguished_by_zipcode(self):
        """
        Five homonym cities "Saint-Paul" should have distinguishable labels.
        """
        mock_get = self.mock_get('search-saint-paul.json')

        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               return_value=mock_get):
            coordinates = geocoding.get_coordinates('Saint-Paul')

        self.assertEqual(len(coordinates), 5)
        for coordinate in coordinates:
            self.assertIn(coordinate['zipcode'], coordinate['label'])
    def test_get_coordinates(self):
        mock_get = self.mock_get('search-lelab.json')

        with mock.patch.object(geocoding.datagouv.requests,
                               'get',
                               return_value=mock_get):
            coordinates = geocoding.get_coordinates(
                '22 Allée Darius Milhaud, 75019 Paris')

        self.assertEqual([{
            'latitude': 48.884085,
            'longitude': 2.38728,
            'label': '22 Allée Darius Milhaud 75019 Paris',
            'city': 'Paris',
            'zipcode': '75019',
        }], coordinates)
Beispiel #8
0
def autocomplete_locations():
    """
    Query string arguments:

        term (str)
    """
    term = request.args.get('term', '').strip()
    suggestions = []
    if term:
        suggestions = geocoding.get_coordinates(
            term, limit=autocomplete.MAX_LOCATIONS)
    if not suggestions:
        suggestions = search_util.build_location_suggestions(term)
        if suggestions:
            pass  # FIXME log BAN LIKELY DOWN event
    for suggestion in suggestions:
        suggestion['value'] = suggestion['label']
    return make_response(json.dumps(suggestions))
Beispiel #9
0
def autocomplete_locations():
    """
    This route trys to get an address from api-adresse.data.gouv.fr API
    If this fails - e.g. the API is down, then we use our internal search mechanism (elastic)
    The api-adresse.data.gouv.fr API is more flexible as
    it allows the users to search based on an address, not just departments names

    Query string arguments:

        term (str)
    """
    term = request.args.get('term', '').strip()
    suggestions = []
    if term:
        suggestions = geocoding.get_coordinates(
            term, limit=autocomplete.MAX_LOCATIONS)
    if not suggestions:
        suggestions = autocomplete.build_location_suggestions(term)
        if suggestions:
            pass  # FIXME log BAN LIKELY DOWN event
    for suggestion in suggestions:
        suggestion['value'] = suggestion['label']
    return make_response(json.dumps(suggestions))
Beispiel #10
0
def get_location(request_args):
    """
    Parse request parameters to extract a desired location and location names.

    Returns:

        location (Location) or None
        named_location (NamedLocation) or None
        departments (str)
    """

    # Parse location from latitude/longitude
    location = None
    named_location = None
    departments = None
    city_code = None
    if 'departments' in request_args:
        departments = request_args.get('departments')
        # FIXME: this is a temporary shortcut, we should use the department API to get the name and zipcode of the department
        zipcode = departments[0]
        location_name = city_name = request_args.get('l')
    else:
        city_code = zipcode = city_name = location_name = ''
        if 'lat' in request_args and 'lon' in request_args:
            try:
                latitude = float(request_args['lat'])
                longitude = float(request_args['lon'])
            except ValueError:
                pass
            else:
                location = Location(latitude, longitude)
                addresses = geocoding.get_address(latitude, longitude, limit=1)
                if addresses:
                    zipcode = addresses[0]['zipcode']
                    city_name = addresses[0]['city']
                    location_name = addresses[0]['label']
                    city_code = addresses[0]['city_code']

        # Parse location from zipcode/city slug (slug is optional)
        if location is None and 'zipcode' in request_args:
            zipcode = request_args['zipcode']
            city_slug = request_args.get('city', '')
            city = CityLocation(zipcode, city_slug)

            location = city.location
            zipcode = city.zipcode
            city_name = city.name
            location_name = city.full_name

        # Autocompletion has probably not worked: do autocompletion here
        if location is None and 'l' in request_args:
            try:
                result = geocoding.get_coordinates(request_args['l'],
                                                   limit=1)[0]
            except IndexError:
                pass
            else:
                if 'latitude' in result and 'longitude' in result:
                    location = Location(result['latitude'],
                                        result['longitude'])
                    zipcode = result['zipcode']
                    city_name = result['city']
                    location_name = result['label']

    if zipcode and city_name and location_name:
        named_location = NamedLocation(zipcode, city_name, location_name,
                                       city_code)

    return location, named_location, departments