Class: GeoRuby::SimpleFeatures::Geometry
- Inherits:
-
Object
- Object
- GeoRuby::SimpleFeatures::Geometry
- 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
Instance Attribute Summary collapse
-
#srid ⇒ Object
SRID of the geometry.
-
#with_m ⇒ Object
(also: #with_m?)
Flag indicating if the m ordinate of the geometry is meaningful.
-
#with_z ⇒ Object
(also: #with_z?)
Flag indicating if the z ordinate of the geometry is meaningful.
Class Method Summary collapse
-
.from_ewkb(ewkb) ⇒ Object
Creates a geometry based on a EWKB string.
-
.from_ewkt(ewkt) ⇒ Object
Creates a geometry based on a EWKT string.
-
.from_geojson(geojson, srid = DEFAULT_SRID) ⇒ Object
Some GeoJSON files do not include srid info, so we provide an optional parameter.
-
.from_georss(georss) ⇒ Object
Creates a geometry based on the GeoRSS string given.
-
.from_georss_with_tags(georss) ⇒ Object
sends back an array: The first element is the goemetry based on the GeoRSS string passed as argument.
-
.from_hex_ewkb(hexewkb) ⇒ Object
Creates a geometry based on a HexEWKB string given.
-
.from_kml(kml) ⇒ Object
Sends back a geometry from a KML encoded geometry string.
Instance Method Summary collapse
-
#as_ewkb(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object
Outputs the geometry as an EWKB string.
-
#as_ewkt(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object
Outputs the geometry as an EWKT string.
-
#as_georss(options = {}) ⇒ Object
Outputs the geometry in georss format.
-
#as_hex_ewkb(allow_srid = true, allow_z = true, allow_m = true) ⇒ Object
Outputs the geometry as a HexEWKB string.
-
#as_hex_wkb ⇒ Object
Outputs the geometry as a strict HexWKB string.
- #as_json(_options = {}) ⇒ Object (also: #as_geojson)
-
#as_kml(options = {}) ⇒ Object
Iutputs the geometry in kml format : options are
:id
,:tesselate
,:extrude
,:altitude_mode
. -
#as_wkb ⇒ Object
Outputs the geometry as a strict WKB string.
-
#as_wkt ⇒ Object
Outputs the geometry as strict WKT string.
-
#bounding_box ⇒ Object
to be implemented in subclasses.
-
#envelope ⇒ Object
Returns an Envelope object for the geometry.
-
#initialize(srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Geometry
constructor
A new instance of Geometry.
-
#m_range ⇒ Object
to be implemented in subclasses.
-
#to_json(options = {}) ⇒ Object
simple geojson representation TODO add CRS / SRID support?.
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
#srid ⇒ Object
SRID of the geometry
11 12 13 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 11 def srid @srid end |
#with_m ⇒ Object 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_z ⇒ Object 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.
188 189 190 191 192 193 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 188 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.
205 206 207 208 209 210 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 205 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
237 238 239 240 241 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 237 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
Creates a geometry based on the GeoRSS string given.
213 214 215 216 217 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 213 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)
222 223 224 225 226 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 222 def self.(georss) georss_parser = GeorssParser.new georss_parser.parse(georss, true) [georss_parser.geometry, georss_parser.] end |
.from_hex_ewkb(hexewkb) ⇒ Object
Creates a geometry based on a HexEWKB string given.
196 197 198 199 200 201 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 196 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.
229 230 231 232 233 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 229 def self.from_kml(kml) factory = GeometryFactory.new parser = KmlParser.new(factory) parser.parse(kml) 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. 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 58 def as_ewkb(allow_srid = true, allow_z = true, allow_m = true) ewkb = 1.chr # little_endian by default type = binary_geometry_type type |= Z_MASK if @with_z && allow_z type |= M_MASK if @with_m && allow_m 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.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 101 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 # to distinguish the M from the Z when there is actually no Z... ewkt << 'M' if @with_m && allow_m && (!@with_z || !allow_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
.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 124 def as_georss( = {}) dialect = [:dialect] || :simple case (dialect) when :simple geom_attr = '' if [:featuretypetag] geom_attr += " featuretypetag=\"#{[:featuretypetag]}\"" end if [:relationshiptag] geom_attr += " relationshiptag=\"#{[:relationshiptag]}\"" end geom_attr += " floor=\"#{[:floor]}\"" if [:floor] geom_attr += " radius=\"#{[:radius]}\"" if [:radius] geom_attr += " elev=\"#{[:elev]}\"" if [:elev] georss_simple_representation(.merge(geom_attr: geom_attr)) when :w3cgeo georss_w3cgeo_representation() when :gml georss_gml_representation() 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 hex 2-char representation in a HexEWKB string.
87 88 89 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 87 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_wkb ⇒ Object
Outputs the geometry as a strict HexWKB string
94 95 96 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 94 def as_hex_wkb as_hex_ewkb(false, false, false) end |
#as_json(_options = {}) ⇒ Object Also known as: as_geojson
180 181 182 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 180 def as_json( = {}) # Implemented by each class 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: clampToGround is the default)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 150 def as_kml( = {}) id_attr = '' id_attr = " id=\"#{[:id]}\"" if [:id] geom_data = '' if [:extrude] geom_data += "<extrude>#{[:extrude]}</extrude>\n" end if [:tesselate] geom_data += "<tesselate>#{[:tesselate]}</tesselate>\n" end if [:altitude_mode] geom_data += "<altitudeMode>#{[:altitude_mode]}</altitudeMode>\n" end allow_z = (with_z || ![:altitude].nil?) && (![:altitude_mode].nil?) && [:atitude_mode] != 'clampToGround' fixed_z = [:altitude] kml_representation(.merge(id_attr: id_attr, geom_data: geom_data, allow_z: allow_z, fixed_z: fixed_z)) end |
#as_wkb ⇒ Object
Outputs the geometry as a strict WKB string.
78 79 80 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 78 def as_wkb as_ewkb(false, false, false) end |
#as_wkt ⇒ Object
Outputs the geometry as strict WKT string.
116 117 118 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 116 def as_wkt as_ewkt(false, false, false) end |
#bounding_box ⇒ Object
to be implemented in subclasses
35 36 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 35 def bounding_box end |
#envelope ⇒ Object
Returns an Envelope object for the geometry
45 46 47 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 45 def envelope Envelope.from_points(bounding_box, srid, with_z) end |
#m_range ⇒ Object
to be implemented in subclasses
39 40 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 39 def m_range end |
#to_json(options = {}) ⇒ Object
simple geojson representation TODO add CRS / SRID support?
176 177 178 |
# File 'lib/geo_ruby/simple_features/geometry.rb', line 176 def to_json( = {}) as_json().to_json() end |