# Helpers for OSM python scripts such as "where am I" and "where is it" # # GPL # # Nick Burch # v0.01 (06/08/2006) import os import sys import time global opts # Grab our options if os.environ.has_key("QUERY_STRING"): opts = os.environ["QUERY_STRING"].split("&") else: opts = sys.argv[1:] def hasOpt(opt): global opts for thisopt in (opts): if thisopt == opt: return True if thisopt.startswith(opt + "="): return True return False def getOpt(opt): global opts ret="" for thisopt in (opts): if thisopt.startswith(opt + "="): if len(ret): ret += "," ret += thisopt[(thisopt.find("=")+1):] return ret def printHTTPHeaders(format): # Everything except HTML is XML content_type = "text/xml" if format == "html": content_type = "text/html" # Print out a content type for them print "Content-Type: %s" % content_type print "" # ########################################################################## def findMinMaxLatLong(objects): global min_lat global min_long global max_lat global max_long (min_lat,min_long,max_lat,max_long) = (90,90,-90,-90) if len(objects) == 0: (min_lat,min_long,max_lat,max_long) = (0,0,0,0) def do_node(node): global min_lat global min_long global max_lat global max_long if node["lat"] < min_lat: min_lat = node["lat"] if node["lat"] > max_lat: max_lat = node["lat"] if node["long"] < min_long: min_long = node["long"] if node["long"] > max_long: max_long = node["long"] def do_seg(seg): do_node(seg["node_a_node"]) do_node(seg["node_b_node"]) for obj in (objects): if obj.has_key("lat"): do_node(obj) else: do_seg(obj) return (min_lat,min_long,max_lat,max_long) # ########################################################################## def renderResults(type,objects,format,title,renderType): # If we got a hash, just get the values if isinstance(objects,dict): objects = objects.values() if format == "xml": print '' print '<%s>' % type.lower() tag = type.lower()[0:-1] for obj in (objects): if not obj.has_key("ref"): obj["ref"] = None latlong = "" if obj.has_key("lat"): latlong = " latitude='%2.6f' longitude='%2.6f'" % (float(obj['lat']),float(obj['long'])) print " <%s ref='%s' type='%s' distance='%d' bearing='%d'%s>%s" % (tag,obj["ref"],obj["type"],int(obj["distance"]),int(obj["bearing"]),latlong,obj["name"],tag) print '' % type.lower() if format == "osm": print '' print '' def osm_do_node(obj): print " " % (obj["id"],obj["lat"],obj["long"]) for tag in obj["tags"]: print " " % (tag[0],tag[1]) print " " def osm_do_seg(obj): osm_do_node(obj["node_a_node"]) osm_do_node(obj["node_b_node"]) print " " % (obj["id"],obj["node_a"],obj["node_b"]) for tag in obj["tags"]: print " " % (tag[0],tag[1]) print " " for obj in (objects): if obj.has_key("lat"): osm_do_node(obj) else: osm_do_seg(obj) print '' if format == "gpx": print '' print '' # Render print '' print '' % findMinMaxLatLong(objects) def gpx_do_node(obj): print '' % (obj["lat"],obj["long"]) if obj.has_key("name"): print ' %s' % obj["name"] if obj.has_key("ref"): print ' %s' % obj["ref"] if obj.has_key("type"): print ' %s' % obj["type"] print ' Waypoint' print '' def gpx_do_seg(obj): print '' print ' %s' % obj["name"] if obj.has_key("ref"): print ' %s' % obj["ref"] print ' %s' % obj["type"] print '' # Do waypoints (nodes) first for obj in (objects): if obj.has_key("lat"): gpx_do_node(obj) else: gpx_do_node(obj["node_a_node"]) gpx_do_node(obj["node_b_node"]) # Now do segments for obj in (objects): if obj.has_key("lat"): pass else: gpx_do_seg(obj) print '' if format == "html": print "%s" % (type) print "

%s

" % type if len(objects) > 1: print "" print "

Data from OpenStreetMap

" if len(objects) == 0: print "

No results found - either OpenStreetMap doesn't have any data on this area, or that data that it does have hasn't yet been correctly tagged. Sorry about that.

" print "" # ########################################################################## def sortByDistance(x,y): if x["distance"] == y["distance"]: return 0 if x["distance"] > y["distance"]: return 1 return -1 # ##########################################################################