-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (31 loc) · 1.72 KB
/
index.js
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
import fetch from 'make-fetch-happen';
import OpenLocationCode from 'open-location-code';
import { getBoundsInMeters, getArcGISPolygon } from './helpers.js';
const layerInfoURL = 'http://copernicus.discomap.eea.europa.eu/arcgis/rest/services/Corine/CLC2018_WM/MapServer/0?f=json';
const layerPromise = fetch(layerInfoURL).then(r => r.json());
const olcInstance = new OpenLocationCode.OpenLocationCode();
export async function getTerrainDataFromPlotCode(plotCode) {
const decodedOCL = olcInstance.decode(plotCode);
const { xmin, ymin, xmax, ymax } = getBoundsInMeters(decodedOCL);
const arcgisbbox = encodeURIComponent(`${xmin},${ymin},${xmax},${ymax}`);
const arcgisPolygon = getArcGISPolygon(decodedOCL);
const identifyUrl = `http://copernicus.discomap.eea.europa.eu/arcgis/rest/services/Corine/CLC2018_WM/MapServer/identify?geometry=${encodeURIComponent(JSON.stringify(arcgisPolygon))}&geometryType=esriGeometryPolygon&tolerance=2&mapExtent=${arcgisbbox}&imageDisplay=10%2C10%2C96&returnGeometry=false&f=pjson`;
const tilePromise = fetch(identifyUrl).then(r => r.json());
const tileData = await tilePromise;
const layerData = await layerPromise;
const terrainValues = tileData.results.map(obj => obj.value);
const terrainTypes = layerData.drawingInfo.renderer.uniqueValueInfos
.filter(info => terrainValues.indexOf(info.value) !== -1)
.map(info => info.label.split(':')[1].trim());
const obj = {};
obj[plotCode] = terrainTypes;
return obj;
}
/**
*
* @param {{ latitude: number, longitude: number }} coords
*/
export async function getTerrainDataFromLatLong (coords) {
const openLocationCode = olcInstance.encode(coords.latitude, coords.longitude);
return getTerrainDataFromPlotCode(openLocationCode);
}