def decipher_location(location="Austin Texas"): """ Receives text describing location, returns geographical coordinates lat and lng :param location: String giving user location, defaults to Austin Tx :return: latitude and longitude """ # If location provided is empty string, then set dafault to Austin if not location: location = "Austin Texas" # geolocator = GoogleV3() geolocator = ArcGIS() geo_loc = geolocator.geocode(location) # If no matching location found, geo_loc == None # If this happens, print no location found, defaulting to Austin Tx if not geo_loc: loc_logger.warning("Location not found, defaulting to Austin Tx") geo_loc = geolocator.geocode("Austin Texas") loc_logger.info("Input location was {}, found location is {}".format( location, geo_loc.address)) loc_logger.info("Location Latitude: {}, Longitude: {}".format( geo_loc.latitude, geo_loc.longitude)) return geo_loc.latitude, geo_loc.longitude, geo_loc.address
def get_coordinates(adress): """ (str) -> (list) This function finds lattitude and longitude of given address using geocoder. Precondition: the address must be appropriate, so it can be found on map. >>>get_coordinates('Mountain View, CA') [37.3860517, -122.0838511] >>>get_coordinates('Russell Winkelaar's flat, Toronto, Ontario, Canada) None >>>get_coordinates(Lviv, Ukraine) [49.839683, 24.029717] """ try: geolocator = ArcGIS() # Get location in appropriate form. location = geolocator.geocode(adress) # Get coordinates of address. coordinates = (location.latitude, location.longitude) # Return coordinatess as [longitude and lattitude] list. print(coordinates) return coordinates except GeocoderTimedOut: # TIME Error return get_coordinates(adress)
def save_service(content): print content address_string = '{address_line_1}, {address_line_2}, {postcode}, {town_city}, {country}'.format( **content) geolocator = ArcGIS() loc = geolocator.geocode(address_string) print loc.raw db = MySQLdb.connect(host=app.DB_HOST, user=app.DB_USER, passwd=app.DB_PASSWD, db=app.DB_NAME, cursorclass=MySQLdb.cursors.DictCursor) with closing(db.cursor()) as cursor: cursor.execute( "INSERT INTO services(name, description, email, " "address_line_1, address_line_2, postcode, website, " "area, country, phone_number, town_city, service, " "latitude, longitude) " " VALUES (%s,%s,%s, %s,%s,%s,%s,'none',%s,%s,%s,%s,%s,%s)", (content['name'], content['description'], content['email'], content['address_line_1'], content['address_line_2'], content['postcode'], content['website'], content['country'], content['phone_number'], content['town_city'], content['service'], loc.latitude, loc.longitude)) db.commit()
def mainFunction(): """ (None) -> html Returns html type of the map, which could be further used to be redirected from the web site to our map """ myMap = creatingMap() dictNameLoc = finalDictionary() geolocator = ArcGIS() friendsGroup = folium.FeatureGroup(name='Friends') for key, value in dictNameLoc.items(): try: location = geolocator.geocode(key) locations = [location.latitude, location.longitude] friendsGroup.add_child( folium.Marker(location=locations, popup='<br>'.join(value), icon=folium.Icon(color='black', icon='thumbs-up'))) except: pass myMap.add_child(friendsGroup) myMap.add_child(folium.LayerControl()) htmlString = myMap.get_root().render() return htmlString
def dataAna(): print('Start with Data Analysis') df1 = pandas.read_csv("supermarkets.csv") ctx = ssl.create_default_context(cafile=certifi.where()) geopy.geocoders.options.default_ssl_context = ctx nom = ArcGIS() print(nom.geocode("242 Fairmeadow Way Milpitas CA")) df1["FullAddr"] = df1["Address"] + " " + df1["City"] + " " + df1[ "State"] + " " + df1["Country"] df1["LatLong"] = df1["FullAddr"].apply(nom.geocode) df1["Latitude"] = df1["LatLong"].apply(lambda x: x.latitude if x != None else None) ##print(dir(df1)) # To iterate over Data Frame # for idx,rws in df1.iterrows(): # n = nom.geocode(rws['FullAddr']) # print((n.latitude)) # To return sum of one column ##tot = df1['Employees'].sum() ##tot = sum(df1['Employees']) return df1
def parse(self, response): self.driver.get(response.url) # Ищем и нажимаем на кнопку со списком ресторанов rest_list_button = self.driver.find_element_by_xpath( '/html[1]/body[1]/div[1]/div[1]/div[2]/' 'div[1]/div[2]/div[2]/button[2]') rest_list_button.click() # Собираем список всех ресторанов rest_list = WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located((By.CLASS_NAME, 'Mujm2VkJ7g'))) for rest in rest_list: # Парсим полученные рестораны на предмет адреса address = rest.find_element_by_class_name('_2XllDYbBnt').text if self.city in address: # В случае если ресторан находится в искомом городе получаем его координаты geolocator = ArcGIS( user_agent= "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" ) location_geo = geolocator.geocode(address) yield BurgerkingRivalsParserItem( latitude=location_geo.latitude, longitude=location_geo.longitude, city=self.city, address=address, brand='kfc')
def success( ): #User does a submission and you want to capture that using a success function global filename #Made filename variable global to use the variable in the app root for ("/download") if request.method == 'POST': #Check for request . . .if we got a request file = request.files[ "file"] #Get the file. ['file'] is the name of <input> located on index.html page (name="file"/>) try: #Use try & except method to avoid error page if there isn't an Address column in CSV file df = pandas.read_csv( file ) #Will read file. This variable is reading the file=request.files["file"] variable nom = ArcGIS() n = nom.geocode("") #df["Address"]=df["Address"]+", "+df["City"] + ", "+ df["State"]+ ", " + df["Country"] df["Coordinates"] = df["Address"].apply(nom.geocode) df["Latitude"] = df["Coordinates"].apply(lambda x: x.latitude if x != None else None) df["Longitude"] = df["Coordinates"].apply(lambda x: x.longitude if x != None else None) #df=df.drop("Coordinates", 1) filename = datetime.datetime.now().strftime( "uploads/%Y-%m-%d-%H-%M-%S-%f" + ".csv" ) #Created this line to make each generate file/download unique df.to_csv( filename, index=None ) #Then generate a CSV file. Sorting it in the filename global variable> return render_template( "index.html", text=df.to_html(), btn="download.html") #Then return the template except: #If there isn't an Address column instead of returning an html table(text=df.to_html()) return text="Missing address etc." return render_template( "index.html", text="Missing address in column of your CSV file!")
def geocoder(q): """ Name: geocoder Inputs: str, geocoder query (q) Outputs: tuple, longitude and latitude Features: Returns coordinates from a geocoder query Ref: For creating an ArcGIS API token, see here: https://developers.arcgis.com/rest/geocode/api-reference/geocoding-authenticate-a-request.htm """ # Get ArcGIS application token from file: token_file = r"C:\Workspace\AdvGIS\advgis-wm.token" my_token = "" with open(token_file) as f: my_token = f.readline() # Create geopy object gc = ArcGIS() gc.token = my_token # See if you get anything: try: a = gc.geocode(q) my_out = (a.longitude, a.latitude) except: my_out = ("N/A", "N/A") return (my_out)
def geocode( address): #This function takes in an address and returns lat and lo nom = ArcGIS() try: x = nom.geocode(address) return (x.latitude, x.longitude) except AttributeError: return ('Nan', "Nan")
def load_csv(): global df, filename, r if "Address" in df or "address" in df: colored = tkinter.Label(window, background="#00cc99") colored.grid(row=4, column=0, sticky="news", padx=300) r = 0 c = 300 geolocator = ArcGIS() df["Longitude"] = None df["Latitude"] = None i = 0 for Address, City, State, Country in zip(df["Address"], df["City"], df["State"], df["Country"]): loc = geolocator.geocode(Address + City + State + Country) df["Longitude"][i] = "{0:.3f}".format(loc.longitude) df["Latitude"][i] = "{0:.3f}".format(loc.latitude) i += 1 for col in df: # for row in col: label = tkinter.Label(window, width=10, height=2, text=col, relief=tkinter.RIDGE) label.grid(row=r + 3, column=0, sticky="w", padx=c) for num in df[col]: r += 1 label = tkinter.Label(window, width=10, height=2, text=num, relief=tkinter.RIDGE) label.grid(row=r + 3, column=0, sticky="w", padx=c) r = 0 c += 75 download_button = tkinter.Button(window, text="Download", width=10, command=download) download_button.grid(row=len(df.index) + 4, column=0, sticky="w", padx=550, pady=10) else: for color in range(20): coloring = tkinter.Label(window, background="#00cc99") coloring.grid(row=color + 3, column=0, sticky='news') new = tkinter.Label( window, text="Please upload a file which contains \"Address\"column", background="#00cc99", foreground="White") new.grid(row=4, column=0, sticky="w", padx=300) new.config(font=("serif", 20))
def f(): s = e1.get() nom = ArcGIS() n = nom.geocode(s) x = n.latitude y = n.longitude t.delete(first=0, last=len(t1.get())) u.delete(first=0, last=len(u1.get())) t.insert(END, str(x)) u.insert(END, str(y))
def find_location(place): ''' :param place: a place where film was made/produced :return: latitude and longitude ''' geolocator = ArcGIS(timeout=10) location = geolocator.geocode(place) lat = location.latitude long = location.longitude return lat, long
def submit(): try: gc = ArcGIS(scheme='http') addr = request.form['address'] coordinates = gc.geocode(addr, exactly_one=True)[1] return render_template("home.html", coordinates=coordinates) except Exception as e: return render_template("home.html", coordinates="Invalid address")
def fetch_hospital_coordinate(): data = pd.read_csv( "https://healthdata.gov/sites/default/files/reported_hospital_capacity_admissions_facility_level_weekly_average_timeseries_20210110.csv" ) hospital = data.groupby(['hospital_pk', 'hospital_name', 'address']).size().reset_index().drop( [0], axis=1).set_index('hospital_pk') hospital = hospital.to_dict("index") geolocator = ArcGIS() i = 0 for k, v in hospital.items(): try: address, (lat, lon) = geolocator.geocode(v["hospital_name"], timeout=10) v["lat"] = lat v["lon"] = lon except: v["lat"] = -999 v["lon"] = -999 i += 1 print(i) # Use hospital address to find coordinates for empty points, if exist unresolved_points = {k: v for k, v in hospital.items() if v["lon"] == -999} if unresolved_points: for k, v in unresolved_points.items(): try: address, (lat, lon) = geolocator.geocode(v["address"], timeout=10) hospital[k]["lat"] = lat hospital[k]["lon"] = lon except: hospital[k]["lat"] = -999 hospital[k]["lon"] = -999 with open('hospital.json', 'w') as fp: json.dump(hospital, fp) print("JSON file saved!")
def success(): if request.method == 'POST': file = request.files["file"] file.save(file.filename) df = pandas.read_csv(file.filename) print(df['address'][0]) nom = ArcGIS() n = nom.geocode(df['address'][0]) lon = n.longitude lat = n.latitude return render_template("index.html", lon=lon, lat=lat, df=df.to_html())
def add_coordinates(dic): '''(dict) -> dict Uses geolocator ArcGIS to find exact location of each film ''' geolocator = ArcGIS(timeout=10) for year in dic: for loc in dic[year]: print('Looking for location of {}...'.format(loc)) location = geolocator.geocode(loc) dic[year][loc].append((location.latitude, location.longitude)) return dic
def new_fun(st): nom = ArcGIS() #print(type(nom)) #print("\n") #print(nom.geocode("38 Elm st, New York,USA")) try: xst = nom.geocode(st) except: return None else: return xst
def load_file(df, file_detail): geolocator = ArcGIS() df["Longitude"] = None df["Latitude"] = None i = 0 for Address, City, State, Country in zip(df["Address"], df["City"], df["State"], df["Country"]): loc = geolocator.geocode(Address + City + State + Country) df["Longitude"][i] = "{0:.3f}".format(loc.longitude) df["Latitude"][i] = "{0:.3f}".format(loc.latitude) i += 1 df.to_csv(file_detail.filename)
def change_adress(adress): """ (str) -> (list) Remake name of position in coordinates return list of two coordinates """ geolocator = ArcGIS() try: location = geolocator.geocode(adress) Coordinates = [location.latitude, location.longitude] return Coordinates except: return change_adress(adress)
def find_location(adress): """ (str) -> (tuple) :return: latitude and longitude of a location """ from geopy.geocoders import ArcGIS try: geolocator = ArcGIS(timeout=20) location = geolocator.geocode(adress) coord = (location.latitude, location.longitude) except: coord = find_location(adress) return coord
def get_coordinates(loc_name): """ (str) -> (float, float) Returns coordinates (latitude, longitude) of location. """ geolocator = ArcGIS() loc = geolocator.geocode(loc_name) if loc.latitude: return (loc.latitude, loc.longitude) else: if loc_name.find(',') != -1: return get_coordinates(loc_name[loc_name.find(',') + 2:]) else: return (None, None)
def geo(location): """ (str) -> list Returns the list of coordinates from locations :param location: :return: list of coordinates """ geolocator = ArcGIS() try: location = geolocator.geocode(location) location = [location.latitude, location.longitude] return location except: pass
def geo_distance(countries,out_path): in_file = open(out_path,"a") for pair in list(combinations_with_replacement(countries,2)): a = pair[0] b = pair[1] geolocator = ArcGIS(timeout=None) loc1 = geolocator.geocode(a) loc2 = geolocator.geocode(b) time.sleep(1) dist = vincenty(loc1[1],loc2[1]).kilometers # Salvo ogni distanza su file in_file.write("%s,%s,%s\n" % (a,b,dist)) in_file.close()
def coordinates(lst_loc): """(list)->(list) The function takes the list of locations and return the list of their coordinates""" lst = [] geo = ArcGIS() for i in lst_loc: try: loc = geo.geocode(i[1]) loc1 = [loc.latitude, loc.longitude] lst.append([i[0], loc1]) except: pass return lst
def get_locations(): """ Parse CSV file and get coordinates for all cities within. Locations are cached for future calls. """ if os.path.isfile('locations.pickle'): with open('locations.pickle', 'rb') as file: locations = pickle.load(file) else: locations = {} with open('locations.csv', 'r') as file: cities = [x.split(';')[0:2] for x in file.read().split('\n')] geolocator = ArcGIS(scheme='http') added_cities = 0 skipped_cities = 0 unknown_cities = 0 try: for city in cities: if city[1] not in locations.keys(): try: locations[city[1]] = geolocator.geocode(city[1], timeout=10).raw['location'] except AttributeError: print("Unknown {0} city".format(city[1])) unknown_cities += 1 continue print("Added {0} city.".format(city[1])) added_cities += 1 else: print("Skipped {0} city.".format(city[1])) skipped_cities += 1 except GeocoderTimedOut: print("Geocoder timed out.") except KeyboardInterrupt: print("Keyboard interrupt.") except Exception: print("Unknown exception.") with open('locations.pickle', 'wb') as file: pickle.dump(locations, file) print("Added: {0} / Skipped: {1} / Unknown: {2} / Total: {3}".format( added_cities, skipped_cities, unknown_cities, added_cities + skipped_cities + unknown_cities )) return locations
def location(place): """ list -> list Function return list of lists with latitude and longitude of locations. """ loc = [] geolocator = ArcGIS() for i in place: try: location = geolocator.geocode(i) location1 = [location.latitude, location.longitude] loc.append(location1) except: pass return loc
def save(self, commit=True): property_obj = super(PropertyForm, self).save(commit=False) geolocator = ArcGIS(user_agent="eKPM") address = self.cleaned_data['address'] city = self.cleaned_data['city'] country = self.cleaned_data['country'] try: property_obj.geographic_location = geolocator.geocode(address + " " + city + " " + country.name) print("**************GeoCode success***************") except GeocoderServiceError: property_obj.geographic_location = (address + " " + city + " " + country.name) print("**************GeoCode failed***************") if commit: property_obj.save() return property_obj
def coordinates(list_of_years_places): # "Coordinates" function converts location names to coordinates. """ :param list_of_years_places: list :return: list """ locators = ArcGIS() for contents in list_of_years_places: try: location = locators.geocode(contents[1], timeout=100) except GeocoderTimedOut: continue contents[1] = [location.latitude, location.longitude] return list_of_years_places
def get_geocodes(df): gis = ArcGIS(timeout=10) lon = [] lat = [] for indx, address in df['Address'].iteritems(): gcode = gis.geocode(address) if gcode: lon.append(gcode.longitude) lat.append(gcode.latitude) else: lon.append(None) lat.append(None) df['Latitude'] = lat df['Longitude'] = lon return df
def get_loc(loc): """ (str) -> tuple(float, float) Function gets place and returns its coordinates param loc: string that is place where the film was filmed return: tuple of two floats that are longitude and latitude >>> get_loc("North Hollywood, Los Angeles, California, USA") (34.2, -118.4) >>> get_loc("Jo's Cafe, San Marcos, Texas, USA") (29.9, -97.9) """ location = ArcGIS(timeout=10) place = location.geocode(loc) return (round(place.latitude, 1), round(place.longitude, 1))
def gettingCoordinates(listOfLocations): """ (list) -> list Takes the list of locations as the parameter and returns list of all coordinates of these parameters """ geolocator = ArcGIS() listOfCoordinates = [] for i in listOfLocations: try: location = geolocator.geocode(i) locations = [location.latitude, location.longitude] listOfCoordinates.append(locations) except: pass return listOfCoordinates
# Copyright © Benjamin D. McGinnes, 2018 # Ben McGinnes <*****@*****.**> # OpenPGP key: DB4724E6FA4286C92B4E55C4321E4E2373590E5D # Licensed under the Apache 2.0 License import sys from geopy.geocoders import ArcGIS if len(sys.argv) > 1: meatspace = " ".join(sys.argv[1:]) else: meatspace = input("Enter the address: ") gloc = ArcGIS() target = gloc.geocode(meatspace) is_positive_lat = target.latitude >= 0 is_positive_lon = target.longitude >= 0 abs_lat = abs(target.latitude) abs_lon = abs(target.longitude) def dd2dms(dd): minutes, seconds = divmod(dd * 3600, 60) degrees, minutes = divmod(minutes, 60) return degrees, minutes, seconds if is_positive_lat is True: dd = abs_lat lat_deg, lat_min, lat_sec = dd2dms(dd) dms_lat = """{0}° {1}' {2}" N""".format(lat_deg, lat_min, lat_sec) else: