| 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)
| members | [R] | Array of Member objects |
Add one or more tags or members to this relation.
The argument can be one of the following:
Returns the relation to allow chaining.
# 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.
# File lib/OSM/objects.rb, line 586
586: def geometry
587: raise NoGeometryError.new("Relations don't have a geometry")
588: end
Return all the member objects of this relation.
# 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’.
# 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 XML for this relation. This method uses the Builder library. The only parameter ist the builder object.
# 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