def test_communes_with_same_zipcodes_are_correctly_found(self):

        oraison = geocoding.get_city_by_zipcode("04700", "oraison")
        puimichel = geocoding.get_city_by_zipcode("04700", "puimichel")
        self.assertEqual(oraison['commune_id'], '04143')
        self.assertEqual(puimichel['commune_id'], '04156')

        vantoux = geocoding.get_city_by_zipcode("57070", "vantoux")
        saint_julien_les_metz = geocoding.get_city_by_zipcode(
            "57070", "saint-julien-les-metz")
        self.assertEqual(vantoux['commune_id'], '57693')
        self.assertEqual(saint_julien_les_metz['commune_id'], '57616')
 def test_paris4eme_is_correctly_found(self):
     cities_zipcodes = [[city['name'], city['zipcode']]
                        for city in geocoding.get_cities()]
     paris4eme_zipcodes = [x[1] for x in cities_zipcodes if x[1] == "75004"]
     self.assertEqual(len(paris4eme_zipcodes), 1)
     zipcode = paris4eme_zipcodes[0]
     self.assertEqual(zipcode, "75004")
     city = geocoding.get_city_by_zipcode(zipcode, "saint-denis")
     self.assertEqual(city['coords']['lat'], 48.8544006347656)
     self.assertEqual(city['coords']['lon'], 2.36240005493164)
 def test_montigny_les_metz_is_correctly_found(self):
     cities_zipcodes = [[city['name'], city['zipcode']]
                        for city in geocoding.get_cities()]
     montigny_zipcodes = [
         x[1] for x in cities_zipcodes
         if x[0].startswith('Montigny-l') and x[0].endswith('s-Metz')
     ]
     self.assertEqual(len(montigny_zipcodes), 1)
     zipcode = montigny_zipcodes[0]
     self.assertEqual(zipcode, "57950")
     city = geocoding.get_city_by_zipcode(zipcode, "paris-4eme")
     self.assertEqual(city['coords']['lat'], 49.09692140157696)
     self.assertEqual(city['coords']['lon'], 6.1549924040022725)
Beispiel #4
0
    def __init__(self, zipcode, name=''):
        self.zipcode = zipcode

        # Location attribute may be None if slug/zipcode combination is incorrect
        self.location = None
        self.slug = slugify(name)
        self.name = name

        city = geocoding.get_city_by_zipcode(self.zipcode, slug=self.slug)
        if not city:
            logger.debug("unable to retrieve a city for zipcode `%s` and slug `%s`", self.zipcode, self.slug)
        else:
            coordinates = city['coords']
            self.location = Location(coordinates['lat'], coordinates['lon'])
            self.slug = city['slug']
            self.name = city['name']
Beispiel #5
0
def isochrone():
    """
    Display the isochrone for a given location and travel mode.
    Before we dropped redis, this was useful for debugging and pre-filling the cache in production.

    E.g: /maps/isochrone?zipcode=75010&dur=30&tr=public

    Request args:
        dur (int): Isochrone duration, in minutes. Must be one of allowed values.
        zipcode (str)
        tr (str): one of TRAVEL_MODES
    """
    zipcode = request.args.get('zipcode')
    duration = request.args.get('dur')
    travel_mode = request.args.get('tr')

    # Check argument validity
    if not zipcode:
        abort(400, description='Missing argument: zipcode')
    city = geocoding.get_city_by_zipcode(zipcode, None)
    if city is None:
        abort(400, description='Invalid zipcode')
    if duration:
        try:
            duration = int(duration)
        except ValueError:
            abort(400, description='Invalid duration')
        if duration not in constants.ISOCHRONE_DURATIONS_MINUTES:
            abort(
                400,
                description='Invalid duration: accepted values are {}'.format(
                    constants.ISOCHRONE_DURATIONS_MINUTES))
    if travel_mode and travel_mode not in constants.TRAVEL_MODES:
        abort(400,
              description='Invalid travel mode: accepted values are {}'.format(
                  constants.TRAVEL_MODES))

    # Check optional arguments
    if not (duration and travel_mode):
        duration = duration or constants.ISOCHRONE_DURATIONS_MINUTES[0]
        travel_mode = travel_mode or constants.DEFAULT_TRAVEL_MODE
        return redirect(
            url_for('maps.isochrone') +
            '?zipcode={}&dur={}&tr={}'.format(zipcode, duration, travel_mode))

    latitude = city['coords']['lat']
    longitude = city['coords']['lon']

    travel_isochrone = travel.isochrone(
        (latitude, longitude), duration, mode=travel_mode) or []

    # We reverse the isochrones to make life easier to js
    travel_isochrone = [[[coords[1], coords[0]] for coords in polygon]
                        for polygon in travel_isochrone]

    return render_template(
        'search/geo.html',
        latitude=latitude,
        longitude=longitude,
        duration=duration,
        isochrone=json.dumps(travel_isochrone),
    )
 def test_saint_denis_reunion_have_correct_coordinates(self):
     city = geocoding.get_city_by_zipcode("97400", "montigny-les-metz")
     self.assertEqual(int(float(city['coords']['lat'])), -20)
     self.assertEqual(int(float(city['coords']['lon'])), 55)