1 # The OSM module provides support functions for OSM.
5 require 'rexml/parsers/sax2parser'
11 # The base class for API Errors.
12 class APIError < RuntimeError
14 { :text => "", :status => :internal_server_error }
18 # Raised when an API object is not found.
19 class APINotFoundError < APIError
21 { :nothing => true, :status => :not_found }
25 # Raised when a precondition to an API action fails sanity check.
26 class APIPreconditionFailedError < APIError
28 { :text => "", :status => :precondition_failed }
32 # Raised when to delete an already-deleted object.
33 class APIAlreadyDeletedError < APIError
35 { :text => "", :status => :gone }
39 # Raised when the user logged in isn't the same as the changeset
40 class APIUserChangesetMismatchError < APIError
42 { :text => "The user doesn't own that changeset", :status => :conflict }
46 # Raised when the changeset provided is already closed
47 class APIChangesetAlreadyClosedError < APIError
49 { :text => "The supplied changeset has already been closed", :status => :conflict }
53 # Raised when a change is expecting a changeset, but the changeset doesn't exist
54 class APIChangesetMissingError < APIError
56 { :text => "You need to supply a changeset to be able to make a change", :status => :conflict }
60 # Raised when a diff is uploaded containing many changeset IDs which don't match
61 # the changeset ID that the diff was uploaded to.
62 class APIChangesetMismatchError < APIError
63 def initialize(provided, allowed)
64 @provided, @allowed = provided, allowed
68 { :text => "Changeset mismatch: Provided #{@provided} but only " +
69 "#{@allowed} is allowed.", :status => :conflict }
73 # Raised when a diff upload has an unknown action. You can only have create,
75 class APIChangesetActionInvalid < APIError
76 def initialize(provided)
81 { :text => "Unknown action #{@provided}, choices are create, modify, delete.",
82 :status => :bad_request }
86 # Raised when bad XML is encountered which stops things parsing as
88 class APIBadXMLError < APIError
89 def initialize(model, xml)
90 @model, @xml = model, xml
94 { :text => "Cannot parse valid #{@model} from xml string #{@xml}",
95 :status => :bad_request }
99 # Raised when the provided version is not equal to the latest in the db.
100 class APIVersionMismatchError < APIError
101 def initialize(id, type, provided, latest)
102 @id, @type, @provided, @latest = id, type, provided, latest
105 attr_reader :provided, :latest, :id, :type
108 { :text => "Version mismatch: Provided " + provided.to_s +
109 ", server had: " + latest.to_s + " of " + type + " " + id.to_s,
110 :status => :conflict }
114 # raised when a two tags have a duplicate key string in an element.
115 # this is now forbidden by the API.
116 class APIDuplicateTagsError < APIError
117 def initialize(type, id, tag_key)
118 @type, @id, @tag_key = type, id, tag_key
121 attr_reader :type, :id, :tag_key
124 { :text => "Element #{@type}/#{@id} has duplicate tags with key #{@tag_key}.",
125 :status => :bad_request }
129 # Raised when a way has more than the configured number of way nodes.
130 # This prevents ways from being to long and difficult to work with
131 class APITooManyWayNodesError < APIError
132 def initialize(provided, max)
133 @provided, @max = provided, max
136 attr_reader :provided, :max
139 { :text => "You tried to add #{provided} nodes to the way, however only #{max} are allowed",
140 :status => :bad_request }
144 # Helper methods for going to/from mercator and lat/lng.
148 #init me with your bounding box and the size of your image
149 def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
150 xsize = xsheet(max_lon) - xsheet(min_lon)
151 ysize = ysheet(max_lat) - ysheet(min_lat)
152 xscale = xsize / width
153 yscale = ysize / height
154 scale = [xscale, yscale].max
156 xpad = width * scale - xsize
157 ypad = height * scale - ysize
162 @tx = xsheet(min_lon) - xpad / 2
163 @ty = ysheet(min_lat) - ypad / 2
165 @bx = xsheet(max_lon) + xpad / 2
166 @by = ysheet(max_lat) + ypad / 2
169 #the following two functions will give you the x/y on the entire sheet
172 log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
179 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
182 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
186 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
193 # initialise with a base position
194 def initialize(lat, lon)
195 @lat = lat * PI / 180
196 @lon = lon * PI / 180
199 # get the distance from the base position to a given position
200 def distance(lat, lon)
203 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
206 # get the worst case bounds for a given radius from the base position
208 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
209 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
210 minlat = (@lat - latradius) * 180 / PI
211 maxlat = (@lat + latradius) * 180 / PI
212 minlon = (@lon - lonradius) * 180 / PI
213 maxlon = (@lon + lonradius) * 180 / PI
214 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
219 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
220 @doc = XML::Document.new
221 @doc.encoding = 'UTF-8'
223 rss = XML::Node.new 'rss'
225 rss['version'] = "2.0"
226 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
227 @channel = XML::Node.new 'channel'
229 title = XML::Node.new 'title'
232 description_el = XML::Node.new 'description'
233 @channel << description_el
235 description_el << feed_description
236 link = XML::Node.new 'link'
239 image = XML::Node.new 'image'
241 url = XML::Node.new 'url'
242 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
244 title = XML::Node.new 'title'
245 title << "OpenStreetMap"
247 width = XML::Node.new 'width'
250 height = XML::Node.new 'height'
253 link = XML::Node.new 'link'
258 def add(latitude=0, longitude=0, title_text='dummy title', author_text='anonymous', url='http://www.example.com/', description_text='dummy description', timestamp=DateTime.now)
259 item = XML::Node.new 'item'
261 title = XML::Node.new 'title'
264 link = XML::Node.new 'link'
268 guid = XML::Node.new 'guid'
272 description = XML::Node.new 'description'
273 description << description_text
276 author = XML::Node.new 'author'
277 author << author_text
280 pubDate = XML::Node.new 'pubDate'
281 pubDate << timestamp.to_s(:rfc822)
285 lat_el = XML::Node.new 'geo:lat'
286 lat_el << latitude.to_s
291 lon_el = XML::Node.new 'geo:long'
292 lon_el << longitude.to_s
306 doc = XML::Document.new
307 doc.encoding = 'UTF-8'
308 root = XML::Node.new 'osm'
309 root['version'] = API_VERSION
310 root['generator'] = GENERATOR
316 def self.IPLocation(ip_address)
317 Timeout::timeout(4) do
318 Net::HTTP.start('api.hostip.info') do |http|
319 country = http.get("/country.php?ip=#{ip_address}").body
320 country = "GB" if country == "UK"
321 Net::HTTP.start('ws.geonames.org') do |http|
322 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
323 xml.elements.each("geonames/country") do |ele|
324 minlon = ele.get_text("bBoxWest").to_s
325 minlat = ele.get_text("bBoxSouth").to_s
326 maxlon = ele.get_text("bBoxEast").to_s
327 maxlat = ele.get_text("bBoxNorth").to_s
328 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
339 # Construct a random token of a given length
340 def self.make_token(length = 30)
341 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
345 token += chars[(rand * chars.length).to_i].chr
351 # Return an encrypted version of a password
352 def self.encrypt_password(password, salt)
353 return Digest::MD5.hexdigest(password) if salt.nil?
354 return Digest::MD5.hexdigest(salt + password)
357 # Return an SQL fragment to select a given area of the globe
358 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
359 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
360 minlat = (minlat * 10000000).round
361 minlon = (minlon * 10000000).round
362 maxlat = (maxlat * 10000000).round
363 maxlon = (maxlon * 10000000).round
365 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"