|
| 1 | +import xpath from 'xpath'; |
| 2 | +import xmldom from 'xmldom'; |
| 3 | + |
| 4 | +//const cswUrl = 'https://nationaalgeoregister.nl/geonetwork/srv/dut/csw-inspire'; |
| 5 | +const cswUrl = 'https://data.linz.govt.nz/services/csw?service=CSW&version=2.0.2&request=GetRecords'; |
| 6 | +const briefTitleSearch = `<?xml version="1.0" encoding="UTF-8"?> |
| 7 | +<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" |
| 8 | + service="CSW" |
| 9 | + version="2.0.2" |
| 10 | + resultType="results" |
| 11 | + startPosition="1" |
| 12 | + maxRecords="10" |
| 13 | + xmlns:ogc="http://www.opengis.net/ogc" |
| 14 | + xmlns:gml="http://www.opengis.net/gml" |
| 15 | + xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 16 | + xmlns:dct="http://purl.org/dc/terms/" |
| 17 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 18 | + xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 |
| 19 | + http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> |
| 20 | + <csw:Query typeNames="csw:Record"> |
| 21 | + <csw:ElementSetName>brief</csw:ElementSetName> |
| 22 | + <csw:Constraint version="1.1.0"> |
| 23 | + <ogc:Filter> |
| 24 | + <ogc:PropertyIsLike wildCard="%" singleChar="_" escapeChar="\"> |
| 25 | + <ogc:PropertyName>dc:title</ogc:PropertyName> |
| 26 | + <ogc:Literal>%water%</ogc:Literal> |
| 27 | + </ogc:PropertyIsLike> |
| 28 | + </ogc:Filter> |
| 29 | + </csw:Constraint> |
| 30 | + </csw:Query> |
| 31 | +</csw:GetRecords>`; |
| 32 | + |
| 33 | +const fullAnySearch = `<?xml version="1.0" encoding="UTF-8"?> |
| 34 | +<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" |
| 35 | + service="CSW" |
| 36 | + version="2.0.2" |
| 37 | + resultType="results" |
| 38 | + startPosition="1" |
| 39 | + maxRecords="10" |
| 40 | + xmlns:ogc="http://www.opengis.net/ogc" |
| 41 | + xmlns:gml="http://www.opengis.net/gml" |
| 42 | + xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 43 | + xmlns:dct="http://purl.org/dc/terms/" |
| 44 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 45 | + xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 |
| 46 | + http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> |
| 47 | + <csw:Query typeNames="csw:Record"> |
| 48 | + <csw:ElementSetName>full</csw:ElementSetName> |
| 49 | + <csw:Constraint version="1.1.0"> |
| 50 | + <ogc:Filter> |
| 51 | + <ogc:And> |
| 52 | + <ogc:PropertyIsLike wildCard="%" singleChar="_" escapeChar="\\"> |
| 53 | + <ogc:PropertyName>AnyText</ogc:PropertyName> |
| 54 | + <ogc:Literal>%water%</ogc:Literal> |
| 55 | + </ogc:PropertyIsLike> |
| 56 | + <ogc:PropertyIsEqualTo> |
| 57 | + <ogc:PropertyName>type</ogc:PropertyName> |
| 58 | + <ogc:Literal>service</ogc:Literal> |
| 59 | + </ogc:PropertyIsEqualTo> |
| 60 | + </ogc:And> |
| 61 | + </ogc:Filter> |
| 62 | + </csw:Constraint> |
| 63 | + </csw:Query> |
| 64 | +</csw:GetRecords>` |
| 65 | + |
| 66 | +const serviceSearch = `<?xml version="1.0" encoding="UTF-8"?> |
| 67 | +<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" |
| 68 | + service="CSW" |
| 69 | + version="2.0.2" |
| 70 | + resultType="results" |
| 71 | + startPosition="1" |
| 72 | + maxRecords="10" |
| 73 | + xmlns:ogc="http://www.opengis.net/ogc" |
| 74 | + xmlns:gml="http://www.opengis.net/gml" |
| 75 | + xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 76 | + xmlns:dct="http://purl.org/dc/terms/" |
| 77 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 78 | + xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 |
| 79 | + http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> |
| 80 | + <csw:Query typeNames="csw:Record"> |
| 81 | + <csw:ElementSetName>full</csw:ElementSetName> |
| 82 | + <csw:Constraint version="1.1.0"> |
| 83 | + <ogc:Filter> |
| 84 | + <ogc:PropertyIsEqualTo> |
| 85 | + <ogc:PropertyName>apiso:Type</ogc:PropertyName> |
| 86 | + <ogc:Literal>service</ogc:Literal> |
| 87 | + </ogc:PropertyIsEqualTo> |
| 88 | + </ogc:Filter> |
| 89 | + </csw:Constraint> |
| 90 | + </csw:Query> |
| 91 | +</csw:GetRecords>` |
| 92 | + |
| 93 | +const extentSearch = `<?xml version="1.0" encoding="UTF-8"?> |
| 94 | +<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" |
| 95 | + service="CSW" |
| 96 | + version="2.0.2" |
| 97 | + resultType="results" |
| 98 | + startPosition="1" |
| 99 | + maxRecords="10" |
| 100 | + xmlns:ogc="http://www.opengis.net/ogc" |
| 101 | + xmlns:gml="http://www.opengis.net/gml" |
| 102 | + xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 103 | + xmlns:dct="http://purl.org/dc/terms/" |
| 104 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 105 | + xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 |
| 106 | + http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> |
| 107 | + <csw:Query typeNames="csw:Record"> |
| 108 | + <csw:ElementSetName>full</csw:ElementSetName> |
| 109 | + <csw:Constraint version="1.1.0"> |
| 110 | + <ogc:Filter> |
| 111 | + <ogc:And> |
| 112 | + <ogc:PropertyIsLike wildCard="%" singleChar="_" escapeChar="\\"> |
| 113 | + <ogc:PropertyName>AnyText</ogc:PropertyName> |
| 114 | + <ogc:Literal>%water%</ogc:Literal> |
| 115 | + </ogc:PropertyIsLike> |
| 116 | + <ogc:PropertyIsEqualTo> |
| 117 | + <ogc:PropertyName>type</ogc:PropertyName> |
| 118 | + <ogc:Literal>service</ogc:Literal> |
| 119 | + </ogc:PropertyIsEqualTo> |
| 120 | + <ogc:BBOX> |
| 121 | + <ogc:PropertyName>ows:BoundingBox</ogc:PropertyName> |
| 122 | + <gml:Envelope> |
| 123 | + <gml:lowerCorner>4.811 52.273</gml:lowerCorner> |
| 124 | + <gml:upperCorner>5.003 52.425</gml:upperCorner> |
| 125 | + </gml:Envelope> |
| 126 | + </ogc:BBOX> |
| 127 | + </ogc:And> |
| 128 | + </ogc:Filter> |
| 129 | + </csw:Constraint> |
| 130 | + </csw:Query> |
| 131 | +</csw:GetRecords>`; |
| 132 | + |
| 133 | +const withinExtentSearch = `<?xml version="1.0" encoding="UTF-8"?> |
| 134 | +<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" |
| 135 | + service="CSW" |
| 136 | + version="2.0.2" |
| 137 | + resultType="results" |
| 138 | + startPosition="1" |
| 139 | + maxRecords="10" |
| 140 | + xmlns:ogc="http://www.opengis.net/ogc" |
| 141 | + xmlns:gml="http://www.opengis.net/gml" |
| 142 | + xmlns:dc="http://purl.org/dc/elements/1.1/" |
| 143 | + xmlns:dct="http://purl.org/dc/terms/" |
| 144 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 145 | + xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2 |
| 146 | + http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd"> |
| 147 | + <csw:Query typeNames="csw:Record"> |
| 148 | + <csw:ElementSetName>full</csw:ElementSetName> |
| 149 | + <csw:Constraint version="1.1.0"> |
| 150 | + <ogc:Filter> |
| 151 | + <ogc:And> |
| 152 | + <ogc:PropertyIsLike wildCard="%" singleChar="_" escapeChar="\\"> |
| 153 | + <ogc:PropertyName>AnyText</ogc:PropertyName> |
| 154 | + <ogc:Literal>%water%</ogc:Literal> |
| 155 | + </ogc:PropertyIsLike> |
| 156 | + <ogc:PropertyIsEqualTo> |
| 157 | + <ogc:PropertyName>type</ogc:PropertyName> |
| 158 | + <ogc:Literal>service</ogc:Literal> |
| 159 | + </ogc:PropertyIsEqualTo> |
| 160 | + <ogc:Within> |
| 161 | + <ogc:PropertyName>ows:BoundingBox</ogc:PropertyName> |
| 162 | + <gml:Envelope> |
| 163 | + <gml:lowerCorner>0.91 50.17</gml:lowerCorner> |
| 164 | + <gml:upperCorner>9.64 54.15</gml:upperCorner> |
| 165 | + </gml:Envelope> |
| 166 | + </ogc:Within> |
| 167 | + </ogc:And> |
| 168 | + </ogc:Filter> |
| 169 | + </csw:Constraint> |
| 170 | + </csw:Query> |
| 171 | +</csw:GetRecords>` |
| 172 | + |
| 173 | +fetch(cswUrl, { |
| 174 | + method: 'POST', |
| 175 | + headers: { |
| 176 | + 'Content-Type': 'application/xml', |
| 177 | + }, |
| 178 | + body: serviceSearch |
| 179 | +}) |
| 180 | +.then(response => response.text()) // Assuming the response is XML, get it as text |
| 181 | +.then(str => { |
| 182 | + //console.log(str); |
| 183 | + return (new xmldom.DOMParser()).parseFromString(str, "text/xml")}) |
| 184 | +.then(data => { |
| 185 | + // Process the XML data here |
| 186 | + // For example, extracting titles from the response |
| 187 | + //const titles = data.querySelectorAll('dc\\:title, title'); |
| 188 | + const select = xpath.useNamespaces({ |
| 189 | + "dc": "http://purl.org/dc/elements/1.1/", |
| 190 | + "csw": "http://www.opengis.net/cat/csw/2.0.2" |
| 191 | + }); |
| 192 | + const records = select('//csw:Record', data); |
| 193 | + console.log(`Number of records found: ${records.length}`) |
| 194 | + records.forEach(record => { |
| 195 | + const title = select('.//dc:title', record)[0].textContent; |
| 196 | + const uri = select('.//dc:URI', record)[0].textContent; |
| 197 | + console.log(`Title: ${title}, URI: ${uri}`); |
| 198 | + }); |
| 199 | +}) |
| 200 | +.catch(error => console.error('Error:', error)); |
0 commit comments