idfm-api/api/stations.go

112 lines
2.9 KiB
Go
Raw Normal View History

2022-10-21 21:54:17 +00:00
package api
import (
"encoding/json"
"io/ioutil"
2024-07-26 10:47:08 +00:00
"log"
2022-10-21 21:54:17 +00:00
"net/http"
2024-07-26 10:47:08 +00:00
"os"
2022-10-22 01:12:59 +00:00
"strings"
"unicode"
2022-10-21 21:54:17 +00:00
"github.com/gin-gonic/gin"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
2022-10-21 21:54:17 +00:00
)
type IDFMStation struct {
2024-07-26 10:47:08 +00:00
DatasetID string `json:"datasetid"`
RecordIDs string `json:"recordid"`
Fields struct {
Id string `json:"id"`
PointGeo []float64 `json:"pointgeo"`
StopId string `json:"stop_id"`
StopName string `json:"stop_name"`
OperatorName string `json:"operatorname"`
NomCommune string `json:"nom_commune"`
RouteLongName string `json:"route_long_name"`
StopLat string `json:"stop_lat"`
StopLon string `json:"stop_lon"`
CodeINSEE string `json:"code_insee"`
} `json:"fields"`
Geometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
} `json:"geometry"`
}
func (s *IDFMStation) ComputeSlug() string {
isMn := func(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
2024-07-26 10:47:08 +00:00
r, _ := ioutil.ReadAll(transform.NewReader(strings.NewReader(s.Fields.StopName), t))
return strings.ToLower(strings.Replace(strings.Replace(strings.Replace(string(r), " ", "+", -1), "'", "+", -1), "-", "+", -1))
2022-10-21 21:54:17 +00:00
}
type PGStation struct {
2024-07-26 10:47:08 +00:00
Id string `json:"slug"`
2022-10-21 21:54:17 +00:00
Name string `json:"name"`
}
var IDFMStations []IDFMStation
2024-07-26 10:47:08 +00:00
func init() {
fd, err := os.Open("arrets-lignes.json")
2022-10-21 21:54:17 +00:00
if err != nil {
2024-07-26 10:47:08 +00:00
log.Fatal("Unable to open `arrets-lignes.json`:", err.Error())
2022-10-21 21:54:17 +00:00
}
2024-07-26 10:47:08 +00:00
defer fd.Close()
2022-10-21 21:54:17 +00:00
2024-07-26 10:47:08 +00:00
dec := json.NewDecoder(fd)
if err = dec.Decode(&IDFMStations); err != nil {
log.Fatal("Unable to decode `arrets-lignes.json`:", err.Error())
2022-10-21 21:54:17 +00:00
}
}
2024-07-26 10:47:08 +00:00
func searchStation(code, station string) ([]string, error) {
code = "IDFM:" + strings.TrimPrefix(code, "line:IDFM:")
var res []string
for _, st := range IDFMStations {
if st.Fields.Id == code && st.ComputeSlug() == station {
if strings.HasPrefix(st.Fields.StopId, "IDFM:monomodalStopPlace:") {
res = append(res, "STIF:StopArea:SP:"+strings.TrimPrefix(strings.TrimPrefix(st.Fields.StopId, "IDFM:"), "monomodalStopPlace:")+":")
} else {
res = append(res, "STIF:StopPoint:Q:"+strings.TrimPrefix(st.Fields.StopId, "IDFM:")+":")
}
}
}
2024-07-26 10:47:08 +00:00
if len(res) == 0 {
return []string{station}, nil
} else {
return res, nil
}
2022-10-21 21:54:17 +00:00
}
func declareStationsRoutes(router *gin.RouterGroup) {
router.GET("/stations/:type/:code", func(c *gin.Context) {
2024-07-26 10:47:08 +00:00
code := convertLineCode(string(c.Param("code")))
var stations []PGStation
for _, station := range IDFMStations {
if station.Fields.Id == "IDFM:"+code {
pgstation := PGStation{
Id: station.ComputeSlug(),
Name: station.Fields.StopName,
}
stations = append(stations, pgstation)
}
2022-10-21 21:54:17 +00:00
}
c.JSON(http.StatusOK, APIResult(c, map[string][]PGStation{
2024-07-26 10:47:08 +00:00
"stations": stations,
2022-10-21 21:54:17 +00:00
}))
})
}