map_print 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b403565fd160476de9c6c7bc64b96f87168c0709
4
- data.tar.gz: 2acbf3eea43cc28719c4d50270de1ad244fb4b37
3
+ metadata.gz: 73eaf58e1ffe788eac9f34f197ba18e682548f15
4
+ data.tar.gz: 3746ee55d0e44f920ce4db99259bf544451abf2e
5
5
  SHA512:
6
- metadata.gz: 96ae6bc8c7a4d0b81539616ce93b8c0cd13f1288b70e7fa20d9f24de5f6207d24665bd651a534d592d73b8e04441d92b8a6e9053716e8fb642c5df8b7b2af246
7
- data.tar.gz: 1a347d350da58f79f5336f13ab07efbea01ca0e28be37233a947d26ac670479e112e355db05daacb1c85fa664d690532c49611126959d522edac6d3f6a47a6d8
6
+ metadata.gz: 9a44c54c8f966d3253229399532f431f04b1e56c1d5489af27799075700db9dd2677de5b8f292d50f56f89016aa70fade5fd743cc242207d393b8b512e1e04a2
7
+ data.tar.gz: 838a40807d303bda6cf4d11fc361959909d1c115e83167512a9b728831f176ac14ae04ea449bc2e543c76edfa4f28910437876bc5f25163c845c914a68046eda
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- map_print (0.1.0)
4
+ map_print (0.2.1)
5
5
  geo-distance (~> 0.1)
6
6
  mini_magick (~> 4.3)
7
- parallel (~> 1.6.1)
7
+ parallel (~> 1.6)
8
8
  prawn (~> 2.0)
9
9
  prawn-fast-png (~> 0.2.3)
10
- thor
10
+ thor (~> 0.19)
11
11
 
12
12
  GEM
13
13
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -48,6 +48,11 @@ BASIC_MAP = {
48
48
  page_size: 'A4', # A0-10, B0-10, C0-10
49
49
  page_layout: :portrait # :portrait, :landscape
50
50
  },
51
+ png_options: {
52
+ width: 800,
53
+ height: 1000,
54
+ background_color: '#ffffff'
55
+ },
51
56
  map: {
52
57
  sw: { # required
53
58
  lat: -35.026862,
@@ -101,19 +106,23 @@ BASIC_MAP = {
101
106
  }]
102
107
  }'
103
108
  },
