示例#1
0
文件: gpx.py 项目: novoid/Memacs
    def reverse_geocode(self, lat, lng):
        """get address for latitude/longitude"""

        if 'google' in self._args.provider:
            geocode = geocoder.google([lat, lng], method='reverse')

        elif 'osm' in self._args.provider:
            if not self._args.url:
                geocode = geocoder.osm([lat, lng], method='reverse')
                time.sleep(1)  # Nominatim Usage Policy
            else:
                if 'localhost' in self._args.url:
                    geocode = geocoder.osm([lat, lng], method='reverse', url='http://localhost/nominatim/search')
                else:
                    geocode = geocoder.osm([lat, lng], method='reverse', url=self._args.url)

        else:
            self._parser.error("invalid provider given")
            raise ValueError('invalid provider given')

        if not geocode.ok:
            logging.error("geocoding failed or api limit exceeded")
            raise RuntimeError('geocoding failed or api limit exceeded')
        else:
            logging.debug(geocode.json)
            return geocode.json
def calc_address(address, when, output=CITIES_FILENAME):
    setup_output(output)

    logger.info('Querying the server for: "%s" ...', address)
    response = geocoder.osm(address)
    logger.info('Got an answer!')

    if not is_osmurl_valid(response):
        osmurl_invalid()
        return

    logger.info('Loading old cities from: %s', output)
    cities = load_json(output)

    osm_id = response.json['osm_id']
    last_osm_id = None
    if cities[when]:
        last_osm_id = cities[when][-1]['osm_id']

    if not cities[when] or osm_id != last_osm_id:
        logger.info('Adding new city: "%s"', response.address)
        cities[when].append(response.json)
        save_json(cities, output)
    else:
        logger.info('This city is already saved. Excluding...')

    return response
示例#3
0
def getPoints(csvFile, fieldNameAddress, delimiter=',', quoting='"', decodeTo='utf8'):
	g = []
	i = 0 
	with open(csvFile) as csvfile:
		reader = csv.DictReader(csvfile)
		for row in reader:
			direc = row[fieldNameAddress]
			direc = direc.decode(decodeTo)
			tmp_g = geocoder.osm(direc)
			listRow = []
			for key in row:
				listRow.append(row[key])

			try:
				listRow.append(tmp_g.x)
				listRow.append(tmp_g.y)
			except:
				listRow.append("err")
				listRow.append("err")
			
			#print(tmp_g.x)
			#print(tmp_g.y)
			g.append(listRow)
			i = i + 1
			print(i)
			print(listRow)
					
	return g
示例#4
0
 def lookupPosition(self):
     #g = geocoder.osm('Deutschland, 21379, Scharnebeck, Hülsenberg 6')
     print self.toString()
     g = geocoder.osm(self.toString())
     if (g.x == None)or(g.y == None):
         raise NameError("can't get lon/lat -> check your internet connection")
     return Position(g.y, g.x)
示例#5
0
def test_multi_results():
    g = geocoder.osm(location, maxRows='5')
    assert len(g) == 5

    expected_results = [
        'Ottawa, Ontario, Canada',
        'Ontario, Ottawa County, Oklahoma, United States of America',
    ]
    assert [result.address for result in g][:2] == expected_results
def calc_my_position_address(address, output, upload=True):
    logger.info('Querying the server about "%s"...', address)
    response = geocoder.osm(address)
    logger.info('LatLng: %s', response.latlng)
    logger.info('Place: %s', response.address)

    save_json(response.latlng, output)

    if upload:
        upload_my_position()
示例#7
0
def get_addresses(verbose=False):
    with pymongo.connection.Connection(MONGO_HOST, MONGO_PORT) as conn:
        coll = conn['sbcatalog']['supplier']
        suppliers_in = coll.find()

    suppliers_out = []
    for s in suppliers_in:
        success = False
        x = {}
        x['name'] = s['name']
        a = s['address']
        # converting address to string
        address = ""
        for k in ['locality', 'zipcode', 'street']:
            if a[k]:
                address += a[k] + ' '
        address = address[:-1]
        if verbose:
            print('"' + address + '"', end=' ')
        # trying with full address
        result = geocoder.osm(address).json
        if result['status'] == 'OK' and result['country'] == 'Italia':
            success = True
        # trying with only city
        elif a['locality']:
            if verbose:
                print('NOT FOUND!  ' + '"' + a['locality'] + '"', end=' ')
            result = geocoder.osm(a['locality']).json
            success = result['status'] == 'OK' and result['country'] == 'Italia'

        if success:
            if verbose:
                print('OK!')
            x['coords'] = result['geometry']['coordinates']
            x['address'] = address
            webSite = s['contacts']['contact']['primary']['webSite']
            if webSite:
                x['webSite'] = webSite
            suppliers_out.append(x)
    return suppliers_out
示例#8
0
def main():
    import argparse
    import sys
    
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('address', metavar='text', nargs='+',
                   help='an address to lookup (use "" when it\'s a sentence / more than one word)')
    args = parser.parse_args()

    location = geocoder.osm(args.address)
    
    sys.stdout.write(bcolors.OKBLUE + str(location.lat) + ", " + str(location.lng) + bcolors.ENDC)
    sys.stdout.write("\n")
示例#9
0
    def geocode(self, appartement):
        """Retrieve the lat and long using different fallback services"""
        g = geocoder.google(appartement["address"])
        if not g.latlng:
            g = geocoder.osm(appartement["address"])
            if not g.latlng and MAPQUEST_API_KEY:
                g = geocoder.mapquest(appartement["address"], key=MAPQUEST_API_KEY)

        # if we can't geocode the address, we return the map center
        if g.latlng:
            return g.latlng
        else:
            return MAP_LATLNG
示例#10
0
def getgeo_osm(ip_list):
    count=1
    latlng_set=[]
    for ip in ip_list:
        geoinfo = geocoder.osm(ip)
        print("[osm] {} ({}/{}) country: {}".format(ip, count, len(ip_list), geoinfo.json.get('country')))
        if geoinfo.latlng:
            latlng_set.append([ip, geoinfo.latlng[0], geoinfo.latlng[1]])
        else:
            print("{} not found".format(ip))
            latlng_set.append([ip, "","" ])
        count = count + 1
    #print(latlng_set)
    return latlng_set 
