]> git.openstreetmap.org Git - rails.git/blob - lib/osm.rb
Use Timeout if SystemTimer isn't loaded
[rails.git] / lib / osm.rb
1 # The OSM module provides support functions for OSM.
2 module OSM
3
4   require 'time'
5   require 'rexml/parsers/sax2parser'
6   require 'rexml/text'
7   require 'xml/libxml'
8   require 'digest/md5'
9   require 'RMagick'
10   require 'nokogiri'
11
12   if defined?(SystemTimer)
13     Timer = SystemTimer
14   else
15     require 'timeout'
16     Timer = Timeout
17   end
18
19   # The base class for API Errors.
20   class APIError < RuntimeError
21     def status
22       :internal_server_error
23     end
24
25     def to_s
26       "Generic API Error"
27     end
28   end
29
30   # Raised when an API object is not found.
31   class APINotFoundError < APIError
32     def status
33       :not_found
34     end
35
36     def to_s
37       "Object not found"
38     end
39   end
40
41   # Raised when a precondition to an API action fails sanity check.
42   class APIPreconditionFailedError < APIError
43     def initialize(message = "")
44       @message = message
45     end
46
47     def status
48       :precondition_failed
49     end
50
51     def to_s
52       "Precondition failed: #{@message}"
53     end
54   end
55
56   # Raised when to delete an already-deleted object.
57   class APIAlreadyDeletedError < APIError
58     def initialize(object = "object", object_id = "")
59       @object, @object_id = object, object_id
60     end
61
62     attr_reader :object, :object_id
63
64     def status
65       :gone
66     end
67
68     def to_s
69       "The #{object} with the id #{object_id} has already been deleted"
70     end
71   end
72
73   # Raised when the user logged in isn't the same as the changeset
74   class APIUserChangesetMismatchError < APIError
75     def status
76       :conflict
77     end
78
79     def to_s
80       "The user doesn't own that changeset"
81     end
82   end
83
84   # Raised when the changeset provided is already closed
85   class APIChangesetAlreadyClosedError < APIError
86     def initialize(changeset)
87       @changeset = changeset
88     end
89
90     attr_reader :changeset
91
92     def status
93       :conflict
94     end
95
96     def to_s
97       "The changeset #{@changeset.id} was closed at #{@changeset.closed_at}"
98     end
99   end
100
101   # Raised when a change is expecting a changeset, but the changeset doesn't exist
102   class APIChangesetMissingError < APIError
103     def status
104       :conflict
105     end
106
107     def to_s
108       "You need to supply a changeset to be able to make a change"
109     end
110   end
111
112   # Raised when a diff is uploaded containing many changeset IDs which don't match
113   # the changeset ID that the diff was uploaded to.
114   class APIChangesetMismatchError < APIError
115     def initialize(provided, allowed)
116       @provided, @allowed = provided, allowed
117     end
118
119     def status
120       :conflict
121     end
122
123     def to_s
124       "Changeset mismatch: Provided #{@provided} but only #{@allowed} is allowed"
125     end
126   end
127
128   # Raised when a diff upload has an unknown action. You can only have create,
129   # modify, or delete
130   class APIChangesetActionInvalid < APIError
131     def initialize(provided)
132       @provided = provided
133     end
134
135     def status
136       :bad_request
137     end
138
139     def to_s
140       "Unknown action #{@provided}, choices are create, modify, delete"
141     end
142   end
143
144   # Raised when bad XML is encountered which stops things parsing as
145   # they should.
146   class APIBadXMLError < APIError
147     def initialize(model, xml, message="")
148       @model, @xml, @message = model, xml, message
149     end
150
151     def status
152       :bad_request
153     end
154
155     def to_s
156       "Cannot parse valid #{@model} from xml string #{@xml}. #{@message}"
157     end
158   end
159
160   # Raised when the provided version is not equal to the latest in the db.
161   class APIVersionMismatchError < APIError
162     def initialize(id, type, provided, latest)
163       @id, @type, @provided, @latest = id, type, provided, latest
164     end
165
166     attr_reader :provided, :latest, :id, :type
167
168     def status
169       :conflict
170     end
171
172     def to_s
173       "Version mismatch: Provided #{provided}, server had: #{latest} of #{type} #{id}"
174     end
175   end
176
177   # raised when a two tags have a duplicate key string in an element.
178   # this is now forbidden by the API.
179   class APIDuplicateTagsError < APIError
180     def initialize(type, id, tag_key)
181       @type, @id, @tag_key = type, id, tag_key
182     end
183
184     attr_reader :type, :id, :tag_key
185
186     def status
187       :bad_request
188     end
189
190     def to_s
191       "Element #{@type}/#{@id} has duplicate tags with key #{@tag_key}"
192     end
193   end
194
195   # Raised when a way has more than the configured number of way nodes.
196   # This prevents ways from being to long and difficult to work with
197   class APITooManyWayNodesError < APIError
198     def initialize(id, provided, max)
199       @id, @provided, @max = id, provided, max
200     end
201
202     attr_reader :id, :provided, :max
203
204     def status
205       :bad_request
206     end
207
208     def to_s
209       "You tried to add #{provided} nodes to way #{id}, however only #{max} are allowed"
210     end
211   end
212
213   ##
214   # raised when user input couldn't be parsed
215   class APIBadUserInput < APIError
216     def initialize(message)
217       @message = message
218     end
219
220     def status
221       :bad_request
222     end
223
224     def to_s
225       @message
226     end
227   end
228
229   ##
230   # raised when bounding box is invalid
231   class APIBadBoundingBox < APIError
232     def initialize(message)
233       @message = message
234     end
235
236     def status
237       :bad_request
238     end
239
240     def to_s
241       @message
242     end
243   end
244
245   ##
246   # raised when an API call is made using a method not supported on that URI
247   class APIBadMethodError < APIError
248     def initialize(supported_method)
249       @supported_method = supported_method
250     end
251
252     def status
253       :method_not_allowed
254     end
255
256     def to_s
257       "Only method #{@supported_method} is supported on this URI"
258     end
259   end
260
261   ##
262   # raised when an API call takes too long
263   class APITimeoutError < APIError
264     def status
265       :request_timeout
266     end
267
268     def to_s
269       "Request timed out"
270     end
271   end
272
273   # Helper methods for going to/from mercator and lat/lng.
274   class Mercator
275     include Math
276
277     #init me with your bounding box and the size of your image
278     def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
279       xsize = xsheet(max_lon) - xsheet(min_lon)
280       ysize = ysheet(max_lat) - ysheet(min_lat)
281       xscale = xsize / width
282       yscale = ysize / height
283       scale = [xscale, yscale].max
284
285       xpad = width * scale - xsize
286       ypad = height * scale - ysize
287
288       @width = width
289       @height = height
290
291       @tx = xsheet(min_lon) - xpad / 2
292       @ty = ysheet(min_lat) - ypad / 2
293
294       @bx = xsheet(max_lon) + xpad / 2
295       @by = ysheet(max_lat) + ypad / 2
296     end
297
298     #the following two functions will give you the x/y on the entire sheet
299
300     def ysheet(lat)
301       log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
302     end
303
304     def xsheet(lon)
305       lon
306     end
307
308     #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
309
310     def y(lat)
311       return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
312     end
313
314     def x(lon)
315       return  ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
316     end
317   end
318
319   class GreatCircle
320     include Math
321
322     # initialise with a base position
323     def initialize(lat, lon)
324       @lat = lat * PI / 180
325       @lon = lon * PI / 180
326     end
327
328     # get the distance from the base position to a given position
329     def distance(lat, lon)
330       lat = lat * PI / 180
331       lon = lon * PI / 180
332       return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
333     end
334
335     # get the worst case bounds for a given radius from the base position
336     def bounds(radius)
337       latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
338
339       begin
340         lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
341       rescue Errno::EDOM
342         lonradius = PI
343       end
344
345       minlat = (@lat - latradius) * 180 / PI
346       maxlat = (@lat + latradius) * 180 / PI
347       minlon = (@lon - lonradius) * 180 / PI
348       maxlon = (@lon + lonradius) * 180 / PI
349
350       return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
351     end
352
353     # get the SQL to use to calculate distance
354     def sql_for_distance(lat_field, lon_field)
355       "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)))"
356     end
357   end
358
359   class GeoRSS
360     def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
361       @doc = XML::Document.new
362       @doc.encoding = XML::Encoding::UTF_8
363
364       rss = XML::Node.new 'rss'
365       @doc.root = rss
366       rss['version'] = "2.0"
367       rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
368       @channel = XML::Node.new 'channel'
369       rss << @channel
370       title = XML::Node.new 'title'
371       title <<  feed_title
372       @channel << title
373       description_el = XML::Node.new 'description'
374       @channel << description_el
375
376       description_el << feed_description
377       link = XML::Node.new 'link'
378       link << feed_url
379       @channel << link
380       image = XML::Node.new 'image'
381       @channel << image
382       url = XML::Node.new 'url'
383       url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
384       image << url
385       title = XML::Node.new 'title'
386       title << "OpenStreetMap"
387       image << title
388       width = XML::Node.new 'width'
389       width << '100'
390       image << width
391       height = XML::Node.new 'height'
392       height << '100'
393       image << height
394       link = XML::Node.new 'link'
395       link << feed_url
396       image << link
397     end
398
399     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)
400       item = XML::Node.new 'item'
401
402       title = XML::Node.new 'title'
403       item << title
404       title << title_text
405       link = XML::Node.new 'link'
406       link << url
407       item << link
408
409       guid = XML::Node.new 'guid'
410       guid << url
411       item << guid
412
413       description = XML::Node.new 'description'
414       description << description_text
415       item << description
416
417       author = XML::Node.new 'author'
418       author << author_text
419       item << author
420
421       pubDate = XML::Node.new 'pubDate'
422       pubDate << timestamp.to_s(:rfc822)
423       item << pubDate
424
425       if latitude
426         lat_el = XML::Node.new 'geo:lat'
427         lat_el << latitude.to_s
428         item << lat_el
429       end
430
431       if longitude
432         lon_el = XML::Node.new 'geo:long'
433         lon_el << longitude.to_s
434         item << lon_el
435       end
436
437       @channel << item
438     end
439
440     def to_s
441       return @doc.to_s
442     end
443   end
444
445   class API
446     def get_xml_doc
447       doc = XML::Document.new
448       doc.encoding = XML::Encoding::UTF_8
449       root = XML::Node.new 'osm'
450       root['version'] = API_VERSION.to_s
451       root['generator'] = GENERATOR
452       doc.root = root
453       return doc
454     end
455   end
456
457   def self.IPToCountry(ip_address)
458     Timeout::timeout(4) do
459       ipinfo = Quova::IpInfo.new(ip_address)
460
461       if ipinfo.status == Quova::Success then
462         country = ipinfo.country_code
463       else
464         Net::HTTP.start('api.hostip.info') do |http|
465           country = http.get("/country.php?ip=#{ip_address}").body
466           country = "GB" if country == "UK"
467         end
468       end
469       
470       return country.upcase
471     end
472
473     return nil
474   rescue Exception
475     return nil
476   end
477
478   def self.IPLocation(ip_address)
479     code = OSM.IPToCountry(ip_address)
480
481     if code and country = Country.find_by_code(code)
482       return { :minlon => country.min_lon, :minlat => country.min_lat, :maxlon => country.max_lon, :maxlat => country.max_lat }
483     end
484
485     return nil
486   end
487
488   # Construct a random token of a given length
489   def self.make_token(length = 30)
490     chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
491     token = ''
492
493     length.times do
494       token += chars[(rand * chars.length).to_i].chr
495     end
496
497     return token
498   end
499
500   # Return an encrypted version of a password
501   def self.encrypt_password(password, salt)
502     return Digest::MD5.hexdigest(password) if salt.nil?
503     return Digest::MD5.hexdigest(salt + password)
504   end
505
506   # Return an SQL fragment to select a given area of the globe
507   def self.sql_for_area(bbox, prefix = nil)
508     tilesql = QuadTile.sql_for_area(bbox, prefix)
509     bbox = bbox.to_scaled
510
511     return "#{tilesql} AND #{prefix}latitude BETWEEN #{bbox.min_lat} AND #{bbox.max_lat} " +
512                       "AND #{prefix}longitude BETWEEN #{bbox.min_lon} AND #{bbox.max_lon}"
513   end
514
515   # Return a spam score for a chunk of text
516   def self.spam_score(text)
517     link_count = 0
518     link_size = 0
519
520     doc = Nokogiri::HTML(text)
521
522     if doc.content.length > 0
523       doc.xpath("//a").each do |link|
524         link_count += 1
525         link_size += link.content.length
526       end
527
528       link_proportion = link_size.to_f / doc.content.length.to_f
529     else
530       link_proportion = 0
531     end
532
533     return [link_proportion - 0.2, 0.0].max * 200 + link_count * 20
534   end
535
536   def self.legal_text_for_country(country_code)
537     file_name = File.join(Rails.root, "config", "legales", country_code.to_s + ".yml")
538     file_name = File.join(Rails.root, "config", "legales", DEFAULT_LEGALE + ".yml") unless File.exist? file_name
539     YAML::load_file(file_name)
540   end
541 end