github.com/pavlo67/common@v0.5.3/common/geolib/tile.go (about)

     1  package geolib
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/pavlo67/common/common/mathlib"
     7  )
     8  
     9  func DPM(lat Degrees, zoom int) float64 {
    10  
    11  	n := math.Pow(2, float64(zoom))
    12  	mpd := 156543.03 * math.Cos(float64(lat)*math.Pi/180) / n // resolution: meters per dot (mpd)
    13  
    14  	if mpd <= mathlib.Eps {
    15  		return math.Inf(1)
    16  	}
    17  
    18  	return 1 / mpd
    19  }
    20  
    21  type Tile struct {
    22  	X, Y int
    23  	Zoom int
    24  }
    25  
    26  // https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
    27  // left-top corner of the tile
    28  
    29  func (tile Tile) LeftTop() Point {
    30  	n := math.Pow(2, float64(tile.Zoom))
    31  	latAngle := math.Atan(math.Sinh(math.Pi * (1 - 2*float64(tile.Y)/n)))
    32  
    33  	return Point{
    34  		Lat: Degrees(latAngle * 180 / math.Pi),
    35  		Lon: Degrees(float64(tile.X)/n*360 - 180),
    36  	}
    37  }
    38  
    39  func (tile Tile) Center() Point {
    40  	tilesN := int(math.Round(math.Pow(2, float64(tile.Zoom))))
    41  	leftTop, rightBottom := tile.LeftTop(), Tile{(tile.X + 1) % tilesN, (tile.Y + 1) % tilesN, tile.Zoom}.LeftTop()
    42  
    43  	return Point{
    44  		Lat: 0.5 * (leftTop.Lat + rightBottom.Lat),
    45  		Lon: 0.5 * (leftTop.Lon + rightBottom.Lon),
    46  	}
    47  }