From 9ba9e3c089e8bb398a2810547f3db0be65f34af7 Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 13:38:50 +0200 Subject: [PATCH 1/6] moved CreateProviedsSpecificParameters -> provider --- gpkg/initprovider.go | 4 +- postgis/getapiprovider.go | 80 --------------------------------------- postgis/initprovider.go | 6 +-- provider/util.go | 78 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 84 deletions(-) diff --git a/gpkg/initprovider.go b/gpkg/initprovider.go index fff1bd2..ba25796 100644 --- a/gpkg/initprovider.go +++ b/gpkg/initprovider.go @@ -10,8 +10,9 @@ type GeoPackageProvider struct { CommonProvider provider.CommonProvider GeoPackage GeoPackage CrsMap map[string]string - Api *openapi3.T Config provider.Config + Api *openapi3.T + ApiProcessed *openapi3.T } func NewGeopackageWithCommonProvider(api *openapi3.T, commonProvider provider.CommonProvider, crsMap map[string]string, config provider.Config) *GeoPackageProvider { @@ -25,5 +26,6 @@ func NewGeopackageWithCommonProvider(api *openapi3.T, commonProvider provider.Co func (gp *GeoPackageProvider) Init() (err error) { gp.GeoPackage, err = NewGeoPackage(gp.Config.Datasource.Geopackage.File, gp.Config.Datasource.Geopackage.Fid) + // gp.ApiProcessed = CreateProvidesSpecificParameters(gp.Api, gp.Config.Datasource.Collections) return } diff --git a/postgis/getapiprovider.go b/postgis/getapiprovider.go index 4f33757..63a7340 100644 --- a/postgis/getapiprovider.go +++ b/postgis/getapiprovider.go @@ -1,10 +1,8 @@ package postgis import ( - "fmt" "net/http" "oaf-server/codegen" - "strings" "github.com/getkin/kin-openapi/openapi3" ) @@ -22,84 +20,6 @@ func (pp *PostgisProvider) NewGetApiProvider(r *http.Request) (codegen.Provider, return p, nil } -func CreateProvidesSpecificParameters(provider *PostgisProvider) *openapi3.T { - - api := provider.Api - - copy := &openapi3.T{ - OpenAPI: api.OpenAPI, - Info: api.Info, - Servers: api.Servers, - Paths: make(map[string]*openapi3.PathItem), - Components: api.Components, - Security: api.Security, - ExternalDocs: api.ExternalDocs, - } - - copy.Components.Extensions = nil - - delete(copy.Components.Parameters, "collectionId") - - for k, v := range provider.Api.Paths { - if !strings.Contains(k, "{collectionId}") { - v.Extensions = nil - copy.Paths[k] = v - } - } - - // adjust swagger to accommodate individual parameters - for _, collection := range provider.PostGis.Collections { - for k, v := range provider.Api.Paths { - if strings.Contains(k, "{collectionId}") { - k := strings.Replace(k, "{collectionId}", strings.ToLower(collection.Tablename), 1) - params := openapi3.NewParameters() - paramsQueryExists := false - - for _, p := range v.Get.Parameters { - if strings.Contains(p.Ref, "collectionId") { - continue - } - - if p.Value.Name != "collectionId" { - params = append(params, p) - if p.Value.In == "query" { - paramsQueryExists = true - } - } - } - // only add vendor specific parameters to query params are already allowed - if paramsQueryExists { - for _, specificParam := range collection.VendorSpecificParameters { - sp := openapi3.NewQueryParameter(specificParam) - sp.Description = fmt.Sprintf("Vendor specific parameter : %s", specificParam) - sp.Required = false - sp.Schema = &openapi3.SchemaRef{ - Ref: "", - Value: &openapi3.Schema{ - Type: "object", - }, - } - params = append(params, &openapi3.ParameterRef{ - Ref: "#/components/parameters/" + specificParam, - Value: sp, - }) - - copy.Components.Parameters[specificParam] = &openapi3.ParameterRef{ - Value: sp, - } - } - } - - copy.Paths[k] = v - copy.Paths[k].Get.Parameters = params - copy.Paths[k].Get.Extensions = nil - - } - } - } - return copy -} - func (gap *GetApiProvider) Provide() (interface{}, error) { return gap.data, nil } diff --git a/postgis/initprovider.go b/postgis/initprovider.go index b6d545d..5d6e940 100644 --- a/postgis/initprovider.go +++ b/postgis/initprovider.go @@ -26,8 +26,8 @@ func NewPostgisWithCommonProvider(api *openapi3.T, commonProvider provider.Commo return p } -func (provider *PostgisProvider) Init() (err error) { - provider.PostGis, err = NewPostgis(provider.Config) - provider.ApiProcessed = CreateProvidesSpecificParameters(provider) +func (pg *PostgisProvider) Init() (err error) { + pg.PostGis, err = NewPostgis(pg.Config) + pg.ApiProcessed = provider.CreateProvidesSpecificParameters(pg.Api, pg.PostGis.Collections) return } diff --git a/provider/util.go b/provider/util.go index 3cb1113..ecd7a30 100644 --- a/provider/util.go +++ b/provider/util.go @@ -7,6 +7,8 @@ import ( "oaf-server/codegen" "strconv" "strings" + + "github.com/getkin/kin-openapi/openapi3" ) const ( @@ -273,3 +275,79 @@ func ParseBBox(stringValue string, defaultValue [4]float64) [4]float64 { func UpperFirst(title string) string { return strings.Title(title) } + +func CreateProvidesSpecificParameters(api *openapi3.T, collections []Collection) *openapi3.T { + + copy := &openapi3.T{ + OpenAPI: api.OpenAPI, + Info: api.Info, + Servers: api.Servers, + Paths: make(map[string]*openapi3.PathItem), + Components: api.Components, + Security: api.Security, + ExternalDocs: api.ExternalDocs, + } + + copy.Components.Extensions = nil + + delete(copy.Components.Parameters, "collectionId") + + for k, v := range api.Paths { + if !strings.Contains(k, "{collectionId}") { + v.Extensions = nil + copy.Paths[k] = v + } + } + + // adjust swagger to accommodate individual parameters + for _, collection := range collections { + for k, v := range api.Paths { + if strings.Contains(k, "{collectionId}") { + k := strings.Replace(k, "{collectionId}", strings.ToLower(collection.Tablename), 1) + params := openapi3.NewParameters() + paramsQueryExists := false + + for _, p := range v.Get.Parameters { + if strings.Contains(p.Ref, "collectionId") { + continue + } + + if p.Value.Name != "collectionId" { + params = append(params, p) + if p.Value.In == "query" { + paramsQueryExists = true + } + } + } + // only add vendor specific parameters to query params are already allowed + if paramsQueryExists { + for _, specificParam := range collection.VendorSpecificParameters { + sp := openapi3.NewQueryParameter(specificParam) + sp.Description = fmt.Sprintf("Vendor specific parameter : %s", specificParam) + sp.Required = false + sp.Schema = &openapi3.SchemaRef{ + Ref: "", + Value: &openapi3.Schema{ + Type: "object", + }, + } + params = append(params, &openapi3.ParameterRef{ + Ref: "#/components/parameters/" + specificParam, + Value: sp, + }) + + copy.Components.Parameters[specificParam] = &openapi3.ParameterRef{ + Value: sp, + } + } + } + + copy.Paths[k] = v + copy.Paths[k].Get.Parameters = params + copy.Paths[k].Get.Extensions = nil + + } + } + } + return copy +} From 8594f84bd4fe2301acc141470c9ce49a2b1012e7 Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 14:13:27 +0200 Subject: [PATCH 2/6] renamed Layers -> Collections --- gpkg/describecollectionprovider.go | 2 +- gpkg/getcollectionsprovider.go | 4 ++-- gpkg/getfeatureprovider.go | 2 +- gpkg/getfeaturesprovider.go | 2 +- gpkg/gpkg.go | 12 ++++++------ 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/gpkg/describecollectionprovider.go b/gpkg/describecollectionprovider.go index 5cc019e..893df9f 100644 --- a/gpkg/describecollectionprovider.go +++ b/gpkg/describecollectionprovider.go @@ -20,7 +20,7 @@ func (gp *GeoPackageProvider) NewDescribeCollectionProvider(r *http.Request) (co p := &DescribeCollectionProvider{} - for _, cn := range gp.GeoPackage.Layers { + for _, cn := range gp.GeoPackage.Collections { // maybe convert to map, but not thread safe! if cn.Identifier != collectionId { continue diff --git a/gpkg/getcollectionsprovider.go b/gpkg/getcollectionsprovider.go index cd57d3a..53bf1ae 100644 --- a/gpkg/getcollectionsprovider.go +++ b/gpkg/getcollectionsprovider.go @@ -25,12 +25,12 @@ func (gp *GeoPackageProvider) NewGetCollectionsProvider(r *http.Request) (codege hrefBase := fmt.Sprintf("%s%s", gp.CommonProvider.ServiceEndpoint, path) // /collections links, _ := provider.CreateLinks("collections", hrefBase, "self", ct) csInfo.Links = append(csInfo.Links, links...) - for _, cn := range gp.GeoPackage.Layers { + for _, cn := range gp.GeoPackage.Collections { clinks, _ := provider.CreateLinks("collection "+cn.Identifier, fmt.Sprintf("%s/%s", hrefBase, cn.Identifier), "item", ct) csInfo.Links = append(csInfo.Links, clinks...) } - for _, cn := range gp.GeoPackage.Layers { + for _, cn := range gp.GeoPackage.Collections { crss := make([]string, 0) for _, v := range gp.CrsMap { diff --git a/gpkg/getfeatureprovider.go b/gpkg/getfeatureprovider.go index 845d2ba..b5dec61 100644 --- a/gpkg/getfeatureprovider.go +++ b/gpkg/getfeatureprovider.go @@ -31,7 +31,7 @@ func (gp *GeoPackageProvider) NewGetFeatureProvider(r *http.Request) (codegen.Pr p.contenttype = ct - for _, cn := range gp.GeoPackage.Layers { + for _, cn := range gp.GeoPackage.Collections { // maybe convert to map, but not thread safe! if cn.Identifier != collectionId { continue diff --git a/gpkg/getfeaturesprovider.go b/gpkg/getfeaturesprovider.go index d2e671c..a1f5cf5 100644 --- a/gpkg/getfeaturesprovider.go +++ b/gpkg/getfeaturesprovider.go @@ -34,7 +34,7 @@ func (gp *GeoPackageProvider) NewGetFeaturesProvider(r *http.Request) (codegen.P p := &GetFeaturesProvider{srsid: fmt.Sprintf("EPSG:%d", gp.GeoPackage.SrsId)} p.contenttype = ct - for _, cn := range gp.GeoPackage.Layers { + for _, cn := range gp.GeoPackage.Collections { // maybe convert to map, but not thread safe! if cn.Identifier != collectionId { continue diff --git a/gpkg/gpkg.go b/gpkg/gpkg.go index 28ac941..e853eaf 100644 --- a/gpkg/gpkg.go +++ b/gpkg/gpkg.go @@ -38,7 +38,7 @@ type GeoPackage struct { UserVersion int64 DB *sqlx.DB FeatureIdKey string - Layers []GeoPackageLayer + Collections []GeoPackageLayer DefaultBBox [4]float64 SrsId int64 } @@ -110,8 +110,8 @@ func (gpkg *GeoPackage) Close() error { func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []GeoPackageLayer, err error) { - if gpkg.Layers != nil { - result = gpkg.Layers + if gpkg.Collections != nil { + result = gpkg.Collections err = nil return } @@ -132,7 +132,7 @@ func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []Ge } defer rowsClose(query, rows) - gpkg.Layers = make([]GeoPackageLayer, 0) + gpkg.Collections = make([]GeoPackageLayer, 0) for rows.Next() { if err = ctx.Err(); err != nil { @@ -150,10 +150,10 @@ func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []Ge row.Features = append(row.Features, match[1]) } - gpkg.Layers = append(gpkg.Layers, row) + gpkg.Collections = append(gpkg.Collections, row) } - result = gpkg.Layers + result = gpkg.Collections return } From 71d3fa86bc127968ac2c82a413feada5a2495299 Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 19:48:17 +0200 Subject: [PATCH 3/6] rename srs_id -> srid --- gpkg/getfeatureprovider.go | 2 +- gpkg/getfeaturesprovider.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gpkg/getfeatureprovider.go b/gpkg/getfeatureprovider.go index b5dec61..f9c8a8a 100644 --- a/gpkg/getfeatureprovider.go +++ b/gpkg/getfeatureprovider.go @@ -20,7 +20,7 @@ func (gp *GeoPackageProvider) NewGetFeatureProvider(r *http.Request) (codegen.Pr featureIdParam := featureId bboxParam := gp.GeoPackage.DefaultBBox - p := &GetFeatureProvider{srsid: fmt.Sprintf("EPSG:%d", gp.GeoPackage.SrsId)} + p := &GetFeatureProvider{srsid: fmt.Sprintf("EPSG:%d", gp.GeoPackage.Srid)} path := r.URL.Path ct := r.Header.Get("Content-Type") diff --git a/gpkg/getfeaturesprovider.go b/gpkg/getfeaturesprovider.go index a1f5cf5..c1f2e4c 100644 --- a/gpkg/getfeaturesprovider.go +++ b/gpkg/getfeaturesprovider.go @@ -31,7 +31,7 @@ func (gp *GeoPackageProvider) NewGetFeaturesProvider(r *http.Request) (codegen.P ct = provider.GEOJSONContentType } - p := &GetFeaturesProvider{srsid: fmt.Sprintf("EPSG:%d", gp.GeoPackage.SrsId)} + p := &GetFeaturesProvider{srsid: fmt.Sprintf("EPSG:%d", gp.GeoPackage.Srid)} p.contenttype = ct for _, cn := range gp.GeoPackage.Collections { @@ -81,9 +81,9 @@ func (gp *GeoPackageProvider) NewGetFeaturesProvider(r *http.Request) (codegen.P fcGeoJSON.Links = links - crsUri, ok := gp.CrsMap[fmt.Sprintf("%d", cn.SrsId)] + crsUri, ok := gp.CrsMap[fmt.Sprintf("%d", cn.Srid)] if !ok { - log.Printf("SRS ID: %s, not found", fmt.Sprintf("%d", cn.SrsId)) + log.Printf("SRS ID: %s, not found", fmt.Sprintf("%d", cn.Srid)) crsUri = "" } fcGeoJSON.Crs = crsUri From 7d4a76094d5931bd98b07aa2f1bcd409377cda03 Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 19:48:58 +0200 Subject: [PATCH 4/6] fix OAS3 for Geopackage datasources --- go.mod | 1 + go.sum | 2 ++ gpkg/getapiprovider.go | 9 +----- gpkg/gpkg.go | 64 ++++++++++++++++++++--------------------- gpkg/initprovider.go | 22 +++++++++++++- postgis/initprovider.go | 2 +- provider/util.go | 6 ++-- 7 files changed, 61 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 61c864f..01b7f37 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/getkin/kin-openapi v0.63.0 github.com/go-openapi/swag v0.19.15 // indirect github.com/go-spatial/geom v0.0.0-20210315165355-0e06498b3362 + github.com/imdario/mergo v0.3.12 github.com/jmoiron/sqlx v1.3.4 github.com/lib/pq v1.10.2 github.com/mailru/easyjson v0.7.7 // indirect diff --git a/go.sum b/go.sum index cdedf26..e5235f2 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= +github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w= github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= diff --git a/gpkg/getapiprovider.go b/gpkg/getapiprovider.go index 2600a6d..21a12dd 100644 --- a/gpkg/getapiprovider.go +++ b/gpkg/getapiprovider.go @@ -1,7 +1,6 @@ package gpkg import ( - "log" "net/http" "oaf-server/codegen" @@ -17,13 +16,7 @@ func (gp *GeoPackageProvider) NewGetApiProvider(r *http.Request) (codegen.Provid p := &GetApiProvider{} p.contenttype = r.Header.Get("Content-Type") - var err error - if gp.Api == nil { - log.Printf("Could not get Swagger Specification") - return p, err - } - - p.data = gp.Api + p.data = gp.ApiProcessed return p, nil } diff --git a/gpkg/gpkg.go b/gpkg/gpkg.go index e853eaf..4179713 100644 --- a/gpkg/gpkg.go +++ b/gpkg/gpkg.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "log" + "oaf-server/provider" pc "oaf-server/provider" "os" "regexp" @@ -38,9 +39,9 @@ type GeoPackage struct { UserVersion int64 DB *sqlx.DB FeatureIdKey string - Collections []GeoPackageLayer + Collections []provider.Collection DefaultBBox [4]float64 - SrsId int64 + Srid int64 } func NewGeoPackage(filepath string, featureIdKey string) (GeoPackage, error) { @@ -69,37 +70,25 @@ func NewGeoPackage(filepath string, featureIdKey string) (GeoPackage, error) { applicationId, _ := gpkg.GetApplicationID(ctx, db) version, _ := gpkg.GetVersion(ctx, db) - layers, _ := gpkg.GetLayers(ctx, db) + collections, _ := gpkg.GetCollections(ctx, db) log.Printf("| GEOPACKAGE DETAILS \n") log.Printf("|\n") log.Printf("| FILE: %s, APPLICATION: %s, VERSION: %d", filepath, applicationId, version) log.Printf("|\n") - log.Printf("| NUMBER OF LAYERS: %d", len(layers)) + log.Printf("| NUMBER OF LAYERS: %d", len(collections)) log.Printf("|\n") // determine query bbox - for i, layer := range layers { - log.Printf("| LAYER: %d. ID: %s, SRS_ID: %d, TABLE: %s PK: %s, FEATURES : %v\n", i+1, layer.Identifier, layer.SrsId, layer.Features[0], layer.Features[1], layer.Features[2:]) + for i, collection := range collections { + log.Printf("| LAYER: %d. ID: %s, SRID: %d, TABLE: %s PK: %s, FEATURES : %v\n", i+1, collection.Identifier, collection.Srid, collection.Properties[0], collection.Properties[1], collection.Properties[2:]) if i == 0 { - gpkg.DefaultBBox = [4]float64{layer.MinX, layer.MinY, layer.MaxX, layer.MaxY} - gpkg.SrsId = layer.SrsId - } - if layer.MinX < gpkg.DefaultBBox[0] { - gpkg.DefaultBBox[0] = layer.MinX - } - if layer.MinY < gpkg.DefaultBBox[1] { - gpkg.DefaultBBox[1] = layer.MinY - } - if layer.MaxX > gpkg.DefaultBBox[2] { - gpkg.DefaultBBox[2] = layer.MaxX - } - if layer.MaxY > gpkg.DefaultBBox[3] { - gpkg.DefaultBBox[3] = layer.MaxY + gpkg.DefaultBBox = collection.BBox + gpkg.Srid = int64(collection.Srid) } } log.Printf("| \n") - log.Printf("| BBOX: [%f,%f,%f,%f], SRS_ID:%d", gpkg.DefaultBBox[0], gpkg.DefaultBBox[1], gpkg.DefaultBBox[2], gpkg.DefaultBBox[3], gpkg.SrsId) + log.Printf("| BBOX: [%f,%f,%f,%f], SRS_ID:%d", gpkg.DefaultBBox[0], gpkg.DefaultBBox[1], gpkg.DefaultBBox[2], gpkg.DefaultBBox[3], gpkg.Srid) return *gpkg, nil } @@ -108,7 +97,7 @@ func (gpkg *GeoPackage) Close() error { return gpkg.DB.Close() } -func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []GeoPackageLayer, err error) { +func (gpkg *GeoPackage) GetCollections(ctx context.Context, db *sqlx.DB) (result []provider.Collection, err error) { if gpkg.Collections != nil { result = gpkg.Collections @@ -132,7 +121,7 @@ func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []Ge } defer rowsClose(query, rows) - gpkg.Collections = make([]GeoPackageLayer, 0) + gpkg.Collections = make([]provider.Collection, 0) for rows.Next() { if err = ctx.Err(); err != nil { @@ -150,7 +139,18 @@ func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []Ge row.Features = append(row.Features, match[1]) } - gpkg.Collections = append(gpkg.Collections, row) + collection := provider.Collection{ + Tablename: row.TableName, + Identifier: row.Identifier, + Description: row.Description, + Columns: &provider.Columns{Geometry: row.ColumnName}, + Geometrytype: row.GeometryType, + BBox: [4]float64{row.MinX, row.MinY, row.MaxX, row.MaxY}, + Srid: int(row.SrsId), + Properties: row.Features, + } + + gpkg.Collections = append(gpkg.Collections, collection) } result = gpkg.Collections @@ -158,7 +158,7 @@ func (gpkg *GeoPackage) GetLayers(ctx context.Context, db *sqlx.DB) (result []Ge return } -func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, layer GeoPackageLayer, collectionId string, offset uint64, limit uint64, featureId interface{}, bbox [4]float64) (result *FeatureCollectionGeoJSON, err error) { +func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, collection provider.Collection, collectionId string, offset uint64, limit uint64, featureId interface{}, bbox [4]float64) (result *FeatureCollectionGeoJSON, err error) { // Features bit of a hack // layer.Features => tablename, PK, ...FEATURES, assuming create table in sql statement first is PK result = &FeatureCollectionGeoJSON{} if len(bbox) > 4 { @@ -169,16 +169,16 @@ func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, layer GeoPa var featureIdKey string if gpkg.FeatureIdKey == "" { - featureIdKey = layer.Features[1] + featureIdKey = collection.Properties[1] } else { featureIdKey = gpkg.FeatureIdKey } - rtreeTablenName := fmt.Sprintf("rtree_%s_%s", layer.TableName, layer.ColumnName) - selectClause := fmt.Sprintf("l.`%s`, l.`%s`", featureIdKey, layer.ColumnName) + rtreeTablenName := fmt.Sprintf("rtree_%s_%s", collection.Tablename, collection.Columns.Geometry) + selectClause := fmt.Sprintf("l.`%s`, l.`%s`", featureIdKey, collection.Columns.Geometry) - for _, tf := range layer.Features[1:] { // [2:] skip tablename and PK - if tf == layer.ColumnName || tf == featureIdKey { + for _, tf := range collection.Properties[1:] { // [2:] skip tablename and PK + if tf == collection.Columns.Geometry || tf == featureIdKey { continue } selectClause += fmt.Sprintf(", l.`%v`", tf) @@ -191,7 +191,7 @@ func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, layer GeoPa } query := fmt.Sprintf("SELECT %s FROM `%s` l INNER JOIN `%s` g ON g.`id` = l.`fid` WHERE %s minx <= $2 AND maxx >= $3 AND miny <= $4 AND maxy >= $5 ORDER BY l.`%s` LIMIT $6 OFFSET $7;", - selectClause, layer.TableName, rtreeTablenName, additionalWhere, featureIdKey) + selectClause, collection.Tablename, rtreeTablenName, additionalWhere, featureIdKey) var rows *sqlx.Rows if featureId != nil { @@ -260,7 +260,7 @@ func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, layer GeoPa feature.ID = identifier } - case layer.ColumnName: + case collection.Columns.Geometry: geomData, ok := vals[i].([]byte) if !ok { diff --git a/gpkg/initprovider.go b/gpkg/initprovider.go index ba25796..5990279 100644 --- a/gpkg/initprovider.go +++ b/gpkg/initprovider.go @@ -1,9 +1,11 @@ package gpkg import ( + "log" "oaf-server/provider" "github.com/getkin/kin-openapi/openapi3" + "github.com/imdario/mergo" ) type GeoPackageProvider struct { @@ -26,6 +28,24 @@ func NewGeopackageWithCommonProvider(api *openapi3.T, commonProvider provider.Co func (gp *GeoPackageProvider) Init() (err error) { gp.GeoPackage, err = NewGeoPackage(gp.Config.Datasource.Geopackage.File, gp.Config.Datasource.Geopackage.Fid) - // gp.ApiProcessed = CreateProvidesSpecificParameters(gp.Api, gp.Config.Datasource.Collections) + + collections := gp.Config.Datasource.Collections + + if len(collections) != 0 { + for _, gpkgc := range gp.GeoPackage.Collections { + for _, configc := range collections { + if gpkgc.Identifier == configc.Identifier { + err = mergo.Merge(&configc, gpkgc) + if err != nil { + log.Fatalln(err) + } + } + } + } + } else { + collections = gp.GeoPackage.Collections + } + + gp.ApiProcessed = provider.CreateProvidesSpecificParameters(gp.Api, &collections) return } diff --git a/postgis/initprovider.go b/postgis/initprovider.go index 5d6e940..496ae05 100644 --- a/postgis/initprovider.go +++ b/postgis/initprovider.go @@ -28,6 +28,6 @@ func NewPostgisWithCommonProvider(api *openapi3.T, commonProvider provider.Commo func (pg *PostgisProvider) Init() (err error) { pg.PostGis, err = NewPostgis(pg.Config) - pg.ApiProcessed = provider.CreateProvidesSpecificParameters(pg.Api, pg.PostGis.Collections) + pg.ApiProcessed = provider.CreateProvidesSpecificParameters(pg.Api, &pg.PostGis.Collections) return } diff --git a/provider/util.go b/provider/util.go index ecd7a30..337314f 100644 --- a/provider/util.go +++ b/provider/util.go @@ -276,7 +276,7 @@ func UpperFirst(title string) string { return strings.Title(title) } -func CreateProvidesSpecificParameters(api *openapi3.T, collections []Collection) *openapi3.T { +func CreateProvidesSpecificParameters(api *openapi3.T, collections *[]Collection) *openapi3.T { copy := &openapi3.T{ OpenAPI: api.OpenAPI, @@ -300,10 +300,10 @@ func CreateProvidesSpecificParameters(api *openapi3.T, collections []Collection) } // adjust swagger to accommodate individual parameters - for _, collection := range collections { + for _, collection := range *collections { for k, v := range api.Paths { if strings.Contains(k, "{collectionId}") { - k := strings.Replace(k, "{collectionId}", strings.ToLower(collection.Tablename), 1) + k := strings.Replace(k, "{collectionId}", strings.ToLower(collection.Identifier), 1) params := openapi3.NewParameters() paramsQueryExists := false From 46a889984e14686f9361ae6c784fc4c7e2874da4 Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 19:56:11 +0200 Subject: [PATCH 5/6] update collection Tag with collection name --- provider/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/provider/util.go b/provider/util.go index 337314f..d1e7efd 100644 --- a/provider/util.go +++ b/provider/util.go @@ -345,6 +345,7 @@ func CreateProvidesSpecificParameters(api *openapi3.T, collections *[]Collection copy.Paths[k] = v copy.Paths[k].Get.Parameters = params copy.Paths[k].Get.Extensions = nil + copy.Paths[k].Get.Tags = []string{collection.Identifier} } } From d93bdf7121b744ae1078ffa557b4fad9595e0fab Mon Sep 17 00:00:00 2001 From: Wouter Date: Fri, 25 Jun 2021 20:03:30 +0200 Subject: [PATCH 6/6] removed dub import --- gpkg/gpkg.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gpkg/gpkg.go b/gpkg/gpkg.go index 4179713..d235096 100644 --- a/gpkg/gpkg.go +++ b/gpkg/gpkg.go @@ -7,7 +7,6 @@ import ( "fmt" "log" "oaf-server/provider" - pc "oaf-server/provider" "os" "regexp" "time" @@ -249,7 +248,7 @@ func (gpkg GeoPackage) GetFeatures(ctx context.Context, db *sqlx.DB, collection switch colName { case featureIdKey: - ID, err := pc.ConvertFeatureID(vals[i]) + ID, err := provider.ConvertFeatureID(vals[i]) if err != nil { return result, err }