104
- images: [ # not supported yet
109
+ images: [
105
110
  {
106
111
  path: './file.png',
107
112
  position: {x: 50, y: 50 },
108
- size: {width: 50, height: 50},
109
- options: {}
113
+ options: {
114
+ fit: {
115
+ width: 25,
116
+ height: 25
117
+ }
118
+ }
110
119
  }
111
120
  ],
112
- texts: [ # not supported yet
121
+ texts: [
113
122
  {
114
123
  text: "some text",
115
124
  position: {x: 50, y: 50 },
116
- size: {width: 50, height: 50},
125
+ box_size: {width: 50, height: 50},
117
126
  options: {}
118
127
  }
119
128
  ],
@@ -6,14 +6,14 @@ require_relative 'providers/bing'
6
6
  require_relative 'providers/open_street_map'
7
7
  require_relative 'layer_handler'
8
8
  require_relative 'scalebar_handler'
9
- require_relative 'image_handler'
10
- require_relative 'text_handler'
11
9
  require_relative 'legend_handler'
12
10
  require_relative 'geo_json_handler'
11
+ require_relative 'pdf_handler'
12
+ require_relative 'png_handler'
13
13
 
14
14
  module MapPrint
15
15
  class Core
16
- attr_accessor :map, :images, :texts, :legend, :scalebar
16
+ attr_accessor :map, :images, :texts, :legend, :scalebar, :pdf_options, :png_options, :output_path
17
17
 
18
18
  PROVIDERS = {
19
19
  'bing' => MapPrint::Providers::Bing,
@@ -23,6 +23,7 @@ module MapPrint
23
23
  def initialize(args)
24
24
  @format = args[:format]
25
25
  @pdf_options = args[:pdf_options]
26
+ @png_options = args[:png_options]
26
27
  @map = args[:map]
27
28
  @images = args[:images]
28
29
  @texts = args[:texts]
@@ -34,62 +35,17 @@ module MapPrint
34
35
  @output_path = output_path
35
36
 
36
37
  if @format == 'pdf'
37
- print_pdf
38
+ handler = PdfHandler.new(self)
38
39
  elsif @format == 'png'
39
- print_png
40
+ handler = PngHandler.new(self)
40
41
  else
41
42
  raise "Unsupported format: #{@format}"
42
43
  end
43
- end
44
-
45
- private
46
- def print_pdf
47
- pdf = init_pdf
48
- map_image = print_layers
49
- map_image = print_geojson(MiniMagick::Image.new(map_image.path))
50
-
51
- FileUtils.cp map_image.path, './map.png' if defined?(DEBUG)
52
44
 
53
- size = @map[:size]
54
- size[:width] ||= map_image.width
55
- size[:height] ||= map_image.height
56
- pdf.image map_image.path, at: [@map[:position][:x], pdf.bounds.top - @map[:position][:y]], fit: size.values
57
-
58
- print_images_on_pdf(pdf)
59
- print_texts_on_pdf(pdf)
60
- print_legend_on_pdf(pdf)
61
-
62
- pdf.render_file(@output_path)
45
+ handler.print
63
46
  @output_path
64
47
  end
65
48
 
66
- def print_png
67
- map_image = print_layers
68
- map_image = print_geojson(MiniMagick::Image.new(map_image.path))
69
-
70
- print_images_on_png(map_image)
71
- print_texts_on_png(map_image)
72
- print_legend_on_png(map_image)
73
-
74
- size = @map[:size]
75
- if size
76
- size[:width] ||= map_image.width
77
- size[:height] ||= map_image.height
78
- puts "Fitting map image (#{map_image.width}x#{map_image.height}) in #{size[:width]}x#{size[:height]}"
79
- map_image.colorspace("RGB").resize("#{size[:width]}x#{size[:height]}\>").colorspace("sRGB").unsharp "0x0.75+0.75+0.008"
80
- end
81
-
82
- FileUtils.cp map_image.path, @output_path
83
- end
84
-
85
- def init_file
86
- @file = File.open @output_path, 'wb'
87
- end
88
-
89
- def init_pdf
90
- Prawn::Document.new @pdf_options || {}
91
- end
92
-
93
49
  def print_layers
94
50
  file = LayerHandler.new(@map[:layers], @map[:sw], @map[:ne], @map[:zoom]).process
95
51
 
@@ -110,24 +66,12 @@ module MapPrint
110
66
  map_image
111
67
  end
112
68
 
113
- def print_images_on_pdf(pdf)
114
- end
115
-
116
- def print_texts_on_pdf(pdf)
117
- end
118
-
119
69
  def print_scalebar
120
70
  end
121
71
 
122
72
  def print_legend_on_pdf(pdf)
123
73
  end
124
74
 
125
- def print_images_on_png(png)
126
- end
127
-
128
- def print_texts_on_png(png)
129
- end
130
-
131
75
  def print_legend_on_png(png)
132
76
  end
133
77
  end
@@ -0,0 +1,34 @@
1
+ require_relative 'pdf_handlers/texts'
2
+ require_relative 'pdf_handlers/images'
3
+
4
+ module MapPrint
5
+ class PdfHandler
6
+ include PdfHandlers::Images
7
+ include PdfHandlers::Texts
8
+
9
+ def initialize(context)
10
+ @context = context
11
+ end
12
+
13
+ def print
14
+ @pdf = Prawn::Document.new @context.pdf_options || {}
15
+
16
+ print_map
17
+ print_images(@context.images, @pdf)
18
+ print_texts(@context.texts, @pdf)
19
+ # print_legend_on_pdf(@pdf)
20
+
21
+ @pdf.render_file(@context.output_path)
22
+ end
23
+
24
+ def print_map
25
+ map_image = @context.print_layers
26
+ map_image = @context.print_geojson(MiniMagick::Image.new(map_image.path))
27
+
28
+ size = @context.map[:size]
29
+ size[:width] ||= map_image.width
30
+ size[:height] ||= map_image.height
31
+ @pdf.image map_image.path, at: [@context.map[:position][:x], @pdf.bounds.top - @context.map[:position][:y]], fit: size.values
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
1
+ module MapPrint
2
+ module PdfHandlers
3
+ module Images
4
+ def print_images(images, pdf)
5
+ images.each do |image|
6
+ if image[:path] =~ /https?:\/\//
7
+ image_file = open(image[:path])
8
+ else
9
+ image_file = image[:path]
10
+ end
11
+
12
+ pdf.image image_file, image_options(image, pdf.bounds.top)
13
+ end
14
+ end
15
+
16
+ def image_options(image, bounds_top)
17
+ position = {}
18
+
19
+ if image[:position]
20
+ position[:at] = image[:position].values
21
+ position[:at][1] = bounds_top - position[:at][1]
22
+ end
23
+
24
+ if image[:options][:fit]
25
+ position[:fit] = [image[:options][:fit][:width], image[:options][:fit][:height]]
26
+ end
27
+
28
+ image[:options].merge position
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module MapPrint
2
+ module PdfHandlers
3
+ module Texts
4
+ def print_texts(texts, pdf)
5
+ texts.each do |text|
6
+ pdf.text_box text[:text], text_options(text)
7
+ end
8
+ end
9
+
10
+ def text_options(text)
11
+ box = {}
12
+
13
+ if text[:position]
14
+ box[:at] = text[:position].values
15
+ box[:at][1] = @pdf.bounds.top - box[:at][1]
16
+ end
17
+
18
+ if text[:box_size]
19
+ box[:width] = text[:box_size][:width] if text[:box_size][:width]
20
+ box[:height] = text[:box_size][:height] if text[:box_size][:height]
21
+ end
22
+
23
+ text[:options].merge box
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ require_relative 'png_handlers/texts'
2
+ require_relative 'png_handlers/images'
3
+
4
+ module MapPrint
5
+ class PngHandler
6
+ include PngHandlers::Images
7
+ include PngHandlers::Texts
8
+
9
+ def initialize(context)
10
+ @context = context
11
+ end
12
+
13
+ def print
14
+ `convert -size #{@context.png_options[:width]}x#{@context.png_options[:height]} xc:#{@context.png_options[:background_color]} #{@context.output_path}`
15
+ @png = MiniMagick::Image.new @context.output_path
16
+
17
+ print_map
18
+
19
+ print_images(@context.images, @png)
20
+ print_texts(@context.texts, @png)
21
+ # print_legend_on_png
22
+ end
23
+
24
+ def print_map
25
+ map_image = @context.print_layers
26
+ map_image = @context.print_geojson(MiniMagick::Image.new(map_image.path))
27
+
28
+ size = @context.map[:size]
29
+ geometry = ''
30
+ if size && (size[:width] || size[:height])
31
+ geometry += size[:width].to_s if size[:width]
32
+ geometry += 'x'
33
+ geometry += size[:height].to_s if size[:height]
34
+ end
35
+
36
+ if @context.map[:position]
37
+ geometry += "+#{@context.map[:position][:x] || 0}+#{@context.map[:position][:y] || 0}"
38
+ end
39
+
40
+ result = @png.composite(map_image) do |c|
41
+ c.geometry geometry
42
+ end
43
+ result.write @context.output_path
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ module MapPrint
2
+ module PngHandlers
3
+ module Images
4
+ def print_images(images, png)
5
+ images.each do |image|
6
+ image_file = MiniMagick::Image.open(image[:path])
7
+
8
+ geometry = ''
9
+ geometry += "#{image[:options][:fit][:width]}x#{image[:options][:fit][:height]}" if image[:options][:fit]
10
+ geometry += "+#{image[:position][:x]}+#{image[:position][:y]}"
11
+ result = png.composite(image_file) do |c|
12
+ c.geometry geometry
13
+ end
14
+ result.write @context.output_path
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module MapPrint
2
+ module PngHandlers
3
+ module Texts
4
+ def print_texts(texts, png)
5
+ texts.each do |text|
6
+ position = "#{text[:position][:x]},#{text[:position][:y]}"
7
+
8
+ draw_text(png, text[:text], position, text[:options])
9
+ end
10
+ end
11
+
12
+ def draw_text(png, text, position, options)
13
+ png.combine_options do |c|
14
+ c.fill options[:fill_color] if options[:fill_color]
15
+ c.stroke options[:color] if options[:color]
16
+ c.font options[:font] || 'Arial'
17
+ c.pointsize options[:pointsize] if options[:pointsize]
18
+ c.gravity options[:gravity] || 'NorthWest'
19
+ c.draw "text #{position} '#{text}'"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module MapPrint
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: map_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Fast
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-28 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -162,17 +162,21 @@ files:
162
162
  - lib/map_print.rb
163
163
  - lib/map_print/core.rb
164
164
  - lib/map_print/geo_json_handler.rb
165
- - lib/map_print/image_handler.rb
166
165
  - lib/map_print/lat_lng.rb
167
166
  - lib/map_print/layer_handler.rb
168
167
  - lib/map_print/legend_handler.rb
169
168
  - lib/map_print/osm/tile.rb
170
169
  - lib/map_print/osm/tile_factory.rb
170
+ - lib/map_print/pdf_handler.rb
171
+ - lib/map_print/pdf_handlers/images.rb
172
+ - lib/map_print/pdf_handlers/texts.rb
173
+ - lib/map_print/png_handler.rb
174
+ - lib/map_print/png_handlers/images.rb
175
+ - lib/map_print/png_handlers/texts.rb
171
176
  - lib/map_print/providers/base.rb
172
177
  - lib/map_print/providers/bing.rb
173
178
  - lib/map_print/providers/open_street_map.rb
174
179
  - lib/map_print/scalebar_handler.rb
175
- - lib/map_print/text_handler.rb
176
180
  - lib/map_print/tiles/bing_tile.rb
177
181
  - lib/map_print/tiles/osm_tile.rb
178
182
  - lib/map_print/tiles/tile.rb
@@ -1,9 +0,0 @@
1
- module MapPrint
2
- class ImageHandler
3
- def initialize(images, file)
4
- end
5
-
6
- def process
7
- end
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- module MapPrint
2
- class TextHandler
3
- def initialize(texts, file)
4
- end
5
-
6
- def process
7
- end
8
- end
9
- end