Class: GeoRuby::SimpleFeatures::Envelope

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

Overview

Contains the bounding box of a geometry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(srid = DEFAULT_SRID, with_z = false) ⇒ Envelope

Creates a enw Envelope with lower_corner as the first element of the corners array and upper_corner as the second element


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

def initialize(srid = DEFAULT_SRID, with_z = false)
  @srid = srid
  @with_z = with_z
end

Instance Attribute Details

#lower_cornerObject

Returns the value of attribute lower_corner


6
7
8
# File 'lib/geo_ruby/simple_features/envelope.rb', line 6

def lower_corner
  @lower_corner
end

#sridObject

Returns the value of attribute srid


7
8
9
# File 'lib/geo_ruby/simple_features/envelope.rb', line 7

def srid
  @srid
end

#upper_cornerObject

Returns the value of attribute upper_corner


6
7
8
# File 'lib/geo_ruby/simple_features/envelope.rb', line 6

def upper_corner
  @upper_corner
end

#with_zObject

Returns the value of attribute with_z


7
8
9
# File 'lib/geo_ruby/simple_features/envelope.rb', line 7

def with_z
  @with_z
end

#zoomObject

Zoom level


57
58
59
# File 'lib/geo_ruby/simple_features/envelope.rb', line 57

def zoom
  @zoom
end

Class Method Details

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

Creates a new envelope. Accept a sequence of point coordinates as argument : ((x,y),(x,y))


159
160
161
162
163
# File 'lib/geo_ruby/simple_features/envelope.rb', line 159

def self.from_coordinates(points,srid=DEFAULT_SRID,with_z=false)
  e = Envelope.new(srid,with_z)
  e.lower_corner, e.upper_corner =  points.collect{|point_coords| Point.from_coordinates(point_coords,srid,with_z)}
  e
end

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

Creates a new envelope. Accept an array of 2 points as argument


151
152
153
154
155
156
# File 'lib/geo_ruby/simple_features/envelope.rb', line 151

def self.from_points(points,srid=DEFAULT_SRID,with_z=false)
  raise "Not an array" unless points.class == Array
  e = Envelope.new(srid,with_z)
  e.lower_corner, e.upper_corner = points
  e
end

Instance Method Details

#==(other_envelope) ⇒ Object

Tests the equality of line strings


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

def ==(other_envelope)
  if other_envelope.class != self.class
    false
  else
    upper_corner == other_envelope.upper_corner and lower_corner == other_envelope.lower_corner
  end
end

#as_georss(options = {}) ⇒ Object

georss serialization: Dialect can be passed as option :dialect and set to :simple (default) :w3cgeo or :gml. Options <tt>:featuretypetag


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/geo_ruby/simple_features/envelope.rb', line 81

def as_georss(options = {})
  dialect= options[:dialect] || :simple
  case(dialect)
  when :simple
    geom_attr = ""
    geom_attr += " featuretypetag=\"#{options[:featuretypetag]}\"" if options[:featuretypetag]
    geom_attr += " relationshiptag=\"#{options[:relationshiptag]}\"" if options[:relationshiptag]
    geom_attr += " floor=\"#{options[:floor]}\"" if options[:floor]
    geom_attr += " radius=\"#{options[:radius]}\"" if options[:radius]
    geom_attr += " elev=\"#{options[:elev]}\"" if options[:elev]

    georss_simple_representation(options.merge(:geom_attr => geom_attr))
  when :w3cgeo
    georss_w3cgeo_representation(options)
  when :gml
    georss_gml_representation(options)
  end
end

#as_kml(options = {}) ⇒ Object

Sends back a latlonaltbox


125
126
127
128
129
130
131
132
# File 'lib/geo_ruby/simple_features/envelope.rb', line 125

def as_kml(options = {})
  geom_data = ""
  geom_data = "<altitudeMode>#{options[:altitude_mode]}</altitudeMode>\n" if options[:altitude_mode]

  allow_z = with_z && (!options[:altitude_mode].nil?) && options[:atitude_mode] != "clampToGround"

  kml_representation(options.merge(:geom_data => geom_data,:allow_z => allow_z))
