Skip to content
Browse files

Initial extensions

  • Loading branch information...
0 parents commit 2642dc585ddd44e4c00e52f69f6f755e207ba3d3 @pka pka committed Oct 17, 2011
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in ruby-mapscript.gemspec
+gemspec
11 Rakefile
@@ -0,0 +1,11 @@
+require 'bundler/gem_tasks'
+
+#Test::Unit
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+end
+
+desc "Run tests"
+task :default => :test
121 lib/mapscript/mapscript_extensions.rb
@@ -0,0 +1,121 @@
+# Ruby Mapscript API extensions
+
+require "mapscript"
+
+module Mapscript
+
+ class MapLayers
+ include Enumerable
+
+ def initialize(map)
+ @map = map
+ end
+
+ def each
+ 0.upto(@map.numlayers-1) do |idx|
+ yield @map.getLayer(idx)
+ end if @map.numlayers > 0
+ end
+
+ def [](idx)
+ case idx
+ when Fixnum
+ if idx >= 0
+ @map.getLayer(idx)
+ else
+ @map.getLayer(@map.numlayers+idx)
+ end
+ when Range
+ to_a[idx]
+ when String
+ @map.getLayerByName(idx)
+ else
+ nil
+ end
+ end
+
+ def <<(layer)
+ @map.insertLayer(layer, @map.numlayers-1)
+ end
+
+ def to_a
+ if @map.numlayers > 0
+ (0..@map.numlayers-1).collect { |idx| @map.getLayer(idx) }
+ else
+ []
+ end
+ end
+ end
+
+ class LayerClasses
+ include Enumerable
+
+ def initialize(layer)
+ @layer = layer
+ end
+
+ def each
+ 0.upto(@layer.numclasses-1) do |idx|
+ yield @layer.getClass(idx)
+ end if @layer.numclasses > 0
+ end
+
+ def [](idx)
+ case idx
+ when Fixnum
+ if idx >= 0
+ @layer.getClass(idx)
+ else
+ @layer.getClass(@layer.numclasses+idx)
+ end
+ when Range
+ to_a[idx]
+ when String
+ find { |cls| cls.name == idx }
+ else
+ nil
+ end
+ end
+
+ def <<(newclass)
+ @layer.insertClass(newclass, @layer.numclasses-1)
+ end
+
+ def to_a
+ if @layer.numclasses > 0
+ (0..@layer.numclasses-1).collect { |idx| @layer.getClass(idx) }
+ else
+ []
+ end
+ end
+ 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
+ (0..[-1,numprocessing-1].max).collect { |idx| getProcessing(idx) }
+ end
+ end
+
+ # ClassObj extensions
+ class ClassObj
+ # Return StyleObj array
+ def styles
+ (0..[-1,numstyles-1].max).collect { |idx| getStyle(idx) }
+ end
+ end
+
+end
2 lib/ruby_mapscript.rb
@@ -0,0 +1,2 @@
+require "ruby_mapscript/version"
+require "mapscript/mapscript_extensions"
36 lib/ruby_mapscript/mapserver.rb
@@ -0,0 +1,36 @@
+# OGC compliant map server Rack Middleware
+# OWS requests include WMS, WFS, WCS and SOS requests supported by MapServer
+
+require "mapscript"
+
+module RubyMapscript
+
+class Mapserver
+ def initialize(app, mapfile)
+ @wms = Mapscript::MapObj.new(mapfile)
+ end
+
+ def call(env)
+ req = Mapscript::OWSRequest.new
+ %w(REQUEST_METHOD QUERY_STRING HTTP_COOKIE).each do |var|
+ ENV[var] = env[var]
+ end
+ req.loadParams
+ #Returns the number of name/value pairs collected.
+ #Warning: most errors will result in a process exit!
+
+ # redirect stdout & handle request
+ Mapscript::msIO_installStdoutToBuffer()
+ retval = @wms.OWSDispatch(req)
+ #Returns MS_DONE (2) if there is no valid OWS request in the req object,
+ # MS_SUCCESS (0) if an OWS request was successfully processed and
+ # MS_FAILURE (1) if an OWS request was not successfully processed.
+ content_type = Mapscript::msIO_stripStdoutBufferContentType()
+ map_image = Mapscript::msIO_getStdoutBufferBytes()
+ Mapscript::msIO_resetHandlers()
+
+ [200, {'Content-Type' => content_type}, map_image]
+ end
+end
+
+end
3 lib/ruby_mapscript/version.rb
@@ -0,0 +1,3 @@
+module RubyMapscript
+ VERSION = "0.1.0"
+end
20 ruby_mapscript.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "ruby_mapscript/version"
+
+Gem::Specification.new do |s|
+ s.name = "ruby_mapscript"
+ s.version = RubyMapscript::VERSION
+ s.authors = ["Pirmin Kalberer"]
+ s.email = ["pka@sourcepole.ch"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "ruby_mapscript"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
17 test/empty.map
@@ -0,0 +1,17 @@
+MAP
+ NAME "Empty Map"
+ SIZE 600 600
+ EXTENT -180 -90 180 90
+ IMAGECOLOR 180 180 250
+ IMAGETYPE PNG
+
+ UNITS DD
+ PROJECTION
+ "init=epsg:4326"
+ END
+
+ WEB
+ IMAGEPATH "/tmp/"
+ IMAGEURL "/tmp/"
+ END
+END
62 test/test.map
@@ -0,0 +1,62 @@
+MAP
+ NAME "WMS Map"
+ SIZE 600 600
+ EXTENT -180 -90 180 90
+ IMAGECOLOR 180 180 250
+ IMAGETYPE PNG
+
+ #CONFIG "MS_ERRORFILE" "stderr" # http://mapserver.org/optimization/debugging.html#step-1-set-the-ms-errorfile-variable
+ #DEBUG 5 # http://mapserver.org/optimization/debugging.html#step-2-set-the-debug-level
+ #CONFIG "CPL_DEBUG" "ON" # http://mapserver.org/optimization/debugging.html#step-3-turn-on-cpl-debug-optional
+ #CONFIG "PROJ_DEBUG" "ON" # http://mapserver.org/optimization/debugging.html#step-4-turn-on-proj-debug-optional
+
+ UNITS DD
+ PROJECTION
+ "init=epsg:4326"
+ END
+
+ WEB
+ IMAGEPATH "/tmp/"
+ IMAGEURL "/tmp/"
+ END
+
+ LAYER
+ NAME "wms_layer"
+ TYPE RASTER
+ STATUS OFF
+ CONNECTIONTYPE WMS
+ CONNECTION "http://iceds.ge.ucl.ac.uk/cgi-bin/icedswms?"
+ METADATA
+ "wms_title" "WMS Layer"
+ "wms_extent" "-180 -90 180 90"
+ "wms_srs" "EPSG:4326"
+ "wms_name" "landsat5,landsat,GRID"
+ "wms_server_version" "1.1.1"
+ "wms_format" "image/jpeg"
+ END
+ END
+
+ LAYER
+ NAME shppoly
+ TYPE line
+ DATA "data/shppoly/poly.shp"
+ STATUS OFF
+ CLASSITEM "AREA"
+ CLASS
+ NAME "test1"
+ STYLE
+ COLOR 20 20 20
+ WIDTH 5
+ END
+ STYLE
+ COLOR 50 50 255
+ WIDTH 3
+ END
+ STYLE
+ COLOR 255 255 0
+ WIDTH 1
+ PATTERN 4 4 END
+ END
+ END
+ END
+END
68 test/test_mapscript_extensions.rb
@@ -0,0 +1,68 @@
+require 'test/unit'
+require "ruby_mapscript"
+include Mapscript
+
+class TestMapscriptExtension < Test::Unit::TestCase
+ def setup
+ mapfile = File.dirname(__FILE__) + '/test.map'
+ @map = MapObj.new(mapfile)
+ end
+
+ def test_arrays
+ assert_equal 0, @map.getLayersDrawingOrder[0]
+ style = @map.layers['shppoly'].classes.first.styles.last
+ assert_equal SWIG::TYPE_p_double, style.pattern.class
+ pattern = Doublearray.frompointer(style.pattern)
+ assert_equal 4.0, pattern[0]
+ assert_equal 4.0, pattern[style.patternlength-1]
+ end
+
+ def test_layer_access
+ assert_equal @map.numlayers, @map.layers.to_a.size
+ assert_equal @map.numlayers, @map.layers[0..-1].size
+ assert_equal 0, @map.layers.first.index
+ @map.layers.each_with_index do |l, i|
+ assert_equal i, @map.layers[i].index
+ end
+ assert_equal 'wms_layer', @map.layers[0].name
+ assert_equal 'wms_layer', @map.layers[-@map.numlayers].name
+ assert_nil @map.layers[99]
+ assert_equal 'wms_layer', @map.layers['wms_layer'].name
+ assert_nil @map.layers['xxx']
+ oldcnt = @map.numlayers
+ @map.layers << @map.layers.first
+ assert_equal oldcnt+1, @map.numlayers
+ assert_equal @map.numlayers, @map.layers.to_a.size
+ assert_equal @map.numlayers, @map.layers[0..-1].size
+ end
+
+ def test_class_access
+ layer = @map.layers['shppoly']
+ assert_equal layer.numclasses, layer.classes.to_a.size
+ assert_equal layer.numclasses, layer.classes[0..-1].size
+ assert_equal 'test1', layer.classes[0].name
+ assert_equal 'test1', layer.classes[-layer.numclasses].name
+ assert_nil layer.classes[99]
+ assert_equal 'test1', layer.classes['test1'].name
+ assert_nil layer.classes['xxx']
+ oldcnt = layer.numclasses
+ layer.classes << layer.classes.first
+ assert_equal oldcnt+1, layer.numclasses
+ assert_equal layer.numclasses, layer.classes.to_a.size
+ assert_equal layer.numclasses, layer.classes[0..-1].size
+ end
+
+ def test_empty_map
+ mapfile = File.dirname(__FILE__) + '/empty.map'
+ @map = MapObj.new(mapfile)
+ assert_equal 0, @map.layers.to_a.size
+ assert_nil @map.layers[0]
+ assert_nil @map.layers['wms_layer']
+ @map.layers.each { }
+ end
+
+ def test_draw
+ mapimage = @map.draw
+ assert_equal 2338, mapimage.getSize
+ end
+end

0 comments on commit 2642dc5

Please sign in to comment.
Something went wrong with that request. Please try again.