Module: MapProject

Defined in:
lib/map_project.rb,
lib/map_project/version.rb,
lib/map_project/map_project.rb

Defined Under Namespace

Classes: MapProject

Constant Summary collapse

LN2 =
0.6931471805599453.freeze
WORLD_PX =
256.freeze
ZOOM_MAX =
21.freeze
VERSION =
"0.0.3"
TILE_SIZE =
256.freeze
FACTOR_TO_RAD =
Rational(Math::PI, 180).freeze
FACTOR_TO_DEG =
Rational(180.0, Math::PI).freeze
PIXEL_PER_DEG =
Rational(TILE_SIZE, 360).freeze
PIXEL_PER_RAD =
Rational(TILE_SIZE, (2 * Math::PI)).freeze
ZERO_ZERO_PX =
[TILE_SIZE / 2, TILE_SIZE / 2].freeze
GOOGLE_MAPS_MAX_LAT =
85.05115.freeze
GOOGLE_MAPS_MAX_LONG =
180.freeze

Instance Method Summary collapse

Instance Method Details

#get_bounds_zoom_level(bounds, viewport_w, viewport_h) ⇒ Object

Get the zoom level for a viewport given latlng boundary



8
9
10
11
12
13
14
15
16
17
# File 'lib/map_project.rb', line 8

def get_bounds_zoom_level(bounds, viewport_w, viewport_h)
  ne = bounds[:ne]
  sw = bounds[:sw]
  lat_fraction = (lat_rad(ne[0]) - lat_rad(sw[0])) / Math::PI
  lng_diff = ne[1] - sw[1]
  lng_fraction = ((lng_diff < 0) ? (lng_diff + 360) : lng_diff) / 360
  lat_zoom = zoom(viewport_h, WORLD_PX, lat_fraction)
  lng_zoom = zoom(viewport_w, WORLD_PX, lng_fraction)
  [[lat_zoom, lng_zoom].min, ZOOM_MAX].min
end

#lat_rad(lat) ⇒ Object



19
20
21
22
23
# File 'lib/map_project.rb', line 19

def lat_rad(lat)
  sin = Math.sin(Rational(lat * Math::PI, 180))
  rad_x2 = Math.log((1 + sin) / (1 - sin)) / 2
  [[rad_x2, Math::PI].min, -Math::PI].max / 2
end

#zoom(map_px, world_px, fraction) ⇒ Object



25
26
27
# File 'lib/map_project.rb', line 25

def zoom(map_px, world_px, fraction)
  (Math.log(Rational(map_px, world_px) / fraction) / LN2).floor
end