Class GeoRuby::SimpleFeatures::Geometry
In: lib/geo_ruby/simple_features/geometry.rb
Parent: Object

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

Methods

Attributes

srid  [RW]  SRID of the geometry
with_m  [RW]  Flag indicating if the m ordinate of the geometry is meaningful
with_z  [RW]  Flag indicating if the z ordinate of the geometry is meaningful

Public Class methods

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.

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 142
142:       def self.from_ewkb(ewkb)
143:         factory = GeometryFactory::new
144:         ewkb_parser= EWKBParser::new(factory)
145:         ewkb_parser.parse(ewkb)
146:         factory.geometry
147:       end

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

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 156
156:       def self.from_ewkt(ewkt)
157:         factory = GeometryFactory::new
158:         ewkt_parser= EWKTParser::new(factory)
159:         ewkt_parser.parse(ewkt)
160:         factory.geometry
161:       end

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

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 164
164:       def self.from_georss(georss)
165:         georss_parser= GeorssParser::new
166:         georss_parser.parse(georss)
167:         georss_parser.geometry
168:       end

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)

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 170
170:       def self.from_georss_with_tags(georss)
171:         georss_parser= GeorssParser::new
172:         georss_parser.parse(georss,true)
173:         [georss_parser.geometry, georss_parser.georss_tags]
174:       end

Creates a geometry based on a HexEWKB string

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 149
149:       def self.from_hex_ewkb(hexewkb)
150:         factory = GeometryFactory::new
151:         hexewkb_parser= HexEWKBParser::new(factory)
152:         hexewkb_parser.parse(hexewkb)
153:         factory.geometry
154:       end

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

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 179
179:       def self.from_kml(kml)
180:         return GeoRuby::SimpleFeatures::Geometry.from_ewkt(kml_to_wkt(kml))
181:       end

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 184
184:       def self.kml_to_wkt(kml)
185:         doc = REXML::Document.new(kml)
186:         wkt = ""
187:         if ["Point", "LineString", "Polygon" ].include?(doc.root.name)
188:           case doc.root.name 
189:           when "Point" then
190:             coords = doc.elements["/Point/coordinates"].text.gsub(/\n/," ")
191:             wkt = doc.root.name.upcase + "(" + split_coords(coords).join(' ') + ")"
192:           when "LineString" then
193:             coords = doc.elements["/LineString/coordinates"].text.gsub(/\n/," ")
194:             coords = split_coords(coords)
195:             wkt = doc.root.name.upcase + "(" + coords.join(",") + ")"
196:           when "Polygon" then
197:             # polygons have one outer ring and zero or more inner rings
198:             bounds = []
199:             bounds << doc.elements["/Polygon/outerBoundaryIs/LinearRing/coordinates"].text
200:             inner_coords_elements = doc.elements.each("/Polygon/innerBoundaryIs/LinearRing/coordinates") do |inner_coords|
201:               inner_coords = inner_coords.text
202:               bounds << inner_coords
203:             end
204:             
205:             wkt = doc.root.name.upcase + "(" + bounds.map do |bound|
206:               bound.gsub!(/\n/, " ")
207:               bound = split_coords(bound)
208:               if bound.first != bound.last
209:                 bound.push bound.first
210:               end
211:               "(" + bound.join(",") + ")"
212:             end.join(",") + ")"
213:           end
214:         end
215:         return wkt 
216:       end

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 16
16:       def initialize(srid=DEFAULT_SRID,with_z=false,with_m=false)
17:         @srid=srid
18:         @with_z=with_z
19:         @with_m=with_m
20:       end

Public Instance methods

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.

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 47
47:       def as_ewkb(allow_srid=true,allow_z=true,allow_m=true)
48:         ewkb="";
49:        
50:         ewkb << 1.chr #little_endian by default
51:         
52:         type= binary_geometry_type
53:         if @with_z and allow_z
54:           type = type | Z_MASK
55:         end
56:         if @with_m and allow_m
57:           type = type | M_MASK
58:         end
59:         if @srid != DEFAULT_SRID and allow_srid
60:           type = type | SRID_MASK
61:           ewkb << [type,@srid].pack("VV")
62:         else
63:           ewkb << [type].pack("V")
64:         end
65:         
66:         ewkb << binary_representation(allow_z,allow_m)
67:       end

