Skip to content

Commit

Permalink
add url utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Jan 13, 2024
1 parent ddd069d commit 0694366
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 164 deletions.
3 changes: 3 additions & 0 deletions fixtures/wmts/arcgis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<ResourceURL format="image/png" resourceType="tile"
template="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/tile/1.0.0/Demographics_USA_Population_Density/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"
/>
<ResourceURL format="image/jpeg" resourceType="tile"
template="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/tile/1.0.0/Demographics_USA_Population_Density/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.jpeg"
/>
</Layer>
<!--TileMatrixSet-->
<TileMatrixSet>
Expand Down
16 changes: 9 additions & 7 deletions src/wmts/capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,10 @@ describe('WMTS Capabilities', () => {
matrixSetLimits: [],
},
],
urlTemplates: [
'http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png',
],
urlTemplates: {
'image/png':
'http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png',
},
dimensions: ['Time'],
},
]);
Expand Down Expand Up @@ -609,9 +610,10 @@ describe('WMTS Capabilities', () => {
},
],
title: 'Demographics_USA_Population_Density',
urlTemplates: [
'https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/tile/1.0.0/Demographics_USA_Population_Density/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png',
],
urlTemplates: {
'image/png':
'https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/WMTS/tile/1.0.0/Demographics_USA_Population_Density/{Style}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png',
},
},
]);
});
Expand Down Expand Up @@ -682,7 +684,7 @@ describe('WMTS Capabilities', () => {
},
],
title: 'Photographies aériennes',
urlTemplates: [],
urlTemplates: {},
},
]);
});
Expand Down
81 changes: 16 additions & 65 deletions src/wmts/capabilities.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { XmlDocument, XmlElement } from '@rgrove/parse-xml';
import {
BoundingBox,
CrsCode,
GenericEndpointInfo,
MimeType,
} from '../shared/models';
import { BoundingBox } from '../shared/models';
import {
findChildElement,
findChildrenElement,
getElementAttribute,
getElementText,
getRootElement,
} from '../shared/xml-utils';
import {
LayerStyle,
MatrixSetLink,
TileMatrix,
WmtsEndpointInfo,
WmtsLayer,
WmtsMatrixSet,
} from './model';

function parseBBox(xmlElement): BoundingBox {
function parseBBox(xmlElement: XmlElement): BoundingBox {
const result = ['LowerCorner', 'UpperCorner']
.map((elName) => findChildElement(xmlElement, elName))
.map((cornerEl) => getElementText(cornerEl).split(' '))
Expand All @@ -23,13 +26,6 @@ function parseBBox(xmlElement): BoundingBox {
return result;
}

export interface WmtsEndpointInfo extends GenericEndpointInfo {
getTileUrls: {
kvp?: string;
rest?: string;
};
}

export function readInfoFromCapabilities(
capabilitiesDoc: XmlDocument
): WmtsEndpointInfo {
Expand Down Expand Up @@ -67,29 +63,10 @@ export function readInfoFromCapabilities(
};
}

interface TileMatrix {
identifier: string;
scaleDenominator: number;
resolution?: number; // FOR OL??? or computeResolution?
topLeft: [number, number];
tileWidth: number;
tileHeight: number;
matrixWidth: number;
matrixHeight: number;
}

export interface WmtsMatrixSet {
identifier: string;
wellKnownScaleSet?: string; // from fixed list?
crs: CrsCode;
boundingBox: BoundingBox;
tileMatrices: TileMatrix[];
}

export function readMatrixSetsFromCapabilities(
capabilitiesDoc: XmlDocument
): WmtsMatrixSet[] {
function parseMatrixSet(element): TileMatrix {
function parseMatrixSet(element: XmlElement): TileMatrix {
const topLeft = getElementText(findChildElement(element, 'TopLeftCorner'))
.split(' ')
.map(parseFloat) as [number, number];
Expand Down Expand Up @@ -135,36 +112,6 @@ export function readMatrixSetsFromCapabilities(
});
}

interface LayerStyle {
name: string;
title: string;
legendUrl?: string;
}

interface MatrixSetLink {
matrixSetId: string;
matrixSetLimits: MatrixSetLimit[];
}

interface MatrixSetLimit {
tileMatrix: string;
minTileRow: number;
maxTileRow: number;
minTileCol: number;
maxTileCol: number;
}

export interface WmtsLayer {
name: string;
urlTemplates: string[];
styles: LayerStyle[];
defaultStyle: string;
matrixSets: MatrixSetLink[];
outputFormats: MimeType[];
latLonBoundingBox?: BoundingBox;
dimensions?: string[];
}

export function readLayersFromCapabilities(
capabilitiesDoc: XmlDocument
): WmtsLayer[] {
Expand Down Expand Up @@ -224,7 +171,11 @@ export function readLayersFromCapabilities(
.filter(
(element) => getElementAttribute(element, 'resourceType') === 'tile'
)
.map((element) => getElementAttribute(element, 'template'));
.reduce((prev, element) => {
const mimeType = getElementAttribute(element, 'format');
const urlTemplate = getElementAttribute(element, 'template');
return { ...prev, [mimeType]: urlTemplate };
}, {});
const matrixSets = findChildrenElement(element, 'TileMatrixSetLink').map(
parseMatrixSetLink
);
Expand Down
Loading

0 comments on commit 0694366

Please sign in to comment.