Module: Gpx2png::Calculations::OsmInstanceMethods

Included in:
OsmBase
Defined in:
lib/gpx2png/calculations/osm_instance_methods.rb

Constant Summary collapse

TILE_WIDTH =
OsmClassMethods::TILE_WIDTH
TILE_HEIGHT =
OsmClassMethods::TILE_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bitmap_point_x_maxObject (readonly)

points for cropping



13
14
15
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 13

def bitmap_point_x_max
  @bitmap_point_x_max
end

#bitmap_point_x_minObject (readonly)

points for cropping



13
14
15
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 13

def bitmap_point_x_min
  @bitmap_point_x_min
end

#bitmap_point_y_maxObject (readonly)

points for cropping



13
14
15
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 13

def bitmap_point_y_max
  @bitmap_point_y_max
end

#bitmap_point_y_minObject (readonly)

points for cropping



13
14
15
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 13

def bitmap_point_y_min
  @bitmap_point_y_min
end

#lat_maxObject (readonly)

Returns the value of attribute lat_max.



10
11
12
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 10

def lat_max
  @lat_max
end

#lat_minObject (readonly)

Returns the value of attribute lat_min.



10
11
12
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 10

def lat_min
  @lat_min
end

#lon_maxObject (readonly)

Returns the value of attribute lon_max.



10
11
12
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 10

def lon_max
  @lon_max
end

#lon_minObject (readonly)

Returns the value of attribute lon_min.



10
11
12
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 10

def lon_min
  @lon_min
end

#tile_x_distanceObject (readonly)

Returns the value of attribute tile_x_distance.



11
12
13
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 11

def tile_x_distance
  @tile_x_distance
end

#tile_y_distanceObject (readonly)

Returns the value of attribute tile_y_distance.



11
12
13
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 11

def tile_y_distance
  @tile_y_distance
end

Instance Method Details

#auto_zoom_for(x = 0, y = 0) ⇒ Object

Calculate zoom level



64
65
66
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 64

def auto_zoom_for(x = 0, y = 0)
  # TODO
end

#calculate_for_cropObject

Calculate some numbers for cropping operation



69
70
71
72
73
74
75
76
77
78
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 69

