Beispiel #1
0
def geocode(city=None):
    """
    Consider writing docstrings in the Google style:
        https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings

    Where possible, add type annotations as well. For example:
        https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html

    Args:
        city(str): String representation of the city name to geocode

    Returns:
        geopy.location.Location: Location object or None

    Raises:
        RuntimeError: When geocding fails
    """

    try:
        geolocator = Photon(user_agent="LazyTimezoneSearch 0.1")
        location = geolocator.geocode(city, exactly_one=True)
    except Exception as err:  # pylint: disable=broad-except
        raise RuntimeError(f"Unable to geocode: '{err}'")

    return location
Beispiel #2
0
def attractions(city, country):
    # checking coordinates of given city
    geolocator = Photon(user_agent="MobileTravel")
    location = geolocator.geocode(f'{city} {country}')
    lat = location.latitude
    lon = location.longitude
    maxLat = lat+0.05
    maxLot = lon+0.05
    apiKEY = '5ae2e3f221c38a28845f05b6b5db6f06770f4edf010d553f8a337b76'
    url = f"https://api.opentripmap.com/0.1/en/places/bbox?lon_min={lon}&lat_min={lat}&lon_max={maxLot}&lat_max={maxLat}&format=geojson&apikey={apiKEY}"
    response = requests.request("GET", url)
    return response.content
def geo_extraction(latitude, longitude):
    # 通过经纬度解析地址
    try:
        geolocator = Photon(user_agent="my-application")
        position = geolocator.reverse(str(latitude) + ',' + str(longitude),
                                      limit=1)
        addr = position.address.split(',')
        result = []
        for attribute in addr:
            # 剔除address非中文部分,大概能精确到市
            if attribute.strip(' ').encode('UTF-8').isalpha():
                result.append(attribute)
    except Exception as err:
        result = []
        print(err)
    return result
Beispiel #4
0
    def __photon(self):
        try:
            from geopy.geocoders import Photon
            geolocator = Photon(timeout=15)
            location = geolocator.reverse(str(self.location["latitude"]) + "," +
                                          str(self.location["longitude"]), exactly_one=True)

            if not self.location.get("country", None) and "country" in location.raw["properties"]:
                self.location["country"] = location.raw["properties"]["country"]
            if not self.location.get("region", None) and "state" in location.raw["properties"]:
                self.location["region"] = location.raw["properties"]["state"]

            if not self.location.get("town", None) and "city" in location.raw["properties"]:
                self.location["town"] = location.raw["properties"]["city"]
        except:
            pass
Beispiel #5
0
def calculate_distance_view(request):
    # initial values
    distance = None
    destination = None

    geolocator = Photon(user_agent='distance', timeout=10)
    form = MeasurementModelForm(request.POST or None)

    obj = get_object_or_404(Measurement, id=1)
    ip_ = get_ip_address(request)
    ip = '46.216.43.208'
    lat, lon = get_geo(ip)  # In production app will use 'ip_'
    location = geolocator.geocode(ip)

    # location coordinates
    l_lat = lat
    l_lon = lon
    pointA = (l_lat, l_lon)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)

        # destination coordinates
        d_lat = destination.latitude
        d_lon = destination.longitude
        pointB = (d_lat, d_lon)

        # distance calculation
        distance = round(geodesic(pointA, pointB).km, 2)

        instance.location = location
        instance.distance = distance
        instance.save()

    context = {
        'distance': distance,
        'destination': destination,
        'form': form,
    }

    return destination
 def handle(self, *args, **options):
     try:
         geo_locator = Photon()
         customers = Customer.objects.all()
         a = 0
         for customer in customers:
             location = geo_locator.geocode(customer.city, timeout=15)
             if location:
                 customer.latitude = location.latitude
                 customer.longitude = location.longitude
                 print(customer.id)
                 customer.save()
             else:
                 a += 1
                 print(a)
         self.stdout.write(
             self.style.SUCCESS('Successfully filled addresses'))
     except CommandError:
         raise
