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