Class: MapPrint::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/map_print/core.rb

Constant Summary collapse

PROVIDERS =
{
  'bing' => MapPrint::Providers::Bing,
  'osm'  => MapPrint::Providers::OpenStreetMap
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Core

Returns a new instance of Core.

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/map_print/core.rb', line 24

def initialize(args)
  @format = args[:format]
  @pdf_options = args[:pdf_options]
  @png_options = args[:png_options]
  @map = args[:map]
  @images = args[:images]
  @texts = args[:texts]
  @legend = args[:legend]
  @scalebar = args[:scalebar]
  raise ParameterError.new("Please indicate the southwest point for the map ({map: {sw: {lat: -35.026862, lng: -58.425003}}})") unless @map && @map[:sw] && @map[:sw][:lat] && @map[:sw][:lng]
  raise ParameterError.new("Please indicate the northeast point for the map ({map: {ne: {lat: -29.980172, lng: -52.959305}}})") unless @map[:ne] && @map[:ne][:lat] && @map[:ne][:lng]
  raise ParameterError.new("Please indicate the zoom level for the map ({map: {zoom: 9})") unless @map[:zoom]
  raise ParameterError.new("Please indicate layers to be printed for the map ({map: {layers: [{type: 'osm'}]})") unless @map[:layers].is_a?(Array)
  Logger.warn 'Found geojson property defined outside map, it must be inside the map property' if @map[:geojson].nil? && args[:geojson]
end

Instance Attribute Details

#imagesObject

Returns the value of attribute images.



17
18
19
# File 'lib/map_print/core.rb', line 17

def images
  @images
end

#legendObject

Returns the value of attribute legend.



17
18
19
# File 'lib/map_print/core.rb', line 17

def legend
  @legend
end

#mapObject

Returns the value of attribute map.



17
18
19
# File 'lib/map_print/core.rb', line 17

def map
  @map
end

#output_pathObject

Returns the value of attribute output_path.



17
18
19
# File 'lib/map_print/core.rb', line 17

def output_path
  @output_path
end

#pdf_optionsObject

Returns the value of attribute pdf_options.



17
18
19
# File 'lib/map_print/core.rb', line 17

def pdf_options
  @pdf_options
end

#png_optionsObject

Returns the value of attribute png_options.



17
18
19
# File 'lib/map_print/core.rb', line 17

def png_options
  @png_options
end

#scalebarObject

Returns the value of attribute scalebar.



17
18
19
# File 'lib/map_print/core.rb', line 17

def scalebar
  @scalebar
end

#textsObject

Returns the value of attribute texts.



17
18
19
# File 'lib/map_print/core.rb', line 17

def texts
  @texts
end

Instance Method Details



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/map_print/core.rb', line 40

def print(output_path)
  @output_path = output_path

  if @format == 'pdf'
    handler = PdfHandler.new(self)
  else
    Logger.warn 'Did not specify format, defaulting to png'
    handler = PngHandler.new(self)
  end

  handler.print
  @output_path
end


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/map_print/core.rb', line 58

def print_geojson(map_image)
  if @map[:geojson]
    geojson_image = GeoJSONHandler.new(@map[:geojson], @map[:sw], @map[:ne], map_image.width, map_image.height).process
    result = MiniMagick::Image.open(map_image.path).composite(geojson_image) do |c|
      c.density 300
      c.compose "atop"
    end
    result.write map_image.path
  end

  map_image
end


54
55
56
# File 'lib/map_print/core.rb', line 54

def print_layers
  LayerHandler.new(@map[:layers], @map[:sw], @map[:ne], @map[:zoom]).process
end


77
78
79
80
81
# File 'lib/map_print/core.rb', line 77

def print_legend
  if @legend
    LegendHandler.new(@legend).process
  end
end


71
72
73
74
75
# File 'lib/map_print/core.rb', line 71

def print_scalebar
  if @scalebar
    ScalebarHandler.new(@scalebar, @map[:zoom]).process
  end
end