Beispiel #7
0
def main():
    geolocator = Photon()

    wb = load_workbook(filename='File.xlsx')
    sheets = wb.sheetnames
    sheet = wb[sheets[0]]  # selecting always the first sheet
    row_count = sheet.max_row
    column_count = sheet.max_column
    for i in range(2, row_count):
        if i % 10 == 0:
            wb.save("File.xlsx")

        address = sheet.cell(row=i, column=ADDRESS_COLUMN).value
        cap = sheet.cell(row=i, column=CAP_COLUMN).value
        loc = sheet.cell(row=i, column=LOC_COLUMN).value

        if address and cap and loc:
            full_address = f'{address} {loc} {cap} ITALIA'
            print(i, full_address)
            try:
                location = geolocator.geocode(full_address, timeout=10)
                sheet.cell(row=i, column=LAT_COLUMN).value = location.latitude
                sheet.cell(row=i, column=LON_COLUMN).value = location.longitude
                sheet.cell(row=i, column=PRECISION_COLUMN).value = ''
            except:
                try:
                    location = geolocator.geocode(f'{cap} {loc} ITALIA',
                                                  timeout=10)
                    sheet.cell(row=i,
                               column=LAT_COLUMN).value = location.latitude
                    sheet.cell(row=i,
                               column=LON_COLUMN).value = location.longitude
                    sheet.cell(row=i, column=PRECISION_COLUMN).value = '0'
                except:
                    sheet.cell(row=i, column=LAT_COLUMN).value = ''
                    sheet.cell(row=i, column=LON_COLUMN).value = ''
                    sheet.cell(row=i, column=PRECISION_COLUMN).value = '-1'

            print(
                f'({sheet.cell(row=i, column=LAT_COLUMN).value}, {sheet.cell(row=i, column=LON_COLUMN).value})'
            )

    wb.save("File.xlsx")
    def __init__(self):
        self.geocoders = [
            TimedGeocoder(Nominatim()),
            TimedGeocoder(GoogleV3()),
            TimedGeocoder(Yandex()),
            TimedGeocoder(ArcGIS()),
            TimedGeocoder(GeocodeFarm()),
            TimedGeocoder(Photon())
        ]

        self.next = 0
        self.size = len(self.geocoders)
def create_geocoders(email):
    user_agent = email + " using geocode benchmark https://github.com/matkoniecz/benchmark-geocoders"
    cached_nominatim = CachedGeocoder(Nominatim(user_agent=user_agent),
                                      "nominatim")
    cached_photon = CachedGeocoder(Photon(user_agent=user_agent), "photon")
    return [
        cached_nominatim,
        cached_photon,
        FallbackGeocoder([cached_nominatim, cached_photon],
                         "nominatim_fallback_to_photon"),
        FallbackGeocoder([cached_photon, cached_nominatim],
                         "photon_fallback_to_nominatim"),
    ]
Beispiel #10
0
    def __photon(self):
        try:
            from geopy.geocoders import Photon
            geolocator = Photon(timeout=15)
            location = geolocator.reverse(str(self.location["latitude"]) +
                                          "," +
                                          str(self.location["longitude"]),
                                          exactly_one=True)

            if not self.location.get(
                    "country",
                    None) and "country" in location.raw["properties"]:
                self.location["country"] = location.raw["properties"][
                    "country"]
            if not self.location.get(
                    "region", None) and "state" in location.raw["properties"]:
                self.location["region"] = location.raw["properties"]["state"]

            if not self.location.get(
                    "town", None) and "city" in location.raw["properties"]:
                self.location["town"] = location.raw["properties"]["city"]
        except:
            pass
Beispiel #11
0
	def setUp(self):
		self.address = "Sunnersta" #static address to be found

		self.userlocation = (59.8585107,17.6368508)

		self.addrNone = "abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba" #non-existing address
		self.scheme = "https"
		self.plainscheme = "http"
		self.geolocators = []

		#set up for Photon
		self.geolocator10 = Photon()
		self.photondomain = "photon.komoot.de"
		self.photonapi = "https://photon.komoot.de/api"
		self.geolocators.append(self.geolocator10)
def collectGeocoders():
    config = configparser.ConfigParser()
    conf = r'..\conf\config.ini'
    config.read(conf)
    keys = {
        'Here_app_id': config['DEFAULT']['Here_app_id'],
        'Here_app_code': config['DEFAULT']['Here_app_code'],
        'TomTom': config['DEFAULT']['TomTom_api_key'],
        'OpenMapQuest': config['DEFAULT']['OpenMapQuest_api_key'],
        'GoogleV3': config['DEFAULT']['GoogleV3_api_key']
    }
    locators = [{
        'locator': Nominatim(user_agent="afan"),
        'name': 'Nominatim',
        'type': 'Geopy'
    }, {
        'locator': GeoNames(username="******"),
        'name': 'GeoNames',
        'type': 'Geopy'
    }, {
        'locator':
        Here(app_id=keys['Here_app_id'], app_code=keys['Here_app_code']),
        'name':
        'Here',
        'type':
        'Geopy'
    }, {
        'locator': TomTom(api_key=keys['TomTom']),
        'name': 'TomTom',
        'type': 'Geopy'
    }, {
        'locator': OpenMapQuest(api_key=keys['OpenMapQuest']),
        'name': 'OpenMapQuest',
        'type': 'Geopy'
    }, {
        'locator': Photon(),
        'name': 'Photon',
        'type': 'Geopy'
    }]
    #locators.append({'locator':GoogleV3(api_key=keys['GoogleV3']),'name':'GoogleV3','type':'Geopy'})
    locators.append({
        'locator': revGeocodingbyIQ,
        'name': 'revGeocodingbyIQ',
        'type': 'Custom'
    })

    return locators
Beispiel #13
0
# As of July 2018 Google requires each request to have a paid API key.
# https://developers.google.com/maps/documentation/geocoding/usage-and-billing

# $ pip3 install geopy

# from geopy.geocoders import GoogleV3
# from geopy.geocoders import Yandex
# from geopy.geocoders import DataBC
# from geopy.geocoders import Nominatim
from geopy.geocoders import Photon

