Skip to content

Commit

Permalink
Updated for API 0.6
Browse files Browse the repository at this point in the history
git-svn-id: svn://rubyforge.org/var/svn/osmlib/osmlib-base/trunk@133 717e3884-936c-430f-b93f-cbfeea02e76b
  • Loading branch information
joto committed May 22, 2009
1 parent 23bfc5f commit 8515c82
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 41 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

2009-05-22
* Support for API 0.6 added

0.1.3 2008-06-24
* Added parser code for using REXML, Libxml, or Expat parser. REXML
is now the default (Thanks to Nolan Darilek for the Expat code).
Expand Down
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ be found at http://www.openstreetmap.org/ .

== Dependencies

* georuby
* georuby (gem install GeoRuby)
* libxml-ruby (optional C-based XML parser, needs reasonably new version)
(Debian/Ubuntu: libxml-ruby1.8)
* xmlparser (optional C-based XML parser) (Debian/Ubuntu: libxml-parser-ruby1.8)
Expand Down
2 changes: 1 addition & 1 deletion lib/OSM/API.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class APIServerError < APIError; end # 500
class API

# the default base URI for the API
DEFAULT_BASE_URI = 'http://www.openstreetmap.org/api/0.5/'
DEFAULT_BASE_URI = 'http://www.openstreetmap.org/api/0.6/'

# Creates a new API object. Without any arguments it uses the default API at
# DEFAULT_BASE_URI. If you want to use a different API, give the base URI
Expand Down
2 changes: 1 addition & 1 deletion lib/OSM/Database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module OSM
#
class Database

@@DEFAULT_API_VERSION = '0.5'
@@DEFAULT_API_VERSION = '0.6'
@@DEFAULT_XML_GENERATOR = 'Ruby-OSMLib'

# OpenStreetMap API version of this database
Expand Down
4 changes: 2 additions & 2 deletions lib/OSM/StreamParser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def end_element(uri, name, qname) # :nodoc:
private

def _start_osm(attr_hash)
if attr_hash['version'] != '0.5'
raise OSM::VersionError, 'OSM::StreamParser only understands OSM file version 0.5'
if attr_hash['version'] != '0.5' && attr_hash['version'] != '0.6'
raise OSM::VersionError, 'OSM::StreamParser only understands OSM file version 0.5 and 0.6'
end
end

Expand Down
30 changes: 21 additions & 9 deletions lib/OSM/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ class OSMObject
# Unique ID
attr_reader :id

# The version of this object (as read from file, it
# is not updated by operations to this object)
# API 0.6 and above only
attr_accessor :version

# The user who last edited this object (as read from file, it
# is not updated by operations to this object)
attr_accessor :user

# The user id of the user who last edited this object (as read from file, it
# is not updated by operations to this object)
# API 0.6 and above only
attr_accessor :uid

# Last change of this object (as read from file, it is not
# updated by operations to this object)
attr_reader :timestamp
Expand All @@ -57,10 +67,12 @@ def self.from_api(id, api=OSM::API.new) #:nodoc:
api.get_object(type, id)
end

def initialize(id, user, timestamp) #:nodoc:
def initialize(id, user, timestamp, uid=-1, version=1) #:nodoc:
raise NotImplementedError.new('OSMObject is a virtual base class for the Node, Way, and Relation classes') if self.class == OSM::OSMObject

@id = id.nil? ? _next_id : _check_id(id)
@version = version
@uid = uid
@user = user
@timestamp = _check_timestamp(timestamp) unless timestamp.nil?
@db = nil
Expand All @@ -80,7 +92,7 @@ def timestamp=(timestamp)

# The list of attributes for this object
def attribute_list # :nodoc:
[:id, :user, :timestamp]
[:id, :version, :uid, :user, :timestamp]
end

# Returns a hash of all non-nil attributes of this object.
Expand Down Expand Up @@ -281,10 +293,10 @@ class Node < OSMObject
# Create new Node object.
#
# If +id+ is +nil+ a new unique negative ID will be allocated.
def initialize(id=nil, user=nil, timestamp=nil, lon=nil, lat=nil)
def initialize(id=nil, user=nil, timestamp=nil, lon=nil, lat=nil, uid=-1, version=1)
@lon = _check_lon(lon) unless lon.nil?
@lat = _check_lat(lat) unless lat.nil?
super(id, user, timestamp)
super(id, user, timestamp, uid, version)
end

