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 success(): global ufile global df if request.method == 'POST': ufile = request.files['file'] if ufile.filename.lower().endswith('.csv'): df = pd.read_csv(ufile, encoding="ISO-8859-1") if 'Address' in df.columns or 'address' in df.columns: if 'address' in df.columns and 'Address' not in df.columns: address = 'address' else: address = 'Address' df['Latitude'] = df[address].apply(ArcGIS().geocode).apply( lambda x: x.latitude if x != None else None) df['Longitude'] = df[address].apply(ArcGIS().geocode).apply( lambda x: x.longitude if x != None else None) return render_template("index.html", tables=[ df.to_html(classes='data', index=False, header="true") ], btn="download.html") else: return render_template("index.html", wrong_file="wrong_file.html") else: return render_template("index.html", wrong_file="wrong_file.html")
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 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 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 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 r_geocoder(): try: my_lat = float(request.form['mylat']) my_long = float(request.form['mylong']) except Exception: my_lat = None my_long = None nom = ArcGIS() if (my_lat or my_long) == None: address = "Invalid Lat/Long values" return render_template("r_geocoder.html", my_lat=my_lat, my_long=my_long, address=address) else: try: # n = nom.reverse((32.839097, -96.663127), timeout=10) # Lat, Long n = nom.reverse((my_lat, my_long), timeout=10) # Lat, Long address = n.address return render_template("r_geocoder.html", my_lat=my_lat, my_long=my_long, address=address) except Exception: return render_template( "r_geocoder.html", my_lat=my_lat, my_long=my_long, address="No Address for the provided Lat/Long values!")
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 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 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 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 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 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 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 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 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 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_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 _get_cluster_names(self, start_ids): """ Assigns to each cluster a name based on the closest city. """ from geopy.point import Point from geopy.geocoders import ArcGIS data = self.summary map_id2name = {} for cluster_id in start_ids: if cluster_id == -1: map_id2name[cluster_id] = 'notClustered' continue # get average startpoint of this cluster lat_mean = data[data['cluster_id'] == cluster_id]['startpos_lat'].mean() lon_mean = data[data['cluster_id'] == cluster_id]['startpos_lon'].mean() lon_mean = data[data['cluster_id'] == cluster_id]['startpos_lon'].mean() p1 = Point(lat_mean, lon_mean) # Get city name of this location attempts = 0 while attempts < 3: try: geolocator = ArcGIS() city = geolocator.reverse(p1).raw['City'] break except Exception: attempts += 1 time.sleep(3) print('Call to geopy.ArcGIS() failed, retry...') # Get mean distance dist_str = str( round( data[data['cluster_id'] == cluster_id]['distance'].mean() / 1000, 2)) name = city + '_' + dist_str # In case one name is used more the once, add counter to name i = 2 while name in map_id2name.values(): name = name + '_' + str(i) i = i + 1 map_id2name[cluster_id] = name return map_id2name
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 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 success(): if request.method == 'POST': file = request.files['file'] try: df = pandas.read_excel(file) geoc = ArcGIS(scheme='http') df.columns = [i.capitalize() for i in df.columns] df['Coordinates'] = df['Address'].apply(geoc.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( "%Y-%m-%d-%H-%M-%S-%f" + ".xlsx") df.to_excel(filename, index=None) return render_template("index.html", filename=filename, text=df.to_html(), bttn='download.html') except Exception: return render_template( "index.html", text= "Please make sure that you have chosen the file and it has an 'Address' or 'address' column." )
def setUpClass(cls): cls.geocoder = ArcGIS( username=env['ARCGIS_USERNAME'], password=env['ARCGIS_PASSWORD'], referer=env['ARCGIS_REFERER'], timeout=3 )
#!/usr/bin/env python3 # 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)
#!/usr/bin/env python3 # 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 decimal coordinates: ") gloc = ArcGIS() target = gloc.reverse(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)