# geolocator = GoogleV3(scheme='http')
# geolocator = Yandex(lang='en', scheme='http')
# geolocator = DataBC(scheme='http')
# geolocator = Nominatim(scheme='http', user_agent='my-application')
geolocator = Photon(scheme='http')


address1 = '3995 23rd St, San Francisco, CA 94114, USA'
address2 = '3730 Cambie St, Vancouver, BC V5Z2X5, Canada'

location = geolocator.geocode(address1)

print(type(location))
# <class 'geopy.location.Location'>
print(dir(location))
# [... 'address', 'altitude', 'latitude', 'longitude', 'point', 'raw']
print(location)
# 3995 23rd St, San Francisco, CA 94114, USA
print(location.address)
# 3995 23rd St, San Francisco, CA 94114, USA
    def extract_location(self, country, state, city, street, postal_code):
        """
        Creates all possible location-related items with the available information.

        Args:
            register -> WHOISregister object with all the information to extract
        """
        # Doesn't even try to create a location entity if there's not at least a country
        if country:

            area = None
            area_code = None

            try:
                country_code = pycountry.countries.lookup(
                    country.decode("utf-8")).alpha_2.decode("utf-8")
            except:
                country_code = None

            # Geolocation
            geolocator = Geocoder(user_agent="Maltego", timeout=2)

            try:
                if street:
                    search += street + ", "

                if city:
                    search += country + ", "

                search += country

                location = geolocator.geocode(search, exactly_one=True)

            except:
                # Tries it again after sleeping a couple of seconds
                try:
                    sleep(2)
                    search = ""

                    if street:
                        search += street + ", "

                    if city:
                        search += country + ", "

                    search += country

                    location = geolocator.geocode(search, exactly_one=True)

                except:
                    location = None

            if country_code:
                choices = pycountry.subdivisions.get(country_code=country_code)
            else:
                choices = pycountry.subdivisions

            # State or province
            if state:
                found = process.extractOne(state, choices)

                # Discards it if the score is lower than 50
                if found and found[1] > 50:
                    state = found[0]

                    area = state.name.decode("utf-8")
                    area_code = state.code.decode("utf-8")

                    if not country_code:
                        # Gets the country code from the state info
                        country_code = state.country_code.decode("utf-8")
            else:
                area = None
                area_code = None

            # Sanitizes all values before creating the XML
            country = country.decode("utf-8")
            street = street.decode("utf-8") if street else None
            city = city.decode("utf-8") if city else None

            return ents.Location(
                name=(city + ", " + country) if city else country,
                city=city,
                country=country,
                streetaddress=street,
                area=area,
                areacode=area_code,
                countrycode=country_code,
                longitude=location.longitude if location else 0,
                latitude=location.latitude if location else 0)

        return None
import csv
from geopy.geocoders import Photon

geolocator = Photon()

hdr = {}

