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
15 # Raised when an API object is not found.
16 class APINotFoundError < APIError
19 # Raised when a precondition to an API action fails sanity check.
20 class APIPreconditionFailedError < APIError
23 # Raised when to delete an already-deleted object.
24 class APIAlreadyDeletedError < APIError
27 # Helper methods for going to/from mercator and lat/lng.
31 #init me with your bounding box and the size of your image
32 def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
33 xsize = xsheet(max_lon) - xsheet(min_lon)
34 ysize = ysheet(max_lat) - ysheet(min_lat)
35 xscale = xsize / width
36 yscale = ysize / height
37 scale = [xscale, yscale].max
39 xpad = width * scale - xsize
40 ypad = height * scale - ysize
45 @tx = xsheet(min_lon) - xpad / 2
46 @ty = ysheet(min_lat) - ypad / 2
48 @bx = xsheet(max_lon) + xpad / 2
49 @by = ysheet(max_lat) + ypad / 2
52 #the following two functions will give you the x/y on the entire sheet
55 log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
62 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
65 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
69 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
76 # initialise with a base position
77 def initialize(lat, lon)
82 # get the distance from the base position to a given position
83 def distance(lat, lon)
86 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
89 # get the worst case bounds for a given radius from the base position
91 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
92 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
93 minlat = (@lat - latradius) * 180 / PI
94 maxlat = (@lat + latradius) * 180 / PI
95 minlon = (@lon - lonradius) * 180 / PI
96 maxlon = (@lon + lonradius) * 180 / PI
97 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
102 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
103 @doc = XML::Document.new
104 @doc.encoding = XML::Encoding::UTF_8
106 rss = XML::Node.new 'rss'
108 rss['version'] = "2.0"
109 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
110 @channel = XML::Node.new 'channel'
112 title = XML::Node.new 'title'
115 description_el = XML::Node.new 'description'
116 @channel << description_el
118 description_el << feed_description
119 link = XML::Node.new 'link'
122 image = XML::Node.new 'image'
124 url = XML::Node.new 'url'
125 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
127 title = XML::Node.new 'title'
128 title << "OpenStreetMap"
130 width = XML::Node.new 'width'
133 height = XML::Node.new 'height'
136 link = XML::Node.new 'link'
141 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)
142 item = XML::Node.new 'item'
144 title = XML::Node.new 'title'
147 link = XML::Node.new 'link'
151 guid = XML::Node.new 'guid'
155 description = XML::Node.new 'description'
156 description << description_text
159 author = XML::Node.new 'author'
160 author << author_text
163 pubDate = XML::Node.new 'pubDate'
164 pubDate << timestamp.to_s(:rfc822)
168 lat_el = XML::Node.new 'geo:lat'
169 lat_el << latitude.to_s
174 lon_el = XML::Node.new 'geo:long'
175 lon_el << longitude.to_s
189 doc = XML::Document.new
190 doc.encoding = XML::Encoding::UTF_8
191 root = XML::Node.new 'osm'
192 root['version'] = API_VERSION
193 root['generator'] = 'OpenStreetMap server'
199 def self.IPLocation(ip_address)
200 Timeout::timeout(4) do
201 Net::HTTP.start('api.hostip.info') do |http|
202 country = http.get("/country.php?ip=#{ip_address}").body
203 country = "GB" if country == "UK"
204 Net::HTTP.start('ws.geonames.org') do |http|
205 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
206 xml.elements.each("geonames/country") do |ele|
207 minlon = ele.get_text("bBoxWest").to_s
208 minlat = ele.get_text("bBoxSouth").to_s
209 maxlon = ele.get_text("bBoxEast").to_s
210 maxlat = ele.get_text("bBoxNorth").to_s
211 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
222 # Construct a random token of a given length
223 def self.make_token(length = 30)
224 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
228 token += chars[(rand * chars.length).to_i].chr
234 # Return an encrypted version of a password
235 def self.encrypt_password(password, salt)
236 return Digest::MD5.hexdigest(password) if salt.nil?
237 return Digest::MD5.hexdigest(salt + password)
240 # Return an SQL fragment to select a given area of the globe
241 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
242 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
243 minlat = (minlat * 10000000).round
244 minlon = (minlon * 10000000).round
245 maxlat = (maxlat * 10000000).round
246 maxlon = (maxlon * 10000000).round
248 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"