示例#11
0
    def geocode(self, address):
        """Retrieve the lat and long using different fallback services"""
        if GEOLOCALISE is False:
            return MAP_LATLNG

        g = geocoder.google(address)
        if not g.latlng:
            g = geocoder.osm(address)
            if not g.latlng and MAPQUEST_API_KEY:
                g = geocoder.mapquest(address, key=MAPQUEST_API_KEY)

        # if we can't geocode the address, we return the map center
        if g.latlng:
            return g.latlng
        else:
            return MAP_LATLNG
示例#12
0
async def ip_address(ip: str):
    """Returns address and geographical coordinates of an IP."""
    try:
        geolocator = geocoder.ip(ip)
        coord = geolocator.json['raw']['loc'].split(",")
        lat, lon = coord[0], coord[1]
        geolocator = geocoder.osm([lat, lon], method='reverse')
        address = geolocator.json['raw']['display_name']
        data = {
            "source": "OSM",
            "longitude": lat,
            "latitude": lon,
            "address": address
        }
        return ResponseModel(data, "success")
    except Exception:
        return ErrorResponseModel(400, "Invalid IP_Address input.")
示例#13
0
def get_fac(numadr):
    while True:
        adresses = [
            'wrocław', 'wałbrzych', 'Legnica', 'bolesławiec',
            'ząbkowice śląskie '
        ]
        adress = adresses[numadr]
        adress = geocoder.osm(adress)
        if not adress:
            print("Nie mogę znaleźć podanego adresu. Spróbuj jeszcze raz")
            continue
        else:
            break
    fac = adress.latlng
    fac.append(rn.randint(20, 40) * 100)
    fac.append(rn.randint(65000, 80000))
    return fac
示例#14
0
def get_lat_lon(place_name):

    # get geocode postion
    g = geocoder.osm(place_name)

    if g.ok == True:
        lat = g.json["lat"]
        lon = g.json["lng"]

    else:
        lat = 0.0
        lon = 0.0
        print("No LatLon")
        self.is_error[0] = 1
        self.error_info = self.error_info + " " + 'Place ' + place_name + ' not found'

    return (str(lat), str(lon))
示例#15
0
    def process(self, address):
        fix_by_osm = True
        """check for address stuff like address numbers from-to"""
        if "-" in address:
            print "there is - in address, preprocessing"
            address = self.preProcess(address)
        self._df_add = getStandardAddress(address)
        engine = create_engine("postgresql://postgres:@128.31.25.188:5432/")
        self.processed_df = processGeocodeCall(self._df_add, engine)
        try:
            print "try: findin postgis"
            self._df_add = getStandardAddress(address)
            engine = create_engine(
                "postgresql://postgres:@128.31.25.188:5432/")
            self.processed_df = processGeocodeCall(self._df_add, engine)
        except:
            if fix_by_osm:
                print "except:fix by osm"
                address_formated = geocoder.osm("{0}".format(address))
                address = "{0} {1}, {2} {3} {4}".format(
                    address_formated.json["housenumber"],
                    address_formated.json["street"],
                    address_formated.json["city"],
                    address_formated.json["state"],
                    address_formated.json["postal"])

            else:
                print "exept: fix by bing"
                address_formated = geocoder.bing(
                    "{0}".format(address),
                    key=
                    "ApdsLDh8yd-ma4xCpdY-qbmYvUl7D9br_qvXYiYzXDz1PQqS_2khXrc5gYAJt88y"
                )
                print "address json"
                address = address_formated.json["address"]
#            print address
            self._df_add = getStandardAddress(address)
            engine = create_engine(
                "postgresql://postgres:@128.31.25.188:5432/")
            self.processed_df = processGeocodeCall(self._df_add, engine)
        else:
            print "else"
        finally:
            return_df = self.prepare_results(self.processed_df)
        return return_df
示例#16
0
def search(request):
    qs = Post.objects.all()
    location_query = request.GET.get('location')
    details_query = request.GET.get('details')
    user_query = request.GET.get('user')
    days_query = request.GET.get('days')
    people_query = request.GET.get('people')
    date_query = request.GET.get('date')
    gender_query = request.GET.get('gender')

    if location_query != '' and location_query is not None:
        qs = qs.filter(location__icontains=location_query)

    elif details_query != '' and details_query is not None:
        qs = qs.filter(detail__icontains=details_query)

    elif user_query != '' and user_query is not None:
        qs = qs.filter(author__icontains=user_query)

    elif days_query != '' and days_query is not None:
        qs = qs.filter(no_days__icontains=days_query)

    elif people_query != '' and people_query is not None:
        qs = qs.filter(no_people__icontains=people_query)

    elif date_query != '' and date_query is not None:
        qs = qs.filter(tour_date__icontains=date_query)

    elif gender_query != '' and gender_query is not None:
        qs = qs.filter(Gender_prefer__icontains=gender_query)
    ##########################################################
    address = 'Kathmandu'
    location = geocoder.osm(address)
    lat = location.lat
    lng = location.lng
    country = location.country
    m = folium.Map(height=500, location=[28, 84], zoom_start=7)
    folium.Marker([lat, lng], popup=country).add_to(m)
    m = m._repr_html_()
    ##########################################################
    context = {
        'qs': qs,
        'm': m,
    }
    return render(request, 'posts/search.html', context)
示例#17
0
    def _geocode_addresses(self,
                           addresses: List[str] = None,
                           bing_key: str = None) -> List[Tuple[float, float]]:
        """
        Helper function to geocode a list of addresses

        Parameters
        ----------
        addresses: list
            list of all street addresses to be geocoded into latitude and longitude format
        bing_geocoder: bool
            If True, Bing's gecoder will be used, else OSM's geocoder is used to geocode street addresses
        Returns
        -------
        coordinates: list
            list of all geocoded street addresses
        """

        coordinates = []
        counter = 0
        for i in range(len(addresses)):

            counter += 1
            print(
                f"Geocode address {addresses[i]} at {counter}/{len(addresses)}"
            )
            # Apply some sleep to ensure to be below 50 requests per second
            time.sleep(0.1)
            address = addresses[i]

            if bing_key is not None:
                g = geocoder.bing(address, key=bing_key)

            else:
                g = geocoder.osm(address)

            if g.status == "OK":
                coords = g.latlng
                coordinates.append(coords)

            else:
                print("status: {}".format(g.status))
                coordinates.append(",")

        return coordinates
