@opengis/gis 0.2.206 → 0.2.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +181 -181
  2. package/dist/{CardIcon-Das_4-zP.js → CardIcon-iGo8QJu9.js} +1 -1
  3. package/dist/{EntityTablePage-VnczwXQf.js → EntityTablePage-BiVoVAhN.js} +2 -2
  4. package/dist/{ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-CSmMFcSb.js → ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-BdL5cQgN.js} +1 -1
  5. package/dist/{MapSettings-DQ8UiQm3.js → MapSettings-CXqiYxV4.js} +2 -2
  6. package/dist/{MapToolbarRight.vue_vue_type_script_setup_true_lang-D1pkL5Zi.js → MapToolbarRight.vue_vue_type_script_setup_true_lang-BHCChxmg.js} +1 -1
  7. package/dist/{RastersTablePage-P5UlG30e.js → RastersTablePage-Dgk1wp3d.js} +2 -2
  8. package/dist/{cartocss-jQ06BuDY.js → cartocss-BIpgQU6F.js} +6 -6
  9. package/dist/{import-utils-BSzqf3hF.js → import-utils-CfXKgZFd.js} +1 -1
  10. package/dist/{index-Bbe7SAdS.js → index-CM_DTEUa.js} +49 -43
  11. package/dist/index.css +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/index.umd.cjs +48 -42
  14. package/dist/{raster-C9rmMnma.js → raster-D_vEBha8.js} +3 -3
  15. package/dist/{register-j_ATka3H.js → register-C5_Un1Ms.js} +3 -3
  16. package/dist/service-JN82JN81.js +3676 -0
  17. package/dist/{vs-datatable-Zl_KZIHN.js → vs-datatable--K-DxayK.js} +2 -2
  18. package/module/gis/cls/service_type.json +42 -42
  19. package/module/gis/form/gis.registers.form.json +127 -127
  20. package/module/test/map/addr.json +302 -302
  21. package/module/test/map/addr1.yml +23 -23
  22. package/module/test/select/address.str_id.sql +3 -3
  23. package/package.json +79 -79
  24. package/plugin.js +64 -64
  25. package/server/migrations/array_intersect.sql +13 -13
  26. package/server/migrations/cartocss.sql +27 -27
  27. package/server/migrations/group_list.sql +74 -74
  28. package/server/migrations/maps.sql +30 -30
  29. package/server/migrations/metadata.sql +415 -415
  30. package/server/migrations/ogc.sql +106 -106
  31. package/server/migrations/rasters.sql +265 -265
  32. package/server/migrations/services.sql +248 -247
  33. package/server/migrations/services_users_rel.sql +22 -22
  34. package/server/migrations/widgets.sql +20 -20
  35. package/server/plugins/crons.js +21 -21
  36. package/server/plugins/mapnik/funcs/checkRasterFile.js +107 -107
  37. package/server/plugins/mapnik/funcs/mapnik.js +151 -151
  38. package/server/plugins/mapnik/funcs/rootFolder.mjs +8 -8
  39. package/server/plugins/mapnik/map.proto +437 -437
  40. package/server/plugins/vite.js +74 -74
  41. package/server/routes/cartocss/cartocss.route.js +31 -31
  42. package/server/routes/cartocss/cartocss.route.test.js +78 -78
  43. package/server/routes/cartocss/cartocss.schema.js +76 -73
  44. package/server/routes/cartocss/functions/add.cartocss.js +39 -39
  45. package/server/routes/cartocss/functions/get.cartocss.js +57 -57
  46. package/server/routes/gis/functions/checkXML.js +107 -107
  47. package/server/routes/gis/functions/dashboard.js +24 -24
  48. package/server/routes/gis/gis.route.js +26 -26
  49. package/server/routes/gis/gis.route.test.js +33 -33
  50. package/server/routes/map/fonts/index.js +1933 -1933
  51. package/server/routes/map/functions/clearTiles.js +35 -35
  52. package/server/routes/map/functions/geojson.js +187 -187
  53. package/server/routes/map/functions/gis.convert.js +65 -65
  54. package/server/routes/map/functions/gis.icon.js +327 -327
  55. package/server/routes/map/functions/jsonData.js +206 -206
  56. package/server/routes/map/functions/layerList.js +144 -144
  57. package/server/routes/map/functions/map.js +122 -122
  58. package/server/routes/map/functions/mapCatalog.js +72 -72
  59. package/server/routes/map/functions/mapCatalogAttribute.js +53 -53
  60. package/server/routes/map/functions/mapFeatures.js +133 -133
  61. package/server/routes/map/functions/mapFormat.js +246 -246
  62. package/server/routes/map/functions/mapTiles.js +162 -162
  63. package/server/routes/map/functions/maps.js +15 -15
  64. package/server/routes/map/functions/marker_icon.js +43 -43
  65. package/server/routes/map/functions/parseGeoFile.js +85 -85
  66. package/server/routes/map/functions/pointHeight.js +51 -51
  67. package/server/routes/map/functions/vtile.js +174 -174
  68. package/server/routes/map/functions/vtile1.js +191 -191
  69. package/server/routes/map/map.route.js +283 -283
  70. package/server/routes/map/map.route.test.js +693 -693
  71. package/server/routes/map/map.schema.js +356 -356
  72. package/server/routes/map/pin/circle1.svg +76 -76
  73. package/server/routes/map/pin/circle2.svg +18 -18
  74. package/server/routes/map/pin/none.svg +12 -12
  75. package/server/routes/map/pin/pin.svg +14 -14
  76. package/server/routes/map/pin/pin1.svg +16 -16
  77. package/server/routes/map/pin/pin2.svg +7 -7
  78. package/server/routes/map/pin/pin3.svg +34 -34
  79. package/server/routes/map/pin/pin4.svg +14 -14
  80. package/server/routes/map/pin/pin5.svg +7 -7
  81. package/server/routes/map/pin/pin6.svg +12 -12
  82. package/server/routes/map/pin/pin7.svg +12 -12
  83. package/server/routes/mapnik/functions/cartoBounds.js +32 -32
  84. package/server/routes/mapnik/functions/clearTiles.js +103 -103
  85. package/server/routes/mapnik/functions/convertBoundToTile.js +27 -27
  86. package/server/routes/mapnik/functions/createXml.js +76 -76
  87. package/server/routes/mapnik/functions/createXmlMulti.js +72 -72
  88. package/server/routes/mapnik/functions/fileSearch.js +35 -35
  89. package/server/routes/mapnik/functions/fileStat.js +28 -28
  90. package/server/routes/mapnik/functions/pretile.js +138 -138
  91. package/server/routes/mapnik/functions/rasterInfo.js +66 -66
  92. package/server/routes/mapnik/functions/readDir.js +22 -22
  93. package/server/routes/mapnik/functions/rtile.js +164 -164
  94. package/server/routes/mapnik/functions/uploadRaster.js +152 -152
  95. package/server/routes/mapnik/functions/uploadXML.js +140 -140
  96. package/server/routes/mapnik/mapnik.route.js +102 -102
  97. package/server/routes/mapnik/mapnik.route.test.js +370 -370
  98. package/server/routes/mapnik/mapnik.schema.mjs +195 -195
  99. package/server/routes/mapnik.prefixless/functions/mapnikLogger.js +31 -31
  100. package/server/routes/mapnik.prefixless/functions/mapnikStat.js +21 -21
  101. package/server/routes/mapnik.prefixless/mapnik.prefixless.route.js +20 -20
  102. package/server/routes/mapnik.prefixless/mapnik.prefixless.route.test.js +91 -91
  103. package/server/routes/mapnik.prefixless/mapnik.prefixless.schema.mjs +27 -27
  104. package/server/routes/maps/functions/add.map.js +42 -42
  105. package/server/routes/maps/functions/del.map.js +17 -17
  106. package/server/routes/maps/functions/get.map.js +119 -119
  107. package/server/routes/maps/maps.route.js +58 -58
  108. package/server/routes/maps/maps.route.test.js +231 -231
  109. package/server/routes/maps/maps.schema.js +9 -9
  110. package/server/routes/metadata/functions/editMetadata.js +35 -35
  111. package/server/routes/metadata/functions/metadataXML.js +19 -19
  112. package/server/routes/metadata/metadata.route.js +26 -26
  113. package/server/routes/metadata/metadata.route.test.js +46 -46
  114. package/server/routes/metadata/metadata.schema.js +53 -53
  115. package/server/routes/ogc/functions/map.info.point.js +117 -117
  116. package/server/routes/ogc/ogc.route.js +21 -21
  117. package/server/routes/ogc/ogc.route.test.js +53 -53
  118. package/server/routes/ogc/ogc.schema.js +16 -16
  119. package/server/routes/permissions/functions/catalog.permissions.edit.js +20 -20
  120. package/server/routes/permissions/functions/catalog.permissions.js +12 -12
  121. package/server/routes/permissions/functions/get.permissions.js +59 -59
  122. package/server/routes/permissions/functions/gis.catalog.js +80 -80
  123. package/server/routes/permissions/permissions.route.js +35 -35
  124. package/server/routes/permissions/permissions.route.test.js +35 -35
  125. package/server/routes/permissions/permissions.schema.js +43 -43
  126. package/server/routes/registers/functions/add.registry.js +37 -37
  127. package/server/routes/registers/functions/classifiers.js +100 -100
  128. package/server/routes/registers/functions/columns.js +3 -3
  129. package/server/routes/registers/functions/content.type.js +9 -9
  130. package/server/routes/registers/functions/del.registry.js +14 -14
  131. package/server/routes/registers/functions/get.info.js +93 -93
  132. package/server/routes/registers/functions/getFilterSQLParametrized/formatValue.js +328 -328
  133. package/server/routes/registers/functions/getFilterSQLParametrized/getFilterQuery.js +126 -126
  134. package/server/routes/registers/functions/getFilterSQLParametrized/getRangeQuery.js +353 -353
  135. package/server/routes/registers/functions/getFilterSQLParametrized/index.js +81 -81
  136. package/server/routes/registers/functions/getTableColumnMeta.js +148 -148
  137. package/server/routes/registers/functions/gis.export.js +159 -159
  138. package/server/routes/registers/functions/gis.registry.js +71 -71
  139. package/server/routes/registers/functions/gis.registry.list.js +61 -61
  140. package/server/routes/registers/functions/gis.suggest.js +310 -310
  141. package/server/routes/registers/functions/handleRegistryRequest.js +178 -178
  142. package/server/routes/registers/functions/map.registry.js +87 -87
  143. package/server/routes/registers/registers.route.js +92 -92
  144. package/server/routes/registers/registers.route.test.js +173 -173
  145. package/server/routes/registers/registers.schema.js +276 -276
  146. package/server/routes/root.mjs +3 -3
  147. package/server/routes/services/functions/add.service.js +62 -62
  148. package/server/routes/services/functions/create.route.js +93 -93
  149. package/server/routes/services/functions/del.service.js +12 -12
  150. package/server/routes/services/functions/get.layer.geom.js +27 -27
  151. package/server/routes/services/functions/get.service.users.js +76 -76
  152. package/server/routes/services/functions/get.services.col.js +34 -34
  153. package/server/routes/services/functions/get.services.js +124 -124
  154. package/server/routes/services/functions/legend.auto.js +77 -77
  155. package/server/routes/services/services.route.js +76 -76
  156. package/server/routes/services/services.route.test.js +171 -171
  157. package/server/routes/services/services.schema.js +459 -459
  158. package/server/routes/widgets/functions/add.widget.js +38 -38
  159. package/server/routes/widgets/functions/del.widget.js +17 -17
  160. package/server/routes/widgets/functions/get.widget.js +41 -41
  161. package/server/routes/widgets/widgets.route.js +61 -61
  162. package/server/routes/widgets/widgets.route.test.js +144 -144
  163. package/server/routes/widgets/widgets.schema.js +24 -24
  164. package/server/shared/listFiles.js +14 -14
  165. package/utils.js +13 -13
  166. package/dist/service-DJAlc1rE.js +0 -3484