def type
Expand All @@ -303,7 +315,7 @@ def lat=(lat)

# List of attributes for a Node
def attribute_list
[:id, :user, :timestamp, :lon, :lat]
[:id, :version, :uid, :user, :timestamp, :lon, :lat]
end

# Add one or more tags to this node.
Expand Down Expand Up @@ -395,9 +407,9 @@ class Way < OSMObject
# user:: Username
# timestamp:: Timestamp of last change
# nodes:: Array of Node objects and/or node IDs
def initialize(id=nil, user=nil, timestamp=nil, nodes=[])
def initialize(id=nil, user=nil, timestamp=nil, nodes=[], uid=-1, version=1)
@nodes = nodes.collect{ |node| node.kind_of?(OSM::Node) ? node.id : node }
super(id, user, timestamp)
super(id, user, timestamp, uid, version)
end

def type
Expand Down Expand Up @@ -542,9 +554,9 @@ class Relation < OSMObject
# Create new Relation object.
#
# If +id+ is +nil+ a new unique negative ID will be allocated.
def initialize(id=nil, user=nil, timestamp=nil, members=[])
def initialize(id=nil, user=nil, timestamp=nil, members=[], uid=-1, version=1)
@members = members
super(id, user, timestamp)
super(id, user, timestamp, uid, version)
end

