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