Class: GeoRuby::SimpleFeatures::Polygon

Inherits:
Geometry
  • Object
show all
Defined in:
lib/geo_ruby/simple_features/polygon.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Geometry

#srid, #with_m, #with_z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Geometry

#as_ewkb, #as_ewkt, #as_georss, #as_hex_ewkb, #as_hex_wkb, #as_kml, #as_wkb, #as_wkt, #envelope, from_ewkb, from_ewkt, from_geojson, from_georss, from_georss_with_tags, from_hex_ewkb, from_kml

Constructor Details

#initialize(srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Polygon

Returns a new instance of Polygon.



13
14
15
16
# File 'lib/geo_ruby/simple_features/polygon.rb', line 13

def initialize(srid = DEFAULT_SRID,with_z=false,with_m=false)
  super(srid,with_z,with_m)
  @rings = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &b) ⇒ Object

Delegate the unknown methods to the rings array



19
20
21
# File 'lib/geo_ruby/simple_features/polygon.rb', line 19

def method_missing(method_name,*args,&b)
  @rings.send(method_name,*args,&b)
end

Instance Attribute Details

#ringsObject (readonly)

the list of rings forming the polygon



11
12
13
# File 'lib/geo_ruby/simple_features/polygon.rb', line 11

def rings
  @rings
end

Class Method Details

