Places: Add new package pkg/geo/latlng #465 #5080

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-07-01 10:12:54 +02:00
parent ee0f104262
commit 08ae7b0229
3 changed files with 62 additions and 0 deletions

25
pkg/geo/latlng/latlng.go Normal file
View File

@@ -0,0 +1,25 @@
/*
Package latlng provides latitude and longitude functions and constants.
Copyright (c) 2018 - 2025 PhotoPrism UG. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
<https://docs.photoprism.app/license/agpl>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
The AGPL is supplemented by our Trademark and Brand Guidelines,
which describe how our Brand Assets may be used:
<https://www.photoprism.app/trademark>
Feel free to send an email to hello@photoprism.app if you have questions,
want to support our work, or just want to say hello.
Additional information can be found in our Developer Guide:
<https://docs.photoprism.app/developer-guide/>
*/
package latlng

15
pkg/geo/latlng/round.go Normal file
View File

@@ -0,0 +1,15 @@
package latlng
import "math"
var RoundDecimals = float64(10000000)
// Round rounds the given coordinate to six decimal places.
func Round(c float64) float64 {
return math.Round(c*RoundDecimals) / RoundDecimals
}
// RoundCoords rounds the given latitude and longitude to six decimal places.
func RoundCoords(lat, lng float64) (float64, float64) {
return Round(lat), Round(lng)
}

View File

@@ -0,0 +1,22 @@
package latlng
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRound(t *testing.T) {
t.Run("Germany", func(t *testing.T) {
assert.Equal(t, 48.5634483, Round(48.56344833333333))
assert.Equal(t, 8.9968783, Round(8.996878333333333))
})
}
func TestRoundCoords(t *testing.T) {
t.Run("Germany", func(t *testing.T) {
lat, lng := RoundCoords(48.56344833333333, 8.996878333333333)
assert.Equal(t, 48.5634483, lat)
assert.Equal(t, 8.9968783, lng)
})
}