refactoring

This commit is contained in:
2023-12-08 07:31:41 +01:00
parent d12c55d740
commit 150c913feb
19 changed files with 1175 additions and 814 deletions

View File

@@ -1,9 +1,17 @@
package utils
import "math"
import (
"encoding/json"
"errors"
"fmt"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
"math"
"net/http"
"strings"
)
func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
// Radius of the Earth in kilometers
R := 6371.0
lat1 = lat1 * math.Pi / 180
@@ -20,3 +28,51 @@ func Haversine(lat1, lon1, lat2, lon2 float64) float64 {
distance := R * c
return distance
}
func CalculateDurationBetweenFeatures(feature1, feature2 *geojson.Feature) (int64, error) {
coords1 := feature1.Point()
coords2 := feature2.Point()
v := viper.New()
v.SetConfigName("config")
v.AddConfigPath(".")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
_ = v.ReadInConfig()
routingBaseUrl := v.GetString("routing.valhalla.base_url")
url := fmt.Sprintf("%sroute?json=%s", routingBaseUrl, createJSONROUTERequest(coords1.X(), coords1.Y(), coords2.X(), coords2.Y()))
response, err := http.Get(url)
if err != nil {
return 0, errors.New("routing service error")
}
var result map[string]interface{}
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&result); err != nil {
return 0, errors.New("routing response decoding error")
}
trip, ok := result["trip"].(map[string]interface{})
if !ok {
return 0, errors.New("routing response decoding error")
}
summary, ok := trip["summary"].(map[string]interface{})
if !ok {
return 0, errors.New("routing response decoding error")
}
duration, ok := summary["time"].(float64)
if !ok {
return 0, errors.New("routing response decoding error")
}
return int64(duration), nil
}
func createJSONROUTERequest(lat1, lon1, lat2, lon2 float64) string {
request := map[string]interface{}{
"locations": []map[string]float64{
{"lat": lat1, "lon": lon1},
{"lat": lat2, "lon": lon2},
},
"costing": "auto",
}
jsonRequest, _ := json.Marshal(request)
return string(jsonRequest)
}