Skip to content

Commit

Permalink
feat(server): simple plateau datasets API
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Apr 23, 2024
1 parent 036431e commit e2f29e5
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/datacatalog/citygml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type CityGMLFile struct {
URL string `json:"url"`
}

func fetchCityGMLFiles(ctx context.Context, r plateauapi.Repo, id string) (*CityGMLFilesResponse, error) {
func FetchCityGMLFiles(ctx context.Context, r plateauapi.Repo, id string) (*CityGMLFilesResponse, error) {
n, err := r.Node(ctx, plateauapi.CityGMLDatasetIDFrom(plateauapi.AreaCode(id)))
if err != nil {
return nil, err
Expand Down
19 changes: 18 additions & 1 deletion server/datacatalog/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (h *reposHandler) CityGMLFiles(admin bool) echo.HandlerFunc {

adminContext(c, true, admin, admin && isAlpha(c))
ctx := c.Request().Context()
maxlod, err := fetchCityGMLFiles(ctx, merged, cid)
maxlod, err := FetchCityGMLFiles(ctx, merged, cid)
if err != nil {
return err
}
Expand All @@ -103,6 +103,23 @@ func (h *reposHandler) CityGMLFiles(admin bool) echo.HandlerFunc {
}
}

func (h *reposHandler) SimplePlateauDatasetsAPI() echo.HandlerFunc {
return func(c echo.Context) error {
merged, err := h.prepareMergedRepo(c, false)
if err != nil {
return err
}

ctx := c.Request().Context()
res, err := FetchSimplePlateauDatasets(ctx, merged)
if err != nil {
return err
}

return c.JSON(http.StatusOK, res)
}
}

func (h *reposHandler) UpdateCacheHandler(c echo.Context) error {
if h.cacheUpdateKey != "" {
b := struct {
Expand Down
180 changes: 180 additions & 0 deletions server/datacatalog/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package datacatalog

import (
"context"
"fmt"
"strings"

"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/plateauapi"
"github.com/samber/lo"
)

type SimpleDatasetsResponse struct {
Datasets []*SimpleDatasetsResponseDataset `json:"datasets"`
}

type SimpleDatasetsResponseDataset struct {
ID string `json:"id"`
Name string `json:"name"`
Pref string `json:"pref"`
PrefCode string `json:"pref_code"`
City *string `json:"city"`
CityCode *string `json:"city_code"`
Ward *string `json:"ward"`
WardCode *string `json:"ward_code"`
Type string `json:"type"`
TypeCode string `json:"type_en"`
URL string `json:"url"`
Year int `json:"year"`
RegistrationYear int `json:"registration_year"`
Spec string `json:"spec"`
Format string `json:"format"`
LOD *string `json:"lod"`
Texture *bool `json:"texture"`
}

func FetchSimplePlateauDatasets(ctx context.Context, r plateauapi.Repo) (*SimpleDatasetsResponse, error) {
ds, err := r.Datasets(ctx, &plateauapi.DatasetsInput{
IncludeTypes: []string{"plateau"},
})
if err != nil {
return nil, err
}

res := &SimpleDatasetsResponse{}
for _, dr := range ds {
d, _ := dr.(*plateauapi.PlateauDataset)
if d == nil {
continue
}

prefID := d.GetPrefectureID()
if prefID == nil {
continue
}

var prefName, prefCode string
{
node, err := r.Node(ctx, *prefID)
if err != nil {
return nil, fmt.Errorf("failed to fetch prefecture: %w", err)
}
pref, _ := node.(*plateauapi.Prefecture)
if pref != nil {
prefName = pref.GetName()
prefCode = pref.GetCode().String()
}
}

var cityName, cityCode *string
if p := d.GetCityID(); p != nil {
node, err := r.Node(ctx, *p)
if err != nil {
return nil, fmt.Errorf("failed to fetch city: %w", err)
}
city, _ := node.(*plateauapi.City)
if city != nil {
cityName = lo.ToPtr(city.GetName())
cityCode = lo.ToPtr(city.GetCode().String())
}
}

var wardName, wardCode *string
if p := d.GetWardID(); p != nil {
node, err := r.Node(ctx, *p)
if err != nil {
return nil, fmt.Errorf("failed to fetch ward: %w", err)
}
ward, _ := node.(*plateauapi.Ward)
if ward != nil {
wardName = lo.ToPtr(ward.GetName())
wardCode = lo.ToPtr(ward.GetCode().String())
}
}

var typeName, typeCode string
{
node, err := r.Node(ctx, d.GetTypeID())
if err != nil {
return nil, fmt.Errorf("failed to fetch type: %w", err)
}
ty, _ := node.(plateauapi.DatasetType)
if ty != nil {
typeName = ty.GetName()
typeCode = ty.GetCode()
}
}

var spec string
{
node, err := r.Node(ctx, d.PlateauSpecMinorID)
if err != nil {
return nil, fmt.Errorf("failed to fetch spec: %w", err)
}
sp, _ := node.(*plateauapi.PlateauSpecMinor)
if sp != nil {
spec = sp.Version
}
}

common := SimpleDatasetsResponseDataset{
Name: d.GetName(),
Pref: prefName,
PrefCode: prefCode,
City: cityName,
CityCode: cityCode,
Ward: wardName,
WardCode: wardCode,
Type: typeName,
TypeCode: typeCode,
Year: d.GetYear(),
RegistrationYear: d.GetRegisterationYear(),
Spec: spec,
}

for _, di := range d.Items {
f := simpleFormatName(di.GetFormat())
if f == "" {
continue
}

c := common
c.ID = strings.TrimPrefix(string(di.GetID()), "di_")
c.URL = di.GetURL()
c.Format = f
c.Texture = simpleTexture(di.Texture)
if di.Lod != nil {
c.LOD = lo.ToPtr(fmt.Sprintf("%d", *di.Lod))
}

res.Datasets = append(res.Datasets, &c)
}
}

return res, nil
}

func simpleFormatName(f plateauapi.DatasetFormat) string {
switch f {
case plateauapi.DatasetFormatCesium3dtiles:
return "3D Tiles"
case plateauapi.DatasetFormatMvt:
return "MVT"
default:
return ""
}
}

func simpleTexture(f *plateauapi.Texture) *bool {
if f == nil {
return nil
}
switch *f {
case plateauapi.TextureNone:
return lo.ToPtr(false)
case plateauapi.TextureTexture:
return lo.ToPtr(true)
default:
return nil
}
}
5 changes: 4 additions & 1 deletion server/datacatalog/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func echov3(conf Config, g *echo.Group) (func(ctx context.Context) error, error)
)

// GraphQL playground
plateauapig.GET("/:pid/graphql", gqlPlaygroundHandler(conf.PlaygroundEndpoint, false))
plateauapig.GET("/graphql", gqlPlaygroundHandler(conf.PlaygroundEndpoint, false))
plateauapig.GET("/:pid/graphql", gqlPlaygroundHandler(conf.PlaygroundEndpoint, false))
plateauapig.GET("/admin/graphql", gqlPlaygroundHandler(conf.PlaygroundEndpoint, true))
plateauapig.GET("/:pid/admin/graphql", gqlPlaygroundHandler(conf.PlaygroundEndpoint, true))

Expand All @@ -41,6 +41,9 @@ func echov3(conf Config, g *echo.Group) (func(ctx context.Context) error, error)
plateauapig.GET("/admin/citygml/:citygmlid", h.CityGMLFiles(true))
plateauapig.GET("/:pid/admin/citygml/:citygmlid", h.CityGMLFiles(true))

// Simple PLATEAU dataset API
plateauapig.GET("/plateau-datasets", h.SimplePlateauDatasetsAPI())

// warning API
plateauapig.GET("/:pid/warnings", h.WarningHandler)

Expand Down

0 comments on commit e2f29e5

Please sign in to comment.