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 get_county_ArcGIS(row): geolocator = ArcGIS(timeout=1) latlng = "" + str(row['lat']) + ", " + str(row['lng']) + "" req = False if latlng in county_dict: return county_dict[latlng], req try: location = geolocator.reverse(latlng) except GeocoderServiceError as e: county_dict[latlng] = None req = True print e print "Unable to find county for: ", latlng return 'NA', req except: time.sleep(5) try: location = geolocator.reverse(latlng) except GeocoderServiceError as e: county_dict[latlng] = None req = True print e print "Unable to find county for: ", latlng return 'NA', req except: time.sleep(60) location = geolocator.reverse(latlng) if 'Subregion' in location.raw.keys(): county_dict[latlng] = location.raw['Subregion'] req = True print county_dict[latlng] return county_dict[latlng], req print 'NA!!!' return 'NA', req
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
class Tacobueno(scrapy.Spider): name = "tacobueno" uid_list = [] domain = "https://www.tacobueno.com/" def __init__(self): place_file = open('citiesusca.json', 'rb') self.place_reader = json.load(place_file) self.geolocator = ArcGIS() def start_requests(self): for city in self.place_reader: info = self.place_reader[city] if info['country'] == 'United States': request_url = "https://www.tacobueno.com/locations/&zip=" + info['zip_code'] yield scrapy.Request(url=request_url, callback=self.parse_store) def parse_store(self, response): stores = response.xpath('//div[@tabindex="0"]') for store in stores: item = ChainItem() item['store_number'] = '' item['store_name'] = store.xpath('./h2/text()').extract_first() item['address'] = store.xpath(".//div[contains(@class, 'map-listing_item')]/address/text()").extract_first() if item['address'] == None: item['address'] = '' item['address2'] = "" item['phone_number'] = store.xpath(".//div[contains(@class, 'map-listing_item')]/ul/li[2]/a").extract_first() if item['phone_number'] == None: item['phone_number'] = '' item['city'] = store.xpath('.//@data-city').extract_first(); item['country'] = "United States" item['latitude'] = "" item['longitude'] = "" gps = store.xpath('.//@data-gps').extract_first() if (gps != ""): item['latitude'] = gps.split(",")[0] item['longitude'] = gps.split(",")[1] location = self.geolocator.reverse("%s, %s" % (str(item['latitude']), str(item['longitude']))).raw item['state'] = location.get('Region', '') item['other_fields'] = "" hours = store.xpath(".//li[contains(@class, 'list-toggle-item')]/text()").extract() item['store_hours'] = ";".join(hours) item['coming_soon'] = 0 yield item
def reverse_geocode(provider, lat, lng): result = ReverseGeocodeResult(lat, lng) if provider == "arcgis": geolocator = ArcGIS() query = str(lat) + ", " + str(lng) print(query) location = geolocator.reverse(query) print(location) address = ap.parse(location.address) if (address['street_name'] is not None and address['street_type'] is not None): result.set_streets( [address['street_name'] + " " + address['street_type']]) elif (address['street_name'] is not None): result.set_streets([address['street_name']]) return result
import sys from geopy.geocoders import ArcGIS sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1) geolocator = ArcGIS() fout = open(sys.argv[2],'w', encoding = 'utf-8') with open(sys.argv[1]) as fin: for line in fin: line = line.strip() location = geolocator.reverse(line) # location = geolocator.reverse("52.509669, 13.376294") fields = location.address.split(',') print(fields[-2].strip(), file =fout)
def find_address(coords): geolocator = ArcGIS() loc = geolocator.reverse(coords) return loc.raw['Match_addr']
# 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) else:
from geopy.geocoders import ArcGIS import pandas #some tests geolocator = ArcGIS() location = geolocator.geocode("francesc eiximenis, Sabadell") #print(location.raw) location_reverse = geolocator.reverse("42.59944444, -5.56666667") #print(location_reverse.address) #code df = pandas.read_csv("./supermarkets.csv") df["Address"] = df["Address"] + ", " + df["City"] + ", " + df[ "State"] + ", " + df["Country"] df["Coordinates"] = df["Address"].apply(geolocator.geocode) #print(df) df["Latitude"] = df["Coordinates"].apply(lambda x: x.latitude) df["Longitude"] = df["Coordinates"].apply(lambda x: x.longitude) print(df)
from geopy.geocoders import ArcGIS nom = ArcGIS() abha = nom.geocode('King Faisl, Abha, Saudi Arabia', timeout=10000) print(abha.latitude, abha.longitude) print(abha.raw) # print(abha.raw.keys()) print(abha.raw['address']) print(abha.raw['location']) print(abha.raw['score']) print(abha.raw['attributes']) print(abha.raw['extent']) lat = input('Enter lat ... ') long = input('Enter long ... ') city = nom.reverse(f'{lat}, {long}', timeout=10000) print(dir(city)) print(city.address) print(city.latitude) print(city.longitude) print(city.raw) # print(city.raw.keys()) print(city.raw['City'])