def calculate_for_crop
  point_min = self.class.point_on_image(@zoom, [@lat_min, @lon_min])
  point_max = self.class.point_on_image(@zoom, [@lat_max, @lon_max])
  @bitmap_point_x_min = (point_min[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_min[:pixel_offset][0]
  @bitmap_point_x_max = (point_max[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_max[:pixel_offset][0]
  @bitmap_point_y_max = (point_min[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_min[:pixel_offset][1]
  @bitmap_point_y_min = (point_max[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_max[:pixel_offset][1]

  @r.set_crop(@bitmap_point_x_min, @bitmap_point_x_max, @bitmap_point_y_min, @bitmap_point_y_max)
end

#calculate_for_crop_with_auto_zoomObject

Calculate some numbers for cropping operation with autozoom



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 81

def calculate_for_crop_with_auto_zoom
  point_min = self.class.point_on_image(@zoom, [@lat_min, @lon_min])
  point_max = self.class.point_on_image(@zoom, [@lat_max, @lon_max])
  @bitmap_point_x_min = (point_min[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_min[:pixel_offset][0]
  @bitmap_point_x_max = (point_max[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_max[:pixel_offset][0]
  @bitmap_point_y_max = (point_min[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_min[:pixel_offset][1]
  @bitmap_point_y_min = (point_max[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_max[:pixel_offset][1]

  bitmap_x_center = (@bitmap_point_x_min + @bitmap_point_x_max) / 2
  bitmap_y_center = (@bitmap_point_y_min + @bitmap_point_y_max) / 2

  @r.set_crop_fixed(bitmap_x_center, bitmap_y_center, @fixed_width, @fixed_height)
end

#download_and_join_tilesObject

Do everything



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 100

def download_and_join_tiles
  self.class.logger.info "Output image dimension #{@full_image_x.to_s.red} x #{@full_image_y.to_s.red}"
  @r.new_image

  # {:x, :y, :blob}
  @images = Array.new


  @tile_x_range.each do |x|
    @tile_y_range.each do |y|
      url = self.class.url(@zoom, [x, y])

      # blob time
      unless simulate_download?
        uri = URI.parse(url)
        response = Net::HTTP.get_response(uri)
        blob = response.body
      else
        blob = @r.blank_tile(TILE_WIDTH, TILE_HEIGHT, x+y)
      end

      @r.add_tile(
        blob,
        (x - @tile_x_range.min) * TILE_WIDTH,
        (y - @tile_y_range.min) * TILE_HEIGHT
      )

      @images << {
        url: url,
        x: x,
        y: y
      }

      self.class.logger.info "processed #{(x - @tile_x_range.min).to_s.red} x #{(y - @tile_y_range.min).to_s.red} (max #{(@tile_x_range.max - @tile_x_range.min).to_s.yellow} x #{(@tile_y_range.max - @tile_y_range.min).to_s.yellow})"
    end
  end

  # sweet, image is joined

  # min/max points used for cropping
  @bitmap_point_x_max = (@full_image_x / 2).round
  @bitmap_point_x_min = (@full_image_x / 2).round
  @bitmap_point_y_max = (@full_image_y / 2).round
  @bitmap_point_y_min = (@full_image_y / 2).round

  # add all coords to the map
  _coords_count = 0
  @layers.each do |layer|
    _coords = layer.coords
    _coords_count += _coords.size
    (1..._coords.size).each do |i|

      lat_from = _coords[i-1][:lat]
      lon_from = _coords[i-1][:lon]

      lat_to = _coords[i][:lat]
      lon_to = _coords[i][:lon]

      point_from = self.class.point_on_image(@zoom, [lat_from, lon_from])
      point_to = self.class.point_on_image(@zoom, [lat_to, lon_to])
      # { osm_title_coord: osm_tile_coord, pixel_offset: [x, y] }

      # first point
      bitmap_xa = (point_from[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_from[:pixel_offset][0]
      bitmap_ya = (point_from[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_from[:pixel_offset][1]
      bitmap_xb = (point_to[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + point_to[:pixel_offset][0]
      bitmap_yb = (point_to[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + point_to[:pixel_offset][1]

      @r.line(
        bitmap_xa, bitmap_ya,
        bitmap_xb, bitmap_yb
      )
    end
  end
  self.class.logger.info "Layers - #{@layers.size.to_s.red} with #{_coords_count.to_s.yellow} coords have been added to image"

  # add points
  @markers.each do |point|
    lat = point[:lat]
    lon = point[:lon]

    p = self.class.point_on_image(@zoom, [lat, lon])
    bitmap_x = (p[:osm_title_coord][0] - @tile_x_range.min) * TILE_WIDTH + p[:pixel_offset][0]
    bitmap_y = (p[:osm_title_coord][1] - @tile_y_range.min) * TILE_HEIGHT + p[:pixel_offset][1]

    point[:x] = bitmap_x
    point[:y] = bitmap_y

    @r.markers << point
  end
  self.class.logger.info "Markers - #{@markers.size.to_s.red} have been added to image"

end

#expand_mapObject



95
96
97
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 95

def expand_map
  # TODO expand min and max ranges
end

#initial_calculationsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gpx2png/calculations/osm_instance_methods.rb', line 15

def initial_calculations
  calculate_minmax_latlon

  # auto zoom must be here
  # drawing must fit into fixed resolution
  # map must be bigger than fixed resolution
  if @fixed_width and @fixed_height
    @new_zoom = self.class.calc_zoom(
      @lat_min, @lat_max,
      @lon_min, @lon_max,
      @fixed_width, @fixed_height
    )
    self.class.logger.debug "Calculated new zoom #{@new_zoom.to_s.red} (was #{@zoom.to_s.red})"
    @zoom = @new_zoom
  end

  @border_tiles = [
    self.class.convert(@zoom, [@lat_min, @lon_min]),
    self.class.convert(@zoom, [@lat_max, @lon_max])
  ]

  @tile_x_range = (@border_tiles[0][0])..(@border_tiles[1][0])
  @tile_y_range = (@border_tiles[1][1])..(@border_tiles[0][1])

  # enlarging ranges to fill up map area
  # both sizes are enlarged
  if @fixed_width and @fixed_height
    x_axis_expand_count = ((@fixed_width - (1 + @tile_x_range.max - @tile_x_range.min) * TILE_WIDTH).to_f / (TILE_WIDTH.to_f * 2.0)).ceil
    y_axis_expand_count = ((@fixed_height - (1 + @tile_y_range.max - @tile_y_range.min) * TILE_HEIGHT).to_f / (TILE_HEIGHT.to_f * 2.0)).ceil
    self.class.logger.debug "Expanding #{"X".to_s.blue} tiles from both sides #{x_axis_expand_count.to_s.green}"
    self.class.logger.debug "Expanding #{"Y".to_s.blue} tiles from both sides #{y_axis_expand_count.to_s.green}"
    @tile_x_range = ((@tile_x_range.min - x_axis_expand_count)..(@tile_x_range.max + x_axis_expand_count))
    @tile_y_range = ((@tile_y_range.min - y_axis_expand_count)..(@tile_y_range.max + y_axis_expand_count))
  end

  # new/full image size
  @full_image_x = (1 + @tile_x_range.max - @tile_x_range.min) * TILE_WIDTH
  @full_image_y = (1 + @tile_y_range.max - @tile_y_range.min) * TILE_HEIGHT
  @r.x = @full_image_x
  @r.y = @full_image_y

  if @fixed_width and @fixed_height
    calculate_for_crop_with_auto_zoom
  else
    calculate_for_crop
  end
end