.from_coordinates(point_sequences, srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Object

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



162
163
164
165
166
# File 'lib/geo_ruby/simple_features/polygon.rb', line 162

def self.from_coordinates(point_sequences,srid=DEFAULT_SRID,with_z=false,with_m=false)
  polygon = new(srid,with_z,with_m)
  polygon.concat( point_sequences.map {|points| LinearRing.from_coordinates(points,srid,with_z,with_m) } )
  polygon
end

.from_linear_rings(linear_rings, srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Object

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



155
156
157
158
159
# File 'lib/geo_ruby/simple_features/polygon.rb', line 155

def self.from_linear_rings(linear_rings,srid = DEFAULT_SRID,with_z=false,with_m=false)
  polygon = new(srid,with_z,with_m)
  polygon.concat(linear_rings)
  polygon
end

.from_points(point_sequences, srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Object

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



169
170
171
172
173
# File 'lib/geo_ruby/simple_features/polygon.rb', line 169

def self.from_points(point_sequences, srid=DEFAULT_SRID,with_z=false,with_m=false)
  polygon = new(srid,with_z,with_m)
  polygon.concat( point_sequences.map {|points| LinearRing.from_points(points,srid,with_z,with_m) } )
  polygon
end

Instance Method Details

#==(other_polygon) ⇒ Object

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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geo_ruby/simple_features/polygon.rb', line 57

def ==(other_polygon)
  if other_polygon.class != self.class or
      length != other_polygon.length
    false
  else
    index=0
    while index<length
      return false if self[index] != other_polygon[index]
      index+=1
    end
    true
  end
end

#as_json(options = {}) ⇒ Object



142
143
144
145
# File 'lib/geo_ruby/simple_features/polygon.rb', line 142

def as_json(options = {})
  {:type => 'Polygon',
   :coordinates => self.to_coordinates}
end

#binary_geometry_typeObject

WKB geometry type



79
80
81
# File 'lib/geo_ruby/simple_features/polygon.rb', line 79

def binary_geometry_type
  3
end

#binary_representation(allow_z = true, allow_m = true) ⇒ Object

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



72
73
74
75
76
# File 'lib/geo_ruby/simple_features/polygon.rb', line 72

def binary_representation(allow_z=true,allow_m=true)
  rep = [length].pack("V")
  each {|linear_ring| rep << linear_ring.binary_representation(allow_z,allow_m)}
  rep
end

#bounding_boxObject

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/geo_ruby/simple_features/polygon.rb', line 24

def bounding_box
  unless with_z
    @rings[0].bounding_box
  else
    result = @rings[0].bounding_box #valid for x and y
    max_z, min_z = result[1].z, result[0].z
    1.upto(size - 1) do |index|
      bbox = @rings[index].bounding_box
      sw = bbox[0]
      ne = bbox[1]
      max_z = ne.z if ne.z > max_z
      min_z = sw.z if sw.z < min_z
    end
    result[1].z, result[0].z = max_z, min_z
    result
  end
end

#contains_point?(point) ⇒ Boolean

Contains a point?

Returns:

  • (Boolean)


94
95
96
# File 'lib/geo_ruby/simple_features/polygon.rb', line 94

def contains_point?(point)
  !@rings.select { |lr| lr.contains_point? point }.empty?
end

#georss_gml_representation(options) ⇒ Object

georss gml representation



112
113
114
115
116
117
# File 'lib/geo_ruby/simple_features/polygon.rb', line 112

def georss_gml_representation(options)
  georss_ns = options[:georss_ns] || "georss"
  gml_ns = options[:gml_ns] || "gml"

  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"
end

#georss_simple_representation(options) ⇒ Object

georss simple representation : outputs only the outer ring



99
100
101
102
103
# File 'lib/geo_ruby/simple_features/polygon.rb', line 99

def georss_simple_representation(options)
  georss_ns = options[:georss_ns] || "georss"
  geom_attr = options[:geom_attr]
  "<#{georss_ns}:polygon#{geom_attr}>" + self[0].georss_poslist + "</#{georss_ns}:polygon>\n"
end

#georss_w3cgeo_representation(options) ⇒ Object

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



106
107
108
109
110
# File 'lib/geo_ruby/simple_features/polygon.rb', line 106

def georss_w3cgeo_representation(options)
  w3cgeo_ns = options[:w3cgeo_ns] || "geo"

  "<#{w3cgeo_ns}:lat>#{self[0][0].y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{self[0][0].x}</#{w3cgeo_ns}:long>\n"
end

#kml_representation(options = {}) ⇒ Object

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)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/geo_ruby/simple_features/polygon.rb', line 122

def kml_representation(options = {})
  result = "<Polygon#{options[:id_attr]}>\n"
  result += options[:geom_data] if options[:geom_data]
  rings.each_with_index do |ring, i|
    if i == 0
      boundary = "outerBoundaryIs"
    else
      boundary = "innerBoundaryIs"
    end
    result += "<#{boundary}><LinearRing><coordinates>\n"
    result += ring.kml_poslist(options)
    result += "\n</coordinates></LinearRing></#{boundary}>\n"
  end
  result += "</Polygon>\n"
end

#m_rangeObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/geo_ruby/simple_features/polygon.rb', line 42

def m_range
  if with_m
    max_m, min_m = -Float::MAX, Float::MAX
    each do |lr|
      lrmr = lr.m_range
      max_m = lrmr[1] if lrmr[1] > max_m
      min_m = lrmr[0] if lrmr[0] < min_m
    end
    [min_m,max_m]
  else
    [0,0]
  end
end

#text_geometry_typeObject

WKT geometry type



89
90
91
# File 'lib/geo_ruby/simple_features/polygon.rb', line 89

def text_geometry_type
  "POLYGON"
end

#text_representation(allow_z = true, allow_m = true) ⇒ Object

Text representation of a polygon



84
85
86
# File 'lib/geo_ruby/simple_features/polygon.rb', line 84

def text_representation(allow_z=true,allow_m=true)
  @rings.collect{|line_string| "(" + line_string.text_representation(allow_z,allow_m) + ")" }.join(",")
end

#to_coordinatesObject



138
139
140
# File 'lib/geo_ruby/simple_features/polygon.rb', line 138

def to_coordinates
  rings.map{|lr| lr.to_coordinates}
end

#to_json(options = {}) ⇒ Object Also known as: as_geojson

simple geojson representation TODO add CRS / SRID support?



149
150
151
# File 'lib/geo_ruby/simple_features/polygon.rb', line 149

def to_json(options = {})
  as_json(options).to_json(options)
end