silvermobi/handler/geo.go

58 lines
1.4 KiB
Go

package handler
import (
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"google.golang.org/genproto/googleapis/maps/routing/v2"
)
func (h *SilvermobiHandler) GeoAutocomplete(text string, lat, lon float64) (*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) {
route_locations := []orb.Point{}
features_type := ""
for _, f := range locations.Features {
ft := f.Geometry.GeoJSONType()
if features_type != "" && ft != features_type {
return nil, fmt.Errorf("mixing different types of geometries in the feature collection is not allowed : %s and %s found", features_type, ft)
}
features_type = ft
if features_type == "Point" {
if point, ok := f.Geometry.(orb.Point); ok {
route_locations = append(route_locations, point)
}
} else {
return nil, fmt.Errorf("feature type %s not supported", features_type)
}
return nil, err
}
return route, nil
}
func (h *SilvermobiHandler) GeoReturnRoute(locations geojson.FeatureCollection) (route *routing.Route, err error) {
loc := locations
route.Polyline.String()
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]
}
}