示例#18
0
    def show_map(self, name):
        """Display the map for a location"""
        g = geocoder.osm(name)
        bbox = g.json["bbox"]
        bbox = bbox["northeast"] + bbox["southwest"]
        map_ = smopy.Map(bbox, zoom=11)
        logging.debug("Bounding box: %s", map_.box)
        ax = map_.show_mpl(figsize=(10, 6))
        xs = []
        ys = []

        # This show all stops on the map, also not the ones
        # used for processing, but heck . . .
        lat_ne, lon_ne, lat_sw, lon_sw = bbox
        stops = (self.session.query(Stop).filter(
            Stop.stop_lat <= lat_ne,
            Stop.stop_lat >= lat_sw,
            Stop.stop_lon <= lon_ne,
            Stop.stop_lon >= lon_sw,
        ).all())

        for stop in stops:
            x, y = map_.to_pixels(stop.stop_lat, stop.stop_lon)
            xs.append(x)
            ys.append(y)

        plt.ion()
        plt.show()
        ax.plot(xs, ys, ".c")

        # Plot the tracks in blue
        for trip in self.trips:
            shape = trip.shape
            xys = [
                map_.to_pixels(element.shape_pt_lat, element.shape_pt_lon)
                for element in shape
            ]

            xs = [elem[0] for elem in xys]
            ys = [elem[1] for elem in xys]
            ax.plot(xs, ys, "-b")

        plt.pause(0.001)

        return map_, ax
示例#19
0
    def checkgeo(self):

        self._status = False
        str1 = self.toString()
        g = geocoder.osm(str1)

        if g.ok:
            try:
                self._status = True
                self._prediction = g.importance
            except Exception as e:
                print(e)

        else:
            print('No se ha podido encontrar nada')
            print(g)

        return self._status
def geocode(name):
    url = 'https://maps.googleapis.com/maps/api/geocode/json'
    params = {
        'sensor': 'false',
        'address': name,
        "key": "AIzaSyASk8Wo4ie4CZ4ZjpRRSW0Zq_OZJKP4Xfs"
    }
    r = requests.get(url, params=params)
    try:
        results = r.json()['results']
        location = results[0]['geometry']['location']
        name = results[0]["formatted_address"]
        return (name, (location['lat'], location['lng']))
    except:
        # fallback to open street map if we run out of tokens or something
        g = geocoder.osm(name)
        latlng = g.latlng
        return (name, latlng)
示例#21
0
    def showLocation(self, selected_ticker_comp: dict) -> None:
        with st.spinner('Now Loading...'):
            self.all_location = pd.DataFrame()

            for comp, ticker in selected_ticker_comp.items():
                self.stock_info = yf.Ticker(ticker)

                self.location = self.stock_info.info["address1"]
                self.ret = geocoder.osm(self.location, timeout=1.0)
                self.location = pd.DataFrame(self.ret.latlng)
                self.location = self.location.T.set_axis(['lat', 'lon'],
                                                         axis=1)
                self.all_location = pd.concat(
                    [self.all_location, self.location])

            if not self.all_location.empty:
                st.write("Location Of The Selected Conmanies")
                st.map(self.all_location)
示例#22
0
def mk_json(lang, pattern, place):
    g = geocoder.osm(place)
    #print('g=',g.json)
    tweet = {
        'text': pattern.replace('$place', place),
        'lang': lang,
        'geo': {
            'type': 'Point',
            'coordinates': [g.latlng[0], g.latlng[1]],
        },
        'place': {
            'place_type': 'city',
            'country_code': 'US',
            'full_name': place,
        },
        'timestamp_ms': 1508573111095
    }
    return json.dumps(tweet)
示例#23
0
def coords(city_name, country=', Sweden'):
    """
    Returns coordinates according the city name and country. Sweden default
    Here we trying to get geo coordinates of a city by it's name. Sweden is default as project
    from where this code is taken , targeting Swedish second hand sail-boat market.
    Then we store coords in Redis and when we need to get coords for the same place next time -
    we get it from Redis without requesting OSM(open street maps) geo data provider again.
    Most likely that place will not change its coords often enough to not keep it coords in cache.
    """
    cache_key = 'coordinates+%s%s' % (city_name, country)
    coords_from_cache = cache.get(cache_key)
    if not coords_from_cache:
        coord_from_osm = geocoder.osm(city_name + country).latlng
        if coord_from_osm:
            cache.set(cache_key, coord_from_osm, 60*60*24*30)
        return coord_from_osm
    else:
        return coords_from_cache
示例#24
0
def loader(todo):
    with open('location_geodata.csv', 'a+') as f:
        i = 0
        j = 0
        todo_size = len(todo)
        for loc in todo:
            time.sleep(5)
            g = geocoder.osm(loc).latlng
            if g:
                loc = loc.replace('"', '""')
                f.write(f'\n"{loc}",{str(g[0])},{str(g[1])}')
                f.flush()
                i += 1
                print(i)
            else:
                print(f"Failed to load coordinates for address {loc}")
            j += 1
            print(f"{todo_size - j} of {todo_size} left")
示例#25
0
def closest_stations(address):
    # return "Khreshatik"
    time_to_sleep = 3
    # sleep(6)
    g = geocoder.osm('Ukraine, Kyiv,' + address)
    if g.lat is None:
        return "Khreshatik"
    print('finded location')
    print(g.lat, g.lng)
    distances = [{
        'station': metro,
        'distance': dist((g.lat, g.lng), coords[metro])
    } for metro in coords]
    distances = sorted(distances, key=lambda el: el['distance'])
    rez = distances[0]['station'] + ' ' + distances[1][
        'station'] + ' ' + distances[2]['station']

    return rez
