Add tiles management

This commit is contained in:
2023-03-29 12:50:25 +02:00
parent 77c8576254
commit bbc682386a
18 changed files with 559 additions and 64 deletions

View File

@@ -115,6 +115,92 @@ func (s MongoDBStorage) GetUserRegularRoutes(userid string) ([]*geojson.FeatureC
return results, nil
}
func (s MongoDBStorage) GetDriverRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{
"properties.schedule.day": day,
"grid_ids": gridId,
"properties.is_driver": true,
}, findOptions)
if err != nil {
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
return nil, err
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
func (s MongoDBStorage) GetPassengerRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{
"properties.schedule.day": day,
"grid_ids": gridId,
"properties.is_passenger": true,
}, findOptions)
if err != nil {
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
return nil, err
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
// func (s MongoDBStorage) CreatePassengerRegularTrips(trips []*geojson.FeatureCollection) error {
// log.Debug().Msg("Storage - CreatePassengerRegularTrips")

View File

@@ -10,6 +10,8 @@ import (
type Storage interface {
CreateRegularRoutes([]*geojson.FeatureCollection) error
GetUserRegularRoutes(userid string) ([]*geojson.FeatureCollection, error)
GetDriverRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
GetPassengerRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
}
func NewStorage(cfg *viper.Viper) (Storage, error) {