def test_queryAreaIDSelector(): nominatim = Nominatim() x = nominatim.query('Dublin') assertForQueryResult(minElements=5, area=x.areaId(), elementType=['node', 'way'], selector=['"name"~"Tesco"', 'opening_hours'])
def test_placeWkt(): nominatim = Nominatim() x = nominatim.query('Heidelberg', wkt=True) assert x.areaId() == 3600285864 assert len(x.toJSON()) > 0 assert len(x.wkt()) > 0 assert x.toJSON()
def test_queryAreaID(): nominatim = Nominatim() x = nominatim.query('Enschede') assertForQueryResult(area=x.areaId(), elementType='node', selector='"highway"="bus_stop"', out='body')
def test_queryBbox(): nominatim = Nominatim() x = nominatim.query('Enschede') assertForQueryResult(bbox=[52.1, 6.7, 52.3, 6.9], elementType='node', selector='"highway"="bus_stop"', out='body')
def extract_lat_long_from_nominatim(address): """ Query OSM Nominatim API for the latitude and longitude coordinates of address :param address: String :return: lat, lng """ lat, lng = None, None nominatim = Nominatim() area = nominatim.query(address) if area is None: return None, None try: """ Try block in case any inputs are invalid """ osm_json = area.toJSON() json_item = None for item in osm_json: if "Donegal" in item["display_name"]: json_item = item break lat = json_item["lat"] lng = json_item["lon"] except: pass return lat, lng
def main(): """Main function to create the output file""" nominatim = Nominatim() overpass = Overpass() config = configparser.ConfigParser() config.read('configuration') area_id = nominatim.query('Italia').areaId() html_string = "" for operator in ast.literal_eval(config.get("MAPPING", "operators")): #create folders for needed saving operations later if not os.path.exists("./home_to_destination/{}".format(operator)): os.mkdir("./home_to_destination/{}".format(operator)) if not os.path.exists("./routes/{}".format(operator)): os.mkdir("./routes/{}".format(operator)) operator_parameter = '"operator" = "{}"'.format(operator) query = overpassQueryBuilder(area=area_id, elementType='rel', selector=operator_parameter, out='body') result = overpass.query(query, timeout=600) html_string += HTML.main_output_generation( config['MAPPING']['address'], result, operator) html_file = open("output.html", "w") html_file.write(html_string) html_file.close()
def get_geolocation_mapping(query=None): """ Returns a list of dicts with 'lat' and 'lon' as keys Parameters ---------- query : str The name of entity to be mapped (default is None) Raises ------ ValueError If no queries or an empty query has been passed in as a parameter """ if query is None or not query: raise ValueError("Query must not be empty!") unneeded_classes = [ 'highway', 'place', 'tourism', 'boundary', 'railway', 'aeroway' ] unneeded_types = ['parking', 'yes', 'brownfield'] nominatim = Nominatim() result = nominatim.query(query).toJSON() df = pd.DataFrame.from_records(result) df = df[df['class'].isin(unneeded_classes) == False] df = df[df['type'].isin(unneeded_types) == False] df = df.filter(items=['lat', 'lon']) return df.to_dict('records')
def main(area, path, loglevel): logging.basicConfig(format='%(asctime)s, %(message)s', datefmt='%Y-%m-%d %H:%M:%S') logging.getLogger().setLevel(loglevel) nominatim = Nominatim() areaId = nominatim.query(area).areaId() logging.info("area: %s, id: %s", area, areaId) selectors = {'power': ['plant', 'generator']} df = pd.DataFrame() for osmkey, osmvalues in selectors.items(): for osmval in osmvalues: logging.info("fetching %s = %s", osmkey, osmval) data = fetchFeatures(areaId, osmkey, osmval) logging.info("Number of Elements: %s", data.countElements()) for i in range(0, data.countElements()): e = data.elements()[i] if (e.tag('type') == 'site'): continue # id, lat, lon, timestamp, tags df = addFeaturetoGDF(df, e) #if getfirstrev: # if (data.elements()[i].version() > 1): # ne = fetchFeatureVersion(e.id(), 1) gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Lon, df.Lat)) gdf.crs = {'init': 'epsg:4326'} saveData(df, gdf, path, area)
def test_place(): nominatim = Nominatim() x = nominatim.query('Heidelberg') assert x.isReverse() == False assert x.displayName().startswith('Heidelberg') assert x.areaId() == 3600285864 assert len(x.toJSON()) > 0 assert x.toJSON()
def get_OSM(): nominatim = Nominatim() overpass = Overpass() areaId = nominatim.query('Le Plateau Mont Royal').areaId() query = overpassQueryBuilder(area=areaId, elementType=['way']) result = overpass.query(query) db = db_from_OSM(result) return db
def random_destino(origen): nominatim = Nominatim() establecimiento = establecimientos[random.randint(0,len(establecimientos)-1)] data = nominatim.query(str(establecimiento) + " " + "redondela" + " galicia").toJSON() # TODO cambiar "redondela" por municipio de origen destino = {"latitud": data[0]["lat"], "longitud": data[0]["lon"]} print("[Usuario random_destino] " + "establecimiento: " + str(establecimiento) + " , en Redondela , name: " + str(data[0]["display_name"])) #print(origen) return destino
def test_queryAreaIDWithCondition2(): nominatim = Nominatim() x = nominatim.query('Enschede') assertForQueryResult(area=x.areaId(), elementType='node', selector='"highway"="bus_stop"', conditions=['count_tags() > 2', 'count_tags() > 1'], out='body')
def test_queryAreaIDTimeout(): nominatim = Nominatim() x = nominatim.query('Enschede') assertForQueryResult(area=x.areaId(), elementType='node', selector='"highway"="bus_stop"', out='body', overpassKwargs={'date': '2017-01-01T00:00:00Z'})
def test_queryBboxGeometry(): nominatim = Nominatim() x = nominatim.query('Enschede') y = assertForQueryResult(bbox=[52.1, 6.7, 52.3, 6.9], elementType='node', selector='"highway"="bus_stop"', out='body') assert (y.nodes()[0].lat() - 52.2) < .5 assert (y.nodes()[0].lon() - 6.8) < .5
def test_queryAreaIDGeometry(): nominatim = Nominatim() x = nominatim.query('Enschede') y = assertForQueryResult(area=x.areaId(), elementType='node', selector='"highway"="bus_stop"', out='body', includeGeometry=True) assert (y.nodes()[0].lat() - 52.2) < .5 assert (y.nodes()[0].lon() - 6.8) < .5
def test_coordinatesCountry(): nominatim = Nominatim() x = nominatim.query(49.4093582, 8.694724, reverse=True, zoom=3) assert x.isReverse() == True assert x.displayName().startswith('Deutschland') assert x.areaId() == 3600051477 assert x.address() == { 'country': 'Deutschland', 'country_code': 'de', } assert len(x.toJSON()) > 0 assert x.toJSON()
def get_OSM(): nominatim = Nominatim() overpass = Overpass() areaId = nominatim.query('Montreal, Canada').areaId() query = overpassQueryBuilder( area=areaId, elementType='node', selector=['shop']) + overpassQueryBuilder( area=areaId, elementType='node', selector='amenity') result = overpass.query(query) db = db_from_OSM(result) db_tags = list_tags(db) return db, db_tags
def query(Key, Value, Location): nominatim = Nominatim() areaId = nominatim.query(f'{Location}').areaId() overpass = Overpass() query = overpassQueryBuilder(area=areaId, elementType=['node', 'way', 'relation'], selector=f'"{Key}"="{Value}"', includeGeometry=True) print("querying OSM") result = overpass.query(query, timeout=250) result = result.toJSON() return result
def test_coordinates(): nominatim = Nominatim() x = nominatim.query(49.4093582, 8.694724, reverse=True, zoom=10) assert x.isReverse() == True assert x.displayName() == 'Heidelberg, Baden-Württemberg, Deutschland' print(x.toJSON()) assert x.areaId() == 3600285864 assert x.address() == { 'city': 'Heidelberg', 'state': 'Baden-Württemberg', 'country': 'Deutschland', 'country_code': 'de', } assert len(x.toJSON()) > 0 assert x.toJSON()
def download_osm(area_name): """ Given an area name, download corresponding OSM elements """ # Get area id nomanatim = Nominatim() area_id = nomanatim.query(area_name).areaId() # Form and ask query overpass = Overpass() query = overpassQueryBuilder(area=area_id, elementType=['way', 'node'], out='body') osm_data = overpass.query(query, timeout=600) # Display results utils.display_results(osm_data) # Keep elements (ways and nodes) in JSON format elements = osm_data.toJSON()['elements'] return elements
def getAreaId(self, locationName): # TODO: check if its a place (otherwise following queries won't work) nominatim = Nominatim() return nominatim.query(locationName).areaId()
def main(): user = '******' sport_sess = 'Sport-sessions' gps_dir = 'GPS-data' elevation_data = 'Elevation-data' cwd = os.getcwd() pathToFile = '/'.join([cwd, user, sport_sess]) all_files = os.listdir(pathToFile) osm_endpoint = 'https://nominatim.openstreetmap.org/reverse' nm = Nominatim(endpoint=osm_endpoint, waitBetweenQueries=5) # nm = Nominatim() data = [] json_file = None json_file_regex = re.compile('[a-zA-Z0-9\-]+.json') for run_data in all_files: if re.match(json_file_regex, run_data): json_file = '/'.join([pathToFile, run_data]) with open(json_file) as f: data.append(json.load(f)) # Examine run data at index 0 # run_data = data[2] # with open('/'.join([os.getcwd(), user, sport_sess, gps_dir, # run_data['id']]) + '.json') as f: # gps_data = json.load(f) # print('Run id: ', run_data['id']) # print('Number of points: ', len(gps_data)) # print('Distance from run data: ', run_data['distance']) # dist = getTotalDistance(gps_data, getGeodesicDistance) # print("Distance computed: ", dist) print("Total runs: ", len(data)) cluster = clusterByCentroids(data) print("Clustered runs: ", len(cluster)) for key, run in cluster.items(): # run = cluster.get(key) print('Latitude: ', run['latitude']) print('Longitude: ', run['longitude']) print('Variations: ', len(run['variations'])) result = nm.query('', params={ 'lat': run['latitude'], 'lon': run['longitude'] }) json_data = result.toJSON() address = json_data['address'] if 'county' in address.keys(): area = address['county'] elif 'neighbourhood' in address.keys(): area = address['neighbourhood'] else: area = 'get key' if 'suburb' in address.keys(): suburb = address['suburb'] elif 'state' in address.keys(): suburb = address['state'] else: suburb = 'get key' # if address['country_code'] == 'au': # area = address['county'] # suburb = address['suburb'] # else: # area = address['neighbourhood'] # suburb = address['village'] print('Area: %s' % (area)) print('Suburb: %s' % (suburb)) print('Country: %s' % (address['country']))
from OSMPythonTools.nominatim import Nominatim from OSMPythonTools.overpass import overpassQueryBuilder, Overpass from shapely.geometry import Point, Polygon, MultiPolygon import pandas as pd, geopandas as gpd if __name__ == "__main__": nominatim = Nominatim() areaId = nominatim.query('Bogota, Colombia').areaId() overpass = Overpass() query = overpassQueryBuilder(area=areaId, elementType=['way'], selector='"amenity"="hospital"', includeGeometry=True) result = overpass.query(query) result = [{ 'tags': x.tags(), 'geometry': x.geometry() } for x in result.elements()] #to pandas for cleaning tags df_hosps = pd.io.json.json_normalize(result) #to geopandas gdf_hosps = gpd.GeoDataFrame(df_hosps) # #gdf_hosps['geometry']=gdf_hosps['geometry.coordinates'].apply(lambda x: Polygon(x[0])) gdf_hosps['geometry.point'] = gdf_hosps['geometry.coordinates'].apply( lambda x: x[0][0]).apply(Point) gdf_hosps = gdf_hosps.set_geometry(col='geometry.point') #gdf_hosps.plot().get_figure().savefig('hospitals_points.png') gdf_hosps.drop(columns=['geometry.coordinates']).to_file( driver='GeoJSON', filename="bogota_hospitales.geojson")
link = 'To get the full key/value list of possibilities, go search on [tagfinder](http://tagfinder.herokuapp.com/search?query=bbq&lang=en)' st.markdown(link, unsafe_allow_html=True) # Filtering area area = st.text_input("Location", "Wallonie") # Filtering tags overpass = Overpass() key = st.text_input("Key", "amenity") value = st.text_input("Value", "bbq") # selector = ['"amenity"="bbq"', '"leisure"="picnic_table"'][0] selector = f'"{key}"="{value}"' ### Get the data ### nominatim = Nominatim() areaId = nominatim.query(area).areaId() query = overpassQueryBuilder(area=areaId, elementType='node', selector=selector, out='body') result = overpass.query(query) coords = np.array([(i.lat(), i.lon()) for i in result.elements()]) # print(area, areaId, key, value, selector) ### Make the map ### m = folium.Map(location=coords.mean(0)) # Add markers for coord in coords: lat_lon = str(coord.tolist())[1:-1] folium.Marker( popup = f"Coordinnates: {lat_lon}", location=coord.tolist()
def run_findCluster(data): """ kwargs contains the run_data """ # runpath_serializer = RunPathSerializer(data=data, partial=True) osm_endpoint = 'http://nominatim.openstreetmap.org/reverse' nm = Nominatim(endpoint=osm_endpoint, waitBetweenQueries=5) # serialize into json clusters = [ClusterSerializer(cluster).data for cluster in Cluster.objects.all()] run_areas = [ RunAreaSerializer(area).data for area in RunArea.objects.all() ] same_cluster = findCluster(data, clusters, dist_thresh=50) if same_cluster is None: # Reverse Geocode longitude and latitude # add a new cluster to the database lat = data['latitude'] lon = data['longitude'] rev_geocode = nm.query('', params={ 'lat': lat, 'lon': lon }) json = rev_geocode.toJSON() address = json['address'] if 'county' in address.keys(): area = address['county'] elif 'neighbourhood' in address.keys(): area = address['neighbourhood'] else: area = 'get key' if 'suburb' in address.keys(): suburb = address['suburb'] elif 'state' in address.keys(): suburb = address['state'] else: suburb = 'get key' area_name = area + ' ' + suburb # find if area_name exists match_run_area = findCluster(data, run_areas) # Add an area if match_run_area is None: runarea_serializer = RunAreaSerializer(data={ 'area_name': area_name, 'latitude': lat, 'longitude': lon }) if runarea_serializer.is_valid(): runarea_serializer.save() match_run_area = runarea_serializer.data # Add the cluster cluster_serializer = ClusterSerializer(data={ 'area': match_run_area['id'], 'latitude': lat, 'longitude': lon }) if cluster_serializer.is_valid(): # rdb.set_trace() # debug cluster_serializer.save() same_cluster = cluster_serializer.data # # set the clusterId on # # save the data using the RunPathSerializer runpath_data = RunPath.objects.filter(id=data['id'])[0] runpath_serializer = RunPathSerializer(runpath_data, data={'cluster': same_cluster['id']}, partial=True) # rdb.set_trace() # debug if runpath_serializer.is_valid(): runpath_serializer.save() # return 'Clustered as %s' % # rdb.set_trace() # debug return
#from OSMPythonTools.api import Api from OSMPythonTools.nominatim import Nominatim nominatim = Nominatim() from places import municipios busqueda = input("Cadena de busqueda: ") data = nominatim.query(busqueda).toJSON() print(data) print() print("MATCHES: " + str(len(data))) print() for place in range(len(data)): print(str(data[place]["osm_type"]) + " - " + str(data[place]["osm_id"])) print(data[place]["display_name"]) print()
import sys import json from datetime import datetime from OSMPythonTools.nominatim import Nominatim from OSMPythonTools.overpass import Overpass, overpassQueryBuilder # E.g. NYC city = sys.argv[1] # E.g. ny (i.e. the scanmap name) location = sys.argv[2] overpass = Overpass() nominatim = Nominatim() place = nominatim.query(city) query = overpassQueryBuilder(area=place.areaId(), elementType='node', selector='"man_made"="surveillance"', out='body') results = overpass.query(query) data = results.toJSON() # Format for POI ingestion script pois = [] for el in data['elements']: el['tags'].pop('man_made') text = '\n'.join('{}={}'.format(k, v) for k, v in el['tags'].items()) text = '{}\nosm_id={}'.format(text, el['id']).strip('\n') loc = nominatim.query(el['lat'], el['lon'], reverse=True) pois.append({
sum += temp[i][0]*temp[i+1][1] - temp[i+1][0]*temp[i][1] #if i != 0: # raz += temp[i][0] * temp[i-1] sum += temp[n-1][0]*temp[0][1] - temp[0][0]*temp[n-1][1] return fabs(sum/2) """ from OSMPythonTools.nominatim import Nominatim nominatim = Nominatim() nyc = nominatim.query('Perm') print(nyc.toJSON())""" #query = overpassQueryBuilder(bbox=[58.0065792, 56.2248863, 58.0103644, 56.2305674], elementType=['way','node'], selector='building', out='body',includeGeometry=True) #query = overpassQueryBuilder(area=nominatim.query('Perm').areaId(), elementType=['way','node'], selector=['building','"name"="Ленинский район"'], out='body',includeGeometry=True) #query = overpassQueryBuilder(area=nominatim.query('Perm, Свердловский район').areaId(), elementType=['way','node'], selector=['building'], out='body',includeGeometry=True) query = overpassQueryBuilder(area=nominatim.query('Perm, Свердловский район').areaId(), elementType=['way','node'], selector=['building'], out='body',includeGeometry=True) overpass = Overpass() huh = overpass.query(query) #print(huh.ways()) print(huh.countElements()) print(huh.countWays()) living_houses_w_levels = ['yes','apartments','dormitory','residential'] living_houses_wo_levels = ['house','bungalow','detached'] unliving_houses = ['roof', 'university', 'kindergarten', 'school', 'retail', 'commercial', 'office', 'church', 'hotel', 'garages', 'construction', 'train_station', 'cathedral', 'supermarket', 'service', 'offices', 'mosque', 'hospital', 'college', 'garage', 'warehouse', 'industrial', 'kiosk'] check_build=[] people = 0 max = 0
def maparea(area_name, lat=None, lon=None): clean_name = str(re.sub(r'[^a-zA-Z\d]', '', area_name)) nominatim = Nominatim() if area_name.isdigit(): areaId = int(area_name) else: areaId = nominatim.query(area_name).areaId() overpass = Overpass() query = overpassQueryBuilder(area=areaId, elementType='way', selector='"landuse"="residential"', out='geom') residential_areas = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="footway"', out='geom') roads_footway = overpass.query(query).ways() or [] query = overpassQueryBuilder( area=areaId, elementType='way', selector=['"highway"="service"', '"service"!="parking_aisle"'], out='geom') roads_cycleway = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="residential"', out='geom') roads_residential = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="tertiary"', out='geom') roads_tertiary = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="secondary"', out='geom') roads_secondary = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="primary"', out='geom') roads_primary = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="motorway"', out='geom') roads_motorway = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="trunk"', out='geom') roads_trunk = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="trunk_link"', out='geom') roads_trunk_link = overpass.query(query).ways() or [] query = overpassQueryBuilder(area=areaId, elementType='way', selector='"highway"="unclassified"', out='geom') roads_unclassified = overpass.query(query).ways() or [] #print (result.countElements()) #ax = plt.axes(projection=ccrs.PlateCarree(globe=ccrs.Globe(datum='WGS84', # ellipse='WGS84'))) #print(bbx) request = cimgt.OSM() terrain = cimgt.MapboxTiles( 'pk.eyJ1IjoiZWVzYWhlIiwiYSI6ImNqdGFjMTI1ODA1ZGc0NHRmN28wcG5ybnEifQ.ZBSMcdbWkjfjRgDVQLjfnw', 'terrain-rgb') plt.style.use('dark_background') fig, ax = plt.subplots(num=1, figsize=(10, 10), subplot_kw=dict(projection=request.crs, facecolor='#000000')) #print("trying to save debug terrain img") #plt.savefig("terrain.png") #print("saved") #ax.set_extent([26.6561, 22.6589, 59.611, 60.8409]) #ax.add_image(request, 10, zorder=0) roads = roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link def roads_in_bbox(bbox): ret = [] for road in roads: if road._json['bounds']['minlat'] < bbox['maxlat'] and road._json[ 'bounds']['maxlat'] > bbox['minlat'] and road._json[ 'bounds']['minlon'] < bbox['maxlon'] and road._json[ 'bounds']['maxlon'] > bbox['minlon']: ret.append(road) return ret # find areas completely enclosed inside a bounding box (not partially) def residential_areas_enclosed_in_bbox(bbox): ret = [] for area in residential_areas: if area._json['bounds']['maxlat'] < bbox['maxlat'] and area._json[ 'bounds']['minlat'] > bbox['minlat'] and area._json[ 'bounds']['maxlon'] < bbox['maxlon'] and area._json[ 'bounds']['minlon'] > bbox['minlon']: ret.append(area) return ret cll = [] bbx = None ''' processed = 0 roads = roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link print("processing total " + str(len(roads)) + " roads") for area in (roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link): if(processed % 500 == 0): print("processing number " + str(processed)) geometry = area._json['geometry'] lons = [] lats = [] for point in geometry: lons.append(point['lon']) lats.append(point['lat']) #mycoords.append( (point['lat'], ) ) xs = np.array(lons) ys = np.array(lats) xynps=ax.projection.transform_points(ccrs.Geodetic(), xs, ys) #print(xynps) #break ax.plot(xynps[:,0], xynps[:,1], "r", zorder=11, color='green', marker='o',linewidth=0.2, markersize=0, antialiased=False) processed+=1 ''' print("Found " + str(len(residential_areas)) + " residential areas in " + area_name) # What are we exactly doing here?? ''' for area in residential_areas: #print(vars(area)) geometry = area._json['geometry'] bbx = area._json['bounds'] lons = [] lats = [] for point in geometry: lons.append(point['lon']) lats.append(point['lat']) #mycoords.append( (point['lat'], ) ) xs = np.array(lons) ys = np.array(lats) xynps=ax.projection.transform_points(ccrs.Geodetic(), xs, ys) #print(xynps) #break plt.figure(1) ax.fill(xynps[:,0], xynps[:,1], "b", zorder=10, antialiased=False) cll.append( (lons,lats) ) #print(len(cll)) #ax.axis('off') ''' counter = 0 for area in residential_areas: #if(lat is not None): # bbx['minlon'] = lon # bbx['maxlon'] = lon # bbx['minlat'] = lat # bbx['maxlat'] = lat # width = 0 # height = 0 #else: bbx = area._json['bounds'] width = bbx['maxlon'] - bbx['minlon'] height = bbx['maxlat'] - bbx['minlat'] if (width < 0.0008 or height < 0.0008): print("area too small, skipping") continue #print(width) #print(height) zoom = 0.7 conv_factor = (2.0 * math.pi) / 360.0 lat = bbx['minlat'] lat = lat * conv_factor m1 = 111132.92 # latitude calculation term 1 m2 = -559.82 # latitude calculation term 2 m3 = 1.175 # latitude calculation term 3 m4 = -0.0023 # latitude calculation term 4 p1 = 111412.84 # longitude calculation term 1 p2 = -93.5 # longitude calculation term 2 p3 = 0.118 # longitude calculation term 3 # Calculate the length of a degree of latitude and longitude in meters latlen = m1 + (m2 * math.cos(2 * lat)) + (m3 * math.cos(4 * lat)) + \ (m4 * math.cos(6 * lat)) longlen = (p1 * math.cos(lat)) + (p2 * math.cos(3 * lat)) + \ (p3 * math.cos(5 * lat)) #print("lat len " + str(latlen)) #print("lon len " + str(longlen)) targetWidth = 2500 targetHeight = 2500 currentWidth = longlen * width currentHeight = latlen * height offset_w_meters = (targetWidth - currentWidth) / 2 offset_h_meters = (targetHeight - currentHeight) / 2 offset_w_angles = offset_w_meters / longlen offset_h_angles = offset_h_meters / latlen #print("currentWidth " + str(currentWidth)) #print("currentHeight " + str(currentHeight)) #print("offsetAngleW " + str(offset_w_angles)) #print("offsetAngleH " + str(offset_h_angles)) offsetW = offset_w_angles offsetH = offset_h_angles #my_cos=math.cos(rad) #print("my cos " + str(my_cos)) #test = 0.1 * abs(math.cos(abs(bbx['minlat']))) #print("test " + str(test)) #print("trying to make it " + str(zoom*test) + " degrees wide") #print("testOffsetW" + str(testOffsetW)) #offsetW = ((zoom*0.051122172576223) - width) / 2 #print("realoffsetW" + str(offsetW)) #offsetH = ((zoom*0.038) - height) / 2 #print("offsetH" + str(offsetH)) if offsetW < 0 or offsetH < 0: print("area too big, skipping") continue test_savename = outdir + "/" + clean_name + "_" + str(counter) + ".png" # continue if we already created this file if os.path.isfile(test_savename): counter += 1 continue #print(bbx) new_bbx = bbx.copy() try: new_bbx['minlon'] = bbx['minlon'] - offsetW new_bbx['maxlon'] = bbx['maxlon'] + offsetW new_bbx['minlat'] = bbx['minlat'] - offsetH new_bbx['maxlat'] = bbx['maxlat'] + offsetH except: print("FAILED, BBX: " + str(bbx)) pprint(area._json) # get population density ring = [[new_bbx['minlon'], new_bbx['minlat']], [new_bbx['minlon'], new_bbx['maxlat']], [new_bbx['maxlon'], new_bbx['maxlat']], [new_bbx['maxlon'], new_bbx['minlat']], [new_bbx['minlon'], new_bbx['minlat']]] ring_string = json.dumps(ring) if (lon is None): r = requests.post("http://sedac.ciesin.columbia.edu/arcgis/rest/services/sedac/geoprocessing/GPServer/pes-v1/execute", \ data={'Input_Feature_Set': '{ "geometryType": "esriGeometryPolygon","fields": [{"name": "id","type": "esriFieldTypeInteger"}],"spatialReference": {"wkid": 4326},"features": [{"geometry": {"rings": [ \ ' + ring_string + ' \ ],"spatialReference": {"wkid": 4326}},"attributes": {"id": 1}}]}' , 'f': 'json'}) json_result = json.loads(r.text) attributes = json_result['results'][0]['value']['features'][0][ 'attributes'] pop = attributes['POPULATION05'] area = attributes['LANDAREA'] density = pop / area if (density < 1000): print("density too small") continue #print(new_bbx) #exit() #print("bbx height " + str(new_bbx['maxlat'] - new_bbx['minlat'])) #red_fill = ax.fill(xynps[:,0], xynps[:,1], "r", zorder=10, antialiased=False) plt.figure("terrain") fig_terrain, ax_terrain = plt.subplots(figsize=(10, 10), subplot_kw=dict( projection=request.crs, facecolor='#000000')) try: ax_terrain.set_extent([ new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'], new_bbx['maxlat'] ]) except Exception: print(traceback.format_exc()) print(sys.exc_info()[0]) continue ax_terrain.add_image(terrain, 13) savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_t.png" plt.savefig(savename, dpi=_dpi, transparent=True, bbox_inches='tight', pad_inches=0, frameon=None) terrain_raster_261 = cv2.imread(savename) terrain_raster_256_b = terrain_raster_261[3:259, 3:259, 0] terrain_raster_256_g = terrain_raster_261[3:259, 3:259, 1] terrain_raster_256_r = terrain_raster_261[3:259, 3:259, 2] plt.figure(2) fig2, ax2 = plt.subplots(figsize=(10, 10), subplot_kw=dict(projection=request.crs, facecolor='#000000')) xynps = ax.projection.transform_points( ccrs.Geodetic(), np.asarray([new_bbx['minlon'], new_bbx['maxlon']]), np.asarray([new_bbx['minlat'], new_bbx['maxlat']])) #max_lonlat=ax.projection.transform_points(ccrs.Geodetic(), new_bbx['maxlon'], new_bbx['maxlat']) #ax2.set_extent([new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'], new_bbx['maxlat']]) ax2.set_extent([ new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'], new_bbx['maxlat'] ], crs=ccrs.Geodetic()) roads_current = roads_in_bbox(new_bbx) all_areas = residential_areas_enclosed_in_bbox(new_bbx) #print(str(len(all_areas)) + " areas in bbox") red_fills = [] for current_residential_area in (all_areas): bbx = current_residential_area._json['bounds'] width = bbx['maxlon'] - bbx['minlon'] height = bbx['maxlat'] - bbx['minlat'] if (width < 0.001 or height < 0.001): #print("area too small, skipping") continue geometry = current_residential_area._json['geometry'] lons = [] lats = [] for point in geometry: lons.append(point['lon']) lats.append(point['lat']) #mycoords.append( (point['lat'], ) ) xs = np.array(lons) ys = np.array(lats) xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys) #print("area " + str(current_residential_area)) red_fill2 = ax2.fill(xynps[:, 0], xynps[:, 1], "k", zorder=11, antialiased=False) red_fills.append(red_fill2) road_refs = [] savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_2.png" #print("trying to save file called " +savename) plt.savefig(savename, dpi=_dpi, facecolor='0.0', edgecolor='0.0', transparent=True, bbox_inches='tight', pad_inches=0, frameon=None) #exit() mask_261 = cv2.imread(savename, 0) mask_256 = mask_261[3:259, 3:259] #print("shape:" + str(mask.shape)) #exit() mask_256 = cv2.threshold(mask_256, 127, 255, cv2.THRESH_BINARY_INV)[1] # ensure binary #kernel = np.ones((3,3), np.uint8) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) mask_dilated_256 = cv2.dilate(mask_256, kernel, iterations=2) savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_3.png" cv2.imwrite(savename, mask_256) #for road_ref in (road_refs): # l = road_ref.pop(0) # wl = weakref.ref(l) # l.remove() # del l plt.figure(1) ax.set_extent([ new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'], new_bbx['maxlat'] ]) #print("fetch roads") #print("got " + str(len(roads))+ " roads ") #exit() #query = overpassQueryBuilder(bbox=[new_bbx['minlat'], new_bbx['minlon'], new_bbx['maxlat'], new_bbx['maxlon']], elementType='way', selector=['"highway"="residential"', '"highway"="tertiary"', '"highway"="secondary"', '"highway"="primary"', '"highway"="unclassified"', '"highway"="trunk"', '"highway"="trunk_link"'], out='geom') #roads_current = overpass.query(query).ways() #print("got num roads:") if len(roads_current) > 0: #print(len(roads_current)) processed = 0 for area in (roads_current): _type = area._json['tags']['highway'] #print(_type) if _type not in ['primary', 'secondary', 'highway', 'trunk']: continue geometry = area._json['geometry'] lons = [] lats = [] for point in geometry: lons.append(point['lon']) lats.append(point['lat']) #mycoords.append( (point['lat'], ) ) xs = np.array(lons) ys = np.array(lats) xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys) #print(xynps) #break plt.figure(1) road_ref = ax.plot(xynps[:, 0], xynps[:, 1], zorder=11, color='#000000', marker='o', linewidth=0.05, markersize=0, antialiased=False) road_refs.append(road_ref) processed += 1 savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_0.png" plt.savefig(savename, dpi=_dpi, facecolor='0.0', edgecolor='1.0', transparent=True, bbox_inches='tight', pad_inches=0, frameon=None) roads_primary_secondary_raster_261 = cv2.imread(savename, 0) roads_primary_secondary_raster_256 = roads_primary_secondary_raster_261[ 3:259, 3:259] roads_primary_secondary_raster_256 = cv2.threshold( roads_primary_secondary_raster_256, 127, 255, cv2.THRESH_BINARY_INV)[1] if len(roads_current) > 0: #print(len(roads_current)) processed = 0 for area in (roads_current): #if(processed % 500 == 0): #print("processing number " + str(processed)) geometry = area._json['geometry'] lons = [] lats = [] for point in geometry: lons.append(point['lon']) lats.append(point['lat']) #mycoords.append( (point['lat'], ) ) xs = np.array(lons) ys = np.array(lats) xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys) #print(xynps) #break plt.figure(1) road_ref = ax.plot(xynps[:, 0], xynps[:, 1], zorder=11, color='#000000', marker='o', linewidth=0.05, markersize=0, antialiased=False) road_refs.append(road_ref) processed += 1 savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_1.png" plt.savefig(savename, dpi=_dpi, facecolor='0.0', edgecolor='1.0', transparent=True, bbox_inches='tight', pad_inches=0, frameon=None) # FINISHED ALL MATPLOTLIB EXPORTS AT THIS POINT roads_raster_261 = cv2.imread(savename, 0) roads_raster_256 = roads_raster_261[3:259, 3:259] #b,g,r = cv2.split(masked_roads)'' roads_raster_256 = cv2.threshold( roads_raster_256, 127, 255, cv2.THRESH_BINARY_INV)[1] # ensure binary # This one will only display the roads that are behind residential areas for the SECOND image masked_roads_256 = cv2.bitwise_and(roads_raster_256, roads_raster_256, mask=mask_dilated_256) #masked_roads = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY_INV)[1] # ensure binary #savename = outdir + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_4.png" #cv2.imwrite(savename, masked_roads) # This one will only show the roads behind residential areas roads_raster_256 = cv2.bitwise_and(roads_raster_256, roads_raster_256, mask=(255 - mask_256)) roads_raster_256 = cv2.bitwise_or(roads_raster_256, roads_primary_secondary_raster_256) __width = mask_256.shape[1] __height = mask_256.shape[0] empty = np.zeros((__height, __width), dtype=np.uint8) #print(str(roads_raster.shape)) #print(str(mask.shape)) #print(str(empty.shape)) out = cv2.merge((roads_raster_256, empty, mask_256)) height = out.shape[0] width = out.shape[1] #print("width") #print(width) if width > 256: out = out[0:height, 0:256] masked_roads = masked_roads[0:height, 0:256] width = 256 if height > 256: out = out[0:256, 0:width] masked_roads = masked_roads[0:256, 0:width] height = 256 if width < 256 or height < 256: width_diff = 256 - width height_diff = 256 - height out = cv2.copyMakeBorder(out, height_diff, 0, width_diff, 0, cv2.BORDER_CONSTANT, value=[0, 0, 0]) masked_roads = cv2.copyMakeBorder(masked_roads, height_diff, 0, width_diff, 0, cv2.BORDER_CONSTANT, value=[0, 0, 0]) height = out.shape[0] width = out.shape[1] #savename = outdir + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_5.png" #cv2.imwrite(savename, out) combined_r = np.zeros((256, 512), dtype=np.uint8) combined_r[0:256, 0:256] = out[:, :, 0] combined_r[0:256, 256:512] = masked_roads_256 combined_g = np.zeros((256, 512), dtype=np.uint8) #combined_g[0:256, 0:256] = terrain_combined_256 #combined_g[0:256, 256:512] = masked_roads combined_b = np.zeros((256, 512), dtype=np.uint8) combined_b[0:256, 0:256] = out[:, :, 2] #combined_b[0:256, 256:512] = masked_roads #b,g,r = cv2.split(terrain_raster) terrain_combined_256 = -10000 + ( (terrain_raster_256_r * 256 * 256 + terrain_raster_256_g * 256 + terrain_raster_256_b) * 0.1) #water = cv2.inRange(terrain_combined_256, 0, 24467) smallest = terrain_combined_256.min() #terrain_combined_256.min() biggest = smallest + 100 # terrain_combined_256.max() #biggest = terrain_combined_256.max() _range = biggest - smallest #print ("biggest " + str(biggest)) #print ("range " + str(_range)) terrain_combined_256 = terrain_combined_256 - smallest print("smallest " + str(terrain_combined_256.min())) print("biggest " + str(terrain_combined_256.max())) terrain_combined_256 = (terrain_combined_256 / _range) * 255 #ret,terrain_combined_256 = cv2.threshold(terrain_combined_256,2,255,cv2.THRESH_BINARY) #terrain_combined_256 = cv2.bitwise_not(terrain_combined_256,terrain_combined_256, mask = water) combined_g[0:256, 0:256] = terrain_combined_256 combined_g[0:256, 0:256] = np.clip(terrain_combined_256, a_min=0, a_max=255) #terrain_debug_savename = outdir + "/" + clean_name + "_" + str(counter) + "_" + str(int(density)) + "_tdbg.png" #cv2.imwrite(terrain_debug_savename,terrain_new) savename = outdir + "/" + clean_name + "_" + str(counter) + "_" + str( int(density)) + ".png" out = cv2.merge((combined_r, combined_g, combined_b)) print("writing out file " + savename) cv2.imwrite(savename, out) for cur_red_fill in (red_fills): l = cur_red_fill.pop(0) wl = weakref.ref(l) l.remove() del l #red_fill2 = ax.fill(xynps[:,0], xynps[:,1], "r", zorder=15, antialiased=False) #savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_2.png" #plt.savefig(savename, dpi=128, facecolor='0.0', edgecolor='0.0', transparent=False, bbox_inches='tight', pad_inches=0, frameon=None) counter += 1 #l = red_fill.pop(0) #wl = weakref.ref(l) #l.remove() #del l #counter +=1 #l = red_fill2.pop(0) #wl = weakref.ref(l) #l.remove() #del l for road_ref in (road_refs): l = road_ref.pop(0) wl = weakref.ref(l) l.remove() del l #ax.fill(xynps[:,0], xynps[:,1], "b", zorder=10, antialiased=False) #ax.set_extent([bbx['minlat'], bbx['maxlat'], bbx['minlon'], bbx['maxlon']]) '''
def get_area_id(area): # Query Nominatom nominatim = Nominatim() return nominatim.query(area).areaId()