示例#26
0
文件: main.py 项目: jarradh/nurture
    def insert(self, value):
        ''' A new thought to be inserted, parses thought input and extracts hashtags, people, reminder and location '''
        with shelve.open(self.dbf, writeback=True) as db:
            thought = value

            # Get Location
            location = None
            if ';' in value:
                thought, _, location = value.rpartition(';')
                location = location.strip()
                g = geocoder.google(location)
                if g.latlng is None:
                    print('Trying OSM')
                    g = geocoder.osm(location)
                location = (location, g.latlng)

            # Prase Recurring event
            r = RecurringEvent() # read more here https://github.com/kvh/recurrent
            print(r.parse(thought))

            # Colour Hashtags
            hashtags = {tag for tag in thought.split() if tag.startswith("#")}
            thought = self.highlight_terms(thought, hashtags, 'ffdc00')
            # Colour People
            people = {tag for tag in thought.split() if tag.startswith("@")}
            thought = self.highlight_terms(thought, people, '7fdbff')

            # Create People who don't exist
            for person in people:
                if person not in db:
                    db[person] = Person(person)
                    
            # Cleanup
            value = thought
            if location:
                value += '; ' + location

            # Save Thought to DB
            db['known_terms'] = list(set(db.get('known_terms', []) + list(hashtags) + list(people)))
            t = Thought(value, hashtags, people, r, location)
            db['thoughts'] = db.get('thoughts', [])
            db['thoughts'].insert(0, t)
            # Insert into UI
            self.rv.data.insert(0, {'value': value or 'empty thought'})
def get_health():
    """
    :return: dictionary with geocoder service health. ::see:: reporter.health.
    """
    try:
        g = geocoder.osm("New York city", maxRows=1)
    except (requests.exceptions.RequestException, Exception) as e:
        # geocoder docs say exception will be raised to the caller
        time = datetime.now().isoformat()
        output = "{}".format(e)
        res = {'status': 'fail', 'time': time, 'output': output}
        return res
    else:
        if g.ok:
            return {'status': 'pass'}

        time = datetime.now().isoformat()
        res = {'status': 'fail', 'time': time, 'output': g.status}
        return res
示例#28
0
def get_hotel(mydict,city):
    loc2 = geocoder.osm(city)

    # map
    main_map = folium.Map(location=[loc2.lat, loc2.lng], zoom_start=13)
    folium.raster_layers.TileLayer('Open Street Map').add_to(main_map)

    # loop through dict
    for i in range (1,len(mydict)+1):
        folium.Marker(location=list(mydict[i].values())[i-1]
                      ,popup=list(mydict[i].keys())[i-1],tooltip=str(list(mydict[i].keys())[i-1]),
                     icon=plugins.BeautifyIcon(number=i,
                                               icon='bus',
                                            border_color='blue',
                                            border_width=0.5,
                                            text_color='red',
                                            inner_icon_style='margin-top:0px;')).add_to(main_map)
    main_map.save('templates/map.html')
    return render_template('index.html')
示例#29
0
    def _get_timezone_from_builtins(self, locale):
        try:
            # This handles common city names, like "Dallas" or "Paris"
            # first get the lat / long.
            g = geocoder.osm(locale)

            # now look it up
            tf = TimezoneFinder()
            timezone = tf.timezone_at(lng=g.lng, lat=g.lat)
            return pytz.timezone(timezone)
        except Exception:
            pass

        try:
            # This handles codes like "America/Los_Angeles"
            return pytz.timezone(locale)
        except Exception:
            pass
        return None
示例#30
0
    def handle(self):

        if "WHAT TIME IS IT IN" in transcript:
            location = transcript[19:]
            geo = geocoder.osm(location)
            tzf = TimezoneFinder()
            timezone = tzf.timezone_at(lng=geo.lng, lat=geo.lat)
            datetime = datetime.now(timezone)
            mic.say("THE TIME IN {} IS ".format(transcript[19:]) +
                    datetime.strftime("%H %M"))
        elif "WHAT TIME IS IT" in transcript:
            t = time.localtime()
            current_time = time.strftime("%H %M", t)
            mic.say("IT IS {}".format(current_time))

        if "WHAT IS THE DATE" in transcript:
            today = date.today()
            date = today.strftime("%B %d, %Y")
            mic.say("TODAY IS {}".format(date))

        if "TELL ME THE DAY OF THE WEEK" in transcript:
            today = date.today()
            mic.say("TODAY IS {}".format(calendar.day_name[today.weekday()]))

        if "WHAT DAY IS" in transcript:
            holiday = transcript[12:]
            year = day.year
            for date in sorted(
                    holidays.US(years=year).get_named(holiday).items()):
                mic.say(str(date[0]))
                break
            else:
                mic.say("ERROR FINDING HOLIDAY")

        if "WHEN IS" in transcript:
            holiday = transcript[8:]
            year = day.year
            for date in sorted(
                    holidays.US(years=year).get_named(holiday).items()):
                mic.say(str(date[0]))
                break
            else:
                mic.say("ERROR FINDING HOLIDAY")
def get_lat_lng(location):

    if len(location) > 0:
        try:
            g = geocoder.osm(location)
            debug.info("location is: " + location + " " + str(g.latlng))
        except Exception as e:
            debug.error(
                "Unable to find {} with Open Street Map, falling back to IP lookup for location.  Error: {}"
                .format(location, e))
            g = geocoder.ip('me')
            debug.info("location is: " + g.city + "," + g.country + " " +
                       str(g.latlng))
    else:
        g = geocoder.ip('me')
        debug.info("location is: " + g.city + "," + g.country + " " +
                   str(g.latlng))

    return g.latlng
示例#32
0
def geolocate(address):
    data = {}
    location_data = geocoder.osm(address)
    if location_data.ok:
        location_data = location_data.json
        data["raw"] = location_data
        data["country"] = location_data.get("country")
        data["country_code"] = location_data.get("country_code")
        data["region"] = location_data.get("region")
        data["address"] = location_data.get("address")
        data["state"] = location_data.get("state")
        data["confidence"] = location_data.get("confidence")
        data["lat"] = location_data.get("lat")
        data["lon"] = location_data.get("lng")
        data["city"] = location_data.get("city")

        data["postal"] = location_data.get("postal")
        data["timezone"] = location_data.get("timezone_short")
    return data
示例#33
0
def get_coordinates(addresses, boroughs):
    """
    Tries to get the coordinates for the given stations.
    :param addresses: the list of location
    :param boroughs: the list of boroughs
    :return:
    """
    latitude = []
    longitude = []
    for address, borough in zip(addresses, boroughs):
        try:
            g = geocoder.osm('{}, {}, New York'.format(address, borough)).json
            latitude.append(g['lat'])
            longitude.append(g['lng'])
        except:
            latitude.append(None)
            longitude.append(None)

    return np.array(latitude).T, np.array(longitude).T
