Class OSM::Relation
In: lib/OSM/objects.rb
Parent: OSMObject

OpenStreetMap Relation.

To create a new OSM::Relation object:

  relation = OSM::Relation.new(331, 'user', '2007-10-31T23:51:53Z')

To get a relation from the API:

  relation = OSM::Relation.from_api(17)

Methods

<<   geometry   member   member_objects   new   polygon   to_s   to_xml   type  

Attributes

members  [R]  Array of Member objects

Public Class methods

Create new Relation object.

If id is nil a new unique negative ID will be allocated.

[Source]

     # File lib/OSM/objects.rb, line 545
545:         def initialize(id=nil, user=nil, timestamp=nil, members=[])
546:             @members = members
547:             super(id, user, timestamp)
548:         end

Public Instance methods

Add one or more tags or members to this relation.

The argument can be one of the following:

  • If the argument is a Hash or an OSM::Tags object, those tags are added.
  • If the argument is an OSM::Member object, it is added to the relation
  • If the argument is an Array the function is called recursively, i.e. all items in the Array are added.

Returns the relation to allow chaining.

[Source]

     # File lib/OSM/objects.rb, line 566
566:         def <<(stuff)
567:             case stuff
568:                 when Array  # call this method recursively
569:                     stuff.each do |item|
570:                         self << item
571:                     end
572:                 when OSM::Member
573:                     members << stuff
574:                 else
575:                     tags.merge!(stuff)
576:             end
577:             self    # return self to allow chaining
578:         end

Raises a NoGeometryError.

Future versions of this library may recognize certain relations that do have a geometry (such as Multipolygon relations) and do the right thing.

[Source]

     # File lib/OSM/objects.rb, line 586
586:         def geometry
587:             raise NoGeometryError.new("Relations don't have a geometry")
588:         end

Return the member with a specified type and id. Returns nil if not found.

[Source]

     # File lib/OSM/objects.rb, line 632
632:         def member(type, id)
633:             members.select{ |member| member.type == type && member.ref == id }[0]
634:         end

Return all the member objects of this relation.

[Source]

     # File lib/OSM/objects.rb, line 607
607:         def member_objects
608:             members.collect do |member|
609:                 obj = case member.type
610:                     when :node,     'node'     then @db.get_node(member.ref)
611:                     when :way,      'way'      then @db.get_way(member.ref)
612:                     when :relation, 'relation' then @db.get_relation(member.ref)
613:                 end
614:                 raise OSM::NotFoundError.new("not in database: #{member.type} #{member.ref}") unless obj
615:                 obj
616:             end
617:         end

Returns a polygon made up of all the ways in this relation. This works only if it is tagged with ‘polygon’ or ‘multipolygon’.

[Source]

     # File lib/OSM/objects.rb, line 592
592:         def polygon
593:             raise OSM::NoDatabaseError.new("can't create Polygon from relation if it is not in a OSM::Database") if @db.nil?
594:             raise OSM::NoDatabaseError.new("can't create Polygon from relation if it does not represent a polygon") if self['type'] != 'multipolygon' and self['type'] != 'polygon'
595: 
596:             c = []
597:             member_objects.each do |way|
598:                 raise TypeError.new("member is not a way so it can't be represented as Polygon") unless way.kind_of? OSM::Way
599:                 raise OSM::NotClosedError.new("way is not closed so it can't be represented as Polygon") unless way.is_closed?
600:                 raise OSM::GeometryError.new("way with less then three nodes can't be turned into a polygon") if way.nodes.size < 3
601:                 c << way.node_objects.collect{ |node| [node.lon.to_f, node.lat.to_f] }
602:             end
603:             GeoRuby::SimpleFeatures::Polygon.from_coordinates(c)
604:         end

Return string version of this Relation object.

[Source]

     # File lib/OSM/objects.rb, line 623
623:         def to_s
624:             "#<OSM::Relation id=\"#{@id}\" user=\"#{@user}\" timestamp=\"#{@timestamp}\">"
625:         end

Return XML for this relation. This method uses the Builder library. The only parameter ist the builder object.

[Source]

     # File lib/OSM/objects.rb, line 638
638:         def to_xml(xml)
639:             xml.relation(attributes) do |xml|
640:                 members.each do |member|
641:                     member.to_xml(xml)
642:                 end
643:                 tags.to_xml(xml)
644:             end
645:         end

[Source]

     # File lib/OSM/objects.rb, line 550
550:         def type
551:             'relation'
552:         end

[Validate]