Skip to content

Commit

Permalink
Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
HarutakaMatsumoto committed Feb 3, 2025
1 parent f3bfd48 commit 59eb499
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
18 changes: 9 additions & 9 deletions geodetic_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewGeodeticBoxFromConvexHull(convexHull []*coordinates.Geodetic, clearance
geocentricMax := coordinates.GeocentricFromGeodetic(geodeticMax)
localMin := localFromGeocentric(geocentricMin)
localMax := localFromGeocentric(geocentricMax)

localMin[0] -= clearance
localMin[1] -= clearance
localMin[2] -= clearance
Expand All @@ -70,11 +70,11 @@ func NewGeodeticBoxFromSpatialIDBox(spatialIDBox SpatialIDBox) *GeodeticBox {

max := float64(int(1) << spatialIDBox.GetMin().GetZ())

*box.Min.Longitude() = 360.0 * float64(spatialIDBox.GetMin().GetX()) / max - 180.0
*box.Max.Longitude() = 360.0 * float64(spatialIDBox.GetMax().GetX()+1) / max - 180.0
*box.Min.Longitude() = 360.0*float64(spatialIDBox.GetMin().GetX())/max - 180.0
*box.Max.Longitude() = 360.0*float64(spatialIDBox.GetMax().GetX()+1)/max - 180.0

*box.Min.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi * (1.0 - 2.0*float64(spatialIDBox.GetMin().GetY())/max)))
*box.Max.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi * (1.0 - 2.0*float64(spatialIDBox.GetMax().GetY()+1)/max)))
*box.Min.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi*(1.0-2.0*float64(spatialIDBox.GetMin().GetY())/max)))
*box.Max.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi*(1.0-2.0*float64(spatialIDBox.GetMax().GetY()+1)/max)))

altitudeResolution := float64(int(1) << (SpatialIDZBaseExponent - spatialIDBox.GetMin().GetZ()))

Expand All @@ -89,11 +89,11 @@ func NewGeodeticBoxFromTileXYZBox(TileXYZBox TileXYZBox) *GeodeticBox {

quadMax := float64(int(1) << TileXYZBox.GetMin().GetQuadkeyZoomLevel())

*box.Min.Longitude() = 360.0 * float64(TileXYZBox.GetMin().GetX()) / quadMax - 180.0
*box.Max.Longitude() = 360.0 * float64(TileXYZBox.GetMax().GetX()+1) / quadMax - 180.0
*box.Min.Longitude() = 360.0*float64(TileXYZBox.GetMin().GetX())/quadMax - 180.0
*box.Max.Longitude() = 360.0*float64(TileXYZBox.GetMax().GetX()+1)/quadMax - 180.0

*box.Min.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi * (1.0 - 2.0*float64(TileXYZBox.GetMin().GetY())/quadMax)))
*box.Max.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi * (1.0 - 2.0*float64(TileXYZBox.GetMax().GetY()+1)/quadMax)))
*box.Min.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi*(1.0-2.0*float64(TileXYZBox.GetMin().GetY())/quadMax)))
*box.Max.Latitude() = mathematics.RadianPerDegree * math.Atan(math.Sinh(math.Pi*(1.0-2.0*float64(TileXYZBox.GetMax().GetY()+1)/quadMax)))

altitudeResolution := float64(int(1) << (TileXYZZBaseExponent - TileXYZBox.GetMin().GetAltitudekeyZoomLevel()))

Expand Down
21 changes: 12 additions & 9 deletions spatial_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,17 @@ func NewSpatialID(
}

func (id *SpatialID) SetX(x int64) {
id.x = x%(1 << id.GetZ())
limit := int64(1 << id.GetZ())

id.x = x%limit
if id.x < 0 {
id.x += 1 << id.GetZ()
id.x += limit
}
}