示例#34
0
    def geocode_osm(self, address):
        """
        Geocode address by OpenStreetMap
        """
        try:
            # query the time
            current_time = dt.now()

            # one request per second
            if self.time_difference(current_time) < 1:
                time.sleep(1)

            # geocode address
            coder_osm = geocoder.osm(address, maxRows=1)

            # set new request time
            self.time = dt.now()

            if coder_osm.ok:

                print("Extract coordinates")
                json = coder_osm.geojson['features'][0]['properties']

                # extract information
                accuracy = json['accuracy']
                lat = json['lat']
                lng = json['lng']

                # convert wgs to etrs
                x_trans, y_trans = self.wgs2etrs(lng, lat)

                return self.return_data(accuracy=accuracy,
                                        x=x_trans,
                                        y=y_trans,
                                        source="OSM")
            else:
                print("Coder not ok: ", address)
                return self.return_data()

        except ConnectionError as e:
            print("Geocoding ConnectionError: ", e)
            return self.return_data()
示例#35
0
def split_latlon(latlon, address):
    if latlon == 'None' and address != None:
        ret = geocoder.osm(address, timeout=5.0)
        latlon_list = ret.latlng
        if latlon_list == None:
            latlon_list = [0.0, 0.0]

    #もしdmsならdegに変換
    if '緯' in latlon:
        latlon = latlon.replace('北緯', 'l')
        latlon = latlon.replace('東経', ' l')
        latlon = latlon.replace('度', 'd')
        latlon = latlon.replace('分', 'm')
        latlon = latlon.replace('秒', 's')
        latlon_list = dms_to_deg(latlon)

    if len(latlon_list) == 2:
        return [float(latlon_list[0]), float(latlon_list[1])]
    else:
        return 0.0, 0.0
示例#36
0
    def __pv_system_yield(self, plz, capacity):
        geocoder_query = '{}, Germany'.format(plz)
        try:
            state_full = geocoder.osm(geocoder_query).osm['addr:state']
            pv_yield = PvYield.objects.get(state_full=state_full)
        except:
            return {
                'error_message': 'The postcode you entered does not exist.'
            }

        try:
            capacity_as_int = int(capacity)
        except:
            return {
                'error_message':
                'The capacity value you entered is not a number.'
            }

        system_yield = capacity_as_int * pv_yield.spec_yield
        return {'yield': system_yield, 'state': pv_yield.state}
示例#37
0
def get_coords_by_address(addr_str):
    """
    Returns coordinates (lat/lon) of the point with a given address (OSM geocoder is used)
    Parameters:
        - addr_str as (str): address of the point 
    Returns:
        - coords as (tuple): the point's coordinates (lat/lon)
        - None if the address wasn't recognized
    """

    gcd = geocoder.osm(addr_str)
    location = gcd.latlng

    if location is None:
        logger.error('Address was not recognized...')
        return None
    else:
        coords = tuple(location)

    return coords
示例#38
0
def place_search(query):
	try:
		search = geocoder.osm(query)[0].json
		p = model.Place()
		p.name = query.strip()
		p.lat = search['lat']
		p.lon = search['lng']
		osm_id = str(search['osm_id'])
		if search['osm_type'] == 'way':
			osm_id = 'W' + osm_id
		elif search['osm_type'] == 'node':
			osm_id = 'N' + osm_id
		elif search['osm_type'] == 'relation':
			osm_id = 'R' + osm_id
		else:
			print('Unrecognized OSM type', search['osm_type'])
		p.osm_id = osm_id
		return p
	except IndexError:
		return None # no results
示例#39
0
    def event():
        #print('you have clicked on button')
        ln = tln.get()
        ln1 = tln1.get()
        ln2 = tln2.get()
        
        ln4 = tln4.get()
        ln5 = tln5.get()
        ln6 = tln6.get()
        ln7 = tln7.get()
        ln8= tln8.get()
        ln9 = tln9.get()
        ln10= tln10.get()
    
        ln_add=ln2+" "+ln4+" "+ln5+" "+ln6
        Gender={'Female':0,'Male':1,'Other':2}
        ln=Gender[ln]
        Weapon={'Other':0,'Nothing':1,'Knife':2,'Gun':3}
        ln7=Weapon[ln7]
        Theft={'Other':0,'Money':1,'Mobile':2,'Jewellery':3,'Bike':4,'Car':5}
        ln9=Theft[ln9]
        ToC={'Other':0,'Snaching':1,'Burglary':2,'Robbery':3,'Assault':4,'Extortion':5}
        ln10=ToC[ln10]
    
       
        import geocoder
        import geopy
        from vega_datasets import data as vds

        address = geocoder.osm(ln_add)
        #print(address.json)
        address_latlng = [address.lat, address.lng]   
    
        fullname1 = [ln,ln1,address.lat, address.lng,ln7,ln8,ln9,ln10]
        fullname=[]
        for i in fullname1: 
            fullname.append(i)
        msg.configure(text="you have clicked on button :")
        pt.grid_remove()
        inputbyuser(fullname)
        return fullname
def test_geocoder(address, method_name='geocoder'):
    """ geocoder
            - can use various providers (Google, OSM, etc.) https://geocoder.readthedocs.org/
            - can output GeoJSON
    """

    #g_geocoder = geocoder.google(address)
    g_geocoder = geocoder.osm(address)

    if g_geocoder.latlng == []:
        g_geocoder = geocoder.google(address)

    if g_geocoder.latlng == []:
        g_geocoder = geocoder.arcgis(address)

    if g_geocoder.latlng == []:
        g_geocoder = geocoder.yahoo(address)

    (latitude, longitude) = g_geocoder.latlng
    print ('{0}\t{1} {2}'.format(method_name, latitude, longitude))
    print(pprint(g_geocoder.geojson))
示例#41
0
def top_trends(place="WorldWide"):

    # Trends for Specific Country
    g = geocoder.osm(
        place)  # getting object that has location's latitude and longitude

    closest_loc = api.trends_closest(g.lat, g.lng)
    # print(closest_loc[0])
    # trends = api.trends_place(closest_loc[0]["woeid"])
    trends = api.trends_place(1)

    for ind, value in enumerate(trends[0]["trends"]):

        if value["tweet_volume"] == None:
            trends[0]["trends"][ind]["tweet_volume"] = 0

    # writing a JSON file that has the latest trends for that location
    with open("twitter_top_trends.json", "w") as wp:
        wp.write(json.dumps(trends, indent=1))

    return True
