Class: GeoRuby::SimpleFeatures::Geometry

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

Overview

Root of all geometric data classes. Objects of class Geometry should not be instantiated.

Direct Known Subclasses

GeometryCollection, LineString, Point, Polygon

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Geometry.



19
20
21
22
23
# File 'lib/geo_ruby/simple_features/geometry.rb', line 19

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

Instance Attribute Details

#sridObject

SRID of the geometry



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

def srid
  @srid
end

#with_mObject Also known as: with_m?

Flag indicating if the m ordinate of the geometry is meaningful



16
17
18
# File 'lib/geo_ruby/simple_features/geometry.rb', line 16

def with_m
  @with_m
end

#with_zObject Also known as: with_z?

Flag indicating if the z ordinate of the geometry is meaningful



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

def with_z
  @with_z
end

Class Method Details

.from_ewkb(ewkb) ⇒ Object

Creates a geometry based on a EWKB string. The actual class returned depends of the content of the string passed as argument. Since WKB strings are a subset of EWKB, they are also valid.



143
144
145
146
147
148
# File 'lib/geo_ruby/simple_features/geometry.rb', line 143

def self.from_ewkb(ewkb)
  factory = GeometryFactory::new
  ewkb_parser= EWKBParser::new(factory)
  ewkb_parser.parse(ewkb)
  factory.geometry
end

.from_ewkt(ewkt) ⇒ Object

Creates a geometry based on a EWKT string. Since WKT strings are a subset of EWKT, they are also valid.



157
158
159
160
161
162
# File 'lib/geo_ruby/simple_features/geometry.rb', line 157

def self.from_ewkt(ewkt)
  factory = GeometryFactory::new
  ewkt_parser= EWKTParser::new(factory)
  ewkt_parser.parse(ewkt)
  factory.geometry
end

.from_geojson(geojson, srid = DEFAULT_SRID) ⇒ Object

Some GeoJSON files do not include srid info, so we provide an optional parameter



221
222
223
224
225
# File 'lib/geo_ruby/simple_features/geometry.rb', line 221

def self.from_geojson(geojson, srid=DEFAULT_SRID)
  geojson_parser= GeojsonParser::new
  geojson_parser.parse(geojson, srid)
  geojson_parser.geometry
end

.from_georss(georss) ⇒ Object

sends back a geometry based on the GeoRSS string passed as argument



165
166
167
168
169
# File 'lib/geo_ruby/simple_features/geometry.rb', line 165

def self.from_georss(georss)
  georss_parser= GeorssParser::new
  georss_parser.parse(georss)
  georss_parser.geometry
end

.from_georss_with_tags(georss) ⇒ Object

sends back an array: The first element is the goemetry based on the GeoRSS string passed as argument. The second one is the GeoRSSTags (found only with the Simple format)



171
172
173
174
175
# File 'lib/geo_ruby/simple_features/geometry.rb', line 171

def self.from_georss_with_tags(georss)
  georss_parser= GeorssParser::new
  georss_parser.parse(georss,true)
  [georss_parser.geometry, georss_parser.georss_tags]
end

.from_hex_ewkb(hexewkb) ⇒ Object

Creates a geometry based on a HexEWKB string



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

def self.from_hex_ewkb(hexewkb)
  factory = GeometryFactory::new
  hexewkb_parser= HexEWKBParser::new(factory)
  hexewkb_parser.parse(hexewkb)
  factory.geometry
end

.from_kml(kml) ⇒ Object

Sends back a geometry from a KML encoded geometry string. Limitations : Only supports points, linestrings and polygons (no collection for now). Addapted from Pramukta’s code



180
181
182
# File 'lib/geo_ruby/simple_features/geometry.rb', line 180

def self.from_kml(kml)
  return GeoRuby::SimpleFeatures::Geometry.from_ewkt(kml_to_wkt(kml))
end

.kml_to_wkt(kml) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/geo_ruby/simple_features/geometry.rb', line 185

def self.kml_to_wkt(kml)
  doc = REXML::Document.new(kml)
  wkt = ""
  if ["Point", "LineString", "Polygon" ].include?(doc.root.name)
    case doc.root.name
    when "Point" then
      coords = doc.elements["/Point/coordinates"].text.gsub(/\n/," ")
      wkt = doc.root.name.upcase + "(" + split_coords(coords).join(' ') + ")"
    when "LineString" then
      coords = doc.elements["/LineString/coordinates"].text.gsub(/\n/," ")
      coords = split_coords(coords)
      wkt = doc.root.name.upcase + "(" + coords.join(",") + ")"
    when "Polygon" then
      # polygons have one outer ring and zero or more inner rings
      bounds = []
      bounds << doc.elements["/Polygon/outerBoundaryIs/LinearRing/coordinates"].text
      inner_coords_elements = doc.elements.each("/Polygon/innerBoundaryIs/LinearRing/coordinates") do |inner_coords|
        inner_coords = inner_coords.text
        bounds << inner_coords
      end

      wkt = doc.root.name.upcase + "(" + bounds.map do |bound|
        bound.gsub!(/\n/, " ")
        bound = split_coords(bound)
        if bound.first != bound.last
          bound.push bound.first
        end
        "(" + bound.join(",") + ")"
      end.join(",") + ")"
    end
  end
  return wkt