with open('public/data/master.csv', 'rb') as csvinput:
    with open('public/data/master_latlong.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader = csv.reader(csvinput)

        all = []
        row = reader.next()
        row.append('latitude')
        row.append('longitude')
        all.append(row)

        for row in reader:
            try:
                location = geolocator.geocode(row[2], timeout=10)
                print location.latitude
                print location.longitude
                row.append(location.latitude)
                row.append(location.longitude)
                all.append(row)
            except IndexError:
                print "INDEXRROR HAPPENED"
                continue

        writer.writerows(all)
from geopy.geocoders import Photon
from geopy.point import Point
import csv

coords_by_state = {'ME': [],'VT': [],'NH': [],'NY': [],'MA': [],'CT': [],'RI': [],'PA': [],'NJ': [],'ND':[],'MN': [],'SD': [],'NE': [],'IA': [],'KS': [],'MO': [],'IL': [],'WI': [],'MI': [],'OH': [],'IN': [],'WA': [],'MT': [],'OR': [],'ID': [],'WY': [],'CA': [],'NV': [],'UT': [],'CO': [],'AZ': [],'NM': [],'OK': [],'TX': [],'AR': [],'LA': [],'KY': [],'TN': [],'MS': [],'AL': [],'WV': [],'VA': [],'MD': [],'DE': [],'NC': [],'SC': [],'GA': [],'FL': [],'AK': [],'HI':[] }


        geolocator = Photon()with open('latlong.csv') as csvfile:
    reader = csv.reader(csvfile,delimiter=',')
    for row in reader:
        print(row)

        print(geolocator)
#        p = 
        result = geolocator.reverse(', '.join(row))
        print(result)
        break;
        
#photon.komoot.de/reverse?lon=47.5279679v&lat=-122.1976519
Beispiel #17
0
def get_geodata(location):
    geolocator = Photon(user_agent='measurements')
    geodata = geolocator.geocode(location)
    return geodata
Beispiel #18
0
def create_map(year, locations):
    """
    (int, dict) -> int
    Create a map of films for the specific year and save it to MoviesMap.html
        locations: dict of {location: number of films}
    """
    import folium
    from geopy.geocoders import Photon
    from geopy.exc import GeocoderTimedOut
    import math

    def get_color(num):
        """
        (int) -> str
        Return a color depending on num
        >>> get_color(1)
        '#05fa20
        >>> get_color(1000)
        '#9e6120
        >>> get_color(10000)
        '#ff2020'
        """
        color = '#'
        red = int(num**(1 / 2) * 5)
        if red > 255:
            color += 'ff'
            red = 255
        else:
            color += '0' + hex(red)[2:] if red < 16 else hex(red)[2:]
        green = 255 - red
        color += '20' if green < 16 else hex(green)[2:]
        color += '20'
        return color

    geolocator = Photon()
    fmap = folium.Map(location=[49, 24], zoom_start=5)
    fg_films = folium.FeatureGroup(name='Фільми на %i рік' % year)
    fg_pp = folium.FeatureGroup(name='Населення країн')
    fg_pp.add_child(
        folium.GeoJson(
            data=open('world.json', 'r', encoding='utf-8-sig').read(),
            tooltip=folium.features.GeoJsonTooltip(fields=['NAME', 'POP2005'],
                                                   aliases=['', '']),
            style_function=lambda x: {
                'opacity':
                0,
                'fillColor':
                '#f0f0f0'
                if x['properties']['POP2005'] < 10000000 else '#b0b0b0'
                if x['properties']['POP2005'] < 50000000 else '#808080'
                if x['properties']['POP2005'] < 100000000 else '#404040'
                if x['properties']['POP2005'] < 500000000 else '#101010'
            }))
    i = 0
    errors = 0
    total = len(locations.keys())
    bar = 20
    print("Generating map...")
    for address in locations.keys():
        i += 1
        percent = i / total
        print('\r[' + '▋' * int(percent * bar) + ' ' *
              (bar - int(percent * bar)) + ']' + str(int(percent * 100)) + '%',
              end='')
        try:
            location = geolocator.geocode(address)
            if location is None:
                location = geolocator.geocode(address[address.find(',') + 2:])
            if location is not None:
                lat = location.latitude
                lon = location.longitude
                color = get_color(locations[address])
                fg_films.add_child(
                    folium.CircleMarker(
                        location=[lat, lon],
                        radius=4 + int(math.sqrt(locations[address]) / 3),
                        popup=address +
                        ': \n%i фільм(ів)' % locations[address],
                        color=color,
                        fill_color=color,
                        fill_opacity=0.5,
                        stroke=0))
            else:
                errors += 1
        except GeocoderTimedOut:
            errors += 1
    print('\n %i found, %i errors' % (i - errors, errors))
    fmap.add_tile_layer(name='CartoDB', tiles='CartoDB')
    fmap.add_child(fg_pp)
    fmap.add_child(fg_films)
    fmap.add_child(folium.LayerControl())
    fmap.save('MoviesMap.html')

    return 0
Beispiel #19
0
def calculate_distance_view(request):
    distance = None
    destination = None
    obj = get_object_or_404(Measurement, id=1)
    form = MeasurementModelForm(request.POST or None)
    geolocator = Photon(user_agent='projectapp')

    ip = '103.98.78.198'
    country, city, lat, lon = get_geo(ip)
    #print('location country',country)
    #print('location city',city)
    #print('location lat,lon',lat,lon)

    location = geolocator.geocode(city)
    # print('###', location)

    # location coordinates

    l_lat = lat
    l_lon = lon
    pointA = (l_lat, l_lon)

    # initial folium map
    m = folium.Map(width=1120,
                   height=640,
                   location=get_center_coordinates(l_lat, l_lon),
                   zoom_start=8)

    # location marker

    folium.Marker([l_lat, l_lon],
                  tooltip='click here for more',
                  popup=city['city'],
                  icon=folium.Icon(color='red')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)
        # print(destination)
        d_lat = (destination.latitude)
        d_lon = (destination.longitude)

        pointB = (d_lat, d_lon)

        distance = round(geodesic(pointA, pointB).km, 2)

        # folium map modification

        m = folium.Map(width=1000,
                       height=600,
                       location=get_center_coordinates(l_lat, l_lon, d_lat,
                                                       d_lon),
                       zoom_start=get_zoom(distance))

        # location marker

        folium.Marker([l_lat, l_lon],
                      tooltip='click here for more',
                      popup=city['city'],
                      icon=folium.Icon(color='red')).add_to(m)

        # destination marker

        folium.Marker([d_lat, d_lon],
                      tooltip='click here for more',
                      popup=destination,
                      icon=folium.Icon(color='blue', icon='cloud')).add_to(m)

        # line between location and destination
        line = folium.PolyLine(locations=[pointA, pointB],
                               weight=2,
                               color='blue')
        m.add_child(line)

        instance.location = location
        instance.distance = distance
        instance.save()

    m = m._repr_html_()

    context = {
        'distance': distance,
        'destination': destination,
        'form': form,
        'map': m,
    }

    return render(request, 'measurements/main.html', context)
Beispiel #20
0
def calculate_distance_view(request):
    #initial distance
    distance = None
    destination = None
    obj = get_object_or_404(Measurement, id=1)
    form = MeasurementModelForm(request.POST or None)
    geolocator = Photon(user_agent="measurements")

    ip_ = get_ip_address(request)
    print(ip_)

    ip = "72.14.207.99"
    country, city, lat, lon = get_geo(ip)

    location = geolocator.geocode(city)

    l_lat = lat
    l_lon = lon
    pointA = (l_lat, l_lon)

    #initial folium map
    m = folium.Map(width=750,
                   height=500,
                   location=get_center_coodinates(l_lat, l_lon),
                   zoom_start=8)
    #location maker
    folium.Marker([l_lat, l_lon],
                  tooltip="Click Here For More",
                  popup=city['city'],
                  icon=folium.Icon(color='purple')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)

        #destination cordinate
        d_lat = destination.latitude
        d_lon = destination.longitude

        pointB = (d_lat, d_lon)

        #distance calculations
        distance = round(geodesic(pointA, pointB).km, 2)

        #folium map modification
        m = folium.Map(width=750,
                       height=500,
                       location=get_center_coodinates(l_lat, l_lon, d_lat,
                                                      d_lon),
                       zoom_start=get_zoom(distance))
        #location maker
        folium.Marker([l_lat, l_lon],
                      tooltip="Click Here For More",
                      popup=city['city'],
                      icon=folium.Icon(color='purple')).add_to(m)
        #destination maker
        folium.Marker([d_lat, d_lon],
                      tooltip="Click Here For More",
                      popup=destination,
                      icon=folium.Icon(color='red', icon='cloud')).add_to(m)

        #Draw the line between location and destination
        line = folium.PolyLine(locations=[pointA, pointB],
                               weight=2,
                               color='blue')
        m.add_child(line)

        instance.location = location
        instance.distance = distance
        instance.save()
    m = m._repr_html_()
    context = {
        'distance': distance,
        'destination': destination,
        'form': form,
        'map': m,
    }
    return render(request, 'measurements/main.html', context)
Beispiel #21
0
def calcDistanceView(request):
    distance = None
    destination = None
    form = MeasurementForm(request.POST or None)
    geolocator = Photon(user_agent="measurements")

    # Location Coordinates
    g = geocoder.ip('me')
    lat = g.latlng[0]
    lon = g.latlng[1]
    location = geolocator.reverse(f"{lat}, {lon}")
    pointA = [lat, lon]

    # Initial Folium Map
    m = folium.Map(width='100%',
                   height='100%',
                   location=get_center_coordinates(lat, lon))
    # Location Marker
    folium.Marker([lat, lon],
                  tooltip='Click here for more',
                  popup=location,
                  icon=folium.Icon(color='blue', icon='home')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)

        # destination coordinates
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)
        d_lat = destination.latitude
        d_lon = destination.longitude
        pointB = (d_lat, d_lon)

        # calc distance
        distance = round(geodesic(pointA, pointB).km, 2)  # calc the distance

        # Destination Marker
        m = folium.Map(width='100%',
                       height='100%',
                       location=get_center_coordinates(lat, lon, d_lat, d_lon),
                       zoom_start=get_zoom(distance))
        # Location Marker
        folium.Marker([lat, lon],
                      tooltip='Click here for more',
                      popup=get_center_coordinates(lat, lon),
                      icon=folium.Icon(color='blue', icon='home')).add_to(m)
        folium.Marker([d_lat, d_lon],
                      tooltip='Click here for more',
                      popup=destination,
                      icon=folium.Icon(color='red', icon='cloud')).add_to(m)

        # Draw a line between location and destination
        line = folium.PolyLine(locations=[pointA, pointB],
                               weight=3,
                               color='blue')
        m.add_child(line)  # Append the Line to the Map

        # Location
        instance.location = location
        # Distance
        instance.distance = distance
        instance.save()
    # Map Representation
    m = m._repr_html_()
    context = {
        'distance': distance,
        'destination': destination,
        'form': form,
        'map': m,
    }
    return render(request, 'measurements/main.html', context)
