Skip to content

Commit

Permalink
Merge pull request #218 from PDOK/feature-relations-fix
Browse files Browse the repository at this point in the history
Feature relations fix
  • Loading branch information
rkettelerij authored Jul 15, 2024
2 parents 0fdbf46 + 6c2cd48 commit 3c8707e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 5 deletions.
19 changes: 15 additions & 4 deletions internal/ogc/features/domain/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"regexp"
"sort"
"strings"
)

Expand All @@ -27,6 +28,9 @@ type Profile struct {
}

func NewProfile(profileName ProfileName, baseURL url.URL, collectionNames []string) Profile {
sort.Slice(collectionNames, func(i, j int) bool {
return len(collectionNames[i]) > len(collectionNames[j])
})
return Profile{
profileName: profileName,
baseURL: baseURL.String(),
Expand All @@ -39,7 +43,7 @@ func (p *Profile) MapRelationUsingProfile(columnName string, columnValue any, ex
switch p.profileName {
case RelAsLink:
newColumnName = regex.ReplaceAllString(columnName, "")
collectionName := p.findCollectionWithPrefix(newColumnName)
collectionName := p.findCollection(newColumnName)
newColumnName += ".href"
if columnValue != nil && collectionName != "" {
newColumnValue = fmt.Sprintf(featurePath, p.baseURL, collectionName, columnValue)
Expand All @@ -50,17 +54,24 @@ func (p *Profile) MapRelationUsingProfile(columnName string, columnValue any, ex
case RelAsURI:
// almost identical to rel-as-link except that there's no ".href" suffix (and potentially a title in the future)
newColumnName = regex.ReplaceAllString(columnName, "")
collectionName := p.findCollectionWithPrefix(newColumnName)
collectionName := p.findCollection(newColumnName)
if columnValue != nil {
newColumnValue = fmt.Sprintf(featurePath, p.baseURL, collectionName, columnValue)
}
}
return
}

func (p *Profile) findCollectionWithPrefix(prefix string) string {
func (p *Profile) findCollection(name string) string {
// prefer exact matches first
for _, collName := range p.collectionNames {
if strings.HasPrefix(prefix, collName) {
if name == collName {
return collName
}
}
// then prefer fuzzy match (to support infix)
for _, collName := range p.collectionNames {
if strings.HasPrefix(name, collName) {
return collName
}
}
Expand Down
11 changes: 10 additions & 1 deletion internal/ogc/features/domain/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func TestMapRelationUsingProfile(t *testing.T) {
expectedColName: "another_collection_some_infix.href",
expectedColVal: "http://example.com/collections/another_collection/items/123",
},
{
name: "RelAsLink with similar collection name (make sure exact match is selected)",
profile: RelAsLink,
columnName: "baz_bazoo_boo_external_fid",
columnValue: "123",
externalFidCol: "external_fid",
expectedColName: "baz_bazoo_boo.href",
expectedColVal: "http://example.com/collections/baz_bazoo_boo/items/123",
},
{
name: "RelAsKey",
profile: RelAsKey,
Expand Down Expand Up @@ -86,7 +95,7 @@ func TestMapRelationUsingProfile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
url, err := neturl.Parse("http://example.com")
assert.NoError(t, err)
profile := NewProfile(tt.profile, *url, []string{"some_collection", "another_collection", "foo", "bar"})
profile := NewProfile(tt.profile, *url, []string{"some_collection", "another_collection", "foo", "bar", "baz_bazoo", "baz_bazoo_boo", "baz_bazoo_boo_foo"})
newColName, newColVal := profile.MapRelationUsingProfile(tt.columnName, tt.columnValue, tt.externalFidCol)
assert.Equal(t, tt.expectedColName, newColName)
assert.Equal(t, tt.expectedColVal, newColVal)
Expand Down
14 changes: 14 additions & 0 deletions internal/ogc/features/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,20 @@ func TestFeatures(t *testing.T) {
statusCode: http.StatusOK,
},
},
{
name: "Request mapsheets as JSON-FG",
fields: fields{
configFile: "internal/ogc/features/testdata/config_mapsheets.yaml",
url: "http://localhost:8080/collections/:collectionId/items?limit=2&f=jsonfg",
collectionID: "example_mapsheets",
contentCrs: "<" + domain.WGS84CrsURI + ">",
format: "json",
},
want: want{
body: "internal/ogc/features/testdata/expected_mapsheets_jsonfg.json",
statusCode: http.StatusOK,
},
},
{
name: "Request mapsheets as HTML",
fields: fields{
Expand Down
111 changes: 111 additions & 0 deletions internal/ogc/features/testdata/expected_mapsheets_jsonfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"type": "FeatureCollection",
"timeStamp": "2000-01-01T00:00:00Z",
"coordRefSys": "http://www.opengis.net/def/crs/OGC/1.3/CRS84",
"links": [
{
"rel": "self",
"title": "This document as JSON-FG",
"type": "application/vnd.ogc.fg+json",
"href": "http://localhost:8080/collections/example_mapsheets/items?f=jsonfg&limit=2"
},
{
"rel": "alternate",
"title": "This document as GeoJSON",
"type": "application/geo+json",
"href": "http://localhost:8080/collections/example_mapsheets/items?f=json&limit=2"
},
{
"rel": "alternate",
"title": "This document as HTML",
"type": "text/html",
"href": "http://localhost:8080/collections/example_mapsheets/items?f=html&limit=2"
},
{
"rel": "next",
"title": "Next page",
"type": "application/vnd.ogc.fg+json",
"href": "http://localhost:8080/collections/example_mapsheets/items?cursor=Dv4%7CNwyr1Q&f=jsonfg&limit=2"
}
],
"conformsTo": [
"http://www.opengis.net/spec/json-fg-1/0.2/conf/core"
],
"features": [
{
"id": "3542",
"type": "Feature",
"time": null,
"place": null,
"geometry": {
"type": "Point",
"coordinates": [
120919.942,
489320.199
]
},
"properties": {
"datum_doc": "1900-01-01",
"datum_eind": null,
"datum_strt": "1900-01-01",
"document": "GV00000402",
"huisletter": null,
"huisnummer": 14,
"nummer_id": "0363200000454013",
"postcode": "1013CR",
"rdf_seealso": "http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000454013",
"status": "Naamgeving uitgegeven",
"straatnaam": "Van Diemenkade",
"toevoeging": null,
"type": "Ligplaats",
"woonplaats": "Amsterdam"
},
"links": [
{
"rel": "enclosure",
"title": "Download feature",
"type": "application/octet-stream",
"href": "http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000454013"
}
]
},
{
"id": "3837",
"type": "Feature",
"time": null,
"place": null,
"geometry": {
"type": "Point",
"coordinates": [
121108.424,
488930.925
]
},
"properties": {
"datum_doc": "1900-01-01",
"datum_eind": null,
"datum_strt": "1900-01-01",
"document": "GV00000402",
"huisletter": null,
"huisnummer": 9,
"nummer_id": "0363200000398886",
"postcode": "1013KW",
"rdf_seealso": "http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000398886",
"status": "Naamgeving uitgegeven",
"straatnaam": "Realengracht",
"toevoeging": null,
"type": "Ligplaats",
"woonplaats": "Amsterdam"
},
"links": [
{
"rel": "enclosure",
"title": "Download feature",
"type": "application/octet-stream",
"href": "http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000398886"
}
]
}
],
"numberReturned": 2
}

0 comments on commit 3c8707e

Please sign in to comment.