gb_mapfish_appserver 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjcyMGM2NTljZDgyZjdhZmQ3YzA4MzNiYzFmMGFmM2IwZWY0ZmUzYg==
4
+ ZmFkMTY4MDk4Njg0YTdmNmJlYTU3MjU5NmZkMjI4Mzg5MWI2ZmFiYw==
5
5
  data.tar.gz: !binary |-
6
- ODFhZGY1NThiOGQ1ODkxMzRhY2U1MDQzZTcyNjUyYjAwYWU5NGFmZA==
6
+ MDczMjlkNmFiY2VjNjkwNzY3OWViNDNhMTUwMTE2MzI1NzgzZDg1Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OThjMDNmNWIwM2FkODMwN2Y0OTQ0NjZiYjFjN2ViZjRmOTZhM2Q2MGNlOTlk
10
- M2RjNjliNWYxZDgyYjRjMTRjOTM5NzU1YWMxNDg4YzZhYjg5ZGJhMDFjNWQ5
11
- YzZmYzNlMGNmYTI5M2NkNDMyYWY1ZjY1NzczOWI1MDBhNTgzYWY=
9
+ ZmRhNmNhNGM1YTJjOGI5YmU3MTZkNjg3ZDRjZmRjZmNjMjA5ZGRlYTk5ODIx
10
+ NDBiMmZlMDllYWFmY2Y1MGE0OWE1YjNhY2E2MjQxNDVmZDFlOTgzNTM5YTA1
11
+ MzE2MTBmOTAwYWU0YjA4ZDRjN2FiYmQ5NDMzYzEyZmZhNDA4ZjI=
12
12
  data.tar.gz: !binary |-
13
- MTFjMzlhMzM3MmVlNWQ5NWE5MzFjY2E5OWQ1NmU0NjdiNmIxN2I0MWIyNzkx
14
- NzYwMzQ3MjFjZGMzZDhjNjg5NjJmNTVjOWMyMGE0MDFlYjliMTk4NWE1Yzli
15
- MmY1YjAyNmIyNTllNmU0YjY1ODc5MzhlN2Y5ZTAwNjgzOGU3ZDM=
13
+ YTgyYTFmMWNhYWNmMjYyMDkxNDBmNjNkNDg0ZDMzYzE5MDI5ODM5ZDk0ZTZh
14
+ NjcwYThiNmZlYTM3ZmJhNTcyMzk2ZWFiZjQ3YWU0MzEwOWY4ODZlZTQxYjY0
15
+ MjY5YmZiZDMxZGYxMjc0NDAyMGRiZjQ4Y2M5MTBhMDE1NTJkN2M=
data/Gemfile CHANGED
@@ -14,5 +14,7 @@ gemspec
14
14
  # gem 'ruby-debug'
15
15
 
16
16
  gem 'therubyracer', :platforms => :ruby
17
- gem 'gb_mapfish_print'
18
17
  gem 'ruby_mapscript', :platforms => :ruby
18
+
19
+ # for Mapfish Print standalone
20
+ # gem 'popen4'
data/README.md CHANGED
@@ -31,12 +31,15 @@ Documentation
31
31
 
