Class: PbfReverseGeocoder::TileCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/pbf_reverse_geocoder/tile_calculator.rb

Constant Summary collapse

ZOOM =

ズームレベル10固定(@geoloniaと同じ、約30km四方)

10

Class Method Summary collapse

Class Method Details

.lng_lat_to_tile(lng, lat) ⇒ Array<Integer>

Google XYZ タイル座標を計算Web Mercator投影を使用

Examples:

TileCalculator.lng_lat_to_tile(139.7671, 35.6812)
#=> [904, 403, 10]

Parameters:

  • lng (Float)

    経度 (-180 ~ 180)

  • lat (Float)

    緯度 (-90 ~ 90)

Returns:

  • (Array<Integer>)
    x, y, zoom


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pbf_reverse_geocoder/tile_calculator.rb', line 22

def self.lng_lat_to_tile(lng, lat)
  # global-mercator の pointToTileFraction ロジック
  n = 2.0**ZOOM
  lat_rad = lat * Math::PI / 180.0
  sin_lat = Math.sin(lat_rad)

  # X座標(経度ベース)
  x = ((lng + 180.0) / 360.0 * n).floor

  # Y座標(緯度ベース、メルカトル投影)
  y = ((0.5 - (0.25 * Math.log((1 + sin_lat) / (1 - sin_lat)) / Math::PI)) * n).floor

  [x, y, ZOOM]
end

.tile_path(x, y, zoom, tiles_dir) ⇒ Pathname

タイルのファイルパスを生成

Examples:

TileCalculator.tile_path(904, 403, 10, '/app/public/tiles')
#=> #<Pathname:/app/public/tiles/10/904/403.pbf>

Parameters:

  • x (Integer)

    タイルX座標

  • y (Integer)

    タイルY座標

  • zoom (Integer)

    ズームレベル

  • tiles_dir (String, Pathname)

    タイルディレクトリのベースパス

Returns:

  • (Pathname)

    PBFファイルのパス



48
49
50
51
# File 'lib/pbf_reverse_geocoder/tile_calculator.rb', line 48

def self.tile_path(x, y, zoom, tiles_dir)
  require 'pathname'
  Pathname.new(tiles_dir).join(zoom.to_s, x.to_s, "#{y}.pbf")
end