def search_around_place():
    distance = request.args.get('distance', 500)
    city = request.args.get('city', 'arlington')

    usr_g_json = geocoder.osm(city).json
    trgt_coords = (usr_g_json['lat'], usr_g_json['lng'])

    db2conn = ibm_db.connect(db2cred['ssldsn'], "", "")
    if db2conn:
        sql = "SELECT * FROM EARTHQUAKE"
        stmt = ibm_db.exec_immediate(db2conn, sql)
        rows = []
        result = ibm_db.fetch_assoc(stmt)
        while result != False:
            curr_coords = (result['LATITUDE'], result['LONGTITUDE'])
            if geopy.distance.vincenty(curr_coords, trgt_coords).km < distance:
                rows.append(result.copy())
            result = ibm_db.fetch_assoc(stmt)

        ibm_db.close(db2conn)
    return render_template('search_around_place.html', ci=rows)
示例#43
0
def geocode(location, provider='google', display_map=False):

    # Try to find the location 10 times before raising the warning and returning coordinates (0.0, 0.0)
    i = 0
    while True:
        if provider == 'google':
            geo = geocoder.google(location)
        elif provider == 'nokia':
            geo = geocoder.nokia(location)
        elif provider == 'osm':
            geo = geocoder.osm(location)
        elif provider == 'bing':
            geo = geocoder.bing(location)
        elif provider == 'tomtom':
            geo = geocoder.tomtom(location)

        if geo.json['status'] == 'OK' or i == 10:
            break
        i+=1

    #print geo.json

    if display_map == True:
        #Check if address is a coordinate pair
        if location.replace(',', '').replace('.', '').replace('-', '').isdigit():
            url = "http://maps.google.com/?q=%s" % geo.address
        else:
            url = "http://www.google.com/maps/place/%s,%s" % (geo.lat, geo.lng)
        webbrowser.open(url)

    if geo.json['status'] == 'OK':
        #time.sleep(0.5) # Try to avoid the rate limit
        return geo.json
    else:
        warn = 'WARNING: %s was not found! Coordinates (0.0, 0.0) was returned instead.' % location
        warnings.warn(warn)
        #time.sleep(0.5) # Try to avoid the rate limit
        return {'lat': 0.0, 'lng': 0.0}
def remove_address(address, output=CITIES_FILENAME):
    logger.info('Querying the server for: "%s" ...', address)
    response = geocoder.osm(address)
    logger.info('Got an answer!')
    logger.info('Place: %s', response.address)

    logger.info('Loading old cities from: %s', output)
    cities = load_json(output)

    removed = False
    next_cities = cities['next']
    for city in next_cities:
        if city['osm_id'] == response.json['osm_id']:
            logger.info('City: %s removed!', city['address'])
            next_cities.remove(city)
            removed = True
            break

    if removed:
        save_json(cities, output)
    else:
        logger.info('City not found!')

    return response
    def get_place_bound_box(cls, place):
        print place
        boundBox = None

        try:
            #print place
            g = geocoder.osm(place)

            box = g.bbox

            #print box
            swlat = box["southwest"][0]
            swlon = box["southwest"][1]
            nelat = box["northeast"][0]
            nelon = box["northeast"][1]

            #print swlat, swlon, nelat, nelon
            boundBox = GeoBoundBox(swlat, swlon, nelat, nelon)

        except:
            InOut.except_info("get_place_bound_box")
            boundBox = None

        return boundBox
示例#46
0
    def get_coordinates(self, address):
        """ Using geocoder to decode address
            - can use various providers (Google, OSM, etc.) https://geocoder.readthedocs.org/
            - can output GeoJSON
            - proxies from http://www.proxy-listen.de/Proxy/Proxyliste.html
        """

        #proxies = {'149.202.249.227:3128', '144.76.232.58:3128'}

        #g_geocoder = geocoder.osm(address, proxies=proxies)
        g_geocoder = geocoder.osm(address)

        # in case OSM didn't provide anything use Google Maps
        if g_geocoder.latlng == []:
            g_geocoder = geocoder.google(address)

        # in case Google Maps didn't provide anything use ArcGIS
        if g_geocoder.latlng == []:
            g_geocoder = geocoder.arcgis(address)

        (latitude, longitude) = g_geocoder.latlng
        if DEBUG: print ('[i] location : {0} {1}'.format(latitude, longitude))

        return {'lat': latitude, 'lng': longitude}
示例#47
0
def test_repr_unicode():
    g = geocoder.osm('Tokyo, Japan')
    assert g
示例#48
0
def test_osm_reverse():
    g = geocoder.osm(ottawa, method='reverse')
    assert g.ok
示例#49
0
            print g
            print "looking at ", dictedinput['Address'] + "," + dictedinput['City'] + "," + dictedinput['State'] + "," + dictedinput['Zip Code']
        if g.lat and g.lng:
            tempoutput.append(row + [g.lat, g.lng])
            successdict['success'] += 1
            if DEBUG:
                print g.lat, g.lng
        else:
            g = geocoder.google(dictedinput['Address'] + "," + dictedinput['City'] + "," + dictedinput['State'] + "," + dictedinput['Zip Code'])
            if g.lat and g.lng:
                tempoutput.append(row + [g.lat, g.lng])
                successdict['success'] += 1
                if DEBUG:
                    print g.lat, g.lng
            else:
                g = geocoder.osm(dictedinput['Address'] + "," + dictedinput['City'] + "," + dictedinput['State'] + "," + dictedinput['Zip Code'])
                if g.lat and g.lng:
                    tempoutput.append(row + [g.lat, g.lng])
                    successdict['success'] += 1
                    if DEBUG:
                        print g.lat, g.lng
                else:
                    successdict['fails'] += 1
                    print "there was an error with", dictedinput['Address'] + "," + dictedinput['City'] + "," + dictedinput['State'] + "," + dictedinput['Zip Code']
                    tempoutput.append(row + ["x", "x"])
        # if DEBUG:
        #     if counter > LIMIT:
        #         break