def type
Expand Down
38 changes: 19 additions & 19 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ def initialize(suffix)
when 'node/1'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<node id="1" lat="48.1" lon="8.1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<osm version="0.6" generator="OpenStreetMap server">
<node id="1" version="1" lat="48.1" lon="8.1" uid="1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<tag k="created_by" v="JOSM"/>
</node>
</osm>
}
when 'node/2'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<node id="1" lat="48.1" lon="8.1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<osm version="0.6" generator="OpenStreetMap server">
<node id="1" version="1" lat="48.1" lon="8.1" uid="1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<tag k="created_by" v="JOSM"/>
</node>
<node id="2" lat="48.2" lon="8.2" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<node id="2" version="1" lat="48.2" lon="8.2" uid="1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<tag k="created_by" v="JOSM"/>
</node>
</osm>
}
when 'way/1'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<way id="1" visible="true" timestamp="2007-06-03T20:02:39+01:00" user="u">
<osm version="0.6" generator="OpenStreetMap server">
<way id="1" version="1" visible="true" timestamp="2007-06-03T20:02:39+01:00" uid="1" user="u">
<nd ref="1"/>
<nd ref="2"/>
<nd ref="3"/>
Expand All @@ -49,15 +49,15 @@ def initialize(suffix)
when 'way/2'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<way id="1" visible="true" timestamp="2007-06-03T20:02:39+01:00" user="u">
<osm version="0.6" generator="OpenStreetMap server">
<way id="1" version="1" visible="true" timestamp="2007-06-03T20:02:39+01:00" uid="1" user="u">
<nd ref="1"/>
<nd ref="2"/>
<nd ref="3"/>
<tag k="created_by" v="osmeditor2"/>
<tag k="highway" v="residential"/>
</way>
<way id="2" visible="true" timestamp="2007-06-03T20:02:39+01:00" user="u">
<way id="2" version="1" visible="true" timestamp="2007-06-03T20:02:39+01:00" uid="1" user="u">
<nd ref="4"/>
<nd ref="5"/>
<nd ref="6"/>
Expand All @@ -69,8 +69,8 @@ def initialize(suffix)
when 'relation/1'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<relation id="1" visible="true" timestamp="2007-07-24T16:18:51+01:00" user="u">
<osm version="0.6" generator="OpenStreetMap server">
<relation id="1" version="1" visible="true" timestamp="2007-07-24T16:18:51+01:00" uid="1" user="u">
<member type="way" ref="1" role=""/>
<member type="way" ref="2" role=""/>
<tag k="type" v="something"/>
Expand All @@ -80,13 +80,13 @@ def initialize(suffix)
when 'relation/2'
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<relation id="1" visible="true" timestamp="2007-07-24T16:18:51+01:00" user="u">
<osm version="0.6" generator="OpenStreetMap server">
<relation id="1" version="1" visible="true" timestamp="2007-07-24T16:18:51+01:00" uid="1" user="u">
<member type="way" ref="1" role=""/>
<member type="way" ref="2" role=""/>
<tag k="type" v="something"/>
</relation>
<relation id="2" visible="true" timestamp="2007-07-24T16:18:51+01:00" user="u">
<relation id="2" version="1" visible="true" timestamp="2007-07-24T16:18:51+01:00" uid="1" user="u">
<member type="way" ref="3" role=""/>
<member type="way" ref="4" role=""/>
<tag k="type" v="something"/>
Expand All @@ -105,11 +105,11 @@ def initialize(suffix)
when /^map\?bbox/
@code = 200
@body = %q{<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.5" generator="OpenStreetMap server">
<node id="1" lat="48.1" lon="8.1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<osm version="0.6" generator="OpenStreetMap server">
<node id="1" version="1" lat="48.1" lon="8.1" uid="1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<tag k="created_by" v="JOSM"/>
</node>
<node id="2" lat="48.2" lon="8.2" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<node id="2" version="1" lat="48.2" lon="8.2" uid="1" user="u" visible="true" timestamp="2007-07-03T00:04:12+01:00">
<tag k="created_by" v="JOSM"/>
</node>
</osm>
Expand All @@ -134,7 +134,7 @@ def @mapi.get(suffix)

def test_create_std
assert_kind_of OSM::API, @api
assert_equal 'http://www.openstreetmap.org/api/0.5/', @api.instance_variable_get(:@base_uri)
assert_equal 'http://www.openstreetmap.org/api/0.6/', @api.instance_variable_get(:@base_uri)
end

def test_create_uri
Expand Down
4 changes: 2 additions & 2 deletions test/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestNode < Test::Unit::TestCase

def test_create
node = OSM::Node.new(17, 'somebody', '2007-02-20T10:29:49+00:00', 8.5, 47.5)
node = OSM::Node.new(17, 'somebody', '2007-02-20T10:29:49+00:00', 8.5, 47.5, 5, 3)
assert_kind_of OSM::Node, node
assert_equal 17, node.id
assert_equal 'somebody', node.user
Expand All @@ -16,7 +16,7 @@ def test_create
assert node.tags.empty?
assert_nil node.tags['foo']

hash = {:id => 17, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00', :lon => '8.5', :lat => '47.5'}
hash = {:id => 17, :version => 3, :uid => 5, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00', :lon => '8.5', :lat => '47.5'}
assert_equal hash, node.attributes
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestRelation < Test::Unit::TestCase

def test_create
relation = OSM::Relation.new(123, 'somebody', '2007-02-20T10:29:49+00:00')
relation = OSM::Relation.new(123, 'somebody', '2007-02-20T10:29:49+00:00', [], 3, 5)
assert_kind_of OSM::Relation, relation
assert_equal 123, relation.id
assert_equal 'somebody', relation.user
Expand All @@ -16,7 +16,7 @@ def test_create
assert relation.tags.empty?
assert_nil relation.tags['foo']

hash = {:id => 123, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00'}
hash = {:id => 123, :version => 5, :uid => 3, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00'}
assert_equal hash, relation.attributes
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_way.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TestWay < Test::Unit::TestCase

def test_create
way = OSM::Way.new(123, 'somebody', '2007-02-20T10:29:49+00:00')
way = OSM::Way.new(123, 'somebody', '2007-02-20T10:29:49+00:00', [], 3, 5)
assert_kind_of OSM::Way, way
assert_equal 123, way.id
assert_equal 'somebody', way.user
Expand All @@ -16,7 +16,7 @@ def test_create
assert way.tags.empty?
assert_nil way.tags['foo']

hash = {:id => 123, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00'}
hash = {:id => 123, :version => 5, :uid => 3, :user => 'somebody', :timestamp => '2007-02-20T10:29:49+00:00'}
assert_equal hash, way.attributes
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_relation
assert_equal 'foo', REXML::XPath.first(element, 'member/@role').value
end

# <osm version="0.5" generator="test">
# <osm version="0.6" generator="test">
# <node id="-1"/>
# <way id="-2"/>
# <relation id="-3"/>
Expand All @@ -194,7 +194,7 @@ def test_database
assert_kind_of REXML::Element, element
assert_equal 'osm', element.name

assert_equal '0.5', REXML::XPath.first(element, '/osm/@version').value
assert_equal '0.6', REXML::XPath.first(element, '/osm/@version').value
assert_equal 'test', REXML::XPath.first(element, '/osm/@generator').value

assert_equal 'node', REXML::XPath.first(element, '/osm/[1]').name
Expand Down

0 comments on commit 8515c82

Please sign in to comment.