GeoRuby 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,8 @@ module GeoRuby
25
25
  class ShpFile
26
26
  attr_reader :shp_type, :record_count, :xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax
27
27
 
28
+ include Enumerable
29
+
28
30
  #Opens a SHP file. Both "abc.shp" and "abc" are accepted. The files "abc.shp", "abc.shx" and "abc.dbf" must be present
29
31
  def initialize(file, access = "r")
30
32
  #strip the shp out of the file if present
@@ -152,8 +152,7 @@ module GeoRuby#:nodoc:
152
152
  georss_parser= GeorssParser::new
153
153
  georss_parser.parse(georss)
154
154
  georss_parser.geometry
155
- end
156
-
155
+ end
157
156
  #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)
158
157
  def self.from_georss_with_tags(georss)
159
158
  georss_parser= GeorssParser::new
@@ -161,6 +160,55 @@ module GeoRuby#:nodoc:
161
160
  [georss_parser.geometry, georss_parser.georss_tags]
162
161
  end
163
162
 
163
+ #Sends back a geometry from a KML encoded geometry string.
164
+ #Limitations : Only supports points, linestrings and polygons (no collection for now).
165
+ #Addapted from Pramukta's code
166
+ def self.from_kml(kml)
167
+ return GeoRuby::SimpleFeatures::Geometry.from_ewkt(kml_to_wkt(kml))
168
+ end
169
+
170
+ require 'rexml/document'
171
+ def self.kml_to_wkt(kml)
172
+ doc = REXML::Document.new(kml)
173
+ wkt = ""
174
+ if ["Point", "LineString", "Polygon" ].include?(doc.root.name)
175
+ case doc.root.name
176
+ when "Point" then
177
+ coords = doc.elements["/Point/coordinates"].text.gsub(/\n/," ")
178
+ wkt = doc.root.name.upcase + "(" + split_coords(coords).join(' ') + ")"
179
+ when "LineString" then
180
+ coords = doc.elements["/LineString/coordinates"].text.gsub(/\n/," ")
181
+ coords = split_coords(coords)
182
+ wkt = doc.root.name.upcase + "(" + coords.join(",") + ")"
183
+ when "Polygon" then
184
+ # polygons have one outer ring and zero or more inner rings
185
+ bounds = []
186
+ bounds << doc.elements["/Polygon/outerBoundaryIs/LinearRing/coordinates"].text
187
+ inner_coords_elements = doc.elements.each("/Polygon/innerBoundaryIs/LinearRing/coordinates") do |inner_coords|
188
+ inner_coords = inner_coords.text
189
+ bounds << inner_coords
190
+ end
191
+
192
+ wkt = doc.root.name.upcase + "(" + bounds.map do |bound|
193
+ bound.gsub!(/\n/, " ")
194
+ bound = split_coords(bound)
195
+ if bound.first != bound.last
196
+ bound.push bound.first
197
+ end
198
+ "(" + bound.join(",") + ")"
199
+ end.join(",") + ")"
200
+ end
201
+ end
202
+ return wkt
203
+ end
204
+
205
+ private
206
+
207
+ def self.split_coords(coords)
208
+ coords.split(" ").collect { |coord|
209
+ coord.gsub(","," ")
210
+ }
211
+ end
164
212
  end
165
213
  end
166
214
  end
data/rakefile.rb CHANGED
@@ -24,7 +24,7 @@ spec = Gem::Specification::new do |s|
24
24
  s.platform = Gem::Platform::RUBY
25
25
 
26
26
  s.name = 'GeoRuby'
27
- s.version = "1.2.1"
27
+ s.version = "1.2.2"
28
28
  s.summary = "Ruby data holder for OGC Simple Features"
29
29
  s.description = <<EOF
30
30
  GeoRuby is intended as a holder for data returned from PostGIS and MySQL Spatial queries. The data model roughly follows the OGC "Simple Features for SQL" specification (see www.opengis.org/docs/99-049.pdf), although without any kind of advanced functionalities (such as geometric operators or reprojections)
@@ -153,5 +153,63 @@ class TestGeorssKml < Test::Unit::TestCase
153
153
 
154
154
  end
155
155
 
156
+ def test_kml_read
157
+ g = Geometry.from_kml("<Point><coordinates>45,12,25</coordinates></Point>")
158
+ assert(g.is_a?(Point))
159
+ assert_equal(g,Point.from_x_y_z(45,12,25))
160
+
161
+ g = Geometry.from_kml("<LineString>
162
+ <extrude>1</extrude>
163
+ <tessellate>1</tessellate>
164
+ <coordinates>
165
+ -122.364383,37.824664,0 -122.364152,37.824322,0
166
+ </coordinates>
167
+ </LineString>")
168
+ assert(g.is_a?(LineString))
169
+ assert(2,g.length)
170
+ assert_equal(LineString.from_points([Point.from_x_y_z(-122.364383,37.824664,0),Point.from_x_y_z(-122.364152,37.824322,0)],DEFAULT_SRID,true),g)
171
+
172
+ g = Geometry.from_kml("<Polygon>
173
+ <extrude>1</extrude>
174
+ <altitudeMode>relativeToGround</altitudeMode>
175
+ <outerBoundaryIs>
176
+ <LinearRing>
177
+ <coordinates>
178
+ -122.366278,37.818844,30
179
+ -122.365248,37.819267,30
180
+ -122.365640,37.819861,30
181
+ -122.366669,37.819429,30
182
+ -122.366278,37.818844,30
183
+ </coordinates>
184
+ </LinearRing>
185
+ </outerBoundaryIs>
186
+ <innerBoundaryIs>
187
+ <LinearRing>
188
+ <coordinates>
189
+ -122.366212,37.818977,30
190
+ -122.365424,37.819294,30
191
+ -122.365704,37.819731,30
192
+ -122.366488,37.819402,30
193
+ -122.366212,37.818977,30
194
+ </coordinates>
195
+ </LinearRing>
196
+ </innerBoundaryIs>
197
+ <innerBoundaryIs>
198
+ <LinearRing>
199
+ <coordinates>
200
+ -122.366212,37.818977,30
201
+ -122.365424,37.819294,30
202
+ -122.365704,37.819731,30
203
+ -122.366488,37.819402,30
204
+ -122.366212,37.818977,30
205
+ </coordinates>
206
+ </LinearRing>
207
+ </innerBoundaryIs>
208
+ </Polygon>")
209
+ assert(g.is_a?(Polygon))
210
+ assert_equal(3,g.length)
211
+
212
+ end
213
+
156
214
 
157
215
  end
data/test/test_shp.rb CHANGED
@@ -73,5 +73,4 @@ class TestShp < Test::Unit::TestCase
73
73
 
74
74
  shpfile.close
75
75
  end
76
-
77
76
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: GeoRuby
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.1
7
- date: 2007-02-20 00:00:00 +01:00
6
+ version: 1.2.2
7
+ date: 2007-03-24 00:00:00 +01:00
8
8
  summary: Ruby data holder for OGC Simple Features
9
9
  require_paths:
10
10
  - lib