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
22 # Raised when a precondition to an API action fails sanity check.
23 class APIPreconditionFailedError < APIError
25 { :text => "", :status => :precondition_failed }
29 # Raised when to delete an already-deleted object.
30 class APIAlreadyDeletedError < APIError
32 { :text => "", :status => :gone }
36 # Raised when the provided version is not equal to the latest in the db.
37 class APIVersionMismatchError < APIError
38 def initialize(provided, latest)
39 @provided, @latest = provided, latest
42 attr_reader :provided, :latest
45 { :text => "Version mismatch: Provided " + provided.to_s +
46 ", server had: " + latest.to_s, :status => :bad_request }
50 # Helper methods for going to/from mercator and lat/lng.
54 #init me with your bounding box and the size of your image
55 def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
56 xsize = xsheet(max_lon) - xsheet(min_lon)
57 ysize = ysheet(max_lat) - ysheet(min_lat)
58 xscale = xsize / width
59 yscale = ysize / height
60 scale = [xscale, yscale].max
62 xpad = width * scale - xsize
63 ypad = height * scale - ysize
68 @tx = xsheet(min_lon) - xpad / 2
69 @ty = ysheet(min_lat) - ypad / 2
71 @bx = xsheet(max_lon) + xpad / 2
72 @by = ysheet(max_lat) + ypad / 2
75 #the following two functions will give you the x/y on the entire sheet
78 log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
85 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
88 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
92 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
99 # initialise with a base position
100 def initialize(lat, lon)
101 @lat = lat * PI / 180
102 @lon = lon * PI / 180
105 # get the distance from the base position to a given position
106 def distance(lat, lon)
109 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
112 # get the worst case bounds for a given radius from the base position
114 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
115 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
116 minlat = (@lat - latradius) * 180 / PI
117 maxlat = (@lat + latradius) * 180 / PI
118 minlon = (@lon - lonradius) * 180 / PI
119 maxlon = (@lon + lonradius) * 180 / PI
120 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
125 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
126 @doc = XML::Document.new
127 @doc.encoding = 'UTF-8'
129 rss = XML::Node.new 'rss'
131 rss['version'] = "2.0"
132 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
133 @channel = XML::Node.new 'channel'
135 title = XML::Node.new 'title'
138 description_el = XML::Node.new 'description'
139 @channel << description_el
141 description_el << feed_description
142 link = XML::Node.new 'link'
145 image = XML::Node.new 'image'
147 url = XML::Node.new 'url'
148 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
150 title = XML::Node.new 'title'
151 title << "OpenStreetMap"
153 width = XML::Node.new 'width'
156 height = XML::Node.new 'height'
159 link = XML::Node.new 'link'
164 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)
165 item = XML::Node.new 'item'
167 title = XML::Node.new 'title'
170 link = XML::Node.new 'link'
174 guid = XML::Node.new 'guid'
178 description = XML::Node.new 'description'
179 description << description_text
182 author = XML::Node.new 'author'
183 author << author_text
186 pubDate = XML::Node.new 'pubDate'
187 pubDate << timestamp.to_s(:rfc822)
191 lat_el = XML::Node.new 'geo:lat'
192 lat_el << latitude.to_s
197 lon_el = XML::Node.new 'geo:long'
198 lon_el << longitude.to_s
212 doc = XML::Document.new
213 doc.encoding = 'UTF-8'
214 root = XML::Node.new 'osm'
215 root['version'] = API_VERSION
216 root['generator'] = 'OpenStreetMap server'
222 def self.IPLocation(ip_address)
223 Timeout::timeout(4) do
224 Net::HTTP.start('api.hostip.info') do |http|
225 country = http.get("/country.php?ip=#{ip_address}").body
226 country = "GB" if country == "UK"
227 Net::HTTP.start('ws.geonames.org') do |http|
228 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
229 xml.elements.each("geonames/country") do |ele|
230 minlon = ele.get_text("bBoxWest").to_s
231 minlat = ele.get_text("bBoxSouth").to_s
232 maxlon = ele.get_text("bBoxEast").to_s
233 maxlat = ele.get_text("bBoxNorth").to_s
234 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
245 # Construct a random token of a given length
246 def self.make_token(length = 30)
247 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
251 token += chars[(rand * chars.length).to_i].chr
257 # Return an encrypted version of a password
258 def self.encrypt_password(password, salt)
259 return Digest::MD5.hexdigest(password) if salt.nil?
260 return Digest::MD5.hexdigest(salt + password)
263 # Return an SQL fragment to select a given area of the globe
264 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
265 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
266 minlat = (minlat * 10000000).round
267 minlon = (minlon * 10000000).round
268 maxlat = (maxlat * 10000000).round
269 maxlon = (maxlon * 10000000).round
271 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"