Beispiel #22
0
def calculate_distance_view(request):
    # initial values
    distance = None
    destination = None

    # obj = get_object_or_404(Measurement, id=1)
    form = MeasurementModelForm(request.POST or None)
    geolocator = Photon(user_agent="measurements")

    ip_ = get_ip_address(request)
    print(ip_)
    ip = '14.201.145.52'
    country, city, lat, lon = get_geo(ip)
    # print('location country', country)
    # print('location city', city)
    # print('location lat, lon', lat, lon)

    location = geolocator.geocode(city)
    # print('###', location)

    # location coordinates
    l_lat = lat
    l_lon = lon
    pointA = (l_lat, l_lon)

    # initial folium map
    m = folium.Map(width=800,
                   height=500,
                   location=get_center_coordinates(l_lat, l_lon),
                   zoom_start=8)
    # location marker
    folium.Marker([l_lat, l_lon],
                  tooltip='click here for more',
                  popup=city['city'],
                  icon=folium.Icon(color='purple')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)
        # print(destination)
        # print(destination.latitude)
        # print(destination.longitude)

        # destination coordinates
        d_lat = destination.latitude
        d_lon = destination.longitude

        pointB = (d_lat, d_lon)

        # distance calculation
        distance = round(geodesic(pointA, pointB).km, 2)

        # folium map modification
        m = folium.Map(width=800,
                       height=500,
                       location=get_center_coordinates(l_lat, l_lon, d_lat,
                                                       d_lon),
                       zoom_start=get_zoom(distance))
        # location marker
        folium.Marker([l_lat, l_lon],
                      tooltip='click here for more',
                      popup=city['city'],
                      icon=folium.Icon(color='purple')).add_to(m)
        # destination marker
        folium.Marker([d_lat, d_lon],
                      tooltip='click here for more',
                      popup=destination,
                      icon=folium.Icon(color='red', icon='cloud')).add_to(m)

        # draw the line between location and destination
        line = folium.PolyLine(locations=[pointA, pointB],
                               weight=5,
                               color='blue')
        # adding the line to the map
        m.add_child(line)

        instance.location = location
        instance.distance = distance
        # instance.location = 'San Francisco'
        # instance.distance = 5000.00
        instance.save()

    # To get an HTML representation of the folium map
    m = m._repr_html_()

    context = {
        'distance': distance,
        'destination': destination,
        'form': form,
        'map': m,
    }

    return render(request, 'measurements/main.html', context)