32
32
  For more documentation see the [mapfish-appserver.github.io](http://mapfish-appserver.github.io/)
33
33
 
34
+ Note: Mapfish Appserver v1.1.0 or later no longer uses the gb_mapfish_print gem, but uses a
35
+ separately installed Mapfish Print v3 for printing (see documentation)
36
+
34
37
  Authors and License
35
38
  -------------------
36
39
 
37
- Primary authors are Pirmin Kalberer and Matthias Walker, Sourcepole,
40
+ Primary authors are Pirmin Kalberer and Mathias Walker, Sourcepole,
38
41
  Stefan Zinggeler and Adrian Herzog, Canton of Zurich.
39
42
 
40
43
  New BSD License
41
44
 
42
- *Copyright (c) 2009-2013 Sourcepole AG & Canton of Zurich*
45
+ *Copyright (c) 2009-2015 Sourcepole AG & Canton of Zurich*
@@ -1,7 +1,6 @@
1
1
  #Mapfish print controller with access control and servlet call
2
2
 
3
3
  class PrintController < ApplicationController
4
- require 'popen4'
5
4
  begin
6
5
  require 'RMagick'
7
6
  rescue LoadError
@@ -15,12 +14,18 @@ class PrintController < ApplicationController
15
14
 
16
15
  class JavaError < Exception
17
16
  def initialize(cmd, message)
18
- super(cmd+"\n"+message)
17
+ super(cmd + "\n\n" + message)
18
+ end
19
+ end
20
+
21
+ class MapfishError < Exception
22
+ def initialize(cmd, message)
23
+ super(cmd + "\n\n" + message)
19
24
  end
20
25
  end
21
26
 
22
27
  def initialize
23
- @configFile = "#{Rails.root}/config/print.yml"
28
+ @configFile = "#{Rails.root}/print/config.yaml"
24
29
  end
25
30
 
26
31
  TMP_PREFIX = "#{PRINT_TMP_PATH}/mfPrintTempFile"
@@ -28,44 +33,77 @@ class PrintController < ApplicationController
28
33
  TMP_PURGE_SECONDS = 600
29
34
 
30
35
  OUTPUT_FORMATS = ["pdf", "png", "jpg", "tif", "gif"]
36
+ MAPFISH_PRINT_OUTPUT_FORMATS = ["pdf", "png", "tif", "gif", "bmp"]
31
37
 
32
38
  def info
33
- #if PRINT_URL.present?
34
- #TODO: call_servlet(request)
35
- cmd = baseCmd + " --clientConfig"
36
- result = ""
37
- errors = ""
38
- status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
39
+ # return v2 print info format for GbPrintPanel
40
+ info = {}
41
+ info['createURL'] = url_for(:protocol => request.protocol, :action=>'create') + '.json'
42
+
43
+ # add output formats
44
+ info['outputFormats'] = []
45
+ OUTPUT_FORMATS.each do |output_format|
46
+ info['outputFormats'] << {:name => output_format}
47
+ end
39
48
 
40
- result = stdout.readlines.join("\n")
41
- errors = stderr.readlines.join("\n")
49
+ # add scales
50
+ info['scales'] = print_scales.collect do |scale|
51
+ {:name => "1:#{scale}", :value => scale}
42
52
  end
43
- if status.nil? || status.exitstatus != 0
44
- raise JavaError.new(cmd, errors)
45
- else
46
- info = ActiveSupport::JSON.decode(result)
47
- info['createURL'] = url_for(:protocol => request.protocol, :action=>'create') + '.json'
48
- # add output formats
49
- info['outputFormats'] = []
50
- OUTPUT_FORMATS.each do |output_format|
51
- info['outputFormats'] << {:name => output_format}
52
- end
53
53
 
54
- respond_to do |format|
55
- format.json do
56
- if params[:var]
57
- render :text=>"var "+params[:var]+"="+result+";"
58
- else
59
- render :json=>info
60
- end
54
+ # add dpis
55
+ info['dpis'] = print_dpis.collect do |dpi|
56
+ {:name => "#{dpi}", :value => dpi}
57
+ end
58
+
59
+ # NOTE: load templates directly from YAML instead of parsing the Mapfish Print capabilities
60
+ mapfish_config = YAML.load(File.read(@configFile))
61
+
62
+ # parse layouts
63
+ info['layouts'] = []
64
+ mapfish_config['templates'].each do |name, template|
65
+ # skip custom templates
66
+ next if template['attributes'].has_key?('gb_custom_template')
67
+
68
+ map = template['attributes']['map']
69
+ # skip if no map
70
+ next if map.nil?
71
+
72
+ info['layouts'] << {
73
+ :name => name,
74
+ :map => {
75
+ :width => map['width'],
76
+ :height => map['height'],
77
+ },
78
+ :rotation => true
79
+ }
80
+ end
81
+
82
+ respond_to do |format|
83
+ format.json do
84
+ if params[:var]
85
+ render :text => "var #{params[:var]} = #{info.to_json};"
86
+ else
87
+ render :json => info
61
88
  end
62
89
  end
63
90
  end
64
91
  end
65
92
 
66
93
  def create
94
+ unless check_permissions
95
+ head :forbidden
96
+ return
97
+ end
98
+
67
99
  cleanupTempFiles
68
100
 
101
+ # remove Rails params
102
+ controller = request.parameters.delete('controller')
103
+ request.parameters.delete('action')
104
+ request.parameters.delete('format')
105
+ request.parameters.delete(controller) unless controller.nil?
106
+
69
107
  accessible_topics = Topic.accessible_by(current_ability).collect{ |topic| topic.name }
70
108
  layers_to_delete = []
71
109
  request.parameters["layers"].each do |layer|
@@ -105,7 +143,6 @@ class PrintController < ApplicationController
105
143
  # remove inaccessible layers
106
144
  request.parameters["layers"] -= layers_to_delete
107
145
 
108
- scales = []
109
146
  request.parameters["pages"].each do |page|
110
147
  # round center coordinates
111
148
  page["center"].collect! {|coord| (coord * 100.0).round / 100.0 }
@@ -119,55 +156,38 @@ class PrintController < ApplicationController
119
156
  # disclaimer
120
157
  topic = Topic.accessible_by(current_ability).where(:name => page["topic"]).first
121
158
  page["disclaimer"] = topic.nil? ? Topic.default_print_disclaimer : topic.print_disclaimer
122
- # scale
123
- scales << page["scale"]
124
159
  end
125
160
 
126
- outputFormat = request.parameters["outputFormat"]
127
- request.parameters["outputFormat"] = 'pdf'
161
+ report = request.parameters["layout"]
162
+ if print_templates.include?(report)
163
+ # Mapfish
164
+ print_params = convert_mapfish_v2_params(request.parameters)
165
+ output_format = print_params["outputFormat"]
128
166
 
129
- logger.info request.parameters.to_yaml
167
+ # add any custom params
168
+ set_custom_print_params(report, print_params)
130
169
 
131
- if request.parameters["report"]
132
- # JasperReport
133
- call_report(request)
134
- elsif PRINT_URL.present?
135
- # MapFish
136
- # FIXME: add custom scales to config file
137
- call_servlet(request)
138
- else
139
- # MapFish
140
- #print-standalone
141
- tempId = SecureRandom.random_number(2**31)
142
-
143
- # use temp config file with added custom scales
144
- print_config = File.read(@configFile)
145
- print_config.gsub!(/scales:/, "scales:\n#{ scales.collect {|s| " - #{s.to_s}"}.join("\n") }")
146
- config_file = TMP_PREFIX + tempId.to_s + "print.yml"
147
- File.open(config_file, "w") { |file| file << print_config }
148
-
149
- temp = TMP_PREFIX + tempId.to_s + TMP_SUFFIX
150
- cmd = baseCmd(config_file) + " --output=" + temp
151
- result = ""
152
- errors = ""
153
- status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
154
- stdin.puts request.parameters.to_json
155
- #body = request.body
156
- #FileUtils.copy_stream(body, stdin)
157
- #body.close
158
- stdin.close
159
- result = stdout.readlines.join("\n")
160
- errors = stderr.readlines.join("\n")
161
- end
162
- if status.nil? || status.exitstatus != 0
163
- raise JavaError.new(cmd, errors)
164
- else
165
- convert_and_send_link(temp, tempId, request.parameters["dpi"], outputFormat)
170
+ # create report
171
+ temp, temp_id = mapfish_print(print_params)
172
+
173
+ # send link to print result
174
+ respond_to do |format|
175
+ format.json do
176
+ render :json => { 'getURL' => url_for(:action => 'show', :id => temp_id) + ".#{output_format}" }
177
+ end
166
178
  end
179
+ else
180
+ # JasperReport
181
+ call_report(request.parameters["report"], request)
167
182
  end
168
183
  end
169
184
 
170
185
  def show
186
+ unless check_permissions
187
+ head :forbidden
188
+ return
189
+ end
190
+
171
191
  output_format = params[:format]
172
192
  type = nil
173
193
  if OUTPUT_FORMATS.include?(output_format)
@@ -183,24 +203,40 @@ class PrintController < ApplicationController
183
203
  when "gif"
184
204
  type = 'image/gif'
185
205
  end
206
+ else
207
+ # invalid format
208
+ head :bad_request
209
+ return
186
210
  end
187
211
  is_mapfish_print_id = (params[:id] =~ /^[0-9]+$/)
188
212
  if is_mapfish_print_id
213
+ # deliver document generated previously by create()
189
214
  temp = TMP_PREFIX + params[:id] + ".#{output_format}"
190
215
  send_file temp, :type => type, :disposition => 'attachment', :filename => params[:id] + ".#{output_format}"
191
216
  else
192
- params['report'] = params[:id]
193
- result = create_report(request)
194
- if result.nil?
195
- render :nothing => true, :status => 500
196
- return
197
- end
198
-
199
- if result.kind_of? Net::HTTPSuccess
200
- send_data result.body, :type => type, :disposition => 'attachment', :filename => "#{params[:report]}.pdf"
217
+ # create document
218
+ report = params[:id]
219
+ if custom_print_templates.include?(report)
220
+ # Mapfish custom report
221
+
222
+ # minimal Mapfish print params
223
+ print_params = params.reject {|p| ['id', 'controller', 'action', 'format'].include?(p) }
224
+ print_params['layout'] = report
225
+ print_params['outputFormat'] = output_format
226
+ print_params['attributes'] = {}
227
+ print_params['dpi'] = print_params['dpi'] || print_dpis.first
228
+
229
+ # add any custom params
230
+ set_custom_print_params(report, print_params)
231
+
232
+ # create report
233
+ temp, temp_id = mapfish_print(print_params)
234
+
235
+ default_filename = "#{report}.#{output_format}"
236
+ send_custom_report(report, print_params, temp, type, default_filename)
201
237
  else
202
- logger.info "#{result.code}: #{result.body}"
203
- render :nothing => true, :status => result.code
238
+ # JasperReport
239
+ create_and_send_jasper_report(report, request, type, "#{report}.pdf")
204
240
  end
205
241
  end
206
242
  end
@@ -240,6 +276,48 @@ class PrintController < ApplicationController
240
276
 
241
277
  protected
242
278
 
279
+ def print_scales
280
+ if defined? PRINT_SCALES
281
+ # from config
282
+ PRINT_SCALES
283
+ else
284
+ # default
285
+ [500, 1000, 2500, 5000, 10000, 15000, 25000, 50000, 100000, 200000, 500000]
286
+ end
287
+ end
288
+
289
+ def print_dpis
290
+ if defined? PRINT_DPIS
291
+ # from config
292
+ PRINT_DPIS
293
+ else
294
+ # default
295
+ [150, 300]
296
+ end
297
+ end
298
+
299
+ def print_templates
300
+ @print_templates ||= begin
301
+ # get all templates from Mapfish print config
302
+ mapfish_config = YAML.load(File.read(@configFile))
303
+ mapfish_config['templates'].keys
304
+ end
305
+ end
306
+
307
+ def custom_print_templates
308
+ @custom_print_templates ||= begin
309
+ # get custom templates from Mapfish print config
310
+ mapfish_config = YAML.load(File.read(@configFile))
311
+ mapfish_config['templates'].keep_if {|k, v| v['attributes'].has_key?('gb_custom_template') }.keys
312
+ end
313
+ end
314
+
315
+ # check permission for printing, e.g. check params to limit reports to user roles
316
+ # override in descendant classes
317
+ def check_permissions
318
+ true
319
+ end
320
+
243
321
  def rewrite_wms_uri(url, use_cgi)
244
322
  #http://wms.zh.ch/basis -> http://127.0.0.1/cgi-bin/mapserv.fcgi?MAP=/opt/geodata/mapserver/maps/intranet/basis.map&
245
323
  out = url
@@ -255,9 +333,128 @@ class PrintController < ApplicationController
255
333
  out
256
334
  end
257
335
 
336
+ # add custom print params according to report type, e.g. add map and zoom to feature in custom reports
337
+ # override in descendant classes
338
+ def set_custom_print_params(report, print_params)
339
+ =begin example
340
+ case report
341
+ when "TopicName"
342
+ # add map and layer
343
+ layer_url = rewrite_wms_uri("#{wms_host}/TopicName", false)
344
+ print_params['attributes']['map'] = {
345
+ :dpi => print_params[:dpi],
346
+ :bbox => [669242, 223923, 716907, 283315],
347
+ :projection => 'EPSG:21781',
348
+ :layers => [
349
+ {
350
+ :type => 'WMS',
351
+ :baseURL => layer_url,
352
+ :layers => ['Layer1', 'Layer2'],
353
+ :imageFormat => 'image/png; mode=8bit',
354
+ :styles => [''],
355
+ :customParams => {
356
+ :TRANSPARENT => true,
357
+ :map_resolution => print_params['dpi']
358
+ }
359
+ }
360
+ ]
361
+ }
362
+ end
363
+ =end
364
+ end
365
+
366
+ def mapfish_print(print_params)
367
+ output_format = print_params["outputFormat"]
368
+ unless MAPFISH_PRINT_OUTPUT_FORMATS.include?(output_format)
369
+ # convert to raster from pdf
370
+ print_params['outputFormat'] = 'pdf'
371
+ end
372
+
373
+ logger.info "Mapfish Print v3: #{print_params.to_yaml}"
374
+
375
+ temp_id = SecureRandom.random_number(2**31)
376
+ temp_mapfish = "#{TMP_PREFIX}#{temp_id.to_s}.#{print_params['outputFormat']}"
377
+
378
+ if PRINT_URL.present?
379
+ # call Mapfish Print servlet
380
+ data = {
381
+ :spec => print_params.to_json
382
+ }
383
+ response = call_servlet('POST', "buildreport.#{print_params['outputFormat']}", data)
384
+ File.open(temp_mapfish, 'wb') {|f| f.write(response.body) }
385
+ else
386
+ # call Mapfish Print standalone
387
+ print_standalone(print_params, temp_mapfish)
388
+ end
389
+
390
+ temp = temp_mapfish
391
+ unless MAPFISH_PRINT_OUTPUT_FORMATS.include?(output_format)
392
+ # convert PDF to image if not supported by Mapfish Print
393
+ pdf = Magick::Image.read(temp_mapfish) { self.density = print_params['dpi'] }.first
394
+ temp_img = "#{TMP_PREFIX}#{temp_id.to_s}.#{output_format}"
395
+ pdf.write(temp_img)
396
+ File.delete(temp_mapfish)
397
+ temp = temp_img
398
+ end
399
+
400
+ return temp, temp_id
401
+ end
402
+
403
+ def call_servlet(method, action, print_params=nil)
404
+ begin
405
+ url = URI.parse(URI.decode("#{PRINT_URL}/#{action}"))
406
+ logger.info "Forward request: #{method} #{url}"
407
+
408
+ case method
409
+ when 'GET'
410
+ # add params to URL
411
+ url = URI.parse("#{url}?#{print_params.to_param}") unless print_params.nil?
412
+ response = Net::HTTP.get_response(url)
413
+ when 'POST'
414
+ http = Net::HTTP.new(url.host, url.port)
415
+ req = Net::HTTP::Post.new(url.path)
416
+ req.set_form_data(print_params)
417
+ response = http.request(req)
418
+ else
419
+ raise Exception.new("Unsupported method '#{method}'")
420
+ end
421
+ rescue => err
422
+ raise MapfishError.new("#{method} #{url}\n#{print_params.to_json}", "#{err.class}: #{err.message}")
423
+ end
424
+
425
+ if response.code != '200'
426
+ raise MapfishError.new("#{method} #{url}\n#{print_params.to_json}", response.body)
427
+ end
428
+
429
+ response
430
+ end
431
+
258
432
  def baseCmd(config_file = nil)
259
433
  config = config_file || @configFile
260
- "java -cp #{GbMapfishPrint::PRINT_JAR} org.mapfish.print.ShellMapPrinter --config=#{config}"
434
+ "java -cp '#{PRINT_STANDALONE_JARS}' org.mapfish.print.cli.Main -config #{config} -verbose 0"
435
+ end
436
+
437
+ def print_standalone(print_params, temp_mapfish)
438
+ require 'popen4'
439
+
440
+ cmd = "#{baseCmd} -output #{temp_mapfish}"
441
+ #result = ""
442
+ errors = ""
443
+ status = POpen4::popen4(cmd) do |stdout, stderr, stdin, pid|
444
+ stdin.puts print_params.to_json
445
+ stdin.close
446
+ #result = stdout.readlines.join("")
447
+ errors = stderr.readlines.join("")
448
+ end
449
+ if status.nil? || status.exitstatus != 0
450
+ raise JavaError.new(cmd + "\n" + print_params.to_json, errors)
451
+ end
452
+ end
453
+
454
+ # handler for sending custom report file, e.g. to customize filename depending on params
455
+ # override in descendant classes
456
+ def send_custom_report(report, print_params, path, type, filename)
457
+ send_file path, :type => type, :disposition => 'attachment', :filename => filename
261
458
  end
262
459
 
263
460
  def cleanupTempFiles
@@ -271,58 +468,100 @@ class PrintController < ApplicationController
271
468
  end
272
469
  end
273
470
 
274
- def call_servlet(request)
275
- url = URI.parse(URI.decode(PRINT_URL))
276
- logger.info "Forward request: #{PRINT_URL}"
277
- printspec = request.parameters.to_json
471
+ # convert Mapfish Print v2 request params to v3
472
+ # NOTE: v2 can be used directly (with -v2), but does not support multiple maps
473
+ # see also mapfish-print/core/src/main/java/org/mapfish/print/servlet/oldapi/OldAPIRequestConverter.java
474
+ def convert_mapfish_v2_params(print_v2_params)
475
+ print_params = {
476
+ 'attributes' => {}
477
+ }
478
+
479
+ # report
480
+ ['layout', 'outputFormat'].each do |k|
481
+ print_params[k] = print_v2_params[k] if print_v2_params.has_key?(k)
482
+ end
278
483
 
279
- response = nil
280
- begin
281
- http = Net::HTTP.new(url.host, url.port)
282
- http.start do
283
- case request.method.to_s
284
- when 'GET' then response = http.get(url.path) #, request.headers
285
- #when 'POST' then response = http.post(url.path, printspec)
286
- when 'POST' then response = http.get("#{url.path}?spec=#{CGI.escape(printspec)}") #-> GET print.pdf
287
- else
288
- raise Exception.new("unsupported method `#{request.method}'.")
289
- end
484
+ # map
485
+ map = {}
486
+ map['dpi'] = print_v2_params['dpi'] if print_v2_params.has_key?('dpi')
487
+ map['projection'] = print_v2_params['srs'] if print_v2_params.has_key?('srs')
488
+
489
+ page_map_params = ['center', 'scale', 'rotation']
490
+ print_v2_params['pages'].each do |page|
491
+ if page.has_key?('center')
492
+ map['center'] = page['center']
493
+ # add text param for center
494
+ print_params['attributes']['center_text'] = page['center']
495
+ end
496
+ if page.has_key?('scale')
497
+ map['scale'] = page['scale']
498
+ # add text param for scale
499
+ print_params['attributes']['scale_text'] = page['scale']
500
+ end
501
+ map['rotation'] = -page['rotation'] if page.has_key?('rotation')
502
+
503
+ # custom params from page
504
+ page.each do |k, v|
505
+ print_params['attributes'][k] = v unless page_map_params.include?(k)
290
506
  end
291
- rescue => err
292
- logger.info("#{err.class}: #{err.message}")
293
- render :nothing => true, :status => 500
294
- return
295
507
  end
296
- #send_data response.body, :status => response.code, :type=>'application/x-pdf', :disposition=>'attachment', :filename=>'map.pdf'
297
- tempId = SecureRandom.random_number(2**31)
298
- temp = TMP_PREFIX + tempId.to_s + TMP_SUFFIX
299
- File.open(temp, 'wb') {|f| f.write(response.body) }
300
- convert_and_send_link(temp, tempId, request.parameters["dpi"], request.parameters["outputFormat"])
301
- end
302
508
 
303
- # optionally convert PDF to image and send link to print result
304
- def convert_and_send_link(temp_pdf, temp_id, dpi, output_format)
305
- temp_suffix = ".pdf"
509
+ # layers
510
+ map['layers'] = []
511
+ if print_v2_params.has_key?('layers')
512
+ print_v2_params['layers'].reverse.each do |v2_layer|
513
+ layer = {}
514
+
515
+ case v2_layer['type'].downcase
516
+ when "wms"
517
+ # WMS
518
+ layer['type'] = 'wms'
519
+ ['customParams', 'imageFormat', 'name', 'layers', 'opacity', 'mergeableParams', 'baseURL', 'styles', 'rasterStyle', 'failOnError', 'useNativeAngle', 'serverType', 'version'].each do |k|
520
+ layer[k] = v2_layer[k] if v2_layer.has_key?(k)
521
+ end
522
+ layer['imageFormat'] = v2_layer['format'] if v2_layer.has_key?('format')
523
+ when "vector"
524
+ # vector
525
+ layer['type'] = 'geojson'
526
+ ['style', 'name', 'opacity', 'renderAsSvg', 'failOnError', 'geoJson'].each do |k|
527
+ layer[k] = v2_layer[k] if v2_layer.has_key?(k)
528
+ end
529
+ if v2_layer.has_key?('styles')
530
+ layer['style'] = v2_layer['styles']
531
+ layer['style'].each do |k, v|
532
+ if v.has_key?('label')
533
+ # set default font
534
+ v['fontFamily'] = 'sans-serif' unless v.has_key?('fontFamily')
535
+ v['fontSize'] = '12px' unless v.has_key?('fontSize')
536
+ end
537
+ end
538
+ layer['style']['styleProperty'] = v2_layer['styleProperty'] if v2_layer.has_key?('styleProperty')
539
+ layer['style']['version'] = 1
540
+ end
541
+ else
542
+ logger.info "Layer type not supported: #{v2_layer.to_yaml}"
543
+ next
544
+ end
306
545
 
307
- if output_format != "pdf" && OUTPUT_FORMATS.include?(output_format)
308
- # convert PDF to image
309
- pdf = Magick::Image.read(temp_pdf) { self.density = dpi }.first
310
- temp_suffix = ".#{output_format}"
311
- temp_img = TMP_PREFIX + temp_id.to_s + temp_suffix
312
- pdf.write(temp_img)
313
- File.delete(temp_pdf)
546
+ map['layers'] << layer
547
+ end
314
548
  end
315
549
 
316
- respond_to do |format|
317
- format.json do
318
- render :json=>{ 'getURL' => url_for(:action=>'show', :id=>temp_id) + temp_suffix }
550
+ print_params['attributes']['map'] = map
551
+
552
+ # custom params
553
+ non_custom_params = ['layout', 'outputFormat', 'units', 'srs', 'dpi', 'layers', 'pages']
554
+ print_v2_params.each do |k, v|
555
+ unless non_custom_params.include?(k)
556
+ print_params['attributes'][k] = v
319
557
  end
320
558
  end
559
+
560
+ print_params
321
561
  end
322
562
 
323
563
  # forward to JasperReport
324
- def create_report(request)
325
- report = request.parameters["report"]
564
+ def create_report(report, request)
326
565
  call_params = {
327
566
  :j_username => JASPER_USER,
328
567
  :j_password => JASPER_PASSWORD
@@ -356,18 +595,16 @@ class PrintController < ApplicationController
356
595
  end
357
596
 
358
597
  #Mapfish print compatible report delivery
359
- def call_report(request)
360
- result = create_report(request)
598
+ def call_report(report, request)
599
+ result = create_report(report, request)
361
600
  if result.nil?
362
601
  render :nothing => true, :status => 500
363
- return
364
- end
365
-
366
- if result.kind_of? Net::HTTPSuccess
602
+ elsif result.kind_of? Net::HTTPSuccess
367
603
  temp_id = SecureRandom.random_number(2**31)
368
604
  temp = TMP_PREFIX + temp_id.to_s + TMP_SUFFIX
369
605
  File.open(temp, 'wb') {|f| f.write(result.body) }
370
606
 
607
+ # send link to print result
371
608
  render :json=>{ 'getURL' => url_for(:action=> 'show', :id=> temp_id) + ".pdf" }
372
609
  else
373
610
  logger.info "#{result.code}: #{result.body}"
@@ -375,4 +612,16 @@ class PrintController < ApplicationController
375
612
  end
376
613
  end
377
614
 
615
+ def create_and_send_jasper_report(report, request, type, filename)
616
+ result = create_report(report, request)
617
+ if result.nil?
618
+ render :nothing => true, :status => 500
619
+ elsif result.kind_of? Net::HTTPSuccess
620
+ send_data result.body, :type => type, :disposition => 'attachment', :filename => filename
621
+ else
622
+ logger.info "#{result.code}: #{result.body}"
623
+ render :nothing => true, :status => result.code
624
+ end
625
+ end
626
+
378
627
  end
@@ -1,3 +1,3 @@
1
1
  module GbMapfishAppserver
2
- VERSION = "1.0.6"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -80,15 +80,17 @@ MAPSERV_CGI_URL = '/cgi-bin/mapserv'
80
80
  MAPPATH = '#{Rails.root}/mapconfig'
81
81
 
82
82
  #Internal URL of print servlet (nil: print-standalone)
83
- PRINT_URL = nil #'http://localhost:8080/print-servlet-1.1/pdf/print.pdf'
83
+ PRINT_URL = nil #'http://localhost:8080/mapfish_print/print/myapp'
84
+ #path to standalone Mapfish Print JARs (if PRINT_URL = nil)
85
+ # PRINT_STANDALONE_JARS = '$PATH_TO/mapfish-print/core/lib/*'
84
86
  }
85
87
  %w[development test production].each do |env|
86
88
  append_to_file("config/environments/#{env}.rb", env_config)
87
89
  end
88
90
  end
89
91
 
90
- def add_print_config_template
91
- template "print.yml", "config/print.yml"
92
+ def copy_print_configs
93
+ directory "print", "print"
92
94
  end
93
95
 
94
96
  def setup_mapconfig
@@ -22,6 +22,10 @@ LOCAL_WMS = [
22
22
  #NOTE: use a shared directory for multi-node setups (e.g. NFS)
23
23
  PRINT_TMP_PATH = "/tmp"
24
24
 
25
+ # scales and dpis for printing (optional)
26
+ #PRINT_SCALES = [500, 1000, 2500, 5000, 10000, 15000, 25000, 50000, 100000, 200000, 500000]
27
+ #PRINT_DPIS = [150, 300]
28
+
25
29
  DEFAULT_TOPIC = {
26
30
  SITE_DEFAULT => (Topic.first rescue nil) #Topic.where(:name => 'MainMap').first
27
31
  }
@@ -0,0 +1,41 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0 -->
3
+ <!-- 2015-12-02T11:19:59 -->
4
+ <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="A4_portrait" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="cbdda6e8-753d-45cb-8017-b2a469eecfe4">
5
+ <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
6
+ <parameter name="map_title" class="java.lang.String"/>
7
+ <parameter name="mapSubReport" class="java.lang.String" isForPrompting="false"/>
8
+ <parameter name="scalebarSubReport" class="java.lang.String" isForPrompting="false"/>
9
+ <queryString>
10
+ <![CDATA[]]>
11
+ </queryString>
12
+ <title>
13
+ <band height="50" splitType="Stretch">
14
+ <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
15
+ <textField>
16
+ <reportElement x="0" y="0" width="280" height="30" uuid="0b8cc9a6-5320-419d-876a-9b0f83512251"/>
17
+ <textElement>
18
+ <font size="20"/>
19
+ </textElement>
20
+ <textFieldExpression><![CDATA[$P{map_title}]]></textFieldExpression>
21
+ </textField>
22
+ </band>
23
+ </title>
24
+ <detail>
25
+ <band height="680" splitType="Stretch">
26
+ <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
27
+ <subreport>
28
+ <reportElement x="0" y="0" width="555" height="680" uuid="5e5015ee-5bc7-4c22-bc1d-11b41ada327e"/>
29
+ <subreportExpression><![CDATA[$P{mapSubReport}]]></subreportExpression>
30
+ </subreport>
31
+ </band>
32
+ </detail>
33
+ <pageFooter>
34
+ <band height="50" splitType="Stretch">
35
+ <subreport>
36
+ <reportElement x="435" y="0" width="120" height="20" uuid="3472f694-cbf8-4e07-9964-099c5f0a3043"/>
37
+ <subreportExpression><![CDATA[$P{scalebarSubReport}]]></subreportExpression>
38
+ </subreport>
39
+ </band>
40
+ </pageFooter>
41
+ </jasperReport>
@@ -0,0 +1,24 @@
1
+ # Mapfish Print v3
2
+ throwErrorOnExtraParameters: true
3
+
4
+ templates:
5
+
6
+ #===========================================================================
7
+ A4 portrait: !template
8
+ #===========================================================================
9
+ reportTemplate: A4_portrait.jrxml
10
+ attributes:
11
+ map_title: !string {default: "Map Title"}
12
+ map: !map
13
+ width: 555
14
+ height: 680
15
+ maxDpi: 300
16
+ dpiSuggestions: [150, 300]
17
+ scalebar: !scalebar
18
+ width: 120
19
+ height: 20
20
+ processors:
21
+ - !reportBuilder
22
+ directory: "."
23
+ - !createMap {}
24
+ - !createScalebar {}
@@ -44,4 +44,6 @@ MAPSERV_CGI_URL = '/cgi-bin/mapserv'
44
44
  MAPPATH = '/home/pi/code/rails/gb_mapfish_appserver/test/dummy/mapconfig'
45
45
 
46
46
  #Internal URL of print servlet (nil: print-standalone)
47
- PRINT_URL = nil #'http://localhost:8080/print-servlet-1.1/pdf/print.pdf'
47
+ PRINT_URL = nil #'http://localhost:8080/mapfish_print/print/myapp'
48
+ #path to standalone Mapfish Print JARs (if PRINT_URL = nil)
49
+ # PRINT_STANDALONE_JARS = '$PATH_TO/mapfish-print/core/lib/*'
@@ -74,4 +74,4 @@ MAPSERV_CGI_URL = '/cgi-bin/mapserv'
74
74
  MAPPATH = '/home/pi/code/rails/dummy/mapconfig'
75
75
 
76
76
  #Internal URL of print servlet (nil: print-standalone)
77
- PRINT_URL = nil #'http://localhost:8080/print-servlet-1.1/pdf/print.pdf'
77
+ PRINT_URL = nil #'http://localhost:8080/mapfish_print/print/myapp'
@@ -44,4 +44,4 @@ MAPSERV_CGI_URL = '/cgi-bin/mapserv'
44
44
  MAPPATH = '/home/pi/code/rails/dummy/mapconfig'
45
45
 
46
46
  #Internal URL of print servlet (nil: print-standalone)
47
- PRINT_URL = nil #'http://localhost:8080/print-servlet-1.1/pdf/print.pdf'
47
+ PRINT_URL = nil #'http://localhost:8080/mapfish_print/print/myapp'
@@ -22,6 +22,10 @@ LOCAL_WMS = [
22
22
  #NOTE: use a shared directory for multi-node setups (e.g. NFS)
23
23
  PRINT_TMP_PATH = "/tmp"
24
24
 
25
+ # scales and dpis for printing (optional)
26
+ #PRINT_SCALES = [500, 1000, 2500, 5000, 10000, 15000, 25000, 50000, 100000, 200000, 500000]
27
+ #PRINT_DPIS = [150, 300]
28
+
25
29
  DEFAULT_TOPIC = {
26
30
  SITE_DEFAULT => (Topic.first rescue nil) #Topic.where(:name => 'MainMap').first
27
31
  }
@@ -0,0 +1,41 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0 -->
3
+ <!-- 2015-12-02T11:19:59 -->
4
+ <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="A4_portrait" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="cbdda6e8-753d-45cb-8017-b2a469eecfe4">
5
+ <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
6
+ <parameter name="map_title" class="java.lang.String"/>
7
+ <parameter name="mapSubReport" class="java.lang.String" isForPrompting="false"/>
8
+ <parameter name="scalebarSubReport" class="java.lang.String" isForPrompting="false"/>
9
+ <queryString>
10
+ <![CDATA[]]>
11
+ </queryString>
12
+ <title>
13
+ <band height="50" splitType="Stretch">
14
+ <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
15
+ <textField>
16
+ <reportElement x="0" y="0" width="280" height="30" uuid="0b8cc9a6-5320-419d-876a-9b0f83512251"/>
17
+ <textElement>
18
+ <font size="20"/>
19
+ </textElement>
20
+ <textFieldExpression><![CDATA[$P{map_title}]]></textFieldExpression>
21
+ </textField>
22
+ </band>
23
+ </title>
24
+ <detail>
25
+ <band height="680" splitType="Stretch">
26
+ <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
27
+ <subreport>
28
+ <reportElement x="0" y="0" width="555" height="680" uuid="5e5015ee-5bc7-4c22-bc1d-11b41ada327e"/>
29
+ <subreportExpression><![CDATA[$P{mapSubReport}]]></subreportExpression>
30
+ </subreport>
31
+ </band>
32
+ </detail>
33
+ <pageFooter>
34
+ <band height="50" splitType="Stretch">
35
+ <subreport>
36
+ <reportElement x="435" y="0" width="120" height="20" uuid="3472f694-cbf8-4e07-9964-099c5f0a3043"/>
37
+ <subreportExpression><![CDATA[$P{scalebarSubReport}]]></subreportExpression>
38
+ </subreport>
39
+ </band>
40
+ </pageFooter>
41
+ </jasperReport>
@@ -0,0 +1,24 @@
1
+ # Mapfish Print v3
2
+ throwErrorOnExtraParameters: true
3
+
4
+ templates:
5
+
6
+ #===========================================================================
7
+ A4 portrait: !template
8
+ #===========================================================================
9
+ reportTemplate: A4_portrait.jrxml
10
+ attributes:
11
+ map_title: !string {default: "Map Title"}
12
+ map: !map
13
+ width: 555
14
+ height: 680
15
+ maxDpi: 300
16
+ dpiSuggestions: [150, 300]
17
+ scalebar: !scalebar
18
+ width: 120
19
+ height: 20
20
+ processors:
21
+ - !reportBuilder
22
+ directory: "."
23
+ - !createMap {}
24
+ - !createScalebar {}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gb_mapfish_appserver
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pirmin Kalberer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -327,7 +327,8 @@ files:
327
327
  - lib/generators/mapfish/install/install_generator.rb
328
328
  - lib/generators/mapfish/install/templates/geodatabase.yml
329
329
  - lib/generators/mapfish/install/templates/initializer.rb
330
- - lib/generators/mapfish/install/templates/print.yml
330
+ - lib/generators/mapfish/install/templates/print/A4_portrait.jrxml
331
+ - lib/generators/mapfish/install/templates/print/config.yaml
331
332
  - lib/generators/mapfish/install/templates/search_rules.rb
332
333
  - lib/generators/mapfish/viewer/templates/viewer.html.erb
333
334
  - lib/generators/mapfish/viewer/viewer_generator.rb
@@ -375,7 +376,6 @@ files:
375
376
  - test/dummy/config/initializers/session_store.rb
376
377
  - test/dummy/config/initializers/wrap_parameters.rb
377
378
  - test/dummy/config/locales/en.yml
378
- - test/dummy/config/print.yml
379
379
  - test/dummy/config/routes.rb
380
380
  - test/dummy/db/migrate/20131120103839_setup_mapfish_tables.mapfish.rb
381
381
  - test/dummy/db/schema.rb
@@ -391,6 +391,8 @@ files:
391
391
  - test/dummy/mapconfig/maps.example.com/cascadingwms.map
392
392
  - test/dummy/mapconfig/maps.example.com/connection.inc.example
393
393
  - test/dummy/mapconfig/maps.example.com/naturalearth.map
394
+ - test/dummy/print/A4_portrait.jrxml
395
+ - test/dummy/print/config.yaml
394
396
  - test/dummy/public/404.html
395
397
  - test/dummy/public/422.html
396
398
  - test/dummy/public/500.html
@@ -527,7 +529,6 @@ test_files:
527
529
  - test/dummy/config/initializers/session_store.rb
528
530
  - test/dummy/config/initializers/wrap_parameters.rb
529
531
  - test/dummy/config/locales/en.yml
530
- - test/dummy/config/print.yml
531
532
  - test/dummy/config/routes.rb
532
533
  - test/dummy/db/migrate/20131120103839_setup_mapfish_tables.mapfish.rb
533
534
  - test/dummy/db/schema.rb
@@ -543,6 +544,8 @@ test_files:
543
544
  - test/dummy/mapconfig/maps.example.com/cascadingwms.map
544
545
  - test/dummy/mapconfig/maps.example.com/connection.inc.example
545
546
  - test/dummy/mapconfig/maps.example.com/naturalearth.map
547
+ - test/dummy/print/A4_portrait.jrxml
548
+ - test/dummy/print/config.yaml
546
549
  - test/dummy/public/404.html
547
550
  - test/dummy/public/422.html
548
551
  - test/dummy/public/500.html
@@ -1,76 +0,0 @@
1
- #===========================================================================
2
- # allowed DPIs
3
- #===========================================================================
4
- dpis:
5
- - 254
6
- - 190
7
- # - 127
8
- # - 56
9
-
10
- #===========================================================================
11
- # the allowed scales
12
- #===========================================================================
13
- scales:
14
- - 25000
15
- - 50000
16
- - 100000
17
- - 200000
18
- - 500000
19
- - 1000000
20
- - 2000000
21
- - 4000000
22
- - 8000000
23
- - 16000000
24
- - 32000000
25
- - 64000000
26
-
27
- #===========================================================================
28
- # the list of allowed hosts
29
- #===========================================================================
30
-
31
- hosts:
32
- - !localMatch
33
- dummy: true
34
- - !ipMatch
35
- ip: demo.mapfish.org
36
- - !dnsMatch
37
- host: labs.metacarta.com
38
- port: 80
39
-
40
-
41
- layouts:
42
- #===========================================================================
43
- A4 portrait:
44
- #===========================================================================
45
- mainPage:
46
- rotation: true
47
- pageSize: A4
48
- header:
49
- height: 50
50
- items:
51
- - !text
52
- font: Helvetica
53
- fontSize: 30
54
- align: right
55
- text: 'hello first print'
56
- items:
57
- - !map
58
- spacingAfter: 30
59
- width: 440
60
- height: 483
61
- - !text
62
- text: 'blablabla'
63
- spacingAfter: 30
64
- footer:
65
- height: 30
66
- items:
67
- - !columns
68
- items:
69
- - !text
70
- backgroundColor: #FF0000
71
- align: left
72
- text: little test
73
- - !text
74
- align: right
75
- text: 'Page ${pageNum}'
76
-
@@ -1,76 +0,0 @@
1
- #===========================================================================
2
- # allowed DPIs
3
- #===========================================================================
4
- dpis:
5
- - 254
6
- - 190
7
- # - 127
8
- # - 56
9
-
10
- #===========================================================================
11
- # the allowed scales
12
- #===========================================================================
13
- scales:
14
- - 25000
15
- - 50000
16
- - 100000
17
- - 200000
18
- - 500000
19
- - 1000000
20
- - 2000000
21
- - 4000000
22
- - 8000000
23
- - 16000000
24
- - 32000000
25
- - 64000000
26
-
27
- #===========================================================================
28
- # the list of allowed hosts
29
- #===========================================================================
30
-
31
- hosts:
32
- - !localMatch
33
- dummy: true
34
- - !ipMatch
35
- ip: demo.mapfish.org
36
- - !dnsMatch
37
- host: labs.metacarta.com
38
- port: 80
39
-
40
-
41
- layouts:
42
- #===========================================================================
43
- A4 portrait:
44
- #===========================================================================
45
- mainPage:
46
- rotation: true
47
- pageSize: A4
48
- header:
49
- height: 50
50
- items:
51
- - !text
52
- font: Helvetica
53
- fontSize: 30
54
- align: right
55
- text: 'hello first print'
56
- items:
57
- - !map
58
- spacingAfter: 30
59
- width: 440
60
- height: 483
61
- - !text
62
- text: 'blablabla'
63
- spacingAfter: 30
64
- footer:
65
- height: 30
66
- items:
67
- - !columns
68
- items:
69
- - !text
70
- backgroundColor: #FF0000
71
- align: left
72
- text: little test
73
- - !text
74
- align: right
75
- text: 'Page ${pageNum}'
76
-