def test_slots_waiting(): api = overpass.API(debug=True) map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635) api.get(map_query) assert isinstance(api.slots_waiting, tuple)
def test_slots_available(): api = overpass.API(debug=True) map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635) api.get(map_query) assert api.slots_available <= 2 and api.slots_available >= 0
def saveGeojson(bottom, left, top, right, output): api = overpass.API() map_query = overpass.MapQuery(bottom, left, top, right) response = api.Get(map_query) f = open(output, 'w') f.write(gjson.dumps(response))
def test_geojson(): api = overpass.API() osm_geo = api.Get( overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635)) assert len(osm_geo['features']) > 1 osm_geo = api.Get('node(area:3602758138)[amenity=cafe]') assert len(osm_geo['features']) > 1
def test_geojson(): api = overpass.API(debug=True) map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635) osm_geo = api.get(map_query) assert len(osm_geo["features"]) > 1 osm_geo = api.get("node(area:3602758138)[amenity=cafe]") assert len(osm_geo["features"]) > 1
def test_geojson(self): api = overpass.API() #osm_elements = api.Get(overpass.MapQuery(37.86517,-122.31851,37.86687,-122.31635)) #print 'DEB osm_elements:', geojson.dumps(osm_elements,sort_keys=True,indent=2) osm_geo = api.Get(overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635), asGeoJSON=True) #with open('test.geojson','w') as f: # geojson.dump(osm_geo,f,indent=2,sort_keys=True) assert len(osm_geo['features']) > 1
def generateQuery(bbox: list) -> None: ''' given coordinates for a small bounding box will create a graph and store all its information in a text file with its coordinates in the directory of this file''' mapquery = overpass.MapQuery(bbox[0], bbox[1], bbox[2], bbox[3]) jsoninfo = api.get(mapquery, responseformat='json') ways = [ feature for feature in jsoninfo['elements'] if feature['type'] == 'way' ] nodes = [ feature for feature in jsoninfo['elements'] if feature['type'] == 'node' ] ## Create Graph currentMap = DirectedGraph(list()) assign_map(nodes, ways, currentMap) ## Create Data To-Store vertices = currentMap.get_vertices() edges = currentMap.get_edges() stored_vertices = dict() stored_edges = dict() for vertex in vertices: vdata = dict() vdata['id'] = vertex.get_num() vdata['lat'] = vertex.get_lat() vdata['lon'] = vertex.get_lon() vdata['edge'] = make_edges(vertex.get_edges()) stored_vertices[vertex.get_num()] = vdata stored_edges = make_edges(edges) graph = dict() graph['vertices'] = stored_vertices graph['edges'] = stored_edges counter.value += 1 name = './MapFiles/{}_{}_{}_{}.json'.format(bbox[0], bbox[1], bbox[2], bbox[3]) with open(name, 'w+') as outfile: json.dump(graph, outfile)
def __init__(self, area, cache_extracts=True, from_file=False, osm_file=None): """ Args: area(str): geographical area to be fetched and turned into a map """ self.area = area self.from_file = from_file self.osm_file = osm_file self.bounds_to_fetch = BOUNDS_DICT[area] self.minlat = self.bounds_to_fetch[0] self.minlon = self.bounds_to_fetch[1] self.maxlat = self.bounds_to_fetch[2] self.maxlon = self.bounds_to_fetch[3] self.extracts_path = os.getcwd() + '/extracts/' if from_file and not cache_extracts: self.filename_to_use = self.osm_file else: self.filename_to_use = None self.oldfile_name = None self.check_for_cached_extract() if self.filename_to_use is None: if self.oldfile_name: os.remove(self.extracts_path + self.oldfile_name) curr_time = str(datetime.datetime.utcnow()).replace(" ", "_") self.filename_to_use = self.area + curr_time + ".osm" api = overpass.API(timeout=600) map_query = overpass.MapQuery(self.minlat, self.minlon, self.maxlat, self.maxlon) response = api.get(map_query, responseformat="xml") self.filename_to_use = self.extracts_path + self.filename_to_use with open(self.filename_to_use, "w") as f: f.write(response) elif cache_extracts: self.filename_to_use = self.extracts_path + self.filename_to_use self.handler = OSMHandler(self.filename_to_use) self.handler.apply_file(self.filename_to_use, locations=True) self.way_list = self.handler.way_list self.bounds = (self.minlat, self.minlon, self.maxlat, self.maxlon) self.graph = Graph(WayGraph(self.way_list))
def getData(self, filespec=None): logging.info("Downloading data using Overpass") api = overpass.API(timeout=600, debug=True) query = overpass.MapQuery(self.bbox[2], self.bbox[0], self.bbox[3], self.bbox[1]) try: response = api.get(query, responseformat="xml") except: logging.error("Overpass query failed! Sometimes the server is overloaded") return False if filespec is None and self.filespec is None: outfile = open('out.osm', 'w') elif filespec is None and self.filespec is not None: outfile = open(self.filespec, 'w') else: outfile = open(filespec, 'w') logging.info("Writing OSM data to %s" % self.filespec) outfile.write(response) outfile.close() return True
import overpass api = overpass.API() #print api.get('node["name"]="Oslo"', responseformat='xml') mapquery = overpass.MapQuery(59.62, 10.7906 , 59.68, 10.79563) requested_map = api.get(mapquery,responseformat='xml') f = open("mymap.xml", "wb") f.write(requested_map.encode('ascii', 'ignore').decode('ascii')) f.close()
''' organizes the stupid bbox coords from the website ''' boundingbox = list() boundingbox.append(lst[1]) boundingbox.append(lst[0]) boundingbox.append(lst[3]) boundingbox.append(lst[2]) return boundingbox ##bbox = bBox([-117.866697,33.622923,-117.797937,33.683809]) ##bbox = bBox([-117.850221,33.636932,-117.830969,33.653298]) ## Generate Query api = overpass.API(timeout=600) mapquery = overpass.MapQuery(bbox[0], bbox[1], bbox[2], bbox[3]) ## Organize Data from JSON jsoninfo = api.get(mapquery, responseformat='json') ways = [ feature for feature in jsoninfo['elements'] if feature['type'] == 'way' ] nodes = [ feature for feature in jsoninfo['elements'] if feature['type'] == 'node' ] ## Create the Graph sampleMap = DirectedGraph(list()) ## Helpers
def createImage(path, fileformat): import tempfile import cairo try: #DPD - get unquote regardless of Python version from urllib.parse import unquote #Python3 except ImportError: from urlparse import unquote #Python2 import overpass #For real-time data query import time import psycopg2 p = parse_query(path) EPSG900913 = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over" C_SCALE_FACTOR = 1.4 SCALE_FACTOR = p['dpi'] / 72.0 S2P = SCALE_FACTOR * 360 / 0.127 # Feature widths/diameters and stroke thicknesses, in m. From the ISOM/ISSOM specs. SC_W = 0.007 * C_SCALE_FACTOR SC_T = 0.00035 * C_SCALE_FACTOR C_R = 0.003 * C_SCALE_FACTOR CDOT_R = 0.00035 * C_SCALE_FACTOR C_T = 0.00035 * C_SCALE_FACTOR CL_H = 0.005 * C_SCALE_FACTOR CTEXT_S = 0.005 * C_SCALE_FACTOR CONTENT_NM = 0.0045 MAP_NM = 0.014 MAP_EM = 0.008 MAP_SM = 0.013 MAP_WM = 0.008 ADORN_TITLE_SM = 0.002 ADORN_SCALE_SM = 0.005 ADORN_SCALEBAR_SM = 0.002 ADORN_SCALEBAR_LARGETICK = 0.002 ADORN_SCALEBAR_SMALLTICK = 0.001 ADORN_SCALEBAR_PADDING = 0.002 ADORN_ATTRIB_NM = 0.0035 ADORN_URL_NM = 0.001 ADORN_LOGO_SCALE = 0.175 * SCALE_FACTOR #0.038 ADORN_LOGO_SCALE_IOA = 0.175 * SCALE_FACTOR #= 0.25 ADORN_ARROW_W = 0.012 ADORN_LOGO_W = 0.018 style = p['style'] if style != "crew" and style != 'blueprint' and style != "urban_skeleton" and style != "streeto" and style != "oterrain" and style != "adhoc": return "Unknown style." PAPER_W = float(p['paper'].split(",")[0]) PAPER_H = float(p['paper'].split(",")[1]) scale = int(p['scale']) clat = int(p['centre'].split(",")[0]) clon = int(p['centre'].split(",")[1]) try: rotation = float(p['rotation']) except: rotation = 0 try: title = p['title'] except: title = "OpenOrienteeringMap" mapid = p.get('mapid', 'new') slon = slat = flon = flat = 0 if 'start' in p: slat = flat = int(p['start'].split(",")[0]) slon = flon = int(p['start'].split(",")[1]) if 'finish' in p: flat = int(p['finish'].split(",")[0]) flon = int(p['finish'].split(",")[1]) controlsArr = [] if 'controls' in p: controlsArr = p['controls'].split(",") crossesArr = [] if 'crosses' in p: crossesArr = p['crosses'].split(",") cpsArr = [] if 'cps' in p: cpsArr = p['cps'].split(",") projection = mapnik.Projection(EPSG900913) wgs84lat = mapnik.Coord(clon, clat).inverse(projection).y wgs84lon = mapnik.Coord(clon, clat).inverse(projection).x scaleCorrectionFactor = math.cos(wgs84lat * math.pi / 180) scaleCorrected = scale / scaleCorrectionFactor #get declination from local Python web service (declination.py; mod_wsgi alias for /wmm) wmmParams = {'lat': str(wgs84lat), 'lon': str(wgs84lon)} wmmResponse = requests.get(web_root + "wmm", params=wmmParams) magdec = float(wmmResponse.text) if style == "adhoc": MAP_EM = MAP_WM MAP_NM = MAP_WM MAP_SM = MAP_WM MAP_W = PAPER_W - MAP_WM - MAP_EM MAP_H = PAPER_H - MAP_NM - MAP_SM EXTENT_W = MAP_W * math.cos(rotation) + MAP_H * abs(math.sin(rotation)) EXTENT_H = MAP_H * math.cos(rotation) + MAP_W * abs(math.sin(rotation)) mapSLat = clat - (EXTENT_H / 2) * scaleCorrected mapNLat = clat + (EXTENT_H / 2) * scaleCorrected mapWLon = clon - (EXTENT_W / 2) * scaleCorrected mapELon = clon + (EXTENT_W / 2) * scaleCorrected pagePolyUnrotated = ((clon - (MAP_W / 2 + MAP_WM) * scaleCorrected, clat - (MAP_H / 2 + MAP_SM) * scaleCorrected), (clon - (MAP_W / 2 + MAP_WM) * scaleCorrected, clat + (MAP_H / 2 + MAP_NM) * scaleCorrected), (clon + (MAP_W / 2 + MAP_EM) * scaleCorrected, clat + (MAP_H / 2 + MAP_NM) * scaleCorrected), (clon + (MAP_W / 2 + MAP_EM) * scaleCorrected, clat - (MAP_H / 2 + MAP_SM) * scaleCorrected)) pagePoly = (rotate((clon, clat), pagePolyUnrotated[0], rotation), rotate((clon, clat), pagePolyUnrotated[1], rotation), rotate((clon, clat), pagePolyUnrotated[2], rotation), rotate((clon, clat), pagePolyUnrotated[3], rotation)) styleFile = home + "/styles/" + style + ".xml" with open(styleFile, mode="r") as f: styleString = f.read() bbox2 = mapnik.Box2d(mapWLon, mapSLat, mapELon, mapNLat).inverse(projection) if len(mapid ) == 13: #If existing id, use that, otherwise generate new one. tmpid = "h" + mapid #Add "h" prefix - postgres tables can't start with a number else: tmpid = "h" + hex(int(time.time()))[2:10] + hex( int(time.time() * 1000000) % 0x100000)[2:7] styleFile = home + "/styles/" + tmpid + ".xml" # Get contour attribution from custom Postgres DB table # based on centre location and type (e.g. "LIDAR") of contour selected. conn = psycopg2.connect(database="gis", user="******", host="127.0.0.1", port="5432") cur = conn.cursor() cur.execute( """ select attrib, tablename from attrib WHERE ST_Within(ST_SetSRID(ST_Point(%s, %s),900913),way) and type = %s; """, (clon, clat, p['contour'])) # If at least 1 hit choose the first one, otherwise no contours available. if cur.rowcount > 0: db_result = cur.fetchone() contour_text = "Contours: " + db_result[0] contour_table = db_result[1] else: contour_text = "" contour_table = "lidar_null" # If stylefile exists, data has been recently fetched - can use existing DB tables. # Recreate stylefile regardless - might need a new style on existing data. if not os.path.isfile(styleFile): # api = overpass.API() api = overpass.API( endpoint="https://overpass.kumi.systems/api/interpreter", timeout=120) MapQuery = overpass.MapQuery(bbox2.miny, bbox2.minx, bbox2.maxy, bbox2.maxx) try: response = api.get(MapQuery, responseformat="xml") except Exception as e: return "Overpass API error: " + type(e).__name__ + ", " + str(e) + "\n" + \ "Use the following ID to recover your map: " + tmpid[1:] tmpname = "/tmp/" + tmpid + ".osm" with open(tmpname, mode="wb") as f: f.write(response.encode("utf-8")) # Populate Postgres db with data, using tables with temporary id prefix os.system("osm2pgsql -d otf1 --hstore --multi-geometry --number-processes 1" + \ " -p " + tmpid + \ " --tag-transform-script /home/osm/openstreetmap-carto/openstreetmap-carto.lua" + \ " --style /home/osm/openstreetmap-carto/openstreetmap-carto.style -C 200 -U osm "+ tmpname) os.unlink(tmpname) #Finished with temporary osm data file - delete. if p['contour'] == "SRTM" or p['contour'] == "COPE": #Now get contours using phyghtmap: #Use Phyghtmap just to get DEM data - process separately if p['contour'] == "SRTM": sourceText = " --source=srtm1 --srtm-version=3 " else: sourceText = " --source=cope1" phyString="phyghtmap --area="+str(bbox2.minx-0.0002)+":"+str(bbox2.miny-0.0002)+":"+ \ str(bbox2.maxx+0.0002)+":"+str(bbox2.maxy+0.0002)+ sourceText + \ " --earthexplorer-user="******" --earthexplorer-password="******" --hgtdir=" + home_base + "/hgt -p " + home_base + "/"+tmpid + " >> " + home_base + "/phy.log" os.system(phyString) #Merge file(s) into single virtual dataset (prevents contour boundaries at degree grid lines) os.system("gdalbuildvrt " + home_base + "/" + tmpid + "_a.vrt " + home_base + "/" + tmpid + "_lon*") #Resample at 10m intervals to get smoother contours. Reproject at the same time os.system("gdalwarp -r cubic -tr 10 10 -s_srs EPSG:4326 -t_srs EPSG:3857 -te_srs EPSG:4326 -te " + \ str(bbox2.minx-0.0001)+" "+str(bbox2.miny-0.0001)+" "+ \ str(bbox2.maxx+0.0001)+" "+str(bbox2.maxy+0.0001)+ \ " -of SAGA -ot Float32 " + home_base + "/"+tmpid + "_a.vrt " + home_base + "/"+tmpid + ".sdat") #Apply Guassian blur to further smooth contours os.system( "saga_cmd grid_filter \"Gaussian Filter\" -SIGMA 2.0 -RADIUS 12 -INPUT " + home_base + "/" + tmpid + ".sdat -RESULT " + home_base + "/" + tmpid + "_s") #Generate contours os.system("gdal_contour -b 1 -a height -i " + p['interval'] + " " + home_base + "/" + tmpid + "_s.sdat " + home_base + "/" + tmpid + ".shp") # If contour generation failed, use a dummy dataset so that the DB table # gets created and the SQL query returns without errors. try: os.stat(home_base + "/" + tmpid + ".shp") except: os.system("cp " + home_base + "/null.shp " + home_base + "/" + tmpid + ".shp") os.system("cp " + home_base + "/null.shx " + home_base + "/" + tmpid + ".shx") os.system("cp " + home_base + "/null.dbf " + home_base + "/" + tmpid + ".dbf") #then load contours to database os.system("shp2pgsql -g way -s 3857 " + home_base + "/" + tmpid + ".shp " + tmpid + "_srtm_line | psql -h localhost -p 5432 -U osm -d otf1") contour_table = tmpid + "_srtm_line" import glob for i in glob.glob(home_base + '/' + tmpid + '*'): os.unlink(i) #Finished with temporary files - delete. else: # If SRTM or COPE contours, still need to point to correct contour table for reused data so: if p['contour'] == "SRTM" or p['contour'] == "COPE": contour_table = tmpid + "_srtm_line" # Need a custom Mapnik style file to find tables with temo id prefix. # Therefore inject "prefix" entity into appropriate base style definition and save using temp id as name. if p.get( 'drives', "no" ) != "no": #Render driveways as near-transparent if not selected. Allows recovery later if needed. driveway_colour = "#010101FF" else: driveway_colour = "#01010101" import re insertstring="%settings;\n<!ENTITY prefix \"" + tmpid + "\">" + \ "\n<!ENTITY driveway \"" + driveway_colour + "\">" + \ "\n<!ENTITY rail \"" + ("yes" if p.get('rail',"yes") != "no" else "no") + "\">" + \ "\n<!ENTITY walls \"" + ("yes" if p.get('walls',"yes") != "no" else "no") + "\">" + \ "\n<!ENTITY trees \"" + ("yes" if p.get('trees',"yes") != "no" else "no") + "\">" + \ "\n<!ENTITY hedges \"" + ("yes" if p.get('hedges',"yes") != "no" else "no") + "\">" + \ "\n<!ENTITY fences \"" + ("yes" if p.get('fences',"yes") != "no" else "no") + "\">" + \ "\n<!ENTITY lidartable \"" + contour_table + "\">" + \ "\n<!ENTITY contourSeparation \"" + p['interval'] + "\">" + \ "\n<!ENTITY layers-contours SYSTEM \"inc/layers_contours_" + p['contour'] + ".xml.inc\">" searchstring = "\%settings;" styleString = re.sub(searchstring, insertstring, styleString) with open(styleFile, mode="w") as f: f.write(styleString) cbbox = mapnik.Box2d(mapWLon, mapSLat, mapELon, mapNLat) # Limit the size of map we are prepared to produce to roughly A2 size. if PAPER_W * PAPER_H > 0.25 and style != "adhoc": return "Map too large. Try increasing the scale value or using a smaller paper size." if scale > 50000 and style != "adhoc": return "Scale too small. Try using a lower scale value." # Calculate scale, for scale bar and grid lines scaleBarMetres = 500 if scale < 10000: scaleBarMetres = 200 scaleBarW = scaleBarMetres / float(scale) # Create map map = mapnik.Map(int(EXTENT_W * S2P), int(EXTENT_H * S2P)) # Load map configuration mapnik.load_map(map, styleFile) # Zoom the map to the Gbounding box map.zoom_to_box(cbbox) file = tempfile.NamedTemporaryFile() surface = None if fileformat == 'jpg' or fileformat == 'pre': surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(PAPER_W * S2P), int(PAPER_H * S2P)) elif fileformat == 'svg': surface = cairo.SVGSurface(file.name, PAPER_W * S2P / SCALE_FACTOR, PAPER_H * S2P / SCALE_FACTOR) surface.set_device_scale(1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR) versions = surface.get_versions() version = versions[1] surface.restrict_to_version(version) else: surface = cairo.PDFSurface(file.name, PAPER_W * S2P / SCALE_FACTOR, PAPER_H * S2P / SCALE_FACTOR) surface.set_device_scale(1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR) # Adornments - Title swoosh back ctx = cairo.Context(surface) ctx.translate(0, 0) ctx.set_line_width(1 * SCALE_FACTOR) ctx.move_to(0, 0) ctx.rel_line_to(0, 0.25 * PAPER_H * S2P) ctx.rel_line_to(0.2 * PAPER_W * S2P, 0) ctx.rel_line_to(0.4 * PAPER_W * S2P, -0.25 * PAPER_H * S2P) ctx.close_path() ctx.set_source_rgb(0.91, 0.15, 0.28) if style != 'blueprint': ctx.fill() #Adornments - Attrib swoosh back ctx = cairo.Context(surface) ctx.translate(0, 0) ctx.set_line_width(1 * SCALE_FACTOR) ctx.move_to(PAPER_W * S2P, PAPER_H * S2P) ctx.rel_line_to(0, -0.25 * PAPER_H * S2P) ctx.rel_line_to(-0.2 * PAPER_W * S2P, 0) ctx.rel_line_to(-0.4 * PAPER_W * S2P, 0.25 * PAPER_H * S2P) ctx.close_path() ctx.set_source_rgb(0.12, 0.5, 0.65) if style != "blueprint": ctx.fill() # Background map ctx = cairo.Context(surface) ctx.set_operator(cairo.Operator.OVER) ctx.translate(MAP_WM * S2P, MAP_NM * S2P) ctx.rectangle(0, 0, MAP_W * S2P, MAP_H * S2P) ctx.clip() #Clip to map area ctx.save() ctx.translate(MAP_W * S2P / 2, MAP_H * S2P / 2) # translate origin to the center ctx.rotate(rotation) ctx.translate(-EXTENT_W * S2P / 2, -EXTENT_H * S2P / 2) mapnik.render(map, ctx, SCALE_FACTOR, 0, 0) ctx.restore() if style == "adhoc": ctx = cairo.Context(surface) ctx.set_source_rgb(1, 1, 1) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(0.5 * SCALE_FACTOR) text = path ctx.translate(MAP_WM * S2P, (MAP_NM + MAP_H + 0.001) * S2P) ctx.show_text(text) if fileformat == 'jpg' or fileformat == 'pre': surface.write_to_png(file.name) else: surface.finish() return file # Add grid lines in same layer - allows darken comp-op if style != "blueprint" and p.get('grid', "yes") != "no": ctx.set_source_rgb(0.5, 0.5, 1) ctx.set_operator(cairo.Operator.MULTIPLY) ctx.set_line_width(0.5 * SCALE_FACTOR) northSpacing = scaleBarW / math.cos(magdec * math.pi / 180 + rotation) shift = MAP_H * S2P * math.tan(magdec * math.pi / 180 + rotation) * 0.5 lines = range(int(-northSpacing * S2P / 2), int((MAP_W + northSpacing) * S2P), int(northSpacing * S2P)) for line in lines: ctx.move_to(line + shift, 0) ctx.line_to(line - shift, MAP_H * S2P) ctx.stroke() # Start control if slon != 0 and slat != 0: ctx = cairo.Context(surface) ctx.set_operator(cairo.Operator.MULTIPLY) ctx.set_source_rgb(0.651, 0.149, 1) ctx.translate(MAP_WM * S2P + MAP_W * S2P / 2, MAP_NM * S2P + MAP_H * S2P / 2) # translate origin to the center ctx.rotate(rotation) # rotate map to correct angle ctx.translate(-EXTENT_W * S2P / 2, -EXTENT_H * S2P / 2) # set origin to NW corner ctx.set_line_width(SC_T * S2P) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.translate((slon - mapWLon) * EXTENT_W * S2P / (mapELon - mapWLon), (mapNLat - slat) * EXTENT_H * S2P / (mapNLat - mapSLat)) startRot = 0 #rotation of start triangle - if linear course and at least 1 control if len(controlsArr) > 0 and p.get('linear', "no") != "no": startRot = math.atan2( slat - float(controlsArr[2]), float(controlsArr[3]) - slon) - math.pi / 6 + rotation ctx.rotate(startRot - rotation) ctx.move_to(0, -0.577 * SC_W * S2P) ctx.rel_line_to(-0.5 * SC_W * S2P, 0.866 * SC_W * S2P) ctx.rel_line_to(SC_W * S2P, 0) ctx.close_path() ctx.stroke() ctx.rotate(-startRot) #Finish control (same place as start, unless separate finish coords) if flon != 0 and flat != 0: ctx.rotate(rotation) ctx.translate((flon - slon) * EXTENT_W * S2P / (mapELon - mapWLon), (slat - flat) * EXTENT_H * S2P / (mapNLat - mapSLat)) ctx.set_line_width(C_T * S2P) ctx.arc(0, 0, C_R * S2P * 1.2, 0, 2 * math.pi) #Outer circle ctx.stroke() ctx.arc(0, 0, C_R * S2P * 0.8, 0, 2 * math.pi) #inner circle ctx.stroke() # Controls and labels if len(controlsArr) > 0: ctx = cairo.Context(surface) ctx.translate(MAP_WM * S2P + MAP_W * S2P / 2, MAP_NM * S2P + MAP_H * S2P / 2) # translate origin to the center ctx.rotate(rotation) ctx.translate(-EXTENT_W * S2P / 2, -EXTENT_H * S2P / 2) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(CTEXT_S * S2P) numControls = len(controlsArr) // 4 #Draw white halo around control numbers for legibility on complex maps ctx.set_operator(cairo.Operator.SOURCE) ctx.set_source_rgb(1, 0.997, 1) for i in range(numControls): text = controlsArr[4 * i] labelAngle = float(controlsArr[4 * i + 1]) controllat = float(controlsArr[4 * i + 2]) controllon = float(controlsArr[4 * i + 3]) controllatP = (mapNLat - controllat) * EXTENT_H / (mapNLat - mapSLat) controllonP = (controllon - mapWLon) * EXTENT_W / (mapELon - mapWLon) x_bearing, y_bearing, width, height = ctx.text_extents(text)[:4] labelX = C_R * 2.5 * math.sin(math.pi * labelAngle / 180) labelY = C_R * 2.5 * math.cos(math.pi * labelAngle / 180) ctx.save() ctx.translate(controllonP * S2P, controllatP * S2P) ctx.rotate(-rotation) ctx.move_to(labelX * S2P - width / 2, -labelY * S2P + height / 2) ctx.text_path(text) ctx.set_line_width(C_T * S2P) ctx.stroke_preserve() ctx.fill() ctx.restore() ctx.set_source_rgb(0.651, 0.149, 1) ctx.set_operator(cairo.Operator.MULTIPLY) lastlonP = (slon - mapWLon) * EXTENT_W / (mapELon - mapWLon) lastlatP = (mapNLat - slat) * EXTENT_H / (mapNLat - mapSLat) for i in range(numControls): text = controlsArr[4 * i] labelAngle = float(controlsArr[4 * i + 1]) controllat = float(controlsArr[4 * i + 2]) controllon = float(controlsArr[4 * i + 3]) controllatP = (mapNLat - controllat) * EXTENT_H / (mapNLat - mapSLat) controllonP = (controllon - mapWLon) * EXTENT_W / (mapELon - mapWLon) ctx.move_to((controllonP + C_R) * S2P, controllatP * S2P) ctx.set_line_width(C_T * S2P) ctx.arc(controllonP * S2P, controllatP * S2P, C_R * S2P, 0, 2 * math.pi) ctx.stroke() ctx.move_to((controllonP + CDOT_R) * S2P, controllatP * S2P) ctx.arc(controllonP * S2P, controllatP * S2P, CDOT_R * S2P, 0, 2 * math.pi) ctx.fill() if p.get('linear', "no") != "no": angle = math.atan2((controllatP - lastlatP), (controllonP - lastlonP)) start2lonP = lastlonP + math.cos(angle) * C_R * (1.3 if i == 0 else 1.0) start2latP = lastlatP + math.sin(angle) * C_R * (1.3 if i == 0 else 1.0) end2lonP = controllonP - math.cos(angle) * C_R end2latP = controllatP - math.sin(angle) * C_R ctx.move_to(start2lonP * S2P, start2latP * S2P) ctx.line_to(end2lonP * S2P, end2latP * S2P) #draw line between controls lastlonP = controllonP lastlatP = controllatP x_bearing, y_bearing, width, height = ctx.text_extents(text)[:4] labelX = C_R * 2.5 * math.sin(math.pi * labelAngle / 180) labelY = C_R * 2.5 * math.cos(math.pi * labelAngle / 180) ctx.save() ctx.translate(controllonP * S2P, controllatP * S2P) ctx.rotate(-rotation) ctx.move_to(labelX * S2P - width / 2, -labelY * S2P + height / 2) ctx.show_text(text) ctx.restore() # draw line from last control to finish if p.get('linear', "no") != "no": controllatP = (mapNLat - flat) * EXTENT_H / (mapNLat - mapSLat) controllonP = (flon - mapWLon) * EXTENT_W / (mapELon - mapWLon) angle = math.atan2((controllatP - lastlatP), (controllonP - lastlonP)) start2lonP = lastlonP + math.cos(angle) * C_R start2latP = lastlatP + math.sin(angle) * C_R end2lonP = controllonP - math.cos(angle) * C_R * 1.2 end2latP = controllatP - math.sin(angle) * C_R * 1.2 ctx.move_to(start2lonP * S2P, start2latP * S2P) ctx.line_to(end2lonP * S2P, end2latP * S2P) ctx.stroke() # Crosses and labels if len(crossesArr) > 0: #ctx = cairo.Context(surface) ctx.set_source_rgb(0.651, 0.149, 1) ctx.set_operator(cairo.Operator.MULTIPLY) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) ctx.set_font_size(CTEXT_S * S2P / 1.5) #ctx.set_source_rgb(1, 0, 0) numCrosses = len(crossesArr) // 2 for i in range(numCrosses): text = "X" controllat = float(crossesArr[2 * i]) controllon = float(crossesArr[2 * i + 1]) controllatP = (mapNLat - controllat) * EXTENT_H / (mapNLat - mapSLat) controllonP = (controllon - mapWLon) * EXTENT_W / (mapELon - mapWLon) x_bearing, y_bearing, width, height = ctx.text_extents(text)[:4] ctx.move_to((controllonP) * S2P - width / 2, (controllatP) * S2P + height / 2) ctx.show_text(text) #Crossing points and labels if len(cpsArr) > 0: #ctx = cairo.Context(surface) ctx.set_source_rgb(0.651, 0.149, 1) ctx.set_operator(cairo.Operator.DARKEN) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(CTEXT_S * S2P / 1.1) #ctx.set_source_rgb(1, 0, 0) numCps = len(cpsArr) // 3 for i in range(numCps): text = "][" controlAngle = float(cpsArr[3 * i]) controllat = float(cpsArr[3 * i + 1]) controllon = float(cpsArr[3 * i + 2]) controlAngleRads = math.pi * controlAngle / 180 controllatP = (mapNLat - controllat) * EXTENT_H / (mapNLat - mapSLat) controllonP = (controllon - mapWLon) * EXTENT_W / (mapELon - mapWLon) x_bearing, y_bearing, width, height, x_advance, y_advance = ctx.text_extents( text)[:6] ctx.move_to((controllonP) * S2P, (controllatP) * S2P) ctx.rotate(controlAngleRads) ctx.rel_move_to(-width / 2, height / 3.5) ctx.show_text(text) ctx.rotate(-1.0 * controlAngleRads) #ctx.save() # Adornments - Title ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL) if style == 'blueprint': ctx.select_font_face("Impact", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) text = unquote(title) if len(text) > 26: ctx.set_font_size(15 * SCALE_FACTOR) elif len(text) > 18: ctx.set_font_size(18 * SCALE_FACTOR) else: ctx.set_font_size(21 * SCALE_FACTOR) ctx.translate((MAP_WM + 0.014) * S2P, (MAP_NM - ADORN_TITLE_SM) * S2P) #add space to left for logo if style == 'blueprint': ctx.set_source_rgb(0, 0.5, 0.8) else: ctx.set_source_rgb(1, 1, 1) ctx.show_text(text.upper()) # Adornments - Scale Text ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) text = "scale 1:" + str(scale) if style == "oterrain" or style == "streeto": text = "scale 1:" + str(scale) + ", contours " + p['interval'] + "m" ctx.set_source_rgb(0, 0, 0) if style == 'blueprint': ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(11 * SCALE_FACTOR) width = ctx.text_extents(text)[4] ctx.translate( (MAP_WM + MAP_W) * S2P - width - (ADORN_ARROW_W + ADORN_LOGO_W) * S2P, (MAP_NM - ADORN_SCALE_SM) * S2P) ctx.show_text(text) # Adornments - Scale Bar and Caption ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) text = str(scaleBarMetres) + "m" ctx.set_source_rgb(0, 0, 0) if style == 'blueprint': ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(7 * SCALE_FACTOR) width = ctx.text_extents(text)[4] barCaptionX = (MAP_WM + MAP_W - (ADORN_ARROW_W + ADORN_LOGO_W)) * S2P - width ctx.translate(barCaptionX, (MAP_NM - ADORN_SCALEBAR_SM) * S2P) ctx.show_text(text) ctx.set_line_width(0.5 * SCALE_FACTOR) ctx.move_to((-scaleBarW - ADORN_SCALEBAR_PADDING) * S2P, 0) ctx.rel_line_to(0, -ADORN_SCALEBAR_LARGETICK * S2P) ctx.rel_line_to(0, ADORN_SCALEBAR_LARGETICK * S2P) ctx.rel_line_to(scaleBarW * S2P / 2, 0) ctx.rel_line_to(0, -ADORN_SCALEBAR_SMALLTICK * S2P) ctx.rel_line_to(0, ADORN_SCALEBAR_SMALLTICK * S2P) ctx.rel_line_to(scaleBarW * S2P / 2, 0) ctx.rel_line_to(0, -ADORN_SCALEBAR_LARGETICK * S2P) ctx.stroke() # Adornments - North Arrow ctx = cairo.Context(surface) ctx.translate((MAP_WM + MAP_W - ADORN_LOGO_W) * S2P - width, (CONTENT_NM + 0.004) * S2P) #set to centre of symbol... ctx.rotate( magdec * math.pi / 180 + rotation) #so that rotation doesn't add translation. Point to mag. N ctx.set_line_width(1 * SCALE_FACTOR) ctx.set_source_rgb(0, 0, 0) if style == 'blueprint': ctx.set_source_rgb(0, 0.5, 0.8) ctx.move_to(0, -0.004 * S2P) ctx.line_to(0.001 * S2P, -0.002 * S2P) ctx.line_to(-0.001 * S2P, -0.002 * S2P) ctx.close_path() ctx.fill() ctx.move_to(0, -0.003 * S2P) ctx.line_to(0, 0.004 * S2P) ctx.stroke() ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.move_to(-0.001 * S2P, 0.001 * S2P) ctx.rel_line_to(0, -0.002 * S2P) ctx.rel_line_to(0.002 * S2P, 0.002 * S2P) ctx.rel_line_to(0, -0.002 * S2P) ctx.stroke() # Adornments - Logo if style != "blueprint": logoSurface = cairo.ImageSurface.create_from_png(home + "/images/oflogo.png") ctx = cairo.Context(surface) width = logoSurface.get_width() * ADORN_LOGO_SCALE ctx.translate((MAP_WM + MAP_W) * S2P - width, CONTENT_NM * S2P) ctx.scale(ADORN_LOGO_SCALE, ADORN_LOGO_SCALE) ctx.set_source_surface(logoSurface, 0, 0) ctx.paint() # Adornments - Attribution left line 1 ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL) ctx.set_source_rgb(0.12, 0.5, 0.65) if style == 'blueprint': ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(7 * SCALE_FACTOR) text = "Map data: © OpenStreetMap contributors; Open Database Licence." ctx.translate((MAP_WM) * S2P, (MAP_NM + MAP_H + ADORN_ATTRIB_NM) * S2P) ctx.show_text(text) # Adornments - Attribution left line 2 - contours ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL) ctx.set_source_rgb(0.12, 0.5, 0.65) ctx.set_font_size(7 * SCALE_FACTOR) ctx.translate((MAP_WM) * S2P, (MAP_NM + MAP_H + ADORN_ATTRIB_NM + 0.002) * S2P) ctx.show_text(contour_text) cur.close() conn.close() #Adornments - Attribution left line 3 ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL) ctx.set_source_rgb(0.12, 0.5, 0.65) if style == "blueprint": ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(7 * SCALE_FACTOR) text = "OOM created by Oliver O'Brien. Make your own: " + web_root ctx.translate((MAP_WM) * S2P, (MAP_NM + MAP_H + ADORN_ATTRIB_NM + 0.004) * S2P) ctx.show_text(text) #Adornments - Attribution right line 1 if style == "oterrain" or style == "streeto" or style == "blueprint": ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_BOLD) ctx.set_source_rgb(1, 1, 1) if style == "blueprint": ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(9 * SCALE_FACTOR) text = "OOM v3 developed with a grant from the Orienteering Foundation" width = ctx.text_extents(text)[4] ctx.translate((MAP_WM + MAP_W) * S2P - width, (MAP_NM + MAP_H + ADORN_ATTRIB_NM) * S2P) ctx.show_text(text) #Attribution right line 2 ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_ITALIC, cairo.FONT_WEIGHT_NORMAL) ctx.set_source_rgb(1, 1, 1) if style == "blueprint": ctx.set_source_rgb(0, 0.5, 0.8) ctx.set_font_size(9 * SCALE_FACTOR) text = "Map ID: " + mapid width = ctx.text_extents(text)[4] ctx.translate((MAP_WM + MAP_W) * S2P - width, (MAP_NM + MAP_H + ADORN_ATTRIB_NM + ADORN_ATTRIB_NM) * S2P) ctx.show_text(text) # Adornments - URL ctx = cairo.Context(surface) ctx.select_font_face("Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(0.5 * SCALE_FACTOR) text = web_root + "render/" + fileformat + '/?' + path ctx.translate(MAP_WM * S2P, (MAP_NM + MAP_H + ADORN_URL_NM) * S2P) ctx.show_text(text) if fileformat == 'jpg' or fileformat == 'pre': from PIL import Image, ImageCms surface.write_to_png(file.name + '.png') im = Image.open(file.name + '.png') bg = Image.new("RGB", im.size, (255, 255, 255)) profile = ImageCms.createProfile("sRGB") profile2 = ImageCms.ImageCmsProfile(profile) bg.paste(im, im) bg.save(file.name, 'JPEG', quality=95, icc_profile=profile2.tobytes()) else: surface.finish() surface.flush() if fileformat == 'pdf': # Add Geospatial PDF metadata map_bounds = (MAP_WM / PAPER_W, (PAPER_H - MAP_SM) / PAPER_H, MAP_WM / PAPER_W, MAP_NM / PAPER_H, (PAPER_W - MAP_EM) / PAPER_W, MAP_NM / PAPER_H, (PAPER_W - MAP_EM) / PAPER_W, (PAPER_H - MAP_SM) / PAPER_H) file2 = tempfile.NamedTemporaryFile() file = add_geospatial_pdf_header(map, file, file2, map_bounds, pagePoly, epsg=3857) #Delete temporary style file and postgres tables (everything beginning with "h"): #BUT - don't delete here as may be needed for related query. Periodically clean out with cron job instead #os.unlink(styleFile) #dropTables = 'psql -U osm otf1 -t -c "select \'drop table \\"\' || tablename || \'\\" cascade;\' from pg_tables where schemaname = \'public\' and tablename like \'h%\'" | psql -U osm otf1' #os.system(dropTables) return file
field_names = [field[0] for field in fields] buffer = [] for sr in reader.shapeRecords(): atr = dict(zip(field_names, sr.record)) geom = sr.shape.__geo_interface__ buffer.append(dict(type="Feature", geometry=geom, properties=atr)) hur_data = gpd.GeoDataFrame.from_features(buffer, crs=crs) # Convert to WGS84 hur_data = hur_data.to_crs(crs={'init': 'epsg:4326'}) # Get road map data api = overpass.API() box = hur_data.total_bounds # Get the total bounds of the hurricane dataset map_query = overpass.MapQuery(box[1], box[0], box[3], box[2]) # box edges are west, south, north, east response = api.Get(map_query) map_data = gpd.GeoDataFrame.from_features(response['features']) # response is all map data, extracting roads and prune empty columns, then filter the useful columns road_data = map_data[map_data.highway.notnull()].dropna(axis=1, how='all') bad_roads = gpd.sjoin(road_data, hur_data) # find the map elements that intersect bad_roads = bad_roads[bad_roads.geom_type == 'LineString'] # get just ways, not points bad_roads = bad_roads[['name', 'highway', 'HURR_CAT', 'geometry']] # strip away useless columns # write to file bad_roads.to_file('./data/road_impact.geojson', driver="GeoJSON")
import overpass api = overpass.API() cam_long = "5.6293520" cam_lat = "50.5350159" box_long = float(cam_long) - 0.01 box_lat = float(cam_lat) - 0.01 box_long2 = float(cam_lat) + 0.01 box_lat2 = float(cam_lat) + 0.01 map_query = overpass.MapQuery(box_long, box_lat, box_long2, box_lat2) response = api.Get(map_query) print(response)
def get_osm_result(cls, center, radius): api = overpass.API() map_query = overpass.MapQuery(center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius) return api.Get(map_query)
# observations = gpd.read_file(filename); # Read in file via command line #%% observations = gpd.read_file('./data/SidewalkObservations/SidewalkObservations.shp') #%% sidewalks = gpd.read_file('./data/map.geojson') api = overpass.API() response = api.Get('node["name"="Salt Lake City"]') #%% print(response) #%% map_query = overpass.MapQuery(50.746,7.154,50.748,7.157) response = api.Get(map_query) # print(sidewalks) # #%% # lines = sidewalks[sidewalks.geometry.type == 'LineString'] # lines.plot() # #%% # print(observations)
f = open('UnMappedRows.txt', 'w') for x in range(0, 1): for y in range(0, 4): xmin = bounds[0] + (xSize * x) xmax = xmin + xSize ymin = bounds[1] + (ySize * y) ymax = ymin + ySize polygon = Polygon([(xmin, ymin), (xmin, ymax), (xmax, ymin), (xmax, ymax)]) currentPoints = observations[observations.geometry.within(polygon)] if len(sys.argv) <= 3: map_query = overpass.MapQuery(ymin, xmin, ymax, xmax) query = 'way [highway=footway] (' + str(ymin) + ',' + str( xmin) + ',' + str(ymax) + ',' + str(xmax) + ');<;>;' response = api.Get(query) OSM = gpd.GeoDataFrame.from_features(response) if (len(OSM.index) > 0): if len(sys.argv) <= 3: sidewalks = OSM[OSM.geometry.type == 'LineString'] if (len(sidewalks.index) > 0): for index, row in currentPoints.iterrows(): point = row.geometry
def query_nodes_ways_given_bbox_swne(bbox): map_query = overpass.MapQuery(bbox[0], bbox[1], bbox[2], bbox[3]) response = overpass_api.Get(map_query) print response
import requests api_url = "https://maps.googleapis.com/maps/api/geocode/" params = "json?address={address}&key={key}" key = "AIzaSyC6rh33flbFuQUaKgU4uOP7u9SkSPO-AKU" def get_latlong(address_list): return_list = [] for address in address_list: result = requests.get(api_url + params.format(address=address, key=key)) return_list.append(result.json()['results'][0]['geometry']['location']) longs = return_list['latitude'] lats = return_list['latitude'] min_long = min(longs) min_lat = min(lats) max_long = max(longs) max_lat = max(lats) min_long-=0.5 min_lat-=0.5 max_long+=0.5 max_lat+=0.5 api = overpass.API() map_query = overpass.MapQuery(min_lat,min_long,max_lat,max_long) response = api.Get(map_query, responseformat="json") print response