Class GeoRuby::SimpleFeatures::Polygon
In: lib/geo_ruby/simple_features/polygon.rb
Parent: Geometry

Represents a polygon as an array of linear rings (see LinearRing). No check is performed regarding the validity of the geometries forming the polygon.

Methods

Attributes

rings  [R]  the list of rings forming the polygon

Public Class methods

creates a new polygon. Accepts a sequence of points as argument : ((x,y).…(x,y)),((x,y).….(x,y))

[Source]

     # File lib/geo_ruby/simple_features/polygon.rb, line 134
134:       def self.from_coordinates(point_sequences,srid=DEFAULT_SRID,with_z=false,with_m=false)
135:         polygon = new(srid,with_z,with_m)
136:         polygon.concat( point_sequences.collect {|points| LinearRing.from_coordinates(points,srid,with_z,with_m) } )
137:         polygon
138:       end

creates a new polygon. Accepts an array of linear strings as argument

[Source]

     # File lib/geo_ruby/simple_features/polygon.rb, line 127
127:       def self.from_linear_rings(linear_rings,srid = DEFAULT_SRID,with_z=false,with_m=false)
128:         polygon = new(srid,with_z,with_m)
129:         polygon.concat(linear_rings)
130:         polygon
131:       end

creates a new polygon from a list of Points (pt1....ptn),(pti....ptj)

[Source]

     # File lib/geo_ruby/simple_features/polygon.rb, line 141
141:       def self.from_points(point_sequences, srid=DEFAULT_SRID,with_z=false,with_m=false)
142:         polygon = new(srid,with_z,with_m)
143:         polygon.concat( point_sequences.collect {|points| LinearRing.from_points(points,srid,with_z,with_m) } )
144:         polygon
145: 
146:       end

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 10
10:       def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
11:         super(srid,with_z,with_m)
12:         @rings = []
13:       end

Public Instance methods

tests for other equality. The SRID is not taken into account.

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 54
54:       def ==(other_polygon)
55:         if other_polygon.class != self.class or
56:             length != other_polygon.length
57:           false
58:         else
59:           index=0
60:           while index<length
61:             return false if self[index] != other_polygon[index]
62:             index+=1
63:           end
64:           true
65:         end
66:       end

WKB geometry type

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 74
74:       def binary_geometry_type
75:         3
76:       end

binary representation of a polygon, without the headers neccessary for a valid WKB string

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 68
68:       def binary_representation(allow_z=true,allow_m=true)
69:         rep = [length].pack("V")
70:         each {|linear_ring| rep << linear_ring.binary_representation(allow_z,allow_m)}
71:         rep
72:       end

Bounding box in 2D/3D. Returns an array of 2 points

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 21
21:       def bounding_box
22:         unless with_z
23:           @rings[0].bounding_box
24:         else
25:           result = @rings[0].bounding_box #valid for x and y
26:           max_z, min_z = result[1].z, result[0].z
27:           1.upto(size - 1) do |index|
28:             bbox = @rings[index].bounding_box
29:             sw = bbox[0]
30:             ne = bbox[1]
31:             max_z = ne.z if ne.z > max_z
32:             min_z = sw.z if sw.z < min_z 
33:           end
34:           result[1].z, result[0].z = max_z, min_z
35:           result
36:         end
37:       end

georss gml representation

[Source]

     # File lib/geo_ruby/simple_features/polygon.rb, line 100
100:       def georss_gml_representation(options)
101:         georss_ns = options[:georss_ns] || "georss"
102:         gml_ns = options[:gml_ns] || "gml"
103:        
104:         result = "<#{georss_ns}:where>\n<#{gml_ns}:Polygon>\n<#{gml_ns}:exterior>\n<#{gml_ns}:LinearRing>\n<#{gml_ns}:posList>\n" + self[0].georss_poslist + "\n</#{gml_ns}:posList>\n</#{gml_ns}:LinearRing>\n</#{gml_ns}:exterior>\n</#{gml_ns}:Polygon>\n</#{georss_ns}:where>\n"
105:       end

georss simple representation : outputs only the outer ring

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 88
88:       def georss_simple_representation(options)
89:         georss_ns = options[:georss_ns] || "georss"
90:         geom_attr = options[:geom_attr]
91:         "<#{georss_ns}:polygon#{geom_attr}>" + self[0].georss_poslist + "</#{georss_ns}:polygon>\n"
92:       end

georss w3c representation : outputs the first point of the outer ring

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 94
94:       def georss_w3cgeo_representation(options)
95:         w3cgeo_ns = options[:w3cgeo_ns] || "geo"
96:         
97:         "<#{w3cgeo_ns}:lat>#{self[0][0].y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{self[0][0].x}</#{w3cgeo_ns}:long>\n"
98:       end

outputs the geometry in kml format : options are :id, :tesselate, :extrude, :altitude_mode. If the altitude_mode option is not present, the Z (if present) will not be output (since it won’t be used by GE anyway: clampToGround is the default)

[Source]

     # File lib/geo_ruby/simple_features/polygon.rb, line 110
110:       def kml_representation(options = {})
111:         result = "<Polygon#{options[:id_attr]}>\n"
112:         result += options[:geom_data]
113:         rings.each_with_index do |ring, i|
114:           if i == 0
115:             boundary = "outerBoundaryIs"
116:           else
117:             boundary = "innerBoundaryIs"
118:           end
119:           result += "<#{boundary}><LinearRing><coordinates>\n"
120:           result += ring.kml_poslist(options)
121:           result += "\n</coordinates></LinearRing></#{boundary}>\n"
122:         end
123:         result += "</Polygon>\n"
124:       end

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 39
39:       def m_range
40:         if with_m
41:           max_m, min_m = -Float::MAX, Float::MAX
42:           each do |lr|
43:             lrmr = lr.m_range
44:             max_m = lrmr[1] if lrmr[1] > max_m
45:             min_m = lrmr[0] if lrmr[0] < min_m
46:           end
47:           [min_m,max_m]
48:         else
49:           [0,0]
50:         end
51:       end

Delegate the unknown methods to the rings array

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 16
16:       def method_missing(method_name,*args,&b)
17:         @rings.send(method_name,*args,&b)
18:       end

WKT geometry type

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 83
83:       def text_geometry_type
84:         "POLYGON"
85:       end

Text representation of a polygon

[Source]

    # File lib/geo_ruby/simple_features/polygon.rb, line 79
79:       def text_representation(allow_z=true,allow_m=true)
80:         @rings.collect{|line_string| "(" + line_string.text_representation(allow_z,allow_m) + ")" }.join(",")
81:       end

[Validate]