print "Success:", successdict['success']
print "Fails:", successdict['fails']
示例#50
0
def getosm(**kwargs):
    """ get osm region from osmapi

    Parameters
    ----------

    address : string
    latlon : tuple or 0
    dist_m : float
    bcart : boolean
    level_height :  float
        typical level height for deriving building height from # levels
    typical_height : float
        typical height for building when no information

    Returns
    -------

        coords
        nodes
        ways
        dpoly
        m
        latlon : tuple or 0

    Notes
    -----

    There are 3 ways to read an Open Street Map structure

    1 - From an osm file ex : filename = 'B11.osm'
    2 - From an osm (lat,lon) string or tuple of float
    3 - From an osm address string

    if latlon tuple is precised, it has priority over the address string

    """

    filename = kwargs.pop('filename','')
    bcart = kwargs.pop('cart', False)
    typ = kwargs.pop('typ','indoor')
    level_height = kwargs.pop('level_height', 3.45)
    typical_height = kwargs.pop('typical_height', 10)

    # from coordinates
    if filename == '':
        address = kwargs.pop('address','Rennes')
        latlon = kwargs.pop('latlon', 0)
        dist_m = kwargs.pop('dist_m', 400)

        rad_to_deg = (180/np.pi)

        if latlon == 0:
            place = geo.osm(address)
            try:
                lat, lon = place.latlng
            except:
                print(place)
        else:
            lat = latlon[0]
            lon = latlon[1]

        r_earth = 6370e3

        alpha = (dist_m/r_earth)*rad_to_deg

    #
    # get map from OsmApi (Database query)
    # same extension in longitude and latitude
    #
        Osm = OsmApi()
        osmmap = Osm.Map(lon-alpha, lat-alpha, lon+alpha, lat+alpha)

    else:
    #
    # get map from osm (xml) file
    # type : 'node'
    #        'ways'
    #
        latlon = 0
        e = xml.parse(filename).getroot()

        osmmap = []

        lnode_key = ['id', 'lat', 'lon']

        lway_key = ['id']

        for i in e:
            d = {}
            d['type'] = i.tag
            d['data'] = i.attrib
            #print(i.tag)

            if d['type'] == 'node':
                for k in lnode_key:
                    try:
                        d['data'][k] = eval(d['data'][k])
                    except:
                        pass
                    if k == 'id':
                        if not 'action' in d['data']:
                            d['data'][k] = -d['data'][k]
                d['data']['tag'] = {}

            elif d['type'] == 'way':
                lk = i.getchildren()
                nd = []
                tag = {}
                for k in lk:
                    if k.tag == 'nd':
                        nd.append(eval(k.get('ref')))
                    if k.tag == 'tag':
                        tag[k.get('k')] = k.get('v')
                d['data']['nd'] = nd
                d['data']['tag'] = tag

                # for k in lway_key:
                #     lk = k.get_children()
                #     print(lk)

                    # d['data'][k]=eval(d['data'][k])
                # d['data']['visible']=eval(d['data']['visible'])
            osmmap.append(d)

    nodes = Nodes()
    nodes.clean()
    nodes.readmap(osmmap)

    coords = Coords()
    coords.clean()
    coords.from_nodes(nodes)
    m = coords.cartesian(cart=bcart)

    ways = Ways()
    ways.clean()

    lat = coords.latlon[list(coords.latlon.keys())[0]][0]
    lon = coords.latlon[list(coords.latlon.keys())[0]][1]

    if typ == 'indoor':
        ways.readmap1(osmmap, coords)
    else:
        ltree = ways.readmap2(osmmap, coords)
    # list of nodes involved in buildings
    lnodes_id=[]
    for iw in ways.w:
        lnodes_id += ways.w[iw][0]
    # list of all nodes of coords

    lnodes_id  = np.unique(np.array(lnodes_id))
    lnodes_full = np.unique(np.array(list(coords.latlon.keys())))
    mask = np.in1d(lnodes_full, lnodes_id, invert=True)

    # nodes not involved in buildings

    #if typ != 'indoor':
    #    lexcluded = lnodes_full[mask]
    #    coords.filter(lexcluded)

    # dpoly = {}
    # for iw in ways.w:
    #     # ways.way[iw].tags = {}
    #     # # material
    #     # if 'material' in ways.w[iw][1]:
    #     #     ways.way[iw].tags['name']=ways.w[iw][1]['material']
    #     # elif 'building:material' in ways.w[iw][1]:
    #     #     ways.way[iw].tags['name']=ways.w[iw][1]['building:material']
    #     # else:
    #     #     ways.way[iw].tags['name']='WALL'

    #     # # min_height
    #     # if 'building:min_height' in ways.w[iw][1]:
    #     #     min_height = eval(ways.w[iw][1]['building:min_height'])
    #     # else:
    #     #     min_height = 0
    #     # # height
    #     # if 'height' in ways.w[iw][1]:
    #     #     ways.way[iw].tags['z'] = (min_height, eval(ways.w[iw][1]['height']))
    #     # elif 'building:height' in ways.w[iw][1]:
    #     #     ways.way[iw].tags['z'] = (min_height, eval(ways.w[iw][1]['building:height']))
    #     # elif 'building:levels' in ways.w[iw][1]:
    #     #     nb_levels = eval(ways.w[iw][1]['building:levels'])
    #     #     if type(nb_levels)!=int:
    #     #         try:
    #     #             nb_levels = max(nb_levels)
    #     #         except:
    #     #             nb_levels=2
    #     #     ways.way[iw].tags['z']=(min_height,nb_levels*level_height)
    #     # elif 'levels' in ways.w[iw][1]:
    #     #     nb_levels = eval(ways.w[iw][1]['levels'])
    #     #     if type(nb_levels)!=int:
    #     #         try:
    #     #             nb_levels=max(nb_levels)
    #     #         except:
    #     #             nb_levels=2
    #     #     ways.way[iw].tags['z'] = (min_height,nb_levels*level_height)
    #     # else:
    #     #     ways.way[iw].tags['z'] = (0,typical_height)

    #     ptpoly = [coords.xy[x] for x in ways.w[iw][0]]
    #     dpoly[iw] = geu.Polygon(ptpoly,vnodes=ways.w[iw][0])
    #     dpoly[iw].coorddeter()

    #return coords,nodes,ways,dpoly,m
    return coords, nodes, ways, m, (lat,lon)
示例#51
0
def test_osm():
    g = geocoder.osm(location)
    assert g.ok
    assert g.city == city