func (id *SpatialID) SetY(y int64) {
max := int64(1 << id.GetZ()) - 1
limit := int64(1 << id.GetZ())
max := limit - 1
min := int64(0)

if y > max {
Expand All @@ -223,9 +226,9 @@ func (id *SpatialID) SetY(y int64) {
}

func (id *SpatialID) SetF(f int64) {
max := int64(1 << id.GetZ())
min := -max
max -= 1
limit := int64(1 << id.GetZ())
max := limit - 1
min := -limit

if f > max {
id.f = max
Expand Down Expand Up @@ -280,8 +283,8 @@ func (id SpatialID) NewMinChild(number int8) (*SpatialID, error) {
func (id SpatialID) NewMaxChild(number int8) (*SpatialID, error) {
return NewSpatialID(
id.GetZ()+number,
(id.GetF() << number) + (1 << number) - 1,
(id.GetX() << number) + (1 << number) - 1,
(id.GetY() << number) + (1 << number) - 1,
(id.GetF() + 1) << number - 1,
(id.GetX() + 1) << number - 1,
(id.GetY() + 1) << number - 1,
)
}
6 changes: 3 additions & 3 deletions spatial_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func NewSpatialKeyFromSpatialID(spatialID SpatialID) *SpatialKey {
}

for i := 0; i < int(spatialID.GetZ()); i += 1 {
spatialKey.SetBit(&spatialKey.Int, 3*i+1, uint(spatialID.GetF()) & (1 << i))
spatialKey.SetBit(&spatialKey.Int, 3*i+2, uint(spatialID.GetX()) & (1 << i))
spatialKey.SetBit(&spatialKey.Int, 3*i+3, uint(spatialID.GetY()) & (1 << i))
spatialKey.SetBit(&spatialKey.Int, 3*i+1, uint(spatialID.GetF())&(1<<i))
spatialKey.SetBit(&spatialKey.Int, 3*i+2, uint(spatialID.GetX())&(1<<i))
spatialKey.SetBit(&spatialKey.Int, 3*i+3, uint(spatialID.GetY())&(1<<i))
}
return spatialKey
}
49 changes: 27 additions & 22 deletions tile_xyz.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ import (

const MaxQuadkeyZoomLevel = 35
const MaxAltitudekeyZoomLevel = 35

var TileXYZZBaseExponent int8 = 14
var TileXYZZBaseOffset int64 = 512

type TileXYZ struct {
quadkeyZoomLevel int8
quadkeyZoomLevel int8
altitudekeyZoomLevel int8
x int64
y int64
z int64
x int64
y int64
z int64
}

func NewTileXYZFromGeodetic(geodetic coordinates.Geodetic, quadkeyZoomLevel int8, altitudeZoomLevel int8) (*TileXYZ, error) {
quadMax := float64(int(1) << quadkeyZoomLevel)

// 経度方向のインデックスの計算
x := int64(math.Floor(quadMax * math.Mod(*geodetic.Longitude() + 180.0, 360.0)))
x := int64(math.Floor(quadMax * math.Mod(*geodetic.Longitude()+180.0, 360.0)))

radianLatitude := mathematics.RadianPerDegree * *geodetic.Latitude()

Expand All @@ -36,17 +37,17 @@ func NewTileXYZFromGeodetic(geodetic coordinates.Geodetic, quadkeyZoomLevel int8
altitudeResolution := float64(common.CalculateArithmeticShift(1, int64(TileXYZZBaseExponent-altitudeZoomLevel)))

// 垂直方向の位置を計算する
z := int64(math.Floor(*geodetic.Altitude() / altitudeResolution))+TileXYZZBaseOffset
z := int64(math.Floor(*geodetic.Altitude()/altitudeResolution)) + TileXYZZBaseOffset

return NewTileXYZ(quadkeyZoomLevel, altitudeZoomLevel, x, y, z)
}

func NewTileXYZ(
quadkeyZoomLevel int8,
altitudekeyZoomLevel int8,
x int64,
y int64,
z int64,
x int64,
y int64,
z int64,
) (*TileXYZ, error) {
tile := &TileXYZ{}

Expand All @@ -68,14 +69,17 @@ func NewTileXYZ(
}

func (tile *TileXYZ) SetX(x int64) {
tile.x = x%(1 << tile.GetQuadkeyZoomLevel())
limit := int64(1 << tile.GetQuadkeyZoomLevel())

tile.x = x % limit
if tile.x < 0 {
tile.x += 1 << tile.GetQuadkeyZoomLevel()
tile.x += limit
}
}

func (tile *TileXYZ) SetY(y int64) {
max := int64(1 << tile.GetQuadkeyZoomLevel()) - 1
limit := int64(1 << tile.GetQuadkeyZoomLevel())
max := limit - 1
min := int64(0)

if y > max {
Expand All @@ -88,7 +92,8 @@ func (tile *TileXYZ) SetY(y int64) {
}

func (tile *TileXYZ) SetZ(z int64) {
max := int64(1 << tile.GetAltitudekeyZoomLevel()) - 1
limit := int64(1 << tile.GetAltitudekeyZoomLevel())
max := limit - 1
min := int64(0)

if z > max {
Expand Down Expand Up @@ -124,28 +129,28 @@ func (tile TileXYZ) NewParent(quadNumber, altitudeNumber int8) (*TileXYZ, error)
return NewTileXYZ(
tile.GetQuadkeyZoomLevel()-quadNumber,
tile.GetAltitudekeyZoomLevel()-altitudeNumber,
tile.GetX() >> quadNumber,
tile.GetY() >> quadNumber,
tile.GetZ() >> altitudeNumber,
tile.GetX()>>quadNumber,
tile.GetY()>>quadNumber,
tile.GetZ()>>altitudeNumber,
)
}

func (tile TileXYZ) NewMinChild(quadNumber, altitudeNumber int8) (*TileXYZ, error) {
return NewTileXYZ(
tile.GetQuadkeyZoomLevel()+quadNumber,
tile.GetAltitudekeyZoomLevel()+altitudeNumber,
tile.GetX() << quadNumber,
tile.GetY() << quadNumber,
tile.GetZ() << altitudeNumber,
tile.GetX()<<quadNumber,
tile.GetY()<<quadNumber,
tile.GetZ()<<altitudeNumber,
)
}

func (tile TileXYZ) NewMaxChild(quadNumber, altitudeNumber int8) (*TileXYZ, error) {
return NewTileXYZ(
tile.GetQuadkeyZoomLevel()+quadNumber,
tile.GetAltitudekeyZoomLevel()+altitudeNumber,
(tile.GetX() << quadNumber) + (1 << quadNumber) - 1,
(tile.GetY() << quadNumber) + (1 << quadNumber) - 1,
(tile.GetZ() << altitudeNumber) + (1 << altitudeNumber) - 1,
(tile.GetX()+1)<<quadNumber-1,
(tile.GetY()+1)<<quadNumber-1,
(tile.GetZ()+1)<<altitudeNumber-1,
)
}
6 changes: 3 additions & 3 deletions tile_xyz_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewTileXYZBoxFromGeodeticBox(geodeticBox GeodeticBox, quadkeyZoomLevel int8
}

func (box *TileXYZBox) AddZoomLevel(quadDelta, altitudeDelta int8) error {
if quadDelta < 0 || altitudeDelta < 0 {
if quadDelta < 0 && altitudeDelta < 0 {
newMin, error := box.min.NewParent(-quadDelta, -altitudeDelta)
if error != nil {
return error
Expand All @@ -78,7 +78,7 @@ func (box *TileXYZBox) AddZoomLevel(quadDelta, altitudeDelta int8) error {
}

box.max = *newMax
} else if quadDelta > 0 || altitudeDelta > 0 {
} else if quadDelta > 0 && altitudeDelta > 0 {
newMin, error := box.min.NewMinChild(quadDelta, altitudeDelta)
if error != nil {
return error
Expand All @@ -94,7 +94,7 @@ func (box *TileXYZBox) AddZoomLevel(quadDelta, altitudeDelta int8) error {
box.max = *newMax
}

return nil
return errors.NewSpatialIdError(errors.InputValueErrorCode, "")
}

func (box TileXYZBox) GetMin() TileXYZ {
Expand Down

0 comments on commit 59eb499

Please sign in to comment.