Class: MapWKT::Geometry::Polygon

Inherits:
MapWKT::Geometry show all
Defined in:
lib/mapwkt/wkt/polygon.rb

Instance Method Summary collapse

Methods inherited from MapWKT::Geometry

parse_linestrings, parse_points, parse_polygons, parse_wkt, parse_x_y

Constructor Details

#initialize(perimeter, *lacunae) ⇒ Polygon

Returns a new MapWKT::Geometry::Polygon with the given outer perimeter and internal lacunae. If the given LineStrings were unclosed, closes them for use in the Polygon, but leaves the original LineStrings unchanged.



12
13
14
15
# File 'lib/mapwkt/wkt/polygon.rb', line 12

def initialize (perimeter, *lacunae)
  @perimeter = perimeter.dup.close!
  @lacunae = lacunae.map {|lacuna| lacuna.dup.close! }
end

Instance Method Details

#centerObject

Returns a Point midway between the N/S- & E/W-most Points in the perimeter.



5
6
7
# File 'lib/mapwkt/wkt/polygon.rb', line 5

def center
  self.perimeter.center
end

#lacunaeObject

Returns an array of LineStrings representing the geographical areas treated as lacunae in this Polygon’s enclosed area. Does not return the objects themselves, nor the actual array containing them.



20
21
22
# File 'lib/mapwkt/wkt/polygon.rb', line 20

def lacunae
  @lacunae.map(&:dup)
end

#perimeterObject

Returns a LineString representing the geographical area treated as the outer perimeter of this Polygon’s enclosed area. Does not return the actual LineString object used in this Polygon.



27
28
29
# File 'lib/mapwkt/wkt/polygon.rb', line 27

def perimeter
  @perimeter.dup
end

#to_sObject

Returns a string representation of this Polygon. ⭓ indicates the perimeter LineString, while ⬠ indicates a lacuna LineString.



33
34
35
# File 'lib/mapwkt/wkt/polygon.rb', line 33

def to_s
  "#{@perimeter.points.join(", ")}#{@lacunae.map {|lacuna| " | ⬠ #{lacuna.points.join(", ")}" }.join} "
end

#wktObject

Returns this Polygon’s WKT representation, with latitudes and longitudes rounded to 7 decimal places.



39
40
41
# File 'lib/mapwkt/wkt/polygon.rb', line 39

def wkt
  "POLYGON((#{[*@perimeter.points, @perimeter.points.first].map {|p| "#{p.longitude_f} #{p.latitude_f}" }.join(", ")})#{@lacunae.map {|lacuna| ", (#{[*lacuna.points, lacuna.points.first].map {|p| "#{p.longitude_f} #{p.latitude_f}" }.join(", ")})" }.join })"
end