示例#52
0
for item in db_city.find().skip(22000).limit(50000):
    if not item['location'] in existing:
        search.append(item)
print 'Remaining:', len(search)

# Scan Database
for item in search:
    location = item['location']
    x, y = item['geometry']['coordinates']
        
    # Geocode Address
    if provider == 'bing': 
        g = geocoder.bing(location)
    elif provider == 'osm':
        time.sleep(1)
        g = geocoder.osm(location)

    # Calculate Distance with Haversine formula
    distance = haversine([y, x], [g.lat, g.lng]) * 1000

    # Set Results
    results = g.json
    results['city'] = city
    results['distance'] = distance

    # Save in Mongo DB
    try:
        db_geocoder.insert(results)
    except:
        print 'Duplicate'
示例#53
0
def address_to_coordinates(user_address):
	the_location = geocoder.osm(user_address)
	lat = the_location.osm.get('y',None)
	lon = the_location.osm.get('x', None)
	return (lat,lon)
示例#54
0
def view_particular_event(event_name):

    #Sets the event value to not past
    past = False

    #Gets the name of the user logged in
    user_in_use = g.user

    #Gets the user's events
    events = db.events.find({'who_made_me':g.user})
    my_events = []
    for i in events:
        my_events.append(i['name'])


    #If logged in
    if g.user:


        #Check if past_event:

        if db.events.find_one({'name':event_name}) != None:
            event = db.events.find_one({'name': event_name})
        elif db.past_events.find_one({'name':event_name}) != None:
             event = db.past_events.find_one({'name':event_name})
             past = True





        #querying the map from OSM API. It takes the Address of the location as OSM search and spits out a lat and lng


        var = event

        geocode = geocoder.osm(var['address'])
        lat_of_event = geocode.json["lat"]
        lng_of_event = geocode.json["lng"]

        #Real time search for users that are attending the event, will be implemented for events also
        results = []
        search_results = None
        search_term = None
        if request.method == 'POST' :

            search_term = request.json['search']
            search_results = db.users.find({'going_to':var['name'], 'name':{'$regex': search_term}})
            for i in search_results:
                results.append(i['name'])
            print results
            return  jsonify(results=results)









        return render_template('one_event.html', var=var,my_events=my_events,user_in_use =user_in_use,lat=lat_of_event,lng=lng_of_event,search_results=search_results,past=past,results=results)
    else:
        return redirect(url_for('login'))
示例#55
0
def test_multi_results():
    g = geocoder.osm(location, maxRows='5')
    assert len(g) == 5
'''
Created on 09-23-2015

@author: Wuga
'''
import folium

import geocoder
import DataOperation as DO
import DataPreprocess as DP
import Vorplots as V
import Constants
import pandas as pd

g=geocoder.osm('dublin,ireland')
loca=g.latlng
print loca
LOCATION=Constants.filelocations.DUBLIN_2010
df=DO.readgeofile(LOCATION)
train,test,train_index,test_index=DO.dataseperator(df)
map_osm = folium.Map(location=loca, zoom_start=9, max_zoom=18)
train=DP.elim(train)
train = train.reset_index(drop=True)
V.voronoiplot(DP.elim(train))
map_osm.geo_json(geo_path=r'autovoronoi.json', data_out='/Users/Wuga/Documents/DATA/SFREHPDATA/pricedata.json',data=pd.read_csv('/Users/Wuga/Documents/DATA/SFREHPDATA/pricedata.csv'),columns=['Id','Price'],key_on='feature.id', threshold_scale=[200000,250000,300000,350000,400000,500000], fill_color='YlOrRd', fill_opacity=0.5, line_opacity=0.5, legend_name='SF house price')
map_osm.create_map(path=Constants.filelocations.MAP_HTML)
示例#57
0
文件: hpd.py 项目: no140/citydata
hpdr = requests.get(url_hpd, params=parms)
print('request complete, now reading json...')
hpdf=pd.read_json(hpdr.text, dtype={'zip': 'str'})
hpdf['address'] = hpdf.housenumber+' '+ hpdf.streetname + ', ' + city+', NEW YORK'# hpdf.boro + ', ' + hpdf.zip
hpdf.dropna()
#url_geo = 'https://maps.googleapis.com/maps/api/geocode/json'
longlist = []
latlist = []
pause = 5
for i,row in hpdf.iterrows():#_using.iterrows():
	#geops = {'sensor': 'false', 'address': row['address'], 'key': API_KEY_GEO}
	#geor = requests.get(url_geo, params=geops)
	#georesponse = geor.json()#['results']
	#print(row['address'])
	try:
		g = geocoder.osm(row['address'])
	except TypeError as e:
		print('TypeError:')
		print(e)
		print('lets pause for %d seconds..' % pause)
		time.sleep(pause)
		pause += 5
		pass
	except:
		print('problem in getting osm response')
		time.sleep(pause)
		pause += 1
		pass
	#print(g.osm)
	if g.osm:
		#hpdf['long'][i] = g.osm["x"]
示例#58
0
def test_detailed_query():
    g = geocoder.osm("",postalcode="45326", street="Ellernstraße", method="details")
    assert g.postal == "45326"
    assert "ellern" in g.street.lower()
    assert g.ok
示例#59
0
def test_osm():
    g = geocoder.osm(location)
    assert g.ok
    osm_count, fields_count = g.debug()[0]
    assert osm_count >= 3
    assert fields_count >= 21
示例#60
0
import geocoder
import requests

# Geocode Address from the OSM server
# Website: https://github.com/DenisCarriere/geocoder
g = geocoder.osm("Westboro, Ottawa ON")

# Define Bounding Box query for Overpass
bbox = '<bbox-query s="{south}" w="{west}" n="{north}" e="{east}"/>'.format(south=g.south, west=g.west, north=g.north, east=g.east)

# Define which keys & values you want to extract with your Overpass request
# Website: http://overpass-turbo.eu/
has_kv = """
<has-kv k="amenity" v="cafe"/>
<has-kv k="cuisine" v="coffee_shop"/>
"""

# Standard XML format for the Overpass query
data = """
<osm-script output="json" timeout="25">
  <union>
    <query type="node">
      {has_kv}
      {bbox}
    </query>
    <query type="way">
      {has_kv}
      {bbox}
    </query>
  </union>
  <print mode="body"/>