diff --git a/valhalla.go b/valhalla.go index 1db7754..a8ab452 100644 --- a/valhalla.go +++ b/valhalla.go @@ -1,258 +1,52 @@ package valhalla -// #cgo LDFLAGS: -L/usr/local/lib/ -lvalhalla-go -// #include -// #include -// #include "./bindings/valhalla_go.h" -import "C" - -import ( - "errors" - "unsafe" - - "github.com/rs/zerolog/log" -) - -type Actor struct { - ptr unsafe.Pointer +type ValhallaHandler struct { + Actor *Actor } -func NewActorFromFile(configPath string) (*Actor, error) { - var isError uint8 = 0 - cs := C.CString(configPath) - resp := C.actor_init_from_file(cs, (*C.char)(unsafe.Pointer(&isError))) - C.free(unsafe.Pointer(cs)) - switch isError { - case 0: - return &Actor{ptr: unsafe.Pointer(resp)}, nil - case 1: - err := C.GoString((*C.char)(resp)) - C.free(unsafe.Pointer(resp)) - return nil, errors.New(err) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return nil, errors.New("Invalid error code from valhalla C binding") +func NewValhallaHandler() (*ValhallaHandler, error) { + actor, err := NewActorFromConfig(DefaultConfig()) + if err != nil { + return nil, err } + + return &ValhallaHandler{ + Actor: actor, + }, nil } -func NewActorFromConfig(config *Config) (*Actor, error) { - var isError uint8 = 0 - cs := C.CString(config.String()) - resp := C.actor_init_from_config(cs, (*C.char)(unsafe.Pointer(&isError))) - C.free(unsafe.Pointer(cs)) - switch isError { - case 0: - return &Actor{ptr: unsafe.Pointer(resp)}, nil - case 1: - err := C.GoString((*C.char)(resp)) - C.free(unsafe.Pointer(resp)) - return nil, errors.New(err) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return nil, errors.New("Invalid error code from valhalla C binding") - } +func (h *ValhallaHandler) Route(input ValhallaRouteInput) (*ValhallaRouteResponse, error) { + return nil, nil } -func (actor *Actor) Centroid(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_centroid((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } +type ValhallaRouteInput struct { + Locations []ValhallaLocation `json:"locations"` + Costing *string `json:"costing,omitempty"` + CostingOptions *ValhallaCostingOptions `json:"costing_options,omitempty"` + Language *string `json:"language,omitempty"` } -func (actor *Actor) Expansion(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_expansion((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } +type ValhallaRouteResponse struct{} + +type ValhallaLocation struct { + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + Type *string `json:"type,omitempty"` + Heading *string `json:"heading,omitempty"` + HeadingTolerance *string `json:"heading_tolerance,omitempty"` + Street *string `json:"street,omitempty"` + Radius *int64 `json:"radius,omitempty"` + RankCandidates *bool `json:"rank_candidates,omitempty"` + PreferedSide *string `json:"prefered_side"` + // DisplayLat + // DisplayLon + // SearchCutoff + // NodeSnapTolerance + // StreetSideTolerance + // StreetSideMaxDistance + // StreetSideCutoff + // SearchFilters + // PreferredLayer } -func (actor *Actor) Height(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_height((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) Isochrone(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_isochrone((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) Locate(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_locate((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) Matrix(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_matrix((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) OptimizedRoute(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_optimized_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) Route(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) Status(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_status((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) TraceAttributes(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_trace_attributes((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) TraceRoute(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_trace_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} - -func (actor *Actor) TransitAvailable(request string) (string, error) { - var isError uint8 = 0 - cs := C.CString(request) - cresp := C.actor_transit_available((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) - resp := C.GoString(cresp) - C.free(unsafe.Pointer(cresp)) - switch isError { - case 0: - return resp, nil - case 1: - return "", errors.New(resp) - default: - log.Panic().Msg("Invalid error code from valhalla C binding") - return "", errors.New("Invalid error code from valhalla C binding") - } -} +type ValhallaCostingOptions struct{} diff --git a/valhalla_actor.go b/valhalla_actor.go new file mode 100644 index 0000000..9b5aef7 --- /dev/null +++ b/valhalla_actor.go @@ -0,0 +1,258 @@ +package valhalla + +// #cgo LDFLAGS: -L/usr/local/lib/ -llibvalhalla-go +// #include +// #include +// #include "./bindings/valhalla_go.h" +import "C" + +import ( + "errors" + "unsafe" + + "github.com/rs/zerolog/log" +) + +type Actor struct { + ptr unsafe.Pointer +} + +func NewActorFromFile(configPath string) (*Actor, error) { + var isError uint8 = 0 + cs := C.CString(configPath) + resp := C.actor_init_from_file(cs, (*C.char)(unsafe.Pointer(&isError))) + C.free(unsafe.Pointer(cs)) + switch isError { + case 0: + return &Actor{ptr: unsafe.Pointer(resp)}, nil + case 1: + err := C.GoString((*C.char)(resp)) + C.free(unsafe.Pointer(resp)) + return nil, errors.New(err) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return nil, errors.New("Invalid error code from valhalla C binding") + } +} + +func NewActorFromConfig(config *Config) (*Actor, error) { + var isError uint8 = 0 + cs := C.CString(config.String()) + resp := C.actor_init_from_config(cs, (*C.char)(unsafe.Pointer(&isError))) + C.free(unsafe.Pointer(cs)) + switch isError { + case 0: + return &Actor{ptr: unsafe.Pointer(resp)}, nil + case 1: + err := C.GoString((*C.char)(resp)) + C.free(unsafe.Pointer(resp)) + return nil, errors.New(err) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return nil, errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Centroid(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_centroid((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Expansion(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_expansion((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Height(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_height((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Isochrone(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_isochrone((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Locate(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_locate((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Matrix(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_matrix((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) OptimizedRoute(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_optimized_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Route(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) Status(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_status((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) TraceAttributes(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_trace_attributes((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) TraceRoute(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_trace_route((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +} + +func (actor *Actor) TransitAvailable(request string) (string, error) { + var isError uint8 = 0 + cs := C.CString(request) + cresp := C.actor_transit_available((C.Actor)(actor.ptr), cs, (*C.char)(unsafe.Pointer(&isError))) + resp := C.GoString(cresp) + C.free(unsafe.Pointer(cresp)) + switch isError { + case 0: + return resp, nil + case 1: + return "", errors.New(resp) + default: + log.Panic().Msg("Invalid error code from valhalla C binding") + return "", errors.New("Invalid error code from valhalla C binding") + } +}