This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corine-api.js
139 lines (109 loc) · 4.47 KB
/
corine-api.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
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
const express = require('express');
const Webtask = require('webtask-tools');
const bodyParser = require('body-parser');
const OpenLocationCode = require('open-location-code').OpenLocationCode;
const geojsonToArcGIS = require('@esri/arcgis-to-geojson-utils').geojsonToArcGIS;
const fetch = require('node-fetch');
const olcInstance = new OpenLocationCode();
const app = express();
app.use(bodyParser.raw({
type: 'text/csv'
}));
/**
* @typedef {DecodedOpenLocationCode}
* @prop {number} latitudeCenter
* @prop {number} longitudeCenter
* @prop {number} latitudeLo
* @prop {number} longitudeLo
* @prop {number} longitudeHi
* @prop {number} latitudeHi
*/
/**
* @typedef {ArcGISPolygon}
* @prop {Array.<number[]} rings
* @prop {number} spatialReference
*/
/**
* Converts latitude and longitue degrees to WSG84 meters.
*
* @param {number} longitude
* @param {number} latitude
* @returns {{ x: number, y: number }}
*/
function degressToMeters (longitude, latitude) {
const x = longitude * 20037508.34 / 180;
let y = Math.log(Math.tan((90 + latitude) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return { x, y };
}
/**
* @param {DecodedOpenLocationCode} decodedOpenLocationCode
* @returns {{ xmin: number, ymin: number, xmax: number, ymax: number }}
*/
function getBoundsInMeters (decodedOpenLocationCode) {
const { x: xmin, y: ymin } = degressToMeters(decodedOpenLocationCode.longitudeLo, decodedOpenLocationCode.latitudeLo);
const { x: xmax, y: ymax } = degressToMeters(decodedOpenLocationCode.longitudeHi, decodedOpenLocationCode.latitudeHi);
return { xmin, ymin, xmax, ymax };
}
/**
* @param {DecodedOpenLocationCode} decodedOpenLocationCode
* @returns {ArcGISPolygon}
*/
function getArcGISPolygon (decodedOpenLocationCode) {
const { xmin, ymin, xmax, ymax } = getBoundsInMeters(decodedOpenLocationCode);
const wsg84geoJSONPolygon = {
type: 'Polygon',
coordinates: [[
[xmin, ymin],
[xmax, ymin],
[xmax, ymax],
[xmin, ymax],
[xmin, ymin]
]]
};
return Object.assign({}, geojsonToArcGIS(wsg84geoJSONPolygon), { spatialReference: 102100 });
}
const layerInfoURL = 'http://copernicus.discomap.eea.europa.eu/arcgis/rest/services/Corine/CLC2012_WM/MapServer/0?f=json';
const layerPromise = fetch(layerInfoURL).then(r => r.json());
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/CLC2012_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());
return Promise.all([
tilePromise,
layerPromise
]).then(([tileData, layerData]) => {
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;
});
}
app.post('/', function (req, res) {
const plotCodes = req.body.toString('utf8').split(',');
const promises = plotCodes.map(getTerrainDataFromPlotCode);
Promise.all(promises).then(results => {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(Object.assign({}, ...results)));
}).catch(error => res.status(500).end(error.message));
});
app.get('/', function (req, res) {
const lat = parseFloat(req.query.lat);
const long = parseFloat(req.query.long);
const queryOCL = req.query.ocl ? req.query.ocl.replace(/\s/, '+') : undefined;
const openLocationCode = queryOCL || olcInstance.encode(lat, long);
// const imageUrl = `http://copernicus.discomap.eea.europa.eu/arcgis/rest/services/Corine/CLC2012_WM/MapServer/export?transparent=true&format=png&bbox=${arcgisbbox}&size=500%2C4&f=image`;
getTerrainDataFromPlotCode(openLocationCode).then(terrainTypes => {
res.setHeader('Content-Type', 'application/json');
res.send({ openLocationCode, terrainTypes });
}).catch(error => {
res.status(500).end(error);
});
});
module.exports = Webtask.fromExpress(app);