Example #1
0
def is_valid_address(order_data, address_type):
        order_country = order_data.get(address_type + '_country')
        order_city = order_data.get(address_type + '_city')
        order_street_address = order_data.get(address_type + '_street_address')
        order_house_number = order_data.get(address_type + '_house_number')

        # empty address are "valid"
        if not len(order_country + order_city + order_street_address + order_house_number):
            return True

        addresses = structured_geocode(order_country, order_city, order_street_address, order_house_number)

        for address in addresses:
            if address["house_number"]  == order_house_number \
                and address["city"]     == order_city \
                and address["country"]  == order_country \
                and address["street_address"]   == order_street_address:

                order_data[address_type + "_raw"] = address["name"]
                order_data[address_type + "_lon"] = address["lon"]
                order_data[address_type + "_lat"] = address["lat"]
                order_data[address_type + "_geohash"] = geohash_encode(address["lon"], address["lat"])
                return True

        return False
Example #2
0
 def test_geo_hash(self):
     """
     Simple geo hasing test.
     """
     logging.info("\nTesting geohash encode/decode.")
     points = ({"lat"       :"31.776933",
                "lon"       :"35.234376",
                "hash_code" :"sv9hcbbfh3wu"},
               {"lat"       :"21.424172",
                "lon"       :"39.826112",
                "hash_code" :"sgu3fk0kzejk"},
               )
     for p in points:
         self.assertEqual(geohash_encode(float(p["lon"]),float(p["lat"])), p["hash_code"], msg="encode error")
         self.assertEqual(geohash_decode(p["hash_code"]), (p["lat"],p["lon"]), msg="decode error")
Example #3
0
 def test_geo_hash(self):
     """
     Simple geo hasing test.
     """
     logging.info("\nTesting geohash encode/decode.")
     points = (
         {
             "lat": "31.776933",
             "lon": "35.234376",
             "hash_code": "sv9hcbbfh3wu"
         },
         {
             "lat": "21.424172",
             "lon": "39.826112",
             "hash_code": "sgu3fk0kzejk"
         },
     )
     for p in points:
         self.assertEqual(geohash_encode(float(p["lon"]), float(p["lat"])),
                          p["hash_code"],
                          msg="encode error")
         self.assertEqual(geohash_decode(p["hash_code"]),
                          (p["lat"], p["lon"]),
                          msg="decode error")
Example #4
0
def get_business_order_form_data(request, passenger):
    form_data = request.POST.copy()
    country_id = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE).id

    if passenger.default_station and passenger.business.confine_orders:
        form_data["confining_station"] = passenger.default_station.id

    form_data["taxi_is_for"] = request.POST.get("taxi_is_for", "")
    form_data["comments"] = request.POST.get("comments", "")

    def resolve_lat_lon_or_default(city, street_address, house_number):
        lat = -1
        lon = -1
        street_address = street_address.strip()
        house_number = house_number.strip()
        address = "%s %s, %s" % (street_address, house_number, city.name)
        geocoding_results = geocode(address, resolve_to_ids=True)
        for result in geocoding_results:
            # make sure it is a match and not a suggestion
            if result["street_address"] == street_address and result[
                    "house_number"] == house_number:
                lat = result["lat"]
                lon = result["lon"]
                break

        return lat, lon

    from_city = City.by_id(form_data.get("from_city"))
    from_hn = re.search("(\d+)", form_data.get("from_street_address", ""))
    from_hn = str(from_hn.groups()[0] if from_hn else "")
    from_street_address = form_data.get("from_street_address",
                                        "").replace(from_hn, "")
    from_lat, from_lon = resolve_lat_lon_or_default(from_city,
                                                    from_street_address,
                                                    from_hn)
    form_data.update({
        "from_country":
        country_id,
        "from_raw":
        "%s %s %s" % (from_street_address, from_hn, from_city.name),
        "from_street_address":
        from_street_address,
        "from_house_number":
        from_hn,
        "from_lat":
        from_lat,
        "from_lon":
        from_lon,
        "from_geohash":
        geohash_encode(from_lon, from_lat),
    })

    to_city = City.by_id(form_data.get("to_city"))
    if to_city:
        to_hn = re.search("(\d+)", form_data.get("to_street_address", ""))
        to_hn = str(to_hn.groups()[0] if to_hn else "")
        to_street_address = form_data.get("to_street_address",
                                          "").replace(to_hn, "")
        to_lat, to_lon = resolve_lat_lon_or_default(to_city, to_street_address,
                                                    to_hn)
        form_data.update({
            "to_country":
            country_id,
            "to_raw":
            "%s %s %s" % (to_street_address, to_hn, to_city.name),
            "to_street_address":
            to_street_address,
            "to_house_number":
            to_hn,
            "to_lat":
            to_lat,
            "to_lon":
            to_lon,
            "to_geohash":
            geohash_encode(to_lon, to_lat)
        })

    return form_data
def get_business_order_form_data(request, passenger):
    form_data = request.POST.copy()
    country_id = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE).id

    if passenger.default_station and passenger.business.confine_orders:
        form_data["confining_station"] = passenger.default_station.id

    form_data["taxi_is_for"] = request.POST.get("taxi_is_for", "")
    form_data["comments"] = request.POST.get("comments", "")

    def resolve_lat_lon_or_default(city, street_address, house_number):
        lat = -1
        lon = -1
        street_address = street_address.strip()
        house_number = house_number.strip()
        address = "%s %s, %s" % (street_address, house_number, city.name)
        geocoding_results = geocode(address, resolve_to_ids=True)
        for result in geocoding_results:
            # make sure it is a match and not a suggestion
            if result["street_address"] == street_address and result["house_number"] == house_number:
                lat = result["lat"]
                lon = result["lon"]
                break

        return lat, lon

    from_city = City.by_id(form_data.get("from_city"))
    from_hn = re.search("(\d+)", form_data.get("from_street_address", ""))
    from_hn = str(from_hn.groups()[0] if from_hn else "")
    from_street_address = form_data.get("from_street_address", "").replace(from_hn, "")
    from_lat, from_lon = resolve_lat_lon_or_default(from_city, from_street_address, from_hn)
    form_data.update(
        {
            "from_country": country_id,
            "from_raw": "%s %s %s" % (from_street_address, from_hn, from_city.name),
            "from_street_address": from_street_address,
            "from_house_number": from_hn,
            "from_lat": from_lat,
            "from_lon": from_lon,
            "from_geohash": geohash_encode(from_lon, from_lat),
        }
    )

    to_city = City.by_id(form_data.get("to_city"))
    if to_city:
        to_hn = re.search("(\d+)", form_data.get("to_street_address", ""))
        to_hn = str(to_hn.groups()[0] if to_hn else "")
        to_street_address = form_data.get("to_street_address", "").replace(to_hn, "")
        to_lat, to_lon = resolve_lat_lon_or_default(to_city, to_street_address, to_hn)
        form_data.update(
            {
                "to_country": country_id,
                "to_raw": "%s %s %s" % (to_street_address, to_hn, to_city.name),
                "to_street_address": to_street_address,
                "to_house_number": to_hn,
                "to_lat": to_lat,
                "to_lon": to_lon,
                "to_geohash": geohash_encode(to_lon, to_lat),
            }
        )

    return form_data