Skip to content

Commit 7b47a43

Browse files
committed
convert fetchRecords to JSON
1 parent 17e7192 commit 7b47a43

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

cswrequests.js

+71-7
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export async function fetchDescribeRecord(url, cswVersion) {
131131
}
132132
const text = await response.text();
133133

134-
const cswDescribeRecord = await parseXml(text, {});
134+
const cswDescribeRecord = await parseXml(text);
135135

136136
const briefRecordTypeName = bareTypeName(nodeArray(evaluateXPath(cswDescribeRecord, '//xs:element[@name="BriefRecord"]/@type')));
137137
const briefRecordType = nodeArray(evaluateXPath(cswDescribeRecord, `//xs:complexType[@name="${briefRecordTypeName}"]/xs:complexContent/xs:extension/xs:sequence/xs:element/@ref`));
@@ -163,17 +163,17 @@ export async function fetchDescribeRecord(url, cswVersion) {
163163
}
164164
}
165165

166-
function fetchRecordsXML(cswVersion, recordType, startPosition, maxRecords, constraint) {
166+
function fetchRecordsXML(cswVersion, elementSetName, startPosition, maxRecords, constraint) {
167167
//return `<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows" outputSchema="http://www.opengis.net/cat/csw/2.0.2" outputFormat="application/xml" version="2.0.2" resultType="results" service="CSW" maxRecords="10" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"><csw:Query typeNames="csw:Record"><csw:ElementSetName>summary</csw:ElementSetName><csw:Constraint version="1.1.0"><ogc:Filter><ogc:PropertyIsLike wildCard="*" singleChar="_" escapeChar="\\"><ogc:PropertyName>csw:AnyText</ogc:PropertyName><ogc:Literal>*None*</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></csw:Constraint></csw:Query></csw:GetRecords>`
168168
return `<?xml version="1.0" encoding="UTF-8"?>
169169
<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" service="CSW" version="${cswVersion}" resultType="results" startPosition="${startPosition}" maxRecords="${maxRecords}" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dct="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd">
170170
<csw:Query typeNames="csw:Record">
171-
<csw:ElementSetName>full</csw:ElementSetName>
171+
<csw:ElementSetName>${elementSetName}</csw:ElementSetName>
172172
</csw:Query>
173173
</csw:GetRecords>`
174174
}
175175

176-
export async function fetchGetRecords(url, cswVersion, serviceSearch) {
176+
export async function fetchGetRecords(url, cswVersion, elementSetName, serviceSearch) {
177177
try {
178178
const params = []
179179
const preparedUrl = prepareUrl(url, params);
@@ -184,15 +184,79 @@ export async function fetchGetRecords(url, cswVersion, serviceSearch) {
184184
'User-Agent': 'JS CSW Client/0.1',
185185
'Content-Type': 'text/xml',
186186
},
187-
body: fetchRecordsXML(cswVersion, 'Record', 1, 10, '')
187+
body: fetchRecordsXML(cswVersion, elementSetName, 1, 10, '')
188188
});
189189
if (!response.ok) {
190190
throw new Error(`HTTP error! status: ${response.status}`);
191191
}
192192
const text = await response.text();
193193
console.log(text);
194-
const cswRecords = await parseXml(text, {});
195-
return cswRecords;
194+
const cswRecords = await parseXml(text);
195+
let xmlRecordName;
196+
switch(elementSetName) {
197+
case 'full':
198+
xmlRecordName = 'Record';
199+
break;
200+
case 'summary':
201+
xmlRecordName = 'SummaryRecord';
202+
break;
203+
case 'brief':
204+
xmlRecordName = 'BriefRecord';
205+
break;
206+
default:
207+
console.error('fetchGetRecords: Unknown elementSetName:', elementSetName);
208+
break;
209+
}
210+
const result = {
211+
cswVersion,
212+
searchStatus: nodeValue(evaluateXPath(cswRecords, '//csw:SearchStatus/@timestamp'), 0),
213+
searchResults: evaluateXPath(cswRecords, '//csw:SearchResults').map(searchResult=> {
214+
return {
215+
numberOfRecordsMatched: searchResult.getAttribute('numberOfRecordsMatched'),
216+
numberOfRecordsReturned: searchResult.getAttribute('numberOfRecordsReturned'),
217+
nextRecord: searchResult.getAttribute('nextRecord'),
218+
elementSet: searchResult.getAttribute('elementSet'),
219+
}
220+
})[0],
221+
records: evaluateXPath(cswRecords, `//csw:${xmlRecordName}`).map(record => {
222+
const result = {};
223+
// get the list of all the fields in the record
224+
const recordFields = Array.from(record.childNodes).filter(child=>child.nodeType === 1).map(child=>child.nodeName);
225+
for (const recordField of recordFields) {
226+
const localFieldName = recordField.split(':').pop();
227+
if (localFieldName in result) {
228+
if (localFieldName === 'BoundingBox') {
229+
continue;
230+
}
231+
if (!Array.isArray(result[localFieldName])) {
232+
result[localFieldName] = [result[localFieldName]];
233+
}
234+
result[localFieldName].push(nodeValue(evaluateXPath(record, `./${recordField}/text()`), 0));
235+
continue;
236+
} else {
237+
result[localFieldName] = nodeValue(evaluateXPath(record, `./${recordField}/text()`), 0);
238+
}
239+
}
240+
if (result.BoundingBox) {
241+
result.bbox = evaluateXPath(record, 'ows:BoundingBox').map(bbox => {
242+
const result = {
243+
crs: bbox.getAttribute('crs'),
244+
lowerCorner: nodeValue(evaluateXPath(bbox, 'ows:LowerCorner/text()'), 0),
245+
upperCorner: nodeValue(evaluateXPath(bbox, 'ows:UpperCorner/text()'), 0),
246+
}
247+
if (result.lowerCorner && result.upperCorner){
248+
result.extent = result.lowerCorner.split(' ').concat(result.upperCorner.split(' ')).map(parseFloat);
249+
}
250+
if (result.crs && result.crs.indexOf('EPSG') > -1 && result.crs.indexOf(':') > -1) {
251+
result.epsgcode = parseInt(result.crs.split(':').pop());
252+
}
253+
return result;
254+
})[0];
255+
}
256+
return result;
257+
}),
258+
};
259+
return result;
196260
} catch (error) {
197261
console.error('Failed to fetch GetRecords:', error);
198262
return {

0 commit comments

Comments
 (0)