X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/3bd92c16704823e79ac61f27d05c1a9be52858f5..cf78f3e6ddbd1c148e91de6500266240bfa24825:/lib/osm.rb diff --git a/lib/osm.rb b/lib/osm.rb index 6e9346bb4..c038ab2d5 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -1,13 +1,6 @@ +# The OSM module provides support functions for OSM. module OSM - # This piece of magic reads a GPX with SAX and spits out - # lat/lng and stuff - # - # This would print every latitude value: - # - # gpx = OSM::GPXImporter.new('somefile.gpx') - # gpx.points {|p| puts p['latitude']} - require 'time' require 'rexml/parsers/sax2parser' require 'rexml/text' @@ -15,11 +8,50 @@ module OSM require 'digest/md5' require 'RMagick' + # The base class for API Errors. + class APIError < RuntimeError + def render_opts + { :text => "", :status => :internal_server_error } + end + end + + # Raised when an API object is not found. + class APINotFoundError < APIError + end + + # Raised when a precondition to an API action fails sanity check. + class APIPreconditionFailedError < APIError + def render_opts + { :text => "", :status => :precondition_failed } + end + end + + # Raised when to delete an already-deleted object. + class APIAlreadyDeletedError < APIError + def render_opts + { :text => "", :status => :gone } + end + end + + # Raised when the provided version is not equal to the latest in the db. + class APIVersionMismatchError < APIError + def initialize(provided, latest) + @provided, @latest = provided, latest + end + + attr_reader :provided, :latest + + def render_opts + { :text => "Version mismatch: Provided " + provided.to_s + + ", server had: " + latest.to_s, :status => :bad_request } + end + end + + # Helper methods for going to/from mercator and lat/lng. class Mercator include Math #init me with your bounding box and the size of your image - def initialize(min_lat, min_lon, max_lat, max_lon, width, height) xsize = xsheet(max_lon) - xsheet(min_lon) ysize = ysheet(max_lat) - ysheet(min_lat) @@ -62,6 +94,13 @@ module OSM end + # This piece of magic reads a GPX with SAX and spits out + # lat/lng and stuff + # + # This would print every latitude value: + # + # gpx = OSM::GPXImporter.new('somefile.gpx') + # gpx.points {|p| puts p['latitude']} class GPXImporter # FIXME swap REXML for libXML attr_reader :possible_points @@ -279,7 +318,7 @@ module OSM def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/') @doc = XML::Document.new @doc.encoding = 'UTF-8' - + rss = XML::Node.new 'rss' @doc.root = rss rss['version'] = "2.0" @@ -425,72 +464,5 @@ module OSM return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}" end - class Potlatch # crazy shit - - # ----- getpresets - # in: none - # does: reads tag preset menus, colours, and autocomplete config files - # out: [0] presets, [1] presetmenus, [2] presetnames, - # [3] colours, [4] casing, [5] areas, [6] autotags - # (all hashes) - def self.get_presets - RAILS_DEFAULT_LOGGER.info(" Message: getpresets") - - # Read preset menus - presets={} - presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[] - presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={} - presettype='' - presetcategory='' - # StringIO.open(txt) do |file| - File.open("#{RAILS_ROOT}/config/potlatch/presets.txt") do |file| - file.each_line {|line| - t=line.chomp - if (t=~/(\w+)\/(\w+)/) then - presettype=$1 - presetcategory=$2 - presetmenus[presettype].push(presetcategory) - presetnames[presettype][presetcategory]=["(no preset)"] - elsif (t=~/^(.+):\s?(.+)$/) then - pre=$1; kv=$2 - presetnames[presettype][presetcategory].push(pre) - presets[pre]={} - kv.split(',').each {|a| - if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end - } - end - } - end - - # Read colours/styling - colours={}; casing={}; areas={} - File.open("#{RAILS_ROOT}/config/potlatch/colours.txt") do |file| - file.each_line {|line| - t=line.chomp - if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then - tag=$1 - if ($2!='-') then colours[tag]=$2.hex end - if ($3!='-') then casing[tag]=$3.hex end - if ($4!='-') then areas[tag]=$4.hex end - end - } - end - - # Read auto-complete - autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={}; - File.open("#{RAILS_ROOT}/config/potlatch/autocomplete.txt") do |file| - file.each_line {|line| - t=line.chomp - if (t=~/^(\w+)\/(\w+)\s+(.+)$/) then - tag=$1; type=$2; values=$3 - if values=='-' then autotags[type][tag]=[] - else autotags[type][tag]=values.split(',').sort.reverse end - end - } - end - - [presets,presetmenus,presetnames,colours,casing,areas,autotags] - end - end end