source: subversion/applications/utils/where_are_they/where_is_it.py @ 29710

Last change on this file since 29710 was 2189, checked in by nickburch, 13 years ago

Add "where are they" stuff (http://wiki.openstreetmap.org/index.php/Where_Are_They) to svn

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.8 KB
Line 
1#!/usr/bin/python
2#                                       Where is it?
3#                                       ------------
4#
5# An OpenStreetMap powered python script to find where places are.
6#
7# Can output as html (format=html - default) or xml (format=xml)
8#  or OSM XML (format=osm) or GPX XML (format=gpx)
9#
10# Requires that Plant.OSM has been loaded into a local postgres database
11#
12# See http://wiki.openstreetmap.org/index.php/Where_Are_They
13#
14# GPL
15#
16# Nick Burch
17#               v0.05  (06/08/2006)
18
19import sys
20from osm_io_helper import hasOpt, getOpt, hasAnyOpts, printHTTPHeaders, renderResults
21from mini_osm import mini_osm_pgsql
22
23# What place types do we accept?
24validPlaceTypes = ["hamlet","village","town","suburb","region","city"]
25
26# Do they want HTML, XML, or OSM?
27formats = [ "html", "xml", "osm", "gpx" ]
28format = ""
29if hasOpt("format"):
30        format = getOpt("format")
31if not formats.__contains__(format):
32        format = formats[0]
33
34# Do the HTTP headers
35printHTTPHeaders(format)
36
37# What place + type did they request?
38searchtype = 'help'
39place = ""
40placeTypes = ["city","town"]
41if hasOpt("place"):
42        place = getOpt("place")
43        searchtype = "place"
44if hasOpt("type"):
45        typestr = getOpt("type")
46        types = typestr.split(",")
47        placeTypes = []
48        for type in (types):
49                if validPlaceTypes.__contains__(type):
50                        placeTypes.append(type)
51
52# Avoid XSS from user string inputs
53if not place == None:
54        new_place = place
55        new_place = new_place.replace('<','&gt;')
56        place = new_place
57
58# Connect to the database
59miniosm = mini_osm_pgsql()
60
61# ##########################################################################
62
63def displayError(message):
64        global format
65        global type
66        global place
67        global placeTypes
68        global validPlaceTypes
69
70        if format == "xml" or format == "osm" or format == "gpx":
71                print '<?xml version="1.0" encoding="UTF-8"?>'
72                print '<error>%s</error>' % message
73        if format == "html":
74                title = "Error - %s" % message
75                if not hasAnyOpts():
76                        title = "Where Is It?"
77
78                print "<html><head><title>%s</title></head><body>" % title
79                print "<h1>%s</h1>" % title
80                print "<h3>More Information:</h3>"
81                print "<p>For more information, see <a href='http://wiki.openstreetmap.org/index.php/Where_Are_They'>http://wiki.openstreetmap.org/index.php/Where_Are_They</a>.</p>"
82                print "<form method='get'>"
83                print "<h3>What to find:</h3>"
84                print "<p><label for='place'>Place:</label> <input type='text' name='place' value='%s' /> (case sensitive)</p>" % place
85                print "<p><label for='type'>Type:</label><br />"
86                for type in (validPlaceTypes):
87                        sel = ""
88                        if placeTypes.__contains__(type):
89                                sel = " checked='yes'"
90                        print "  <span style='margin-left:20px'><input type='checkbox' name='type' value='%s'%s>%s</option></span><br />" % (type,sel,type)
91                print "</p>"
92                print "<p><label for='format'>Output Format:</label><br />"
93                print "<select name='format'>"
94                print "  <option value='html' selected>HTML</option>"
95                print "  <option value='xml'>XML (simple)</option>"
96                print "  <option value='osm'>OSM</option>"
97                print "  <option value='gpx'>GPX (waypoints)</option>"
98                print "</select>"
99                print "</p>"
100                print "<p><input type='submit' value='search' /></p>"
101                print "</form></body></html>"
102
103        sys.exit()
104
105def displayResults(type,objects):
106        global format
107        global place
108        renderResults(type,objects,format,"Places with name %s"%place,"latlong")
109
110# ##########################################################################
111
112# Check we got sensible things
113if place == None or place == "":
114        displayError("No place given")
115if len(placeTypes) == 0:
116        displayError("Invalid place types given")
117
118# Do the work
119if searchtype == "place":
120        # Grab places
121        places = miniosm.getNodesWithTagNameAndType(place,"place",placeTypes)
122        miniosm.splatTagsOntoObjects(places, ["name","place"])
123
124        # Set a 0 distance and bearing
125        for node in (places.keys()):
126                places[node]["bearing"] = 0
127                places[node]["distance"] = 0
128                places[node]["type"] = places[node]["place"]
129
130        displayResults("places", places)
131else:
132        displayError("No search type given")
Note: See TracBrowser for help on using the repository browser.