Beispiel #23
0
 def setUpClass(cls):
     cls.geocoder = Photon()
     cls.known_country_de = "Frankreich"
     cls.known_country_fr = "France"
def calculate_distance_view(request):
    # device=False;
    obj = get_object_or_404(Measurement, id=1)
    form = MeasurementModelForm(request.POST or None)
    geolocator = Photon(user_agent='measurements')

    ip = '103.87.141.65'
    country, city, lat, lon = get_geo(ip)
    print('location country', country)
    print('location city', city)

    # print('location lat, lon', lat, lon)

    # just to see the location
    location = geolocator.geocode(city)
    print('### location:', location)

    #Location coordinates
    l_lat = lat
    l_lon = lon
    pointA = (l_lat, l_lon)

    # Inital Folium map
    m = folium.Map(width=8000,
                   height=500,
                   location=get_center_coordinates(l_lat, l_lon),
                   zoom_start=8)

    # Location marker
    folium.Marker([l_lat, l_lon],
                  tooltip='click here for more',
                  popup=city['city'],
                  icon=folium.Icon(color='red')).add_to(m)

    if form.is_valid():
        instance = form.save(commit=False)
        destination_ = form.cleaned_data.get('destination')
        destination = geolocator.geocode(destination_)
        print('destination', destination)

        # Destination coordinates
        d_lat = destination.latitude
        d_lon = destination.longitude
        pointB = (d_lat, d_lon)

        # Distance calculation
        distance = round(geodesic(pointA, pointB).km, 2)

        # Inital Folium map
        m = folium.Map(width=8000,
                       height=500,
                       location=get_center_coordinates(l_lat, l_lon, d_lat,
                                                       d_lon),
                       zoom_start=get_zoom(distance))

        # Location marker
        folium.Marker([l_lat, l_lon],
                      tooltip='click here for more',
                      popup=city['city'],
                      icon=folium.Icon(color='red')).add_to(m)
        # Destination marker
        # if(device):
        folium.Marker([d_lat, d_lon],
                      tooltip='click here for more',
                      popup=destination,
                      icon=folium.Icon(color='green')).add_to(m)
        # else:
        #            folium.Marker([d_lat, d_lon], tooltip='click here for more', popup=destination,
        #             icon=folium.Icon(color='red')).add_to(m)

        # Draw line between destination and location
        #line = folium.PolyLine(locations=[pointA,pointB], weight = 2, color= 'blue')
        #m.add_child(line)

        instance.location = location
        instance.distance = distance
        instance.save()

    m = m._repr_html_()

    context = {
        'distance': obj,
        'form': form,
        'map': m,
    }

    return render(request, 'main.html', context)