end

Instance Method Details

#as_ewkb(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object

Outputs the geometry as an EWKB string. The allow_srid, allow_z and allow_m arguments allow the output to include srid, z and m respectively if they are present in the geometry. If these arguments are set to false, srid, z and m are not included, even if they are present in the geometry. By default, the output string contains all the information in the object.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geo_ruby/simple_features/geometry.rb', line 49

def as_ewkb(allow_srid=true,allow_z=true,allow_m=true)
  ewkb="";

  ewkb << 1.chr #little_endian by default

  type= binary_geometry_type
  if @with_z and allow_z
    type = type | Z_MASK
  end
  if @with_m and allow_m
    type = type | M_MASK
  end
  if allow_srid
    type = type | SRID_MASK
    ewkb << [type,@srid].pack("VV")
  else
    ewkb << [type].pack("V")
  end

  ewkb << binary_representation(allow_z,allow_m)
end

#as_ewkt(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object

Outputs the geometry as an EWKT string.



87
88
89
90
91
92
93
94
95
96
# File 'lib/geo_ruby/simple_features/geometry.rb', line 87

def as_ewkt(allow_srid=true,allow_z=true,allow_m=true)
  if allow_srid
    ewkt="SRID=#{@srid};"
  else
    ewkt=""
  end
  ewkt << text_geometry_type
  ewkt << "M" if @with_m and allow_m and (!@with_z or !allow_z) #to distinguish the M from the Z when there is actually no Z...
  ewkt << "(" << text_representation(allow_z,allow_m) << ")"
end

#as_georss(options = {}) ⇒ Object

Outputs the geometry in georss format. Assumes the geometries are in latlon format, with x as lon and y as lat. Pass the :dialect option to swhit format. Possible values are: :simple (default), :w3cgeo and :gml.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/geo_ruby/simple_features/geometry.rb', line 106

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_hex_ewkb(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object

Outputs the geometry as a HexEWKB string. It is almost the same as a WKB string, except that each byte of a WKB string is replaced by its hexadecimal 2-character representation in a HexEWKB string.



77
78
79
# File 'lib/geo_ruby/simple_features/geometry.rb', line 77

def as_hex_ewkb(allow_srid=true,allow_z=true,allow_m=true)
  as_ewkb(allow_srid, allow_z, allow_m).unpack('H*').join('').upcase
end

#as_hex_wkbObject

Outputs the geometry as a strict HexWKB string



82
83
84
# File 'lib/geo_ruby/simple_features/geometry.rb', line 82

def as_hex_wkb
  as_hex_ewkb(false,false,false)
end

#as_kml(options = {}) ⇒ Object

Iutputs 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)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/geo_ruby/simple_features/geometry.rb', line 127

def as_kml(options = {})
  id_attr = ""
  id_attr = " id=\"#{options[:id]}\"" if options[:id]

  geom_data = ""
  geom_data += "<extrude>#{options[:extrude]}</extrude>\n" if options[:extrude]
  geom_data += "<tesselate>#{options[:tesselate]}</tesselate>\n" if options[:tesselate]
  geom_data += "<altitudeMode>#{options[:altitude_mode]}</altitudeMode>\n" if options[:altitude_mode]

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

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

#as_wkbObject

Outputs the geometry as a strict WKB string.



72
73
74
# File 'lib/geo_ruby/simple_features/geometry.rb', line 72

def as_wkb
  as_ewkb(false,false,false)
end

#as_wktObject

Outputs the geometry as strict WKT string.



99
100
101
# File 'lib/geo_ruby/simple_features/geometry.rb', line 99

def as_wkt
  as_ewkt(false,false,false)
end

#bounding_boxObject

to be implemented in subclasses



35
36
# File 'lib/geo_ruby/simple_features/geometry.rb', line 35

def bounding_box
end

#envelopeObject

Returns an Envelope object for the geometry



43
44
45
# File 'lib/geo_ruby/simple_features/geometry.rb', line 43

def envelope
  Envelope.from_points(bounding_box,srid,with_z)
end

#m_rangeObject

to be implemented in subclasses



39
40
# File 'lib/geo_ruby/simple_features/geometry.rb', line 39

def m_range
end