Permalink
Browse files

More iterators

  • Loading branch information...
1 parent 8f7dd12 commit 4c6b4aea57a37b54159dbfbe775d384bf1907caf @pka pka committed Jan 3, 2012
Showing with 100 additions and 21 deletions.
  1. +78 −21 lib/mapscript/mapscript_extensions.rb
  2. +22 −0 test/test_mapscript_extensions.rb
@@ -4,6 +4,7 @@
module Mapscript
+ # Generic iterator for Mapscript collections
class MapscriptEnumerable
include Enumerable
@@ -41,6 +42,7 @@ def to_a
end
end
+ # LayerObj iterator
class MapLayers < MapscriptEnumerable
def initialize(map)
@@ -70,6 +72,7 @@ def <<(layer)
end
end
+ # ClassObj iterator
class LayerClasses < MapscriptEnumerable
def initialize(layer)
@@ -100,29 +103,9 @@ def <<(newclass)
end
- # MapObj extensions
- class MapObj
- # Return LayerObj array
- def layers
- @map_layers ||= MapLayers.new(self)
- end
- end
-
- # LayerObj extensions
- class LayerObj
- def classes
- @classes ||= LayerClasses.new(self)
- end
-
- # Return string array
- def processings
- @processings ||= MapscriptEnumerable.new(self, :numprocessing, :getProcessing)
- end
- end
-
# ClassObj extensions
class ClassObj
- # Return StyleObj array
+ # Return StyleObj iterator
def styles
@styles ||= MapscriptEnumerable.new(self, :numstyles, :getStyle)
end
@@ -191,4 +174,78 @@ def inspect
end
end
+
+ # LabelObj extensions
+ class LabelObj
+ # Return styleObj iterator
+ def styles
+ @styles ||= MapscriptEnumerable.new(self, :numstyles, :getStyle)
+ end
+ end
+
+ # LayerObj extensions
+ class LayerObj
+ # Return ClassObj iterator
+ def classes
+ @classes ||= LayerClasses.new(self)
+ end
+
+ # Return string iterator
+ def processings
+ @processings ||= MapscriptEnumerable.new(self, :numprocessing, :getProcessing)
+ end
+ end
+
+ # LineObj extensions
+ class LineObj
+ # Return pointObj iterator
+ def points
+ @points ||= MapscriptEnumerable.new(self, :numpoints, :get)
+ end
+ end
+
+ # MapObj extensions
+ class MapObj
+ # Return LayerObj iterator
+ def layers
+ @map_layers ||= MapLayers.new(self)
+ end
+ end
+
+ # ResultCacheObj extensions
+ class ResultCacheObj
+ # Return resultCacheObj iterator
+ def results
+ @results ||= MapscriptEnumerable.new(self, :numresults, :getResult)
+ end
+ end
+
+ # ShapefileObj extensions
+ class ShapefileObj
+ # Return shapeObj iterator
+ def shapes
+ @shapes ||= MapscriptEnumerable.new(self, :numshapes, :getShape)
+ end
+ end
+
+ # ShapeObj extensions
+ class ShapeObj
+ # Return lineObj iterator
+ def lines
+ @lines ||= MapscriptEnumerable.new(self, :numlines, :get)
+ end
+
+ # Return shape attribute (string) iterator
+ def values
+ @values ||= MapscriptEnumerable.new(self, :numvalues, :getValue)
+ end
+ end
+
+ # SymbolSetObj extensions
+ class SymbolSetObj
+ # Return symbolObj iterator
+ def symbols
+ @symbols ||= MapscriptEnumerable.new(self, :numsymbols, :getSymbol) #:getSymbolByName
+ end
+ end
end
@@ -111,6 +111,28 @@ def test_class_access
assert_equal layer.numclasses, layer.classes[0..-1].size
end
+ def test_iterators
+ assert_equal 0, LabelObj.new.styles.size
+
+ assert_equal 0, ResultCacheObj.new.results.size
+
+ shapefile = File.dirname(__FILE__) + '/data/world_testpoly.shp'
+ shapefile_obj = ShapefileObj.new(shapefile)
+ assert_equal 4, shapefile_obj.shapes.size
+ shape = shapefile_obj.shapes[0]
+ assert_equal 0, shape.index
+ assert_equal 1, shape.lines.size
+ lines = shape.lines[0]
+ assert_equal 0, shape.values.size
+ assert_equal 5, lines.points.size
+ point = lines.points[0]
+ assert_not_nil point
+
+ symbol_set = SymbolSetObj.new
+ assert_equal 1, symbol_set.symbols.size
+ assert_not_nil symbol_set.symbols[0]
+ end
+
def test_empty_map
mapfile = File.dirname(__FILE__) + '/empty.map'
@map = MapObj.new(mapfile)

0 comments on commit 4c6b4ae

Please sign in to comment.