def updateRow(row):
    global rId
    vybeRow = {}
    global countriesDict, iterationStatusCode, errorRecordDict
    row["category"] = None
    row['cuisine'] = stripAndSplit(row['cuisine'])
    row['phone'] = stripAndSplit(row['phone'])
    row['__v'] = 0
    row['rating'] = None
    row['tags'] = None
    row['videoLink'] = None
    row['reviewCount'] = None
    row['isClaimed'] = None
    row['faq'] = None
    # row['category'] = None
    row['securityDocumentUrl'] = None
    row['securityDocumentName'] = None
    row['socialMediaHandles'] = {
        "facebook": None,
        "instagram": None,
        "twitter": None,
        "snapchat": None,
        "pinterest": None
    }
    row['hoursType'] = "REGULAR"
    row['isOpenNow'] = True
    row['specialHours'] = [{
        "date": None,
        "start": None,
        "end": None,
        "isOvernight": None
    }]
    row['attributes'] = {
        "businessParking": {
            "garage": None,
            "street": None
        },
        "genderNeutralRestrooms": None,
        "openToAll": None,
        "restaurantsTakeOut": None,
        "wheelChairAccessible": None
    }

    postRestaurantName = row['name']
    postAddress = row['oldAddress']
    postCity = row["area"].split(",")[-1]
    url = "https://qozddvvl55.execute-api.us-east-1.amazonaws.com/Prod/vybe/"
    payload = {"name": postRestaurantName, "address": postAddress}
    headers = {'Content-Type': 'text/plain'}

    for i in range(5):
        if i == 1:
            payload['address'] = postCity
        elif i == 2:
            payload['address'] = postAddress.split(",")[-1]
        elif i == 3:
            params = {
                'name': postRestaurantName,
                "city": postCity,
                'address1': postAddress,
                'state': stateName,
                'country': "US"
            }

            yelpReq = requests.get(yelpUrl, params=params, headers=head)
            try:
                address = " ".join(yelpReq.json()["businesses"][0]["location"]
                                   ["display_address"])
                payload['address'] = address
            except:
                root_logger.warning(
                    f"Yelp can't find the address for {payload['name']}")
                continue
        elif i == 4:
            try:
                geolocator = Photon(user_agent="My-app")
                location = geolocator.geocode(postAddress)
                if location is not None:
                    payload['address'] = location.address
                else:
                    root_logger.warning(
                        f"Photon could not find address for {payload['name']}")
            except Exception as e:
                root_logger.warning(f"Photon failed for {payload['name']}")
                root_logger.exception(e)
        response = requests.request("POST",
                                    url,
                                    headers=headers,
                                    data=json.dumps(payload))
        statusCode = response.status_code
        root_logger.info(
            f"{statusCode} - ({postRestaurantName}) - ({payload['address']}) iteration ({i})"
        )
        if statusCode == 200:
            iterationStatusCode[str(i)] += 1
        if statusCode == 200 or statusCode == 502:
            break

    if statusCode == 200:
        res = response.json().get('message')
        restaurantId = 'RestId' + datetime.now().strftime("%d%m%y%H%M") + \
            str(rId).zfill(3)
        # increase this if want to use threading to execute more than 1000 records at a time
        rId = (rId + 1) % 1000
        hour = parseTime(res.get('timings'))
        website = res.get('website')
        address = parseAddress(res.get('address_broken'))
        coordinates = {
            "coordinates": [
                res.get('coordinates').get("longitude"),
                res.get('coordinates').get("latitude")
            ],
            "type":
            "Point"
        }
        newAddress = res.get('address')
        vybe = parseVybe(res.get('vybe'))

        if vybe:
            minTimeSpent = res.get('timeSpent')[0] if res.get(
                'timeSpent') else None
            maxTimeSpent = res.get('timeSpent')[1] if res.get(
                'timeSpent') else None
            vybeRow.update({
                "restaurantId": restaurantId,
                'vybe': vybe,
                'minTimeSpent': minTimeSpent,
                'maxTimeSpent': maxTimeSpent
            })
            countriesVybeDict.append(vybeRow)
        else:
            root_logger.warning("{} No VybeData ( {} ) ( {} )".format(
                statusCode, postRestaurantName, postAddress))

        row.update({
            "restaurantId": restaurantId,
            "location": coordinates,
            "newAddress": newAddress,
            "hours": hour,
            "website": website,
            "address": address
        })

        countriesDict.append(row)
        #print(countriesDict)

    else:
        hour = None
        website = None
        address = None
        newAddress = None
        address = None
        row.update({
            "newAddress": newAddress,
            "hours": hour,
            "website": website,
            "address": address
        })
        errorRecordDict.append(row)
        root_logger.info(
            "{} Entered restaurant with name ( {} ) and address( {} ) into error file"
            .format(statusCode, postRestaurantName, postAddress))
    return statusCode
def calculate_distance_view(request):

    distance=None
    destination=None
    obj=get_object_or_404(Measurements,id=1)
    form=MeasurementModelForm(request.POST or None)
    ##user_agent = name of app
    geolocator=Photon(user_agent="measurements")

    ip_=get_ip_address(request)
    print(ip_)
    ip="72.14.207.99"
    country,city,lat,lon =get_geo(ip)
    #print("Location country:",country)
    #print("Location city:",city)
    #print("Location latitude and longitude:",lat,lon)

    location=geolocator.geocode(city)
    #print("###",location)

    #location coordinates
    l_lat=lat
    l_lon=lon
    pointA=(l_lat,l_lon)

    #initial folium map
    m=folium.Map(width=1100,height=400,location=get_center_coordinates(l_lat,l_lon),zoom_start=8)

    #location marker
    folium.Marker([l_lat,l_lon],tooltip="Click here for more",popup=city["city"],
                  icon=folium.Icon(color="purple")).add_to(m)

    if form.is_valid():
        instance=form.save(commit=False)
        destination_=form.cleaned_data.get("destination")
        destination=geolocator.geocode(destination_)
        print(destination)
        #destination coordinates
        d_lat=destination.latitude
        d_lon=destination.longitude

        pointB=(d_lat,d_lon)
        #distance calculation
        distance=round(geodesic(pointA,pointB).km,2)

        #folium map modification
        m=folium.Map(width=800,height=400,location=get_center_coordinates(l_lat,l_lon,d_lat,d_lon),
                     zoom_start=get_zoom(distance))

        #location marker
        folium.Marker([l_lat,l_lon],tooltip="Click here for more",popup=city["city"],
                  icon=folium.Icon(color="purple")).add_to(m)
        #destination marker
        folium.Marker([d_lat,d_lon],tooltip="Click here for more",popup=destination,
                  icon=folium.Icon(color="red",icon="cloud")).add_to(m)
        #draw the line between the location and destination
        line=folium.PolyLine(locations=[pointA,pointB],weight=8,color="blue")
        m.add_child(line)

        instance.location=location
        instance.distance=distance
        instance.save()
    else:
        pass
    #converting into html which is later used by using {{map}safe}} in main.html
    m=m._repr_html_()


    context={
        'distance':distance,
        'destination':destination,
        'form':form,
        'map':m,
        }

    return render(request,"measurements/main.html",context)