Outputs the geometry as an EWKT string.

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 86
86:       def as_ewkt(allow_srid=true,allow_z=true,allow_m=true)
87:         if @srid!=DEFAULT_SRID and allow_srid #the default SRID is not output like in PostGIS
88:           ewkt="SRID=#{@srid};"
89:         else
90:           ewkt=""
91:         end
92:         ewkt << text_geometry_type 
93:         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... 
94:         ewkt << "(" << text_representation(allow_z,allow_m) << ")"        
95:       end

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.

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 105
105:       def as_georss(options = {})
106:         dialect= options[:dialect] || :simple
107:         case(dialect)
108:         when :simple
109:           geom_attr = ""
110:           geom_attr += " featuretypetag=\"#{options[:featuretypetag]}\"" if options[:featuretypetag]
111:           geom_attr += " relationshiptag=\"#{options[:relationshiptag]}\"" if options[:relationshiptag]
112:           geom_attr += " floor=\"#{options[:floor]}\"" if options[:floor]
113:           geom_attr += " radius=\"#{options[:radius]}\"" if options[:radius]
114:           geom_attr += " elev=\"#{options[:elev]}\"" if options[:elev]
115:           georss_simple_representation(options.merge(:geom_attr => geom_attr))
116:         when :w3cgeo
117:           georss_w3cgeo_representation(options)
118:         when :gml
119:           georss_gml_representation(options)
120:         end
121:       end

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.

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 75
75:       def as_hex_ewkb(allow_srid=true,allow_z=true,allow_m=true)
76:         str = ""
77:         as_ewkb(allow_srid,allow_z,allow_m).each_byte {|char| str << sprintf("%02x",char).upcase}
78:         str
79:       end

Outputs the geometry as a strict HexWKB string

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 81
81:       def as_hex_wkb
82:         as_hex_ewkb(false,false,false)
83:       end

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)

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 126
126:       def as_kml(options = {})
127:         id_attr = ""
128:         id_attr = " id=\"#{options[:id]}\"" if options[:id]
129: 
130:         geom_data = ""
131:         geom_data += "<extrude>#{options[:extrude]}</extrude>\n" if options[:extrude]
132:         geom_data += "<tesselate>#{options[:tesselate]}</tesselate>\n" if options[:tesselate]
133:         geom_data += "<altitudeMode>#{options[:altitude_mode]}</altitudeMode>\n" if options[:altitude_mode]
134:         
135:         allow_z = (with_z || !options[:altitude].nil? )&& (!options[:altitude_mode].nil?) && options[:atitude_mode] != "clampToGround"
136:         fixed_z = options[:altitude]
137:         
138:         kml_representation(options.merge(:id_attr => id_attr, :geom_data => geom_data, :allow_z => allow_z, :fixed_z => fixed_z))
139:       end

Outputs the geometry as a strict WKB string.

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 70
70:       def as_wkb
71:         as_ewkb(false,false,false)
72:       end

Outputs the geometry as strict WKT string.

[Source]

     # File lib/geo_ruby/simple_features/geometry.rb, line 98
 98:       def as_wkt
 99:         as_ewkt(false,false,false)
100:       end

to be implemented in subclasses

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 33
33:       def bounding_box
34:       end

Returns an Envelope object for the geometry

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 41
41:       def envelope
42:         Envelope.from_points(bounding_box,srid,with_z)
43:       end

to be implemented in subclasses

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 37
37:       def m_range
38:       end

[Source]

    # File lib/geo_ruby/simple_features/geometry.rb, line 22
22:       def srid=(new_srid)
23:         @srid = new_srid
24:         unless self.is_a?(Point)
25:           self.each do |geom|
26:             geom.srid=new_srid
27:           end
28:         end
29:       end

[Validate]