Skip to content

Commit

Permalink
Merge pull request #40 from trajectoryjp/fix/convert-altitudekey
Browse files Browse the repository at this point in the history
Fixed ConvertZToMinMaxAltitudekey
  • Loading branch information
HarutakaMatsumoto authored Nov 29, 2024
2 parents e00bbc3 + 098abac commit c33ef76
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
8 changes: 4 additions & 4 deletions detector/check_spatial_id_overlap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestCheckSpatialIdsOverlap09(t *testing.T) {
spatialId2: "18/1/58198/25804",
// 期待値
expectValue: false,
expectError: "InputValueError,入力チェックエラー,output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset @spatialId1[0] = 25/16777216/0/3225",
expectError: "InputValueError,入力チェックエラー,outputIndex=33554432 is out of range at outputZoom=25 @spatialId1[0] = 25/16777216/0/3225",
},
{
// 入力空間IDのfインデックスが不正
Expand All @@ -290,7 +290,7 @@ func TestCheckSpatialIdsOverlap09(t *testing.T) {
spatialId2: "25/16777216/0/3225",
// 期待値
expectValue: false,
expectError: "InputValueError,入力チェックエラー,output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset @spatialId2[0] = 25/16777216/0/3225",
expectError: "InputValueError,入力チェックエラー,outputIndex=33554432 is out of range at outputZoom=25 @spatialId2[0] = 25/16777216/0/3225",
},
{
// 入力可能な高度インデックス範囲を超えている(下限より小さい)
Expand All @@ -300,7 +300,7 @@ func TestCheckSpatialIdsOverlap09(t *testing.T) {
// 期待値
expectValue: false,
// 高度変換が負数を許容しないため高度変換エラーになる
expectError: "InputValueError,入力チェックエラー,output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset @spatialId1[0] = 25/-16777217/0/3225",
expectError: "InputValueError,入力チェックエラー,outputIndex=-1 is out of range at outputZoom=25 @spatialId1[0] = 25/-16777217/0/3225",
},
{
// 入力可能な高度インデックス範囲を超えている(下限より小さい)
Expand All @@ -310,7 +310,7 @@ func TestCheckSpatialIdsOverlap09(t *testing.T) {
// 期待値
expectValue: false,
// 高度変換が負数を許容しないため高度変換エラーになる
expectError: "InputValueError,入力チェックエラー,output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset @spatialId2[0] = 25/-16777217/0/3225",
expectError: "InputValueError,入力チェックエラー,outputIndex=-1 is out of range at outputZoom=25 @spatialId2[0] = 25/-16777217/0/3225",
},
}
for _, testCase := range testCases {
Expand Down
48 changes: 23 additions & 25 deletions transform/convert_quadkey_and_Vertical_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,15 @@ func convertVerticallIDToBit(vZoom int64, vIndex int64, outputZoom int64, maxHei
// 出力インデックス不正 :出力altitudekeyが出力ズームレベル(outputZoom)で存在しないインデックス値になった場合。
func ConvertZToMinMaxAltitudekey(inputIndex int64, inputZoom int64, outputZoom int64, zBaseExponent int64, zBaseOffset int64) (minAltitudeKey int64, maxAltitudeKey int64, err error) {

// check that the input index exists in the input system
if !validateIndexExists(inputIndex, inputZoom, true) {
err = errors.NewSpatialIdError(
errors.InputValueErrorCode,
fmt.Sprintf("inputIndex=%v is out of range at inputZoom=%v", inputIndex, inputZoom),
)
return 0, 0, err
}

// determine the upper and lower index bounds to search for matches in height solution space
lowerBound, err := convertZToMinAltitudekey(inputIndex, inputZoom, outputZoom, zBaseExponent, zBaseOffset)
if err != nil {
Expand All @@ -1047,28 +1056,23 @@ func ConvertZToMinMaxAltitudekey(inputIndex int64, inputZoom int64, outputZoom i
}
}

func convertZToMinAltitudekey(inputIndex int64, inputZoom int64, outputZoom int64, zBaseExponent int64, zBaseOffset int64) (int64, error) {

// 1. check that the input index exists in the input system
err, ok := validateIndexExists(inputIndex, inputZoom, true)
if !ok {
return 0, err
}
func convertZToMinAltitudekey(inputIndex int64, inputZoom int64, outputZoom int64, zBaseExponent int64, zBaseOffset int64) (outputIndex int64, err error) {

// 2. Calculate outputIndex
outputIndex := common.CalculateArithmeticShift(inputIndex, -(inputZoom - consts.ZOriginValue))
// Calculate outputIndex
outputIndex = common.CalculateArithmeticShift(inputIndex, -(inputZoom - consts.ZOriginValue))
outputIndex += zBaseOffset
outputIndex = common.CalculateArithmeticShift(outputIndex, (outputZoom - zBaseExponent))

// 3. Check to make sure outputIndex exists in the output system
_, ok = validateIndexExists(outputIndex, outputZoom, false)

if !ok {
return 0, errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
// Check to make sure outputIndex exists in the output system
if !validateIndexExists(outputIndex, outputZoom, false) {
err = errors.NewSpatialIdError(
errors.InputValueErrorCode,
fmt.Sprintf("outputIndex=%v is out of range at outputZoom=%v", outputIndex, outputZoom),
)
return
}

return outputIndex, nil

return
}

// validateIndexExists 指定した(拡張)空間IDインデックスが指定ズームレベルにおいて存在するか確認する
Expand All @@ -1088,13 +1092,7 @@ func convertZToMinAltitudekey(inputIndex int64, inputZoom int64, outputZoom int6
// 戻り値 :
//
// インデックスが存在すればtrue,しなければfalse
//
// 戻り値(エラー) :
//
// 戻り値がfalseの場合、同時に以下の内容のエラーインスタンスが返却される。
// 入力インデックス不正 :inputIndexにそのズームレベル(inputZoom)で存在しないインデックス値が入力されていた場合。
// 出力インデックス不正 :変換後のインデックスが入力ズームレベル(inputZoom)で存在しないインデックス値になった場合。
func validateIndexExists(inputIndex int64, inputZoom int64, minValueIsNegative bool) (error, bool) {
func validateIndexExists(inputIndex int64, inputZoom int64, minValueIsNegative bool) bool {
inputResolution := common.CalculateArithmeticShift(1, inputZoom)

maxInputIndex := inputResolution - 1
Expand All @@ -1106,9 +1104,9 @@ func validateIndexExists(inputIndex int64, inputZoom int64, minValueIsNegative b
}

if inputIndex > maxInputIndex || inputIndex < minInputIndex {
return errors.NewSpatialIdError(errors.InputValueErrorCode, "input index does not exist"), false
return false
}
return nil, true
return true
}

// 高さのbit形式のインデックスを計算する。
Expand Down
39 changes: 34 additions & 5 deletions transform/convert_quadkey_and_Vertical_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ func TestConvertZToMinMaxAltitudekey_4(t *testing.T) {
}

func TestConvertZToMinMaxAltitudekey_5(t *testing.T) {
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-11 is out of range at outputZoom=21")

result, _, error := ConvertZToMinMaxAltitudekey(
100,
Expand All @@ -1631,7 +1631,7 @@ func TestConvertZToMinMaxAltitudekey_5(t *testing.T) {
}

func TestConvertZToMinMaxAltitudekey_6(t *testing.T) {
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-412 is out of range at outputZoom=25")

result, _, error := ConvertZToMinMaxAltitudekey(
100,
Expand Down Expand Up @@ -1684,6 +1684,35 @@ func TestConvertZToMinMaxAltitudekey_9(t *testing.T) {
assertConvertZToMinMaxAltitudekey(t, expectedMin, expectedMax, args)
}

func TestConvertZToMinMaxAltitudekey_10(t *testing.T) {
testCases := []struct {
expectedError error
inputIndex int64
}{
{
errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-1 is out of range at outputZoom=0"),
-1,
},
{
errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=1 is out of range at outputZoom=0"),
0,
},
}

for _, tc := range testCases {
result, _, error := ConvertZToMinMaxAltitudekey(
tc.inputIndex,
0,
0,
consts.ZOriginValue,
consts.ZBaseOffsetForNegativeFIndex,
)
if error != tc.expectedError {
t.Fatal(result, error)
}
}
}

func TestConvertZToMinAltitudekey_1(t *testing.T) {
expected := int64(47)

Expand Down Expand Up @@ -1780,7 +1809,7 @@ func TestConvertZToMinAltitudekey_5(t *testing.T) {
}

func TestConvertZToMinAltitudekey_6(t *testing.T) {
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-1 is out of range at outputZoom=25")

result, error := convertZToMinAltitudekey(
100,
Expand Down Expand Up @@ -1852,7 +1881,7 @@ func TestConvertZToMinAltitudekey_9(t *testing.T) {
}

func TestConvertZToMinAltitudekey_10(t *testing.T) {
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-1 is out of range at outputZoom=25")

result, error := convertZToMinAltitudekey(
-1,
Expand All @@ -1867,7 +1896,7 @@ func TestConvertZToMinAltitudekey_10(t *testing.T) {
}

func TestConvertZToMinAltitudekey_11(t *testing.T) {
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "output index does not exist with given outputZoom, zBaseExponent, and zBaseOffset")
expectedError := errors.NewSpatialIdError(errors.InputValueErrorCode, "outputIndex=-196 is out of range at outputZoom=26")

result, error := convertZToMinAltitudekey(
-100,
Expand Down

0 comments on commit c33ef76

Please sign in to comment.