Beispiel #27
0
 def test_user_agent_custom(self):
     geocoder = Photon(user_agent='my_user_agent/1.0')
     self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
                                    datetime[5:7]+'/'+datetime[8:]
                                print 'datetime', datetime
                            if key == 'Model':
                                model = exif[key].__str__()
                                #print 'model',model
            except IOError:
                print 'can not open and get exif'

            # locate the address from coordinates
            country = ''
            state = ''
            city = ''
            address = ''
            event_id = -1
            if lat != -999 and lon != -999:
                geolocator = Photon()
                #print str(lat)+','+str(lon)
                location = geolocator.reverse(str(lat)+','+str(lon))
                if location != None:
                    address = location.address.encode("utf-8")
                    for key in location.raw['properties']:
                        if key == 'country':
                            country = location.raw['properties'][key].encode(
                                "utf-8")
                        if key == 'state':
                            state = location.raw['properties'][key].encode(
                                "utf-8")
                        if key == 'city':
                            city = location.raw['properties'][key].encode(
                                "utf-8")
Beispiel #29
0
 def setUpClass(cls):
     cls.geocoder = Photon()
     cls.known_country_it = "Francia"
     cls.known_country_fr = "France"
Beispiel #30
0
 def make_geocoder(cls, **kwargs):
     return Photon(**kwargs)
Beispiel #31
0
 async def test_user_agent_custom(self):
     geocoder = Photon(user_agent='my_user_agent/1.0')
     assert geocoder.headers['User-Agent'] == 'my_user_agent/1.0'
def geolocation_calc(address_data, dates_served):
    '''
    Defines the geolocation of address data.
    Used module: geopy-package
    Further Info on rate_limit:
        https://www.fintu.ai/en/working-with-geodata-in-python/

    Parameter:
        address_data: dict. address data with postal code, street name and number
        dates_served: list. Dates of served routes

    Returns:
        address data with geo locations
    '''

    ## Geo location determination
    ## define geopy geocoder functions
    geolocator = Photon(user_agent="*****@*****.**")

    @sleep_and_retry
    @limits(1, 1)
    def rate_limited_geocode(query):
        try:
            coordinates = geolocator.geocode(query, timeout=60)

        except GeocoderTimedOut:  # as e:
            print("Error: geocode failed on input %s with message %s")

        return coordinates

    def geocode(data, row):
        # Formulates query: address, Number, postal code
        query = data.iloc[row][1] + "," + data.iloc[row][0].split()[0]
        # Compute gelocation with rate limited
        geocode = rate_limited_geocode(query)

        return geocode

    # Define dict to store geolocation data
    geolocation_data_dict_main = {}

    for i in range(0, len(dates_served)):
        print('geo location calculation:', dates_served[i])
        #Load each day tour
        data_filtered = address_data[dates_served[i]]
        #Prepare address data for geo location and distance calc
        data = data_filtered['address'].str.split(',', expand=True)

        ## calculate coordinates for given address data
        geolocation_data_dict = {}
        lat = []
        lng = []
        address = []

        for j in range(0, len(data)):
            #geo location calculation
            location = geocode(data, j)
            #check if geo location can be calculated
            if location != None:
                address.append(data.iloc[j][1] + ',' +
                               data.iloc[j][0].split()[0])
                lat.append(location.latitude)
                lng.append(location.longitude)
            else:
                print("No geocode found for",
                      data.iloc[j][1] + ',' + data.iloc[j][0].split()[0])
                address.append(data.iloc[j][1] + ',' +
                               data.iloc[j][0].split()[0])
                lat.append('No loc')
                lng.append('No loc')

        #save address plus added geo-data to pandas dataframe/dict
        address_geo_data = pd.DataFrame({'address':address,
                                         'lat':lat,
                                         'lng':lng}, \
                                         columns = ['address','lat','lng'])
        geolocation_data_dict[dates_served[i]] = address_geo_data

        ## Update main address_data_dict with all address data processed
        geolocation_data_dict_main.update(geolocation_data_dict)

    return geolocation_data_dict_main