1 # The OSM module provides support functions for OSM.
5 require 'rexml/parsers/sax2parser'
11 if defined?(SystemTimer)
18 # The base class for API Errors.
19 class APIError < RuntimeError
21 :internal_server_error
29 # Raised when an API object is not found.
30 class APINotFoundError < APIError
40 # Raised when a precondition to an API action fails sanity check.
41 class APIPreconditionFailedError < APIError
42 def initialize(message = "")
51 "Precondition failed: #{@message}"
55 # Raised when to delete an already-deleted object.
56 class APIAlreadyDeletedError < APIError
57 def initialize(object = "object", object_id = "")
58 @object, @object_id = object, object_id
61 attr_reader :object, :object_id
68 "The #{object} with the id #{object_id} has already been deleted"
72 # Raised when the user logged in isn't the same as the changeset
73 class APIUserChangesetMismatchError < APIError
79 "The user doesn't own that changeset"
83 # Raised when the changeset provided is already closed
84 class APIChangesetAlreadyClosedError < APIError
85 def initialize(changeset)
86 @changeset = changeset
89 attr_reader :changeset
96 "The changeset #{@changeset.id} was closed at #{@changeset.closed_at}"
100 # Raised when a change is expecting a changeset, but the changeset doesn't exist
101 class APIChangesetMissingError < APIError
107 "You need to supply a changeset to be able to make a change"
111 # Raised when a diff is uploaded containing many changeset IDs which don't match
112 # the changeset ID that the diff was uploaded to.
113 class APIChangesetMismatchError < APIError
114 def initialize(provided, allowed)
115 @provided, @allowed = provided, allowed
123 "Changeset mismatch: Provided #{@provided} but only #{@allowed} is allowed"
127 # Raised when a diff upload has an unknown action. You can only have create,
129 class APIChangesetActionInvalid < APIError
130 def initialize(provided)
139 "Unknown action #{@provided}, choices are create, modify, delete"
143 # Raised when bad XML is encountered which stops things parsing as
145 class APIBadXMLError < APIError
146 def initialize(model, xml, message="")
147 @model, @xml, @message = model, xml, message
155 "Cannot parse valid #{@model} from xml string #{@xml}. #{@message}"
159 # Raised when the provided version is not equal to the latest in the db.
160 class APIVersionMismatchError < APIError
161 def initialize(id, type, provided, latest)
162 @id, @type, @provided, @latest = id, type, provided, latest
165 attr_reader :provided, :latest, :id, :type
172 "Version mismatch: Provided #{provided}, server had: #{latest} of #{type} #{id}"
176 # raised when a two tags have a duplicate key string in an element.
177 # this is now forbidden by the API.
178 class APIDuplicateTagsError < APIError
179 def initialize(type, id, tag_key)
180 @type, @id, @tag_key = type, id, tag_key
183 attr_reader :type, :id, :tag_key
190 "Element #{@type}/#{@id} has duplicate tags with key #{@tag_key}"
194 # Raised when a way has more than the configured number of way nodes.
195 # This prevents ways from being to long and difficult to work with
196 class APITooManyWayNodesError < APIError
197 def initialize(id, provided, max)
198 @id, @provided, @max = id, provided, max
201 attr_reader :id, :provided, :max
208 "You tried to add #{provided} nodes to way #{id}, however only #{max} are allowed"
213 # raised when user input couldn't be parsed
214 class APIBadUserInput < APIError
215 def initialize(message)
229 # raised when bounding box is invalid
230 class APIBadBoundingBox < APIError
231 def initialize(message)
245 # raised when an API call is made using a method not supported on that URI
246 class APIBadMethodError < APIError
247 def initialize(supported_method)
248 @supported_method = supported_method
256 "Only method #{@supported_method} is supported on this URI"
261 # raised when an API call takes too long
262 class APITimeoutError < APIError
272 # Helper methods for going to/from mercator and lat/lng.
276 #init me with your bounding box and the size of your image
277 def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
278 xsize = xsheet(max_lon) - xsheet(min_lon)
279 ysize = ysheet(max_lat) - ysheet(min_lat)
280 xscale = xsize / width
281 yscale = ysize / height
282 scale = [xscale, yscale].max
284 xpad = width * scale - xsize
285 ypad = height * scale - ysize
290 @tx = xsheet(min_lon) - xpad / 2
291 @ty = ysheet(min_lat) - ypad / 2
293 @bx = xsheet(max_lon) + xpad / 2
294 @by = ysheet(max_lat) + ypad / 2
297 #the following two functions will give you the x/y on the entire sheet
300 log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
307 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
310 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
314 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
321 # initialise with a base position
322 def initialize(lat, lon)
323 @lat = lat * PI / 180
324 @lon = lon * PI / 180
327 # get the distance from the base position to a given position
328 def distance(lat, lon)
331 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
334 # get the worst case bounds for a given radius from the base position
336 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
339 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
344 minlat = (@lat - latradius) * 180 / PI
345 maxlat = (@lat + latradius) * 180 / PI
346 minlon = (@lon - lonradius) * 180 / PI
347 maxlon = (@lon + lonradius) * 180 / PI
349 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
352 # get the SQL to use to calculate distance
353 def sql_for_distance(lat_field, lon_field)
354 "6372.795 * 2 * asin(sqrt(power(sin((radians(#{lat_field}) - #{@lat}) / 2), 2) + cos(#{@lat}) * cos(radians(#{lat_field})) * power(sin((radians(#{lon_field}) - #{@lon})/2), 2)))"
359 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
360 @doc = XML::Document.new
361 @doc.encoding = XML::Encoding::UTF_8
363 rss = XML::Node.new 'rss'
365 rss['version'] = "2.0"
366 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
367 @channel = XML::Node.new 'channel'
369 title = XML::Node.new 'title'
372 description_el = XML::Node.new 'description'
373 @channel << description_el
375 description_el << feed_description
376 link = XML::Node.new 'link'
379 image = XML::Node.new 'image'
381 url = XML::Node.new 'url'
382 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
384 title = XML::Node.new 'title'
385 title << "OpenStreetMap"
387 width = XML::Node.new 'width'
390 height = XML::Node.new 'height'
393 link = XML::Node.new 'link'
398 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)
399 item = XML::Node.new 'item'
401 title = XML::Node.new 'title'
404 link = XML::Node.new 'link'
408 guid = XML::Node.new 'guid'
412 description = XML::Node.new 'description'
413 description << description_text
416 author = XML::Node.new 'author'
417 author << author_text
420 pubDate = XML::Node.new 'pubDate'
421 pubDate << timestamp.to_s(:rfc822)
425 lat_el = XML::Node.new 'geo:lat'
426 lat_el << latitude.to_s
431 lon_el = XML::Node.new 'geo:long'
432 lon_el << longitude.to_s
446 doc = XML::Document.new
447 doc.encoding = XML::Encoding::UTF_8
448 root = XML::Node.new 'osm'
449 root['version'] = API_VERSION.to_s
450 root['generator'] = GENERATOR
456 def self.IPToCountry(ip_address)
458 ipinfo = Quova::IpInfo.new(ip_address)
460 if ipinfo.status == Quova::Success then
461 country = ipinfo.country_code
463 Net::HTTP.start('api.hostip.info') do |http|
464 country = http.get("/country.php?ip=#{ip_address}").body
465 country = "GB" if country == "UK"
469 return country.upcase
477 def self.IPLocation(ip_address)
478 code = OSM.IPToCountry(ip_address)
480 if code and country = Country.find_by_code(code)
481 return { :minlon => country.min_lon, :minlat => country.min_lat, :maxlon => country.max_lon, :maxlat => country.max_lat }
487 # Construct a random token of a given length
488 def self.make_token(length = 30)
489 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
493 token += chars[(rand * chars.length).to_i].chr
499 # Return an encrypted version of a password
500 def self.encrypt_password(password, salt)
501 return Digest::MD5.hexdigest(password) if salt.nil?
502 return Digest::MD5.hexdigest(salt + password)
505 # Return an SQL fragment to select a given area of the globe
506 def self.sql_for_area(bbox, prefix = nil)
507 tilesql = QuadTile.sql_for_area(bbox, prefix)
508 bbox = bbox.to_scaled
510 return "#{tilesql} AND #{prefix}latitude BETWEEN #{bbox.min_lat} AND #{bbox.max_lat} " +
511 "AND #{prefix}longitude BETWEEN #{bbox.min_lon} AND #{bbox.max_lon}"
514 # Return a spam score for a chunk of text
515 def self.spam_score(text)
519 doc = Nokogiri::HTML(Rinku.auto_link(text, :urls))
521 if doc.content.length > 0
522 doc.xpath("//a").each do |link|
524 link_size += link.content.length
527 link_proportion = link_size.to_f / doc.content.length.to_f
532 return [link_proportion - 0.2, 0.0].max * 200 + link_count * 20
535 def self.legal_text_for_country(country_code)
536 file_name = File.join(Rails.root, "config", "legales", country_code.to_s + ".yml")
537 file_name = File.join(Rails.root, "config", "legales", DEFAULT_LEGALE + ".yml") unless File.exist? file_name
538 YAML::load_file(file_name)