@@ -1,327 +1,327 @@
1
- import { mkdir, readFile, writeFile } from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { existsSync } from 'node:fs';
4
- import { fileURLToPath } from 'node:url';
5
-
6
- import * as solidIcons from '@fortawesome/free-solid-svg-icons';
7
- import sharp from 'sharp';
8
- import pathBounds from 'svg-path-bounds';
9
- import { openSync } from 'fontkit';
10
-
11
- import { BadRequestError, NotFoundError } from '@opengis/fastify-table/errors.js';
12
-
13
- import fonts from '../fonts/index.js';
14
-
15
- const iconFilenamePattern = /^([a-z][a-z0-9]*)-(sm|s|m|l|xl)-(fa|ar|ab|ty|mi|gl|ma|mn)-([\p{L}\p{N}_-]+)\+([a-z]+|[a-f0-9]{3,8})\.(png|svg)$/iu;
16
-
17
- const validColorLengths = new Set([3, 4, 6, 8]);
18
- const validColors = new Set(['red', 'green', 'black', 'gray', 'yellow', 'white', 'blue', 'orange', 'purple', 'pink', 'brown']);
19
-
20
- const iconSizeByCode = {
21
- s: 24,
22
- m: 32,
23
- l: 48,
24
- xl: 64,
25
- };
26
-
27
- const contentTypeByFormat = {
28
- png: 'image/png',
29
- svg: 'image/svg+xml',
30
- };
31
-
32
- // Templates with a transparent or white center can safely recolor their background.
33
- const pinConfig = {
34
- pin: { x: 98, y: 99, iconSize: 110 },
35
- pin1: { x: 237, y: 205, iconSize: 200 },
36
- pin2: {
37
- x: 195, y: 185, iconSize: 210, recolorBackground: true,
38
- },
39
- pin3: {
40
- x: 197, y: 205, iconSize: 215, recolorBackground: true,
41
- },
42
- pin4: {
43
- x: 265, y: 230, iconSize: 250, recolorBackground: true,
44
- },
45
- pin5: {
46
- x: 205, y: 203, iconSize: 245, recolorBackground: true,
47
- },
48
- pin6: {
49
- x: 220, y: 215, iconSize: 281, recolorBackground: true,
50
- },
51
- pin7: {
52
- x: 220, y: 215, iconSize: 281, recolorBackground: true,
53
- },
54
- circle1: { x: 321, y: 308, iconSize: 260 },
55
- circle2: { x: 251, y: 248, iconSize: 300 },
56
- none: { x: 250, y: 250, iconSize: 300 },
57
- };
58
-
59
- const dirname = path.dirname(fileURLToPath(import.meta.url));
60
- const pinDirectory = path.join(dirname, '../pin');
61
- const iconDirectory = path.join(process.cwd(), 'log', 'icon');
62
- const iconDirectoryReady = mkdir(iconDirectory, { recursive: true });
63
-
64
- function parseIconFilename(filename) {
65
- const match = iconFilenamePattern.exec(filename);
66
- if (!match) return null;
67
-
68
- const [, pinName, sizeCode, iconType, iconValue, colorEl, format] = match;
69
- const isHexColor = colorEl && /[a-f0-9]{3,8}/.test(colorEl.toLowerCase()); // hex or plain text
70
- if (isHexColor && !validColorLengths.has(colorEl.length)) return null;
71
- if (!isHexColor && !validColors.has(colorEl)) return null;
72
-
73
- const iconFont = iconType.toLowerCase();
74
-
75
- const iconValue1 = iconFont === 'fa'
76
- ? iconValue.toLowerCase()
77
- : iconValue.toUpperCase();
78
-
79
- return {
80
- filename: filename.toLowerCase(),
81
- pinName: pinName.toLowerCase(),
82
- size: iconSizeByCode[sizeCode.toLowerCase()],
83
- iconType: iconFont,
84
- iconValue: iconValue1,
85
- color: isHexColor ? `#${colorEl}` : colorEl,
86
- format: format.toLowerCase(),
87
- };
88
- }
89
-
90
- function findIcon(iconName, iconFont) {
91
- const exportName = `fa${iconName.replace(/(^|-)([a-z0-9])/g, (_, __, char) => char.toUpperCase())}`;
92
- const exportedIcon = solidIcons[exportName];
93
-
94
- if (exportedIcon?.prefix === 'fas') return exportedIcon;
95
-
96
- const icon1 = Object.values(solidIcons).find(icon => (
97
- icon?.prefix === 'fas' && icon.iconName === iconName
98
- ));
99
-
100
- if (icon1) return icon1;
101
-
102
- const { ar, font: fontName } = fonts[iconFont] || {};
103
- const iconCode = fonts[iconFont].ar?.[iconName.toLowerCase()];
104
-
105
- if (!fontName || !iconCode || !ar?.[iconName.toLowerCase()]) {
106
- return null;
107
- }
108
-
109
- const dirname = path.dirname(fileURLToPath(import.meta.url));
110
-
111
- const fontPath = path.join(dirname, `../fonts/${fontName}`);
112
-
113
- if (!existsSync(fontPath)) {
114
- return null;
115
- }
116
-
117
- const font = openSync(fontPath);
118
- const unicodePoint = Number.parseInt(iconCode, 16);
119
- const glyph = font.glyphForCodePoint(unicodePoint);
120
-
121
- const { minX, minY, maxX, maxY } = glyph.bbox;
122
-
123
- return {
124
- prefix: font,
125
- iconName: iconName.toLowerCase(),
126
- icon: [
127
- maxX - minX,
128
- maxY - minY,
129
- iconName.toLowerCase(),
130
- 99,
131
- glyph.path.toSVG(),
132
- ],
133
- bbox: glyph.bbox,
134
- };
135
- }
136
-
137
- async function readFileIfExists(filePath, encoding) {
138
- try {
139
- return await readFile(filePath, encoding);
140
- }
141
- catch (error) {
142
- if (error.code !== 'ENOENT') throw error;
143
- return null;
144
- }
145
- }
146
-
147
- function getSvgAttribute(svg, attribute, fallback) {
148
- const match = new RegExp(`\\b${attribute}="([0-9.]+)"`, 'i').exec(svg);
149
- return match ? Number(match[1]) : fallback;
150
- }
151
-
152
- function getPinPlacement(pinName, width, height) {
153
- return pinConfig[pinName] || {
154
- x: width / 2,
155
- y: height / 2,
156
- iconSize: Math.min(width, height) * 0.55,
157
- };
158
- }
159
-
160
- function createSymbolSvg(symbol, placement, color) {
161
- const fontSize = Math.round(placement.iconSize * 0.75);
162
-
163
- return `<text data-gis-icon-layer="foreground" x="${placement.x}" y="${placement.y}" fill="${color}" font-family="Arial, sans-serif" font-size="${fontSize}" font-weight="700" text-anchor="middle" dominant-baseline="middle">${symbol}</text>`;
164
- }
165
-
166
- // ! if icon is vertically inverted -> invertedYAxis should be changed for this font type
167
- // ? example: fa-smile - image coordinate system Y-axis increases upward => invertedYAxis=false => OK
168
- // ? example 2: ma-oil-well - image coordinate system Y-axis is inverted => invertedYAxis=true => OK
169
- function createFontIconSvg(icon, placement, color, invertedYAxis = true) {
170
- const [, , , , pathData] = icon.icon;
171
- const paths = Array.isArray(pathData) ? pathData : [pathData];
172
-
173
- const bounds = paths.reduce(
174
- (result, path) => {
175
- const [minX, minY, maxX, maxY] = pathBounds(path);
176
-
177
- return {
178
- minX: Math.min(result.minX, minX),
179
- minY: Math.min(result.minY, minY),
180
- maxX: Math.max(result.maxX, maxX),
181
- maxY: Math.max(result.maxY, maxY),
182
- };
183
- },
184
- {
185
- minX: Infinity,
186
- minY: Infinity,
187
- maxX: -Infinity,
188
- maxY: -Infinity,
189
- },
190
- );
191
-
192
- const width = bounds.maxX - bounds.minX;
193
- const height = bounds.maxY - bounds.minY;
194
-
195
- const scale = placement.iconSize / Math.max(width, height);
196
-
197
- const centerX = (bounds.minX + bounds.maxX) / 2;
198
- const centerY = (bounds.minY + bounds.maxY) / 2;
199
-
200
- return `<g
201
- data-gis-icon-layer="foreground"
202
- transform="
203
- translate(${placement.x} ${placement.y})
204
- scale(${scale} ${invertedYAxis ? -scale : scale})
205
- translate(${-centerX} ${-centerY})
206
- "
207
- fill="${color}"
208
- >
209
- ${paths.map(path => `<path d="${path}"/>`).join('')}
210
- </g>`;
211
- }
212
-
213
- function createForegroundSvg(options, placement) {
214
- if (options.iconValue && options.iconValue.length === 1) {
215
- return createSymbolSvg(options.iconValue, placement, options.color);
216
- }
217
-
218
- const icon = findIcon(options.iconValue, options.iconType);
219
-
220
- return icon
221
- ? createFontIconSvg(icon, placement, options.color, options.iconType !== 'fa')
222
- : null;
223
- }
224
-
225
- function setSvgViewport(svg, size, width, height) {
226
- return svg.replace(/<svg\b([^>]*)>/i, (_, attributes) => {
227
- const rootAttributes = attributes
228
- .replace(/\swidth="[^"]*"/i, '')
229
- .replace(/\sheight="[^"]*"/i, '')
230
- .replace(/\sviewBox="[^"]*"/i, '');
231
-
232
- return `<svg${rootAttributes} width="${size}" height="${size}" viewBox="0 0 ${width} ${height}">`;
233
- });
234
- }
235
-
236
- async function createPinSvg({
237
- pinName,
238
- size,
239
- color,
240
- ...iconOptions
241
- }) {
242
- const templatePath = path.join(pinDirectory, `${pinName}.svg`);
243
- const template = await readFileIfExists(templatePath, 'utf8');
244
-
245
- if (!template) return null;
246
-
247
- const width = getSvgAttribute(template, 'width', 512);
248
- const height = getSvgAttribute(template, 'height', 512);
249
-
250
- const placement = getPinPlacement(pinName, width, height);
251
-
252
- const coloredTemplate = placement.recolorBackground
253
- ? template.replace(/#ff0000/gi, color)
254
- : template;
255
-
256
- const svg = setSvgViewport(
257
- coloredTemplate,
258
- size,
259
- width,
260
- height
261
- );
262
-
263
- const foreground = createForegroundSvg(
264
- { ...iconOptions, color },
265
- placement,
266
- );
267
-
268
- if (!foreground) return null;
269
-
270
- return svg.replace(
271
- /<\/svg>\s*$/i,
272
- `${foreground}</svg>`
273
- );
274
- }
275
-
276
- async function createIcon(options) {
277
- const svg = await createPinSvg({
278
- ...options,
279
- });
280
- if (!svg) return null;
281
- if (options.format === 'svg') return Buffer.from(svg);
282
-
283
- return sharp(Buffer.from(svg)).png().toBuffer();
284
- }
285
-
286
- async function getIcon(options, nocache = false) {
287
- await iconDirectoryReady;
288
-
289
- const iconPath = path.join(iconDirectory, options.filename);
290
- if (!nocache) {
291
- const cachedIcon = await readFileIfExists(iconPath);
292
- if (cachedIcon) return cachedIcon;
293
- }
294
-
295
- const icon = await createIcon(options);
296
- if (!icon) return null;
297
-
298
- await writeFile(iconPath, icon);
299
- return icon;
300
- }
301
-
302
- export default async function gisIconApi({ name, nocache }) {
303
- if (!name) {
304
- throw BadRequestError('not enough params: name');
305
- }
306
-
307
- const options = parseIconFilename(name);
308
-
309
- if (!options) {
310
- throw BadRequestError('invalid icon name');
311
- }
312
-
313
- // fixed icon size of 32px
314
- const image = await getIcon({ ...options, size: 32 }, nocache);
315
-
316
- if (!image) {
317
- throw NotFoundError('icon not found');
318
- }
319
-
320
- return {
321
- headers: {
322
- 'Content-Type': contentTypeByFormat[options.format],
323
- 'Cache-Control': nocache ? 'no-cache' : 'public, max-age=31536000, immutable'
324
- },
325
- data: image
326
- }
327
- }
1
+ import { mkdir, readFile, writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { existsSync } from 'node:fs';
4
+ import { fileURLToPath } from 'node:url';
5
+
6
+ import * as solidIcons from '@fortawesome/free-solid-svg-icons';
7
+ import sharp from 'sharp';
8
+ import pathBounds from 'svg-path-bounds';
9
+ import { openSync } from 'fontkit';
10
+
11
+ import { BadRequestError, NotFoundError } from '@opengis/fastify-table/errors.js';
12
+
13
+ import fonts from '../fonts/index.js';
14
+
15
+ const iconFilenamePattern = /^([a-z][a-z0-9]*)-(sm|s|m|l|xl)-(fa|ar|ab|ty|mi|gl|ma|mn)-([\p{L}\p{N}_-]+)\+([a-z]+|[a-f0-9]{3,8})\.(png|svg)$/iu;
16
+
17
+ const validColorLengths = new Set([3, 4, 6, 8]);
18
+ const validColors = new Set(['red', 'green', 'black', 'gray', 'yellow', 'white', 'blue', 'orange', 'purple', 'pink', 'brown']);
19
+
20
+ const iconSizeByCode = {
21
+ s: 24,
22
+ m: 32,
23
+ l: 48,
24
+ xl: 64,
25
+ };
26
+
27
+ const contentTypeByFormat = {
28
+ png: 'image/png',
29
+ svg: 'image/svg+xml',
30
+ };
31
+
32
+ // Templates with a transparent or white center can safely recolor their background.
33
+ const pinConfig = {
34
+ pin: { x: 98, y: 99, iconSize: 110 },
35
+ pin1: { x: 237, y: 205, iconSize: 200 },
36
+ pin2: {
37
+ x: 195, y: 185, iconSize: 210, recolorBackground: true,
38
+ },
39
+ pin3: {
40
+ x: 197, y: 205, iconSize: 215, recolorBackground: true,
41
+ },
42
+ pin4: {
43
+ x: 265, y: 230, iconSize: 250, recolorBackground: true,
44
+ },
45
+ pin5: {
46
+ x: 205, y: 203, iconSize: 245, recolorBackground: true,
47
+ },
48
+ pin6: {
49
+ x: 220, y: 215, iconSize: 281, recolorBackground: true,
50
+ },
51
+ pin7: {
52
+ x: 220, y: 215, iconSize: 281, recolorBackground: true,
53
+ },
54
+ circle1: { x: 321, y: 308, iconSize: 260 },
55
+ circle2: { x: 251, y: 248, iconSize: 300 },
56
+ none: { x: 250, y: 250, iconSize: 300 },
57
+ };
58
+
59
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
60
+ const pinDirectory = path.join(dirname, '../pin');
61
+ const iconDirectory = path.join(process.cwd(), 'log', 'icon');
62
+ const iconDirectoryReady = mkdir(iconDirectory, { recursive: true });
63
+
64
+ function parseIconFilename(filename) {
65
+ const match = iconFilenamePattern.exec(filename);
66
+ if (!match) return null;
67
+
68
+ const [, pinName, sizeCode, iconType, iconValue, colorEl, format] = match;
69
+ const isHexColor = colorEl && /[a-f0-9]{3,8}/.test(colorEl.toLowerCase()); // hex or plain text
70
+ if (isHexColor && !validColorLengths.has(colorEl.length)) return null;
71
+ if (!isHexColor && !validColors.has(colorEl)) return null;
72
+
73
+ const iconFont = iconType.toLowerCase();
74
+
75
+ const iconValue1 = iconFont === 'fa'
76
+ ? iconValue.toLowerCase()
77
+ : iconValue.toUpperCase();
78
+
79
+ return {
80
+ filename: filename.toLowerCase(),
81
+ pinName: pinName.toLowerCase(),
82
+ size: iconSizeByCode[sizeCode.toLowerCase()],
83
+ iconType: iconFont,
84
+ iconValue: iconValue1,
85
+ color: isHexColor ? `#${colorEl}` : colorEl,
86
+ format: format.toLowerCase(),
87
+ };
88
+ }
89
+
90
+ function findIcon(iconName, iconFont) {
91
+ const exportName = `fa${iconName.replace(/(^|-)([a-z0-9])/g, (_, __, char) => char.toUpperCase())}`;
92
+ const exportedIcon = solidIcons[exportName];
93
+
94
+ if (exportedIcon?.prefix === 'fas') return exportedIcon;
95
+
96
+ const icon1 = Object.values(solidIcons).find(icon => (
97
+ icon?.prefix === 'fas' && icon.iconName === iconName
98
+ ));
99
+
100
+ if (icon1) return icon1;
101
+
102
+ const { ar, font: fontName } = fonts[iconFont] || {};
103
+ const iconCode = fonts[iconFont].ar?.[iconName.toLowerCase()];
104
+
105
+ if (!fontName || !iconCode || !ar?.[iconName.toLowerCase()]) {
106
+ return null;
107
+ }
108
+
109
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
110
+
111
+ const fontPath = path.join(dirname, `../fonts/${fontName}`);
112
+
113
+ if (!existsSync(fontPath)) {
114
+ return null;
115
+ }
116
+
117
+ const font = openSync(fontPath);
118
+ const unicodePoint = Number.parseInt(iconCode, 16);
119
+ const glyph = font.glyphForCodePoint(unicodePoint);
120
+
121
+ const { minX, minY, maxX, maxY } = glyph.bbox;
122
+
123
+ return {
124
+ prefix: font,
125
+ iconName: iconName.toLowerCase(),
126
+ icon: [
127
+ maxX - minX,
128
+ maxY - minY,
129
+ iconName.toLowerCase(),
130
+ 99,
131
+ glyph.path.toSVG(),
132
+ ],
133
+ bbox: glyph.bbox,
134
+ };
135
+ }
136
+
137
+ async function readFileIfExists(filePath, encoding) {
138
+ try {
139
+ return await readFile(filePath, encoding);
140
+ }
141
+ catch (error) {
142
+ if (error.code !== 'ENOENT') throw error;
143
+ return null;
144
+ }
145
+ }
146
+
147
+ function getSvgAttribute(svg, attribute, fallback) {
148
+ const match = new RegExp(`\\b${attribute}="([0-9.]+)"`, 'i').exec(svg);
149
+ return match ? Number(match[1]) : fallback;
150
+ }
151
+
152
+ function getPinPlacement(pinName, width, height) {
153
+ return pinConfig[pinName] || {
154
+ x: width / 2,
155
+ y: height / 2,
156
+ iconSize: Math.min(width, height) * 0.55,
157
+ };
158
+ }
159
+
160
+ function createSymbolSvg(symbol, placement, color) {
161
+ const fontSize = Math.round(placement.iconSize * 0.75);
162
+
163
+ return `<text data-gis-icon-layer="foreground" x="${placement.x}" y="${placement.y}" fill="${color}" font-family="Arial, sans-serif" font-size="${fontSize}" font-weight="700" text-anchor="middle" dominant-baseline="middle">${symbol}</text>`;
164
+ }
165
+
166
+ // ! if icon is vertically inverted -> invertedYAxis should be changed for this font type
167
+ // ? example: fa-smile - image coordinate system Y-axis increases upward => invertedYAxis=false => OK
168
+ // ? example 2: ma-oil-well - image coordinate system Y-axis is inverted => invertedYAxis=true => OK
169
+ function createFontIconSvg(icon, placement, color, invertedYAxis = true) {
170
+ const [, , , , pathData] = icon.icon;
171
+ const paths = Array.isArray(pathData) ? pathData : [pathData];
172
+
173
+ const bounds = paths.reduce(
174
+ (result, path) => {
175
+ const [minX, minY, maxX, maxY] = pathBounds(path);
176
+
177
+ return {
178
+ minX: Math.min(result.minX, minX),
179
+ minY: Math.min(result.minY, minY),
180
+ maxX: Math.max(result.maxX, maxX),
181
+ maxY: Math.max(result.maxY, maxY),
182
+ };
183
+ },
184
+ {
185
+ minX: Infinity,
186
+ minY: Infinity,
187
+ maxX: -Infinity,
188
+ maxY: -Infinity,
189
+ },
190
+ );
191
+
192
+ const width = bounds.maxX - bounds.minX;
193
+ const height = bounds.maxY - bounds.minY;
194
+
195
+ const scale = placement.iconSize / Math.max(width, height);
196
+
197
+ const centerX = (bounds.minX + bounds.maxX) / 2;
198
+ const centerY = (bounds.minY + bounds.maxY) / 2;
199
+
200
+ return `<g
201
+ data-gis-icon-layer="foreground"
202
+ transform="
203
+ translate(${placement.x} ${placement.y})
204
+ scale(${scale} ${invertedYAxis ? -scale : scale})
205
+ translate(${-centerX} ${-centerY})
206
+ "
207
+ fill="${color}"
208
+ >
209
+ ${paths.map(path => `<path d="${path}"/>`).join('')}
210
+ </g>`;
211
+ }
212
+
213
+ function createForegroundSvg(options, placement) {
214
+ if (options.iconValue && options.iconValue.length === 1) {
215
+ return createSymbolSvg(options.iconValue, placement, options.color);
216
+ }
217
+
218
+ const icon = findIcon(options.iconValue, options.iconType);
219
+
220
+ return icon
221
+ ? createFontIconSvg(icon, placement, options.color, options.iconType !== 'fa')
222
+ : null;
223
+ }
224
+
225
+ function setSvgViewport(svg, size, width, height) {
226
+ return svg.replace(/<svg\b([^>]*)>/i, (_, attributes) => {
227
+ const rootAttributes = attributes
228
+ .replace(/\swidth="[^"]*"/i, '')
229
+ .replace(/\sheight="[^"]*"/i, '')
230
+ .replace(/\sviewBox="[^"]*"/i, '');
231
+
232
+ return `<svg${rootAttributes} width="${size}" height="${size}" viewBox="0 0 ${width} ${height}">`;
233
+ });
234
+ }
235
+
236
+ async function createPinSvg({
237
+ pinName,
238
+ size,
239
+ color,
240
+ ...iconOptions
241
+ }) {
242
+ const templatePath = path.join(pinDirectory, `${pinName}.svg`);
243
+ const template = await readFileIfExists(templatePath, 'utf8');
244
+
245
+ if (!template) return null;
246
+
247
+ const width = getSvgAttribute(template, 'width', 512);
248
+ const height = getSvgAttribute(template, 'height', 512);
249
+
250
+ const placement = getPinPlacement(pinName, width, height);
251
+
252
+ const coloredTemplate = placement.recolorBackground
253
+ ? template.replace(/#ff0000/gi, color)
254
+ : template;
255
+
256
+ const svg = setSvgViewport(
257
+ coloredTemplate,
258
+ size,
259
+ width,
260
+ height
261
+ );
262
+
263
+ const foreground = createForegroundSvg(
264
+ { ...iconOptions, color },
265
+ placement,
266
+ );
267
+
268
+ if (!foreground) return null;
269
+
270
+ return svg.replace(
271
+ /<\/svg>\s*$/i,
272
+ `${foreground}</svg>`
273
+ );
274
+ }
275
+
276
+ async function createIcon(options) {
277
+ const svg = await createPinSvg({
278
+ ...options,
279
+ });
280
+ if (!svg) return null;
281
+ if (options.format === 'svg') return Buffer.from(svg);
282
+
283
+ return sharp(Buffer.from(svg)).png().toBuffer();
284
+ }
285
+
286
+ async function getIcon(options, nocache = false) {
287
+ await iconDirectoryReady;
288
+
289
+ const iconPath = path.join(iconDirectory, options.filename);
290
+ if (!nocache) {
291
+ const cachedIcon = await readFileIfExists(iconPath);
292
+ if (cachedIcon) return cachedIcon;
293
+ }
294
+
295
+ const icon = await createIcon(options);
296
+ if (!icon) return null;
297
+
298
+ await writeFile(iconPath, icon);
299
+ return icon;
300
+ }
301
+
302
+ export default async function gisIconApi({ name, nocache }) {
303
+ if (!name) {
304
+ throw BadRequestError('not enough params: name');
305
+ }
306
+
307
+ const options = parseIconFilename(name);
308
+
309
+ if (!options) {
310
+ throw BadRequestError('invalid icon name');
311
+ }
312
+
313
+ // fixed icon size of 32px
314
+ const image = await getIcon({ ...options, size: 32 }, nocache);
315
+
316
+ if (!image) {
317
+ throw NotFoundError('icon not found');
318
+ }
319
+
320
+ return {
321
+ headers: {
322
+ 'Content-Type': contentTypeByFormat[options.format],
323
+ 'Cache-Control': nocache ? 'no-cache' : 'public, max-age=31536000, immutable'
324
+ },
325
+ data: image
326
+ }
327
+ }