silvermobi/handler/geo.go

68 lines
1.5 KiB
Go

package handler
import (
"fmt"
"git.coopgo.io/coopgo-platform/routing-service"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
)
func (h *SilverMobiHandler) GeoAutocomplete(text string) (*geojson.FeatureCollection, error) {
result, err := h.Services.Geocoder.Autocomplete(text)
if err != nil {
return nil, err
}
return result, nil
}
func (h *SilverMobiHandler) GeoRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
var (
routeLocations []orb.Point
featuresType string
)
for _, f := range locations.Features {
ft := f.Geometry.GeoJSONType()
if ft != featuresType {
return nil, fmt.Errorf("mixing different types of geometries in"+
" the feature collection is not allowed : %s and %s found", featuresType, ft)
}
featuresType = ft
if featuresType == "Point" {
if point, ok := f.Geometry.(orb.Point); ok {
routeLocations = append(routeLocations, point)
} else {
return nil, fmt.Errorf("invalid geometry type for point")
}
} else {
return nil, fmt.Errorf("feature type %s not supported", featuresType)
}
}
if route, err = h.Services.Routing.Route(routeLocations); err != nil {
return nil, err
}
return route, nil
}
func (h *SilverMobiHandler) GeoReturnRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
loc := locations
reverse(loc.Features)
return h.GeoRoute(loc)
}
func reverse[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}