end

#centerObject

Sends back the center of the envelope


52
53
54
# File 'lib/geo_ruby/simple_features/envelope.rb', line 52

def center
  Point.from_x_y((lower_corner.x + upper_corner.x)/2,(lower_corner.y + upper_corner.y)/2)
end

#extend(envelope) ⇒ Object

Merges the argument with the current evelope and sends back a new envelope without changing the current one


26
27
28
29
30
31
# File 'lib/geo_ruby/simple_features/envelope.rb', line 26

def extend(envelope)
  e = Envelope.from_points([Point.from_x_y(lower_corner.x,lower_corner.y),
                    Point.from_x_y(upper_corner.x,upper_corner.y)],srid,with_z)
  e.extend!(envelope)
  e
end

#extend!(envelope) ⇒ Object

Merges the argument with the current evelope


16
17
18
19
20
21
22
# File 'lib/geo_ruby/simple_features/envelope.rb', line 16

def extend!(envelope)
  lower_corner.x = [lower_corner.x,envelope.lower_corner.x].min
  lower_corner.y = [lower_corner.y,envelope.lower_corner.y].min
  upper_corner.x = [upper_corner.x,envelope.upper_corner.x].max
  upper_corner.y = [upper_corner.y,envelope.upper_corner.y].max
  self
end

#georss_gml_representation(options = {}) ⇒ Object

georss gml representation


115
116
117
118
119
120
121
122
# File 'lib/geo_ruby/simple_features/envelope.rb', line 115

def georss_gml_representation(options = {}) #:nodoc:
  georss_ns = options[:georss_ns] || "georss"
  gml_ns = options[:gml_ns] || "gml"
  result = "<#{georss_ns}:where>\n<#{gml_ns}:Envelope>\n"
  result += "<#{gml_ns}:LowerCorner>" + "#{lower_corner.y} #{lower_corner.x}" + "</#{gml_ns}:LowerCorner>"
  result += "<#{gml_ns}:UpperCorner>" + "#{upper_corner.y} #{upper_corner.x}" + "</#{gml_ns}:UpperCorner>"
  result += "</#{gml_ns}:Envelope>\n</#{georss_ns}:where>\n"
end

#georss_simple_representation(options = {}) ⇒ Object

georss simple representation


101
102
103
104
105
# File 'lib/geo_ruby/simple_features/envelope.rb', line 101

def georss_simple_representation(options = {}) #:nodoc:
  georss_ns = options[:georss_ns] || "georss"
  geom_attr = options[:geom_attr]
  "<#{georss_ns}:box#{geom_attr}>#{lower_corner.y} #{lower_corner.x} #{upper_corner.y} #{upper_corner.x}</#{georss_ns}:box>\n"
end

#georss_w3cgeo_representation(options = {}) ⇒ Object

georss w3c representation : outputs the first point of the line


108
109
110
111
112
# File 'lib/geo_ruby/simple_features/envelope.rb', line 108

def georss_w3cgeo_representation(options = {}) #:nodoc:
  w3cgeo_ns = options[:w3cgeo_ns] || "geo"
  point = self.center
  "<#{w3cgeo_ns}:lat>#{point.y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{point.x}</#{w3cgeo_ns}:long>\n"
end

#kml_representation(options = {}) ⇒ Object

:nodoc:


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/geo_ruby/simple_features/envelope.rb', line 134

def kml_representation(options = {})#:nodoc:
  result = "<LatLonAltBox>\n"
  result += options[:geom_data]
  result += "<north>#{upper_corner.y}</north>\n"
  result += "<south>#{lower_corner.y}</south>\n"
  result += "<east>#{upper_corner.x}</east>\n"
  result += "<west>#{lower_corner.x}</west>\n"

  if with_z
    result += "<minAltitude>#{lower_corner.z}</minAltitude>"
    result += "<maxAltitude>#{upper_corner.z}</maxAltitude>"
  end

  result += "</LatLonAltBox>\n"
end