-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeSetConfig.js
402 lines (328 loc) · 15.1 KB
/
nodeSetConfig.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const SftpClient = require("ssh2-sftp-client");
const dotenv = require("dotenv");
const axios = require('axios');
const fs = require('fs');
const parseString = require('xml2js').parseString;
// import defaultOptions from "../config";
const path = require('path');
const configFilePath = path.join('./src', 'config.js');
const configNodeFilePath = path.join('./src', 'configNode.js');
const defaultOptions = require('./src/configNode.js');
const stylePath = path.join('./src/constants/', 'Styles.js');
const terrainPath = path.join('./src/constants/', 'Terrains.js');
const layersPath = path.join('./src/constants/', 'Layers.js');
const https = require('https');
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
const inFilePath = `${process.env.FTP_LOCA_PATH}${process.env.FILE_JS_UMD}`;
const renamedFilePath = `${process.env.FTP_LOCA_PATH}${process.env.FILE_JS}`;
// import SftpClient from "ssh2-sftp-client";
//import dotenv from "dotenv";
dotenv.config();
let vectorLimitsLayersOptions ;
let fgbLayersOptions;
let vectorLayersOptions;
let ortoLayersOptions;
let wmsLayersOptions;
let stylesOptions;
let terrainOptions;
getVectorLimitsLayers()
getFGBLimitsLayers()
getOrtoWMSLayers()
getWMSLayers()
getVectorLayers()
getStyles()
getTerrain()
function camelize(str) {
return str.replace(/[-_\s]+(.)?/g, function(match, char) {
return char ? char.toUpperCase() : '';
});
}
async function getVectorLimitsLayers() {
try {
const response = await axios.get('https://tilemaps.icgc.cat/vt/limits-tilejsonV3.json');
const tileJson = response.data;
if (tileJson.vector_layers && Array.isArray(tileJson.vector_layers)) {
const vectorLayerIds = tileJson.vector_layers.map((layer) => layer.id);
vectorLimitsLayersOptions = vectorLayerIds.map((id) => ({
name: camelize(id),
key: id.toLowerCase().replace(/ /g, '_'),
}));
console.log('Dades Limits vector actualitzats' );
} else {
console.log('no vector-layers');
}
} catch (error) {
console.error('Error fetching limits vector-layers:', error.message);
}
}
async function getFGBLimitsLayers() {
try {
const response = await axios.get('https://tilemaps.icgc.cat/vt/limits-tilejsonV3.json');
const tileJson = response.data;
if (tileJson.vector_layers && Array.isArray(tileJson.vector_layers)) {
const fgbLayerIds = tileJson.vector_layers
.filter(layer => layer.id.toLowerCase().includes('vigent'))
.map((layer) => ({
name: camelize(layer.id),
key: layer.id.toLowerCase().replace(/ /g, '_'),
url: `https://tilemaps.icgc.cat/vector/fgb/${layer.id.toLowerCase().replace(/ /g, '_')}.fgb`,
}));
console.log('fgb', fgbLayerIds)
fgbLayersOptions = fgbLayerIds
console.log('Dades Limits fgb actualitzats');
} else {
console.log('no fgb-layers');
}
} catch (error) {
console.error('Error fetching limits fgb-layers:', error.message);
}
}
async function getOrtoWMSLayers() {
try {
axios.get("https://geoserveis.icgc.cat/servei/catalunya/orto-territorial/wms?request=GetCapabilities&service=WMS", {
httpsAgent
})
.then(function (response) {
const xmlData = response.data;
// Parse XML to JavaScript object
parseString(xmlData, (err, result) => {
if (err) {
console.error('Error parsing XML:', err);
return;
}
// console.log('result', result)
// Extract layer names from the parsed result
const layers = result?.WMS_Capabilities?.Capability[0]?.Layer[0]?.Layer;
// console.log('layers',layers)
if (layers) {
const vectorArray = layers.map(layer => ({
name: camelize(layer.Name[0]),
key: layer.Name[0],
url: `https://geoserveis.icgc.cat/servei/catalunya/orto-territorial/wms?&bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&STYLES=&srs=EPSG:3857&transparent=true&width=256&height=256&layers=${layer.Name[0]}`,
}));
ortoLayersOptions = vectorArray
} else {
console.error('No layers found in the GetCapabilities response.');
}
});
})
.catch(function (error) {
console.log(error);
});
} catch (error) {
console.error('Error fetching orto wms-layers:', error.message);
}
}
async function getVectorLayers() {
try {
let test= [
{ key: 'cobertes2018', url: "https://tilemaps.icgc.cat/tileserver/cobertes_tilejson.json", legend: "https://eines.icgc.cat/recursos//images/llegendaCobertesSol2018.jpg"}
]
vectorLayersOptions = test
console.info('Dades vector Layers actualitzats' )
} catch (error) {
console.error('Error fetching vector-layers:', error.message);
}
}
async function getWMSLayers() {
try {
let layerRelleu = 'relleu'
let layerGeologia = 'geologia'
let layerOsm = 'osm_suau'
let layerCims = '0'
let layerCobertesSol = 'cobertes_2009'
let test= [
{ key: 'espaisInteresGeologic', layer:"", url: `https://geoserveis.icgc.cat/servei/catalunya/espais-interes-geologic/wms/service?&service=WMS&request=GetMap&layers=espais-interes-geologic&styles=&format=image%2Fpng&transparent=true&version=1.1.1&tipus=wms&width=512&height=512&srs=EPSG%3A3857&bbox={bbox-epsg-3857}`},
{ key: 'gravimetriaBouguer500000', layer:"", url: `https://geoserveis.icgc.cat/servei/catalunya/gravimetria/wms/service?&service=WMS&request=GetMap&layers=anomalia_bouguer_500000&styles=&format=image%2Fpng&transparent=true&version=1.1.1&tipus=wms&width=512&height=512&srs=EPSG%3A3857&bbox={bbox-epsg-3857}`},
{ key: 'cobertesSol2018', layer:"", url: `https://geoserveis.icgc.cat/servei/catalunya/cobertes-sol/wms/service?&service=WMS&request=GetMap&layers=cobertes_2018&styles=&format=image%2Fpng&transparent=true&version=1.1.1&tipus=wms&width=512&height=512&srs=EPSG%3A3857&bbox={bbox-epsg-3857}`},
{ key: 'administratiu', layer:"", url:`http://geoserveis.icgc.cat/servei/catalunya/mapa-base/wmts/administratiu/MON3857NW/{z}/{x}/{y}.png`},
{ key: 'simplificat', layer:"", url:`http://geoserveis.icgc.cat/servei/catalunya/mapa-base/wmts/simplificat/MON3857NW/{z}/{x}/{y}.png`},
{ key:"cims", layer:"", url:`https://geoserveis.icgc.cat/icc_100cims/wms/service?REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&LAYERS=${layerCims}&STYLES=&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&SRS=EPSG:25831&BBOX=137118.923076923,4488408.75,650959.076923077,4749634.75&WIDTH=895&HEIGHT=455`},
{ key: 'cobertesSol', layer:"", url: `http://geoserveis.icgc.cat/servei/catalunya/cobertes-sol/wms?REQUEST=GetMap&SERVICE=WMS&VERSION=1.3.0&LAYERS=${layerCobertesSol}&STYLES=&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE&CRS=EPSG:25831&BBOX=374110.828167253,4639230.79853085,452621.120632226,4703578.45000215&WIDTH=1020&HEIGHT=836`},
]
;
wmsLayersOptions = test
console.info('Dades WMS Layers actualitzats' );
} catch (error) {
console.error('Error fetching wms-layers:', error.message);
}
}
async function getStyles() {
try {
let test= [
{ key: "TOPO", url: "https://geoserveis.icgc.cat/styles/icgc_mapa_base_topografic.json",image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_mapa_estandard.png" },
{ key: "ORTO", url: "https://geoserveis.icgc.cat/styles/icgc_orto_estandard.json" , image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_orto_estandard.png"},
{ key: "ORTO3D", url: "https://eines.icgc.cat/recursos//styles/icgc_orto_3d.json" , image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_orto_estandard.png"},
{ key: "ORTOHYBRID", url: "https://geoserveis.icgc.cat/styles/icgc_orto_hibrida.json" , image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_orto_hibrida.png"},
{ key: "ADMIN", url: "https://geoserveis.icgc.cat/styles/icgc_limits_administratius_gris.json" , image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_delimitacio_limits_administratius.png"},
{ key: "DARK", url: "https://geoserveis.icgc.cat/styles/icgc_mapa_base_fosc.json" , image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_mapa_base_fosc.png"},
{ key: "LIGHT", url: "https://geoserveis.icgc.cat/styles/icgc_mapa_base_gris.json", image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_mapa_base_gris.png" },
{ key: "GEOLOGY", url: "https://geoserveis.icgc.cat/styles/icgc_geologic_informacio.json", image: "https://visors.icgc.cat/contextmaps/imatges_estil/icgc_geologic_informacio.png" },
]
stylesOptions = test
console.info('Dades styles actualitzats' );
} catch (error) {
console.error('Error fetching style -layers:', error.message);
}
}
async function getTerrain() {
try {
let test= [
{name: "ICGC5M", url: "https://tilemaps.icgc.cat/tileserver/tileserver/terreny-5m-30m-rgb-extent/{z}/{x}/{y}.png"},
{name: "WORLD30M", url: "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png", encoding: "terrarium",}
]
terrainOptions = test
console.info('Dades terrain Layers actualitzats' );
} catch (error) {
console.error('Error fetching terrain -layers:', error.message);
}
}
setTimeout(() => {
replace()
}, 5000);
function replace(){
defaultOptions.vectorLayersICGC = vectorLimitsLayersOptions
defaultOptions.vectorLayers = vectorLayersOptions
defaultOptions.fgbLayers = fgbLayersOptions
defaultOptions.ortoLayersICGC = ortoLayersOptions
defaultOptions.wmsLayers = wmsLayersOptions
defaultOptions.baseStyles = stylesOptions
defaultOptions.urlTerrainICGC = terrainOptions
function stringifyWithoutQuotes(obj, indent = 0) {
const padding = ' '.repeat(indent);
let result = '';
for (let key in obj) {
const value = obj[key];
key = camelize(key)
const formattedValue = typeof value === 'object'
? stringifyWithoutQuotes(value, indent + 2)
: JSON.stringify(value);
result += `${padding}${key}: ${formattedValue},\n`;
}
return `{\n${result}${' '.repeat(Math.max(indent - 2, 0))}}`;
}
// Convierte el objeto defaultOptions a una cadena JSON
const updatedConfigNode = `
const urlImages = "https://visors.icgc.cat/contextmaps/imatges_estil/";
const urlStyles = "https://geoserveis.icgc.cat/contextmaps/";
const defaultOptions = ${stringifyWithoutQuotes(defaultOptions, null, 2)};
\nmodule.exports = defaultOptions;
`;
const updatedConfig = `
const urlImages = "https://visors.icgc.cat/contextmaps/imatges_estil/";
const urlStyles = "https://geoserveis.icgc.cat/contextmaps/";
const defaultOptions = ${stringifyWithoutQuotes(defaultOptions, null, 2)};
\nexport default defaultOptions;
`;
// Escribe el contenido actualizado en el archivo config.js
fs.writeFileSync(configNodeFilePath, updatedConfigNode);
fs.writeFileSync(configFilePath, updatedConfig)
setTimeout(() => {
replaceConstants()
}, 5000);
console.log('Variables actualitzades correctament a config.js');
}
function replaceConstants(){
// console.log('replaceeee')
function stringifyWithoutQuotes(obj, indent = 0) {
const padding = ' '.repeat(indent);
let result = '';
for (let key in obj) {
let value = obj[key];
//start camelize
key = camelize(key)
// console.log('v', key)
//end camelize
const formattedValue = typeof value === 'object'
? stringifyWithoutQuotes(value, indent + 2)
: JSON.stringify(value);
result += `${padding}${key}: ${formattedValue},\n`;
}
return `{\n${result}${' '.repeat(Math.max(indent - 2, 0))}}`;
}
//baseStyles
const convertedObject = {};
let originalObject = defaultOptions.baseStyles
for (const key in originalObject) {
if (originalObject.hasOwnProperty(key)) {
const item = originalObject[key];
convertedObject[item.key] = item.url;
}
}
const stylesConfig = `
const Styles = ${stringifyWithoutQuotes(convertedObject, null, 2)};
\nexport default Styles;
`;
// Escribe el contenido actualizado en el archivo config.js
fs.writeFileSync(stylePath, stylesConfig);
//setTerrain
const convertedTerrain = {};
let originalTerrain = defaultOptions.urlTerrainICGC
for (const key in originalTerrain) {
if (originalTerrain.hasOwnProperty(key)) {
const item = originalTerrain[key];
convertedTerrain[item.name] = item.url;
}
}
const terrainConfig = `
const Terrains = ${stringifyWithoutQuotes(convertedTerrain, null, 2)};
\nexport default Terrains;
`;
// Escribe el contenido actualizado en el archivo config.js
fs.writeFileSync(terrainPath, terrainConfig);
//setlayers
let originalOrtoLayers = defaultOptions.ortoLayersICGC
const convertedOrtoLayers = {};
for (const key in originalOrtoLayers) {
if (originalOrtoLayers.hasOwnProperty(key)) {
const item = originalOrtoLayers[key];
convertedOrtoLayers[item.name] = item.url;
}
}
let originalVectorICGCLayers = defaultOptions.vectorLayersICGC
const convertedVectorICGCLayers = {};
for (const key in originalVectorICGCLayers) {
if (originalVectorICGCLayers.hasOwnProperty(key)) {
const item = originalVectorICGCLayers[key];
convertedVectorICGCLayers[item.name] = item.key;
}
}
let originalFGBICGCLayers = defaultOptions.fgbLayers
const convertedFGBICGCLayers = {};
for (const key in originalFGBICGCLayers) {
if (originalFGBICGCLayers.hasOwnProperty(key)) {
const item = originalFGBICGCLayers[key];
convertedFGBICGCLayers[item.name] = item.url;
}
console.log('con', convertedFGBICGCLayers)
}
let originalVectorLayers = defaultOptions.vectorLayers
const convertedVectorLayers = {};
for (const key in originalVectorLayers) {
if (originalVectorLayers.hasOwnProperty(key)) {
const item = originalVectorLayers[key];
convertedVectorLayers[item.key] = item.url;
}
}
let originalWmsLayers = defaultOptions.wmsLayers
const convertedWmsLayers = {};
for (const key in originalWmsLayers) {
if (originalWmsLayers.hasOwnProperty(key)) {
const item = originalWmsLayers[key];
convertedWmsLayers[item.key] = item.url;
}
}
const layersConfig = `
const Orto = ${stringifyWithoutQuotes(convertedOrtoLayers, null, 2)};
const VectorAdmin = ${stringifyWithoutQuotes(convertedVectorICGCLayers, null, 2)};
const FGBAdmin = ${stringifyWithoutQuotes(convertedFGBICGCLayers, null, 2)}
const Vector = ${stringifyWithoutQuotes(convertedVectorLayers, null, 2)};
const WMS = ${stringifyWithoutQuotes(convertedWmsLayers, null, 2)};
\nexport default {Orto,VectorAdmin, WMS, FGBAdmin, Vector};
`;
// Escribe el contenido actualizado en el archivo config.js
fs.writeFileSync(layersPath, layersConfig);
console.log ('converted', convertedObject)
}