-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add WFS download links #972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dbd1792
feat: add WFS download links
maudetes 2add16f
add WfsMetadata type
maudetes 78dc5c2
Apply suggestions from code review
maudetes 809dc98
Update datagouv-components/src/components/ResourceExplorer/ResourceEx…
maudetes bd1d731
update WFS params query
maudetes 40caeb3
Extract to helper function to allow testing
maudetes 94c74f8
review fixes
maudetes 8cd44ff
fix types
maudetes f16ee99
Update tests/resources/use-resource-capabilities.spec.ts
maudetes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Resource, WfsMetadata, OgcLayerInfo } from '../types/resources' | ||
|
|
||
| const WFS_EXPORT_FORMATS = [ | ||
| { | ||
| name: 'csv', | ||
| mimetype: 'csv', | ||
| }, | ||
| { | ||
| name: 'json', | ||
| mimetype: 'application/json', | ||
| }, | ||
| { | ||
| name: 'shp', | ||
| mimetype: 'SHAPE-ZIP', | ||
| }, | ||
| { | ||
| name: 'gml', | ||
| mimetype: 'application/gml+xml', | ||
| }, | ||
| { | ||
| name: 'kml', | ||
| mimetype: 'KML', | ||
| }, | ||
| { | ||
| name: 'gpkg', | ||
| mimetype: 'application/geopackage+sqlite3', | ||
| }, | ||
| ] | ||
|
|
||
| function buildWfsDownloadUrl(baseUrl: string, wfsMetadata: WfsMetadata, format: { name: string, mimetype: string }, layer: OgcLayerInfo) { | ||
| const version = wfsMetadata.version | ||
| const query = new URLSearchParams({ | ||
| SERVICE: 'WFS', | ||
| REQUEST: 'GetFeature', | ||
| VERSION: version, | ||
| ...(Number(version.split('.')[0]) >= 2 ? { TYPENAMES: layer.name } : { TYPENAME: layer.name }), | ||
| OUTPUTFORMAT: format.mimetype, | ||
maudetes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ...(layer.default_crs ? { SRSNAME: layer.default_crs } : {}), | ||
| }) | ||
| return `${baseUrl.split('?')[0]}?${query.toString()}` | ||
| } | ||
|
|
||
| export function getWfsExportFormats(resource: Pick<Resource, 'extras' | 'url'>) { | ||
| const wfsMetadata = resource.extras['analysis:parsing:ogc_metadata'] as WfsMetadata | null | ||
| if (!wfsMetadata || wfsMetadata.format !== `wfs`) return [] | ||
| const outputFormats = wfsMetadata.output_formats.map((format: string) => format.toLowerCase()) | ||
| const layer = wfsMetadata.detected_layer | ||
| if (!layer) return [] | ||
| const formats = WFS_EXPORT_FORMATS.filter(format => outputFormats.includes(format.mimetype.toLowerCase())) | ||
| .map(format => ({ | ||
| url: buildWfsDownloadUrl(resource.url, wfsMetadata, format, layer), | ||
| format: format.name, | ||
| })) | ||
| return formats | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { getWfsExportFormats } from '~/datagouv-components/src/functions/resourceCapabilities' | ||
| import { test, expect } from '../base' | ||
|
|
||
| test('WFS format download URLs generation', async () => { | ||
| const resource = { | ||
| url: 'https://example.com/wfs?service=WFS', | ||
| extras: { | ||
| 'analysis:parsing:ogc_metadata': { | ||
| format: 'wfs', | ||
| version: '2.0.0', | ||
| output_formats: ['application/json', 'SHAPE-ZIP'], | ||
| detected_layer: { name: 'my_layer', default_crs: 'EPSG:4326' }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const wfsFormats = getWfsExportFormats(resource) | ||
|
|
||
| expect(wfsFormats).toEqual([ | ||
| { | ||
| url: `https://example.com/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my_layer&OUTPUTFORMAT=${encodeURIComponent('application/json')}&SRSNAME=${encodeURIComponent('EPSG:4326')}`, | ||
| format: 'json', | ||
| }, | ||
| { | ||
| url: `https://example.com/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=2.0.0&TYPENAMES=my_layer&OUTPUTFORMAT=SHAPE-ZIP&SRSNAME=${encodeURIComponent('EPSG:4326')}`, | ||
| format: 'shp', | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('WFS format download URLs generation with version 1.0.0', async () => { | ||
| const resource = { | ||
| url: 'https://example.com/wfs?service=WFS', | ||
| extras: { | ||
| 'analysis:parsing:ogc_metadata': { | ||
| format: 'wfs', | ||
| version: '1.1.0', | ||
| output_formats: ['application/json', 'SHAPE-ZIP'], | ||
| detected_layer: { name: 'my_layer', default_crs: 'EPSG:4326' }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const wfsFormats = getWfsExportFormats(resource) | ||
|
|
||
| expect(wfsFormats).toEqual([ | ||
| { | ||
| url: `https://example.com/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.1.0&TYPENAME=my_layer&OUTPUTFORMAT=${encodeURIComponent('application/json')}&SRSNAME=${encodeURIComponent('EPSG:4326')}`, | ||
| format: 'json', | ||
| }, | ||
| { | ||
| url: `https://example.com/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.1.0&TYPENAME=my_layer&OUTPUTFORMAT=SHAPE-ZIP&SRSNAME=${encodeURIComponent('EPSG:4326')}`, | ||
| format: 'shp', | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('WFS format download URLs generation with no detected layer', async () => { | ||
| const resource = { | ||
| url: 'https://example.com/wfs?service=WFS', | ||
| extras: { | ||
| 'analysis:parsing:ogc_metadata': { | ||
| format: 'wfs', | ||
| version: '1.1.0', | ||
| output_formats: ['application/json', 'SHAPE-ZIP'], | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const wfsFormats = getWfsExportFormats(resource) | ||
|
|
||
| expect(wfsFormats).toEqual([]) | ||
| }) | ||
maudetes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| test('WMS service don\'t expose WFS export formats', async () => { | ||
| const resource = { | ||
| url: 'https://example.com/wfs?service=WFS', | ||
| extras: { | ||
| 'analysis:parsing:ogc_metadata': { | ||
| format: 'wms', | ||
| version: '1.1.0', | ||
| output_formats: ['application/json', 'SHAPE-ZIP'], | ||
maudetes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| detected_layer: null, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const wfsFormats = getWfsExportFormats(resource) | ||
|
|
||
| expect(wfsFormats).toEqual([]) | ||
| }) | ||
|
|
||
| test('WFS format download URLs generation with null default_crs', async () => { | ||
| const resource = { | ||
| url: 'https://example.com/wfs?service=WFS', | ||
| extras: { | ||
| 'analysis:parsing:ogc_metadata': { | ||
| format: 'wfs', | ||
| version: '1.1.0', | ||
| output_formats: ['application/json'], | ||
| detected_layer: { name: 'my_layer', default_crs: null }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const wfsFormats = getWfsExportFormats(resource) | ||
|
|
||
| expect(wfsFormats).toEqual([ | ||
| { | ||
| url: `https://example.com/wfs?SERVICE=WFS&REQUEST=GetFeature&VERSION=1.1.0&TYPENAME=my_layer&OUTPUTFORMAT=${encodeURIComponent('application/json')}`, | ||
| format: 'json', | ||
| }, | ||
| ]) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.