grassgis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eca4b61ff65ed5f6bb173f1502e27b2f17ccbdfa
4
+ data.tar.gz: 65794615e3eb17457e67b055c2f056e43d6b32da
5
+ SHA512:
6
+ metadata.gz: af06a15d577d6ebcc835f75cf03a1b42fbdb71229a7b820213ba6c1a62257572981a88fd635197b6a917ebbe9766173aca1fb7f108673eec7c2ae65811a6e986
7
+ data.tar.gz: 31def098f90ef421d0e99f47b3dcb62be72bc62d085021632c496c360037de2b0d8839dd3f7d7ea9f3e3f64376de0a6d09fa724afd780b023065f4caa4fc8176
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ - 2.1.6
5
+ - 1.9.3
6
+ before_install:
7
+ - gem update bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grassgis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Javier Goizueta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # GrassGis
2
+
3
+ Support for scripting GRASS with Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'grassgis'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install grassgis
18
+
19
+ ## Usage
20
+
21
+ This library can prepare environments to execute GRASS commands
22
+ from Ruby scripts.
23
+
24
+ Example:
25
+
26
+ ```ruby
27
+ require 'grassgis'
28
+
29
+ configuration = {
30
+ gisbase: '/usr/local/Cellar/grass-70/7.0.0/grass-7.0.0',
31
+ location: 'world'
32
+ }
33
+
34
+ GrassGis.session configuration do
35
+ r.resamp.stats '-n', input: "map1@mapset1", output: "map2"
36
+ puts g.list('vect').output
37
+ end
38
+ ```
39
+
40
+ ## Roadmap
41
+
42
+ Create a DSL to define GRASS processing recipes.
43
+
44
+ It should handle errors gracefully during recipe execution,
45
+ take care of logging, parsing command output, temporary files, etc.
46
+
47
+ A recipe could have requirements such as which other recipes it depends
48
+ upon (so must be executed first), which map layers must exist, etc.
49
+
50
+ It could also define what map layers or other files are generated by the
51
+ recipe.
52
+
53
+ Example of intended recipe syntax:
54
+
55
+ ```ruby
56
+ recipe :resamp_average do |options = {}|
57
+ input_raster = options[:input]
58
+ input_res = options[:input_res] # TODO: extract from input_rater info
59
+ output_raster = options[:output]
60
+ output_res = options[:output_res] # TODO: extract from output_raster info
61
+ if options[:direction]
62
+ unless raster_exists?("#{input_raster}_sin")
63
+ g.region res: input_res
64
+ r.mapcalc "#{input_raster}_sin = sin(#{input_raster})"
65
+ end
66
+ unless raster_exists?("#{input_raster}_cos")
67
+ g.region res: input_res
68
+ r.mapcalc "#{input_raster}_cos = cos(#{input_raster})"
69
+ end
70
+ g.region res: output_res
71
+ r.resamp.stats input: "#{input_raster}_cos", output: "#{output_raster}_cos"
72
+ r.resamp.stats input: "#{input_raster}_sin", output: "#{output_raster}_sin"
73
+ r.mapcalc "#{output_raster} = atan(#{output_raster}_cos,#{output_raster}_sin)"
74
+ g.remove rast: ["#{output_raster}_cos", "#{output_raster}_sin"]
75
+ else
76
+ g.region res: output_res
77
+ r.resamp.stats input: input_raster, output: output_raster
78
+ end
79
+ end
80
+
81
+ recipe :generate_working_dem do |...|
82
+ define :working_dem, 'dem'
83
+ define :working_slope, 'slope'
84
+ define :working_aspect, 'aspect'
85
+ g.region res: working_res
86
+ apply :resamp_average, input: base_dem, output: working_dem, input_res: base_dem_res, output_res: working_res
87
+ apply :resamp_average, input: base_slope, output: working_slope, input_res: base_dem_res, output_res: working_res
88
+ apply :resamp_average, input: base_aspect, output: working_aspect, input_res: base_dem_res, output_res: working_res, direction: true
89
+ describe working_slope, "Pendiente en grados a #{working_res} #{region_units}"
90
+ # ...
91
+ end
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it ( https://github.com/[my-github-username]/grassgis/fork )
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ require 'rdoc/task'
11
+ Rake::RDocTask.new do |rdoc|
12
+ version = GrassGis::VERSION
13
+
14
+ rdoc.rdoc_dir = 'rdoc'
15
+ rdoc.title = "GrassGis #{version}"
16
+ rdoc.main = "README.md"
17
+ rdoc.rdoc_files.include('README*')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ rdoc.markup = 'markdown' if rdoc.respond_to?(:markup)
20
+ end
21
+
22
+ task :default => :test
data/grassgis.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grassgis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grassgis"
8
+ spec.version = GrassGis::VERSION
9
+ spec.authors = ["Javier Goizueta"]
10
+ spec.email = ["jgoizueta@gmail.com"]
11
+ spec.summary = %q{Support for scripting GRASS GIS in Ruby}
12
+ spec.description = %q{Support for scripting GRASS GIS in Ruby.}
13
+ spec.homepage = "https://github.com/jgoizueta/grassgis"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sys_cmd", "~> 1.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.9"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest", "~> 5.4"
26
+
27
+ spec.required_ruby_version = '>= 1.9.3'
28
+ end
data/lib/grassgis.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'sys_cmd'
2
+ require 'grassgis/version'
3
+ require 'grassgis/module'
4
+ require 'grassgis/context'
@@ -0,0 +1,138 @@
1
+ require 'tempfile'
2
+
3
+ module GrassGis
4
+
5
+ class Context
6
+ ROOT_MODULES = %w(d g i r v s m p)
7
+ REQUIRED_CONFIG = [:gisbase, :location]
8
+
9
+ def initialize(config)
10
+ # apply configuration defaults
11
+ config[:gisdbase] ||= File.join(ENV['HOME'], 'grassdata')
12
+ config[:mapset] ||= ENV['USER']
13
+ config[:version] ||= File.read(File.join(config[:gisbase], 'etc', 'VERSIONNUMBER')).split.first
14
+ config[:message_format] ||= 'plain'
15
+ config[:true_color] = true unless config.has_key?(:true_color)
16
+ config[:transparent] = true unless config.has_key?(:transparent)
17
+ config[:png_auto_write] = true unless config.has_key?(:png_auto_write)
18
+ config[:gnuplot] ||= 'gnuplot -persist'
19
+ @config = config
20
+
21
+ locals = config[:locals] || {}
22
+ locals.each do |var_name, value|
23
+ define_singleton_method(var_name){ value }
24
+ end
25
+ end
26
+
27
+ def allocate
28
+ @gisrc = Tempfile.new('gisrc')
29
+ @gisrc.puts "LOCATION_NAME: #{@config[:location]}"
30
+ @gisrc.puts "GISDBASE: #{@config[:gisdbase]}"
31
+ @gisrc.puts "MAPSET: #{@config[:mapset]}"
32
+ @gisrc.close
33
+ ENV['GISRC'] = @gisrc.path
34
+ ENV['GISBASE'] = @config[:gisbase]
35
+ ENV['GRASS_VERSION'] = @config[:version]
36
+ ENV['GRASS_MESSAGE_FORMAT'] = @config[:message_format].to_s
37
+ ENV['GRASS_TRUECOLOR'] = bool_var(@config[:true_color])
38
+ ENV['GRASS_TRANSPARENT'] = bool_var(@config[:transparent])
39
+ ENV['GRASS_PNG_AUTO_WRITE'] = bool_var(@config[:png_auto_write])
40
+ ENV['GRASS_GNUPLOT'] = @config[:gnuplot]
41
+ @path = ENV['PATH']
42
+ paths = []
43
+ paths << File.join(@config[:gisbase], 'bin')
44
+ paths << File.join(@config[:gisbase], 'scripts')
45
+ paths << @path
46
+ ENV['PATH'] = paths.join(File::PATH_SEPARATOR)
47
+ @ld_path = ENV['LD_LIBRARY_PATH']
48
+ ld_path = File.join(@config[:gisbase], 'lib')
49
+ if @ld_path
50
+ ENV['LD_LIBRARY_PATH'] = [ld_path, @ld_path].join(File::PATH_SEPARATOR)
51
+ else
52
+ ENV['LD_LIBRARY_PATH'] = ld_path
53
+ end
54
+ ENV['GRASS_LD_LIBRARY_PATH'] = ENV['LD_LIBRARY_PATH']
55
+ @man_path = ENV['MANPATH']
56
+ man_path = File.join(@config[:gisbase], 'man')
57
+ if @man_path
58
+ ENV['MANPATH'] = [man_path, @man_path].join(File::PATH_SEPARATOR)
59
+ else
60
+ ENV['MANPATH'] = man_path
61
+ end
62
+ end
63
+
64
+ def dispose
65
+ @gisrc.unlink if @gisrc
66
+ @gisrc = nil
67
+ ENV['PATH'] = @path if @path
68
+ @path = nil
69
+ ENV['LD_LIBRARY_PATH'] = @ld_path
70
+ ENV['MANPATH'] = @man_path
71
+ end
72
+
73
+ # setup access to the root modules in the context
74
+ ROOT_MODULES.each do |root_module|
75
+ define_method root_module.to_sym do
76
+ var_name = "@#{root_module}"
77
+ m = instance_variable_get(var_name)
78
+ m ||= Module.new(root_module, configuration: @config)
79
+ instance_variable_set var_name, m
80
+ m
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def bool_var(value)
87
+ value ? 'TRUE' : 'FALSE'
88
+ end
89
+
90
+ end
91
+
92
+ # Evaluate a block in a GRASS session environment
93
+ # The configuration must include at leaast:
94
+ #
95
+ # * :gibase The base GRASS instalation directory
96
+ # * :location The location to work with
97
+ #
98
+ # Optional parameters:
99
+ #
100
+ # * :gisdbase The base GRASS data directory
101
+ # * :mapset The default mapset
102
+ # * :version The GRASS version
103
+ #
104
+ # Example:
105
+ #
106
+ # configuration = {
107
+ # gisbase: '/usr/local/Cellar/grass-70/7.0.0/grass-7.0.0',
108
+ # location: 'world'
109
+ # }
110
+ #
111
+ # GrassGis.session configuration do
112
+ # r.resamp.stats '-n', input: "map1@mapset1", output: "map2"
113
+ # cmd = g.list 'vect'
114
+ # puts cmd.output
115
+ # end
116
+ #
117
+ # Note that block is evaluated in a spacial context, so
118
+ # that the lexical scope's self and instance variables
119
+ # are not directly available inside it.
120
+ # Local variables in the scope can be used to access self-related
121
+ # information. Also, local values can be injected in the block
122
+ # with the +:locals+ option:
123
+ #
124
+ # this = self # make self available a local variable
125
+ # locals = { context: self } # inject locals
126
+ # GrassGis.session configuration.merge(locals:) do
127
+ # r.resamp.stats '-n', input: this.input, output: context.output
128
+ # end
129
+ #
130
+ def self.session(config, &blk)
131
+ context = Context.new(config)
132
+ context.allocate
133
+ context.instance_eval(&blk)
134
+ ensure
135
+ context.dispose
136
+ end
137
+
138
+ end
@@ -0,0 +1,70 @@
1
+ module GrassGis
2
+
3
+ # Generate and execute GRASS commands
4
+ #
5
+ # r = GrassGis::Module.new('r')
6
+ # r.resamp.stats '-n', input: "map1@mapset1", output: "map2"
7
+ #
8
+ # To execute a command without arguments, +run+ must be invoked explicitly:
9
+ #
10
+ # g = GrassGis::Module.new('g')
11
+ # g.region.run
12
+ #
13
+ class Module
14
+ def initialize(id, options = {})
15
+ @id = id.to_s
16
+ @parent = options[:parent]
17
+ @configuration = options[:configuration] || {}
18
+ end
19
+
20
+ def name
21
+ if @parent
22
+ "#{@parent.name}.#{@id}"
23
+ else
24
+ @id
25
+ end
26
+ end
27
+
28
+ # Executes the command (with given arguments)
29
+ # returns a SysCmd object (with status, status_value, output, error_output methods)
30
+ def run(*args)
31
+ stdin = nil
32
+ cmd = SysCmd.command name do
33
+ args.each do |arg|
34
+ case arg
35
+ when Hash
36
+ arg.each do |key, value|
37
+ case value
38
+ when Array
39
+ value = value*","
40
+ when String
41
+ if value.include?("\n")
42
+ raise "Cannot pass multiple options through STDIN" if stdin
43
+ stdin = value
44
+ value = "-"
45
+ input stdin
46
+ end
47
+ end
48
+ option key.to_s, equal_value: value
49
+ end
50
+ else
51
+ option arg
52
+ end
53
+ end
54
+ end
55
+ cmd.run unless @configuration[:dry]
56
+ cmd
57
+ end
58
+
59
+ def method_missing(method, *args)
60
+ m = Module.new(method, parent: self, configuration: @configuration)
61
+ if args.size > 0
62
+ m.run *args
63
+ else
64
+ m
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,3 @@
1
+ module GrassGis
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grassgis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Javier Goizueta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sys_cmd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.4'
69
+ description: Support for scripting GRASS GIS in Ruby.
70
+ email:
71
+ - jgoizueta@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - grassgis.gemspec
83
+ - lib/grassgis.rb
84
+ - lib/grassgis/context.rb
85
+ - lib/grassgis/module.rb
86
+ - lib/grassgis/version.rb
87
+ homepage: https://github.com/jgoizueta/grassgis
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 1.9.3
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Support for scripting GRASS GIS in Ruby
111
+ test_files: []