forked from camptocamp/ogc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
206 lines (181 loc) · 6.64 KB
/
parser.ts
File metadata and controls
206 lines (181 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// src/tms/parser.ts
import {
TileMapInfo,
TileMapService,
TmsEndpointInfo,
TileMapReference,
TileSet,
TmsProfile,
} from './model.js';
import {
getRootElement,
findChildElement,
findChildrenElement,
getElementText,
getElementAttribute,
} from '../shared/xml-utils.js';
import { XmlDocument } from '@rgrove/parse-xml';
/**
* Parses a TMS Service XML string into a TileMapService object.
*/
export function parseTileMapServiceXML(xmlDoc: XmlDocument): TileMapService {
const root = getRootElement(xmlDoc);
const title = getElementText(findChildElement(root, 'Title'))?.trim() || '';
const abstract =
getElementText(findChildElement(root, 'Abstract'))?.trim() || '';
const version = getElementAttribute(root, 'version') || '';
const keywordList = findChildElement(root, 'KeywordList');
const keywords = keywordList ? [getElementText(keywordList).trim()] : [];
const tileMapsContainer = findChildElement(root, 'TileMaps');
const tileMaps = [];
if (tileMapsContainer) {
const tileMapEls = findChildrenElement(tileMapsContainer, 'TileMap');
tileMaps.push(
...tileMapEls.map((el) => ({
title: getElementAttribute(el, 'title') || '',
srs: getElementAttribute(el, 'srs') || '',
profile: getElementAttribute(el, 'profile') || '',
href: getElementAttribute(el, 'href') || '',
}))
);
}
return { title, abstract, version, keywords, tileMaps };
}
/**
* Parses an individual TileMap resource XML string into a TileMapInfo object.
*/
export function parseTileMapXML(xmlDoc: XmlDocument): TileMapInfo {
const root = getRootElement(xmlDoc);
const title = getElementText(findChildElement(root, 'Title'))?.trim() || '';
const abstract =
getElementText(findChildElement(root, 'Abstract'))?.trim() || '';
const version = getElementAttribute(root, 'version') || '';
const tileMapService = getElementAttribute(root, 'tilemapservice') || '';
const srs = getElementText(findChildElement(root, 'SRS'))?.trim() || '';
const bbEl = findChildElement(root, 'BoundingBox');
const boundingBox = [
parseFloat(getElementAttribute(bbEl, 'minx') || '0'),
parseFloat(getElementAttribute(bbEl, 'miny') || '0'),
parseFloat(getElementAttribute(bbEl, 'maxx') || '0'),
parseFloat(getElementAttribute(bbEl, 'maxy') || '0'),
] as [number, number, number, number];
const originEl = findChildElement(root, 'Origin');
const origin = {
x: parseFloat(getElementAttribute(originEl, 'x') || '0'),
y: parseFloat(getElementAttribute(originEl, 'y') || '0'),
};
const tfEl = findChildElement(root, 'TileFormat');
const tileFormat = {
width: parseInt(getElementAttribute(tfEl, 'width') || '0'),
height: parseInt(getElementAttribute(tfEl, 'height') || '0'),
mimeType: getElementAttribute(tfEl, 'mime-type') || '',
extension: getElementAttribute(tfEl, 'extension') || '',
};
const tmEl = findChildElement(root, 'TileMap'); // Nested TileMap element (first format)
const tileSetsEl = findChildElement(root, 'TileSets'); // Direct TileSets element (second format)
const tileSets: TileSet[] = [];
let profile: TmsProfile = 'none';
if (tileSetsEl) {
profile =
(getElementAttribute(tileSetsEl, 'profile') as TmsProfile) || 'none';
}
// Handle format 1: TileSet elements are inside nested TileMap
if (tmEl) {
profile =
profile || (getElementAttribute(tmEl, 'profile') as TmsProfile) || 'none';
const tileSetEls = findChildrenElement(tmEl, 'TileSet');
tileSets.push(
...tileSetEls.map((el) => ({
href: getElementAttribute(el, 'href') || '',
unitsPerPixel: parseFloat(
getElementAttribute(el, 'units-per-pixel') || '0'
),
order: parseInt(getElementAttribute(el, 'order') || '0'),
minrow: parseInt(getElementAttribute(el, 'minrow') || '0'),
maxrow: parseInt(getElementAttribute(el, 'maxrow') || '0'),
mincol: parseInt(getElementAttribute(el, 'mincol') || '0'),
maxcol: parseInt(getElementAttribute(el, 'maxcol') || '0'),
}))
);
}
// Handle format 2: TileSet elements are inside TileSets
if (tileSetsEl) {
const tileSetEls = findChildrenElement(tileSetsEl, 'TileSet');
tileSets.push(
...tileSetEls.map((el) => ({
href: getElementAttribute(el, 'href') || '',
unitsPerPixel: parseFloat(
getElementAttribute(el, 'units-per-pixel') || '0'
),
order: parseInt(getElementAttribute(el, 'order') || '0'),
minrow: parseInt(getElementAttribute(el, 'minrow') || '0'),
maxrow: parseInt(getElementAttribute(el, 'maxrow') || '0'),
mincol: parseInt(getElementAttribute(el, 'mincol') || '0'),
maxcol: parseInt(getElementAttribute(el, 'maxcol') || '0'),
}))
);
}
const metadataEls = findChildrenElement(root, 'Metadata');
const metadata = metadataEls.map((el) => ({
type: getElementAttribute(el, 'type') || '',
mimeType: getElementAttribute(el, 'mime-type') || '',
href: getElementAttribute(el, 'href') || '',
}));
let attribution = null;
const attrEl = findChildElement(root, 'Attribution');
if (attrEl) {
attribution = {
title: getElementText(findChildElement(attrEl, 'Title'))?.trim() || '',
logo: null,
};
const logoEl = findChildElement(attrEl, 'Logo');
if (logoEl) {
attribution.logo = {
width: parseInt(getElementAttribute(logoEl, 'width') || '0'),
height: parseInt(getElementAttribute(logoEl, 'height') || '0'),
href: getElementAttribute(logoEl, 'href') || '',
mimeType: getElementAttribute(logoEl, 'mime-type') || '',
};
}
}
const kwEl = findChildElement(root, 'KeywordList');
const keywords = kwEl ? [getElementText(kwEl).trim()] : [];
const wmcEl = findChildElement(root, 'WebMapContext');
const webMapContext = wmcEl ? getElementAttribute(wmcEl, 'href') || '' : '';
return {
title,
abstract,
version,
tileMapService,
srs,
boundingBox,
origin,
tileFormat,
profile,
tileSets,
metadata: metadata.length ? metadata : undefined,
attribution: attribution || undefined,
webMapContext: webMapContext || undefined,
keywords: keywords.length ? keywords : undefined,
};
}
/**
* Extracts a simplified endpoint info object from the service data.
*/
export function extractEndpointInfo(
serviceData: TileMapService
): TmsEndpointInfo {
return {
title: serviceData.title,
abstract: serviceData.abstract,
keywords: serviceData.keywords,
};
}
/**
* Extracts an array of TileMap references from the service data.
*/
export function extractTileMapReferences(
serviceData: TileMapService
): TileMapReference[] {
return serviceData.tileMaps || [];
}