mapfish 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mapfish.rb ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,179 @@
1
+ #
2
+ # Copyright (C) 2008 Pirmin Kalberer, Sourcepole AG
3
+ #
4
+ # This file is part of MapFish Server
5
+ #
6
+ # MapFish Server is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # MapFish Server is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with MapFish Server. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ module MapfishCoreExtensions
21
+ module ActiveRecord
22
+ module Base
23
+
24
+ def to_geojson(options = {})
25
+ only = options.delete(:only)
26
+ geoson = { :type => 'Feature' }
27
+ geoson[:properties] = attributes.delete_if do |name, value|
28
+ if value.is_a?(Geometry) then
29
+ geoson[:geometry] = value if name == self.class.geometry_column.name
30
+ true
31
+ elsif name == self.class.primary_key then
32
+ geoson[:id] = value
33
+ true
34
+ elsif only
35
+ !only.include?(name.to_sym)
36
+ end
37
+ end
38
+ geoson.to_json
39
+ end
40
+
41
+ def update_attributes_from_feature(feature)
42
+ attr = feature.properties
43
+ attr[self.class.geometry_column.name] = feature.geometry
44
+ update_attributes(attr)
45
+ end
46
+
47
+ def self.included(base)
48
+ base.extend(ClassMethods)
49
+ end
50
+
51
+ module ClassMethods
52
+
53
+ def geometry_column
54
+ #Returns first geometry column found. TODO: override by configuration (set_geometry_column)?
55
+ @geometry_column ||= columns.detect {|col| col.respond_to?(:geometry_type) }
56
+ end
57
+
58
+ def params_from_geojson(geoson)
59
+ name = self.to_s.downcase #unless geoson.is_a?(Array)
60
+ data = geoson.delete(:properties) || {}
61
+ data[geometry_column.name] = Geometry.from_geojson(geoson.delete(:geometry), geometry_column.srid)
62
+ geoson.delete(:type)
63
+ {name => data, primary_key => geoson.delete(:id)}
64
+ end
65
+
66
+ def mapfish_filter(params)
67
+ filter = scoped
68
+
69
+ #Create geometry filter
70
+ srid = geometry_column.srid
71
+ pnt = nil
72
+ pnt = Point.from_x_y(params['lon'].to_f, params['lat'].to_f, srid) if params['lon'] && params['lat']
73
+ box = nil
74
+ if pnt && params['tolerance']
75
+ rad = params['tolerance'].to_f/2
76
+ box = [[pnt.x+rad, pnt.y+rad], [pnt.x-rad, pnt.y-rad], srid]
77
+ end
78
+ if params['bbox']
79
+ x1, y1, x2, y2 = params['bbox'].split(',').collect(&:to_f)
80
+ box = [[x1, y1], [x2, y2], srid]
81
+ end
82
+ if box
83
+ box3d = "'BOX3D(#{box[0].join(" ")},#{box[1].join(" ")})'::box3d"
84
+ filter = filter.where("#{table_name}.#{connection.quote_column_name(geometry_column.name)} && SetSRID(#{box3d}, #{box[2] || DEFAULT_SRID} ) ")
85
+ end
86
+
87
+ #Add attribute filter
88
+ params.each do |key, value|
89
+ next if value.nil? || !value.respond_to?(:empty?) || value.empty?
90
+ field, op = key.split('__')
91
+ col = "#{table_name}.#{connection.quote_column_name(field)}"
92
+ case op
93
+ when 'eq'
94
+ filter = filter.where("#{col} = ?", value)
95
+ when 'ne'
96
+ filter = filter.where("#{col} <> ?", value)
97
+ when 'lt'
98
+ filter = filter.where("#{col} < ?", value)
99
+ when 'lte'
100
+ filter = filter.where("#{col} <= ?", value)
101
+ when 'gt'
102
+ filter = filter.where("#{col} > ?", value)
103
+ when 'gte'
104
+ filter = filter.where("#{col} >= ?", value)
105
+ when 'ilike'
106
+ #TODO: support wildcarded ILIKE: https://trac.mapfish.org/trac/mapfish/ticket/495
107
+ filter = filter.where("#{col} ILIKE ?", value)
108
+ end
109
+ end
110
+
111
+ filter
112
+ end
113
+
114
+ #Rails 2 backwards compatible finder
115
+ def find_by_mapfish_filter(params, args = {})
116
+ filter = {}
117
+ #Create geometry filter
118
+ srid = geometry_column.srid
119
+ pnt = nil
120
+ pnt = Point.from_x_y(params['lon'].to_f, params['lat'].to_f, srid) if params['lon'] && params['lat']
121
+ box = nil
122
+ if pnt && params['tolerance']
123
+ rad = params['tolerance'].to_f/2
124
+ box = [[pnt.x+rad, pnt.y+rad], [pnt.x-rad, pnt.y-rad], srid]
125
+ end
126
+ if params['bbox']
127
+ x1, y1, x2, y2 = params['bbox'].split(',').collect(&:to_f)
128
+ box = [[x1, y1], [x2, y2], srid]
129
+ end
130
+ if box
131
+ box3d = "'BOX3D(#{box[0].join(" ")},#{box[1].join(" ")})'::box3d"
132
+ filter = "#{table_name}.#{connection.quote_column_name(geometry_column.name)} && SetSRID(#{box3d}, #{box[2] || DEFAULT_SRID} ) "
133
+ end
134
+ conditions = sanitize_sql_for_conditions(filter)
135
+
136
+ #Add attribute filter
137
+ attrfilter = []
138
+ params.each do |key, value|
139
+ next if value.nil? || !value.respond_to?(:empty?) || value.empty?
140
+ field, op = key.split('__')
141
+ case op
142
+ when 'eq'
143
+ attrfilter << ["#{field} = ?", value]
144
+ when 'ne'
145
+ attrfilter << ["#{field} <> ?", value]
146
+ when 'lt'
147
+ attrfilter << ["#{field} < ?", value]
148
+ when 'lte'
149
+ attrfilter << ["#{field} <= ?", value]
150
+ when 'gt'
151
+ attrfilter << ["#{field} > ?", value]
152
+ when 'gte'
153
+ attrfilter << ["#{field} >= ?", value]
154
+ when 'ilike'
155
+ #TODO: support wildcarded ILIKE: https://trac.mapfish.org/trac/mapfish/ticket/495
156
+ attrfilter << ["#{field} ILIKE ?", value]
157
+ end
158
+ end
159
+ unless attrfilter.empty?
160
+ sql, sqlparams = attrfilter.transpose
161
+ if conditions.nil?
162
+ conditions = ''
163
+ else
164
+ conditions << " AND "
165
+ end
166
+ conditions << sanitize_sql_for_conditions([sql.join(' AND ')] + sqlparams)
167
+ end
168
+
169
+ #Create finder arguments
170
+ args.merge!(:conditions => conditions)
171
+ args.merge!({:limit => params['maxfeatures'] }) if params['maxfeatures']
172
+ args.merge!({:limit => params['limit'] }) if params['limit']
173
+ args.merge!(:offset => params['offset']) if params['offset']
174
+ find(:all, args)
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,31 @@
1
+ #
2
+ # Copyright (C) 2008 Pirmin Kalberer, Sourcepole AG
3
+ #
4
+ # This file is part of MapFish Server
5
+ #
6
+ # MapFish Server is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # MapFish Server is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with MapFish Server. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ module MapfishCoreExtensions
21
+ module Array
22
+
23
+ def to_geojson(options = {})
24
+ geojson = '{"type": "FeatureCollection", "features": ['
25
+ geojson << collect {|e| e.to_geojson(options) }.join(',')
26
+ geojson << ']}'
27
+ geojson
28
+ end
29
+
30
+ end
31
+ end
data/lib/print.rb ADDED
@@ -0,0 +1,133 @@
1
+ #
2
+ # Copyright (C) 2007-2008 Camptocamp
3
+ #
4
+ # This file is part of MapFish Server
5
+ #
6
+ # MapFish Server is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # MapFish Server is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with MapFish Server. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'popen4'
21
+ require 'tmpdir'
22
+
23
+ Mime::Type.register 'application/pdf', :pdf
24
+
25
+ module MapFish
26
+ module Print
27
+ class JavaError < Exception
28
+ def initialize(cmd, message)
29
+ super(cmd+"\n"+message)
30
+ end
31
+ end
32
+
33
+ # Used to add the needed routes for the print module
34
+ def self.add_routes(map, controller = 'print', base = 'print')
35
+ map.connect(base+'/info.:format', :controller=>controller, :action=>'info', :method=>:get)
36
+ map.connect(base+'/create.:format', :controller=>controller, :action=>'create', :method=>:post)
37
+ map.connect(base+'/:id.:format', :controller=>controller, :action=>'show', :method=>:get)
38
+ end
39
+
40
+ # To have a controller for the print module, just mix-in this module to
41
+ # one of your controllers. This controller must have an attribute @configFile.
42
+ # For example:
43
+ # class PrintController < ApplicationController
44
+ # include MapFish::Print::Controller
45
+ #
46
+ # def initialize()
47
+ # @configFile = "/home/toto/rails/Test/vendor/java/print/print-standalone/samples/config.yaml"
48
+ # end
49
+ # end
50
+ module Controller
51
+ TEMP_PREFIX = Dir::tmpdir+"/mfPrintTempFile"
52
+ TEMP_SUFFIX = ".pdf"
53
+ TEMP_PURGE_SECONDS = 600
54
+
55
+ def info
56
+ cmd = baseCmd() + " --clientConfig"
57
+ result = ""
58
+ errors = ""
59
+ status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
60
+
61
+ result = stdout.readlines().join("\n")
62
+ errors = stderr.readlines().join("\n")
63
+ end
64
+ if status.nil? || status.exitstatus != 0
65
+ raise JavaError.new(cmd, errors)
66
+ else
67
+ info = ActiveSupport::JSON.decode(result)
68
+ info['createURL']=url_for(:action=>'create')+'.json'
69
+ respond_to do |format|
70
+ format.json do
71
+ if params[:var]
72
+ render :text=>"var "+params[:var]+"="+result+";"
73
+ else
74
+ render :json=>info
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def create
82
+ cleanupTempFiles
83
+
84
+ tempId = rand(2**31) #TODO: not secure enough
85
+ temp = TEMP_PREFIX + tempId.to_s + TEMP_SUFFIX
86
+ cmd = baseCmd() + " --output=" + temp
87
+ result = ""
88
+ errors = ""
89
+ status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
90
+ body = request.body
91
+ FileUtils.copy_stream(body, stdin)
92
+ body.close()
93
+ stdin.close()
94
+ result = stdout.readlines().join("\n")
95
+ errors = stderr.readlines().join("\n")
96
+ end
97
+ if status.nil? || status.exitstatus != 0
98
+ raise JavaError.new(cmd, errors)
99
+ else
100
+ respond_to do |format|
101
+ format.json do
102
+ render :json=>{ 'getURL' => url_for(:action=>'show', :id=>tempId)+".pdf" }
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ def show
109
+ temp = TEMP_PREFIX + params[:id] + TEMP_SUFFIX
110
+ respond_to do |format|
111
+ format.pdf do
112
+ send_file temp, :type=>'application/x-pdf', :disposition=>'attachment', :filename=>params[:id]+'.pdf'
113
+ end
114
+ end
115
+ end
116
+
117
+ protected
118
+
119
+ def baseCmd
120
+ return "java -cp #{File.dirname(__FILE__)}/../print/print-standalone.jar org.mapfish.print.ShellMapPrinter --config=#{@configFile}"
121
+ end
122
+
123
+ def cleanupTempFiles
124
+ minTime = Time.now - TEMP_PURGE_SECONDS;
125
+ Dir.glob(TEMP_PREFIX + "*" + TEMP_SUFFIX).each do |path|
126
+ if File.mtime(path) < minTime
127
+ File.delete(path)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,37 @@
1
+ namespace :mapfish do
2
+
3
+ include FileUtils
4
+
5
+ desc "Install MapFish client"
6
+ task :install_client do
7
+ version = ENV['VERSION'] || 'trunk' # e.g. 'branches/1.2'
8
+ rm_rf("public/build")
9
+ rm_rf("public/mfbase/")
10
+ system("svn export http://www.mapfish.org/svn/mapfish/framework/client/#{version}/mfbase public/mfbase")
11
+ system("svn export http://www.mapfish.org/svn/mapfish/framework/client/#{version}/build public/build")
12
+ end
13
+
14
+ desc "Build MapFish release script"
15
+ task :build_scripts do
16
+ puts 'Building MapFish..'
17
+ puts <<EOS
18
+ If you get error: Could not find suitable distribution for Requirement.parse('JSTools'), install it manually
19
+ source public/build/venv/bin/activate
20
+ easy_install 'http://github.com/whitmo/jstools/tarball/master'
21
+ and call rake mapfish:build_scripts again
22
+ EOS
23
+ system("sh public/build/build.sh")
24
+ end
25
+
26
+ desc "Copy scripts to public/javascripts"
27
+ task :copy_scripts do
28
+ system("cp -r public/mfbase/release/* public/javascripts")
29
+ rm_rf("public/javascripts/ext")
30
+ mkdir_p("public/javascripts/ext/adapter/ext")
31
+ mkdir_p("public/javascripts/ext/resources")
32
+ system("cp public/mfbase/ext/ext-all.js public/javascripts/ext")
33
+ system("cp public/mfbase/ext/adapter/ext/ext-base.js public/javascripts/ext/adapter/ext")
34
+ system("cp -r public/mfbase/ext/resources/* public/javascripts/ext/resources")
35
+ end
36
+
37
+ end
data/mapfish.gemspec ADDED
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mapfish}
8
+ s.version = "1.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pirmin Kalberer"]
12
+ s.date = %q{2010-06-18}
13
+ s.description = %q{MapFish is a flexible and complete framework for building rich web-mapping applications. Homepage: mapfish.org}
14
+ s.email = %q{pka@sourcepole.ch}
15
+ s.files = [
16
+ "COPYING.LESSER",
17
+ "README",
18
+ "Rakefile",
19
+ "VERSION",
20
+ "generators/mapfish_resource/USAGE",
21
+ "generators/mapfish_resource/mapfish_resource_generator.rb",
22
+ "generators/mapfish_resource/templates/controller.rb",
23
+ "generators/mapfish_resource/templates/functional_test.rb",
24
+ "generators/mapfish_resource/templates/helper.rb",
25
+ "generators/print_controller/USAGE",
26
+ "generators/print_controller/print_controller_generator.rb",
27
+ "generators/print_controller/templates/config.yaml",
28
+ "generators/print_controller/templates/controller.rb",
29
+ "generators/print_controller/templates/functional_test.rb",
30
+ "generators/print_controller/templates/helper.rb",
31
+ "generators/print_controller/templates/helper_test.rb",
32
+ "init.rb",
33
+ "install.rb",
34
+ "lib/geojson.rb",
35
+ "lib/mapfish.rb",
36
+ "lib/mapfish_core_extensions/active_record/base.rb",
37
+ "lib/mapfish_core_extensions/array.rb",
38
+ "lib/print.rb",
39
+ "lib/tasks/mapfish_tasks.rake",
40
+ "mapfish.gemspec",
41
+ "print/print-standalone.jar",
42
+ "test/geojson_test.rb",
43
+ "uninstall.rb"
44
+ ]
45
+ s.homepage = %q{http://mapfish.org/doc/implementations/rails.html}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.6}
49
+ s.summary = %q{Mapfish server plugin for Ruby on Rails}
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ s.add_runtime_dependency(%q<spatial_adapter>, [">= 0"])
57
+ s.add_runtime_dependency(%q<POpen4>, [">= 0.1.5"])
58
+ else
59
+ s.add_dependency(%q<spatial_adapter>, [">= 0"])
60
+ s.add_dependency(%q<POpen4>, [">= 0.1.5"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<spatial_adapter>, [">= 0"])
64
+ s.add_dependency(%q<POpen4>, [">= 0.1.5"])
65
+ end
66
+ end
67
+