@opengis/gis 0.2.208 → 0.2.209
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.
- package/README.md +181 -181
- package/dist/{CardIcon-iGo8QJu9.js → CardIcon-B6rIgAoD.js} +1 -1
- package/dist/{EntityTablePage-BiVoVAhN.js → EntityTablePage-CfLq5znM.js} +2 -2
- package/dist/{ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-BdL5cQgN.js → ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-CB_4eJz9.js} +1 -1
- package/dist/{MapSettings-CXqiYxV4.js → MapSettings-fwcVWgdK.js} +2 -2
- package/dist/{MapToolbarRight.vue_vue_type_script_setup_true_lang-BHCChxmg.js → MapToolbarRight.vue_vue_type_script_setup_true_lang-BWqBARGq.js} +1 -1
- package/dist/{RastersTablePage-Dgk1wp3d.js → RastersTablePage-XHV40tf_.js} +2 -2
- package/dist/{cartocss-BIpgQU6F.js → cartocss-BjGlng_r.js} +526 -503
- package/dist/{import-utils-CfXKgZFd.js → import-utils-WfWvX9B5.js} +1 -1
- package/dist/{index-CM_DTEUa.js → index-CDy0MSfg.js} +3509 -3461
- package/dist/index.css +1 -1
- package/dist/index.js +1 -1
- package/dist/index.umd.cjs +42 -48
- package/dist/{raster-D_vEBha8.js → raster-D2X_jR9m.js} +3 -3
- package/dist/{register-C5_Un1Ms.js → register-Ctn9pgUE.js} +3 -3
- package/dist/service-CiUwLEwY.js +3484 -0
- package/dist/{vs-datatable--K-DxayK.js → vs-datatable-BixMLOv-.js} +4 -3
- package/module/gis/card/gis.metadata.table/index.yml +22 -22
- package/module/gis/card/gis.rasters.table/index.yml +11 -11
- package/module/gis/card/gis.registers.table/index.yml +23 -23
- package/module/gis/card/gis.services.table/index.yml +25 -25
- package/module/gis/cls/bool.yes_no.json +12 -12
- package/module/gis/cls/encoding.json +14 -14
- package/module/gis/cls/geom_type.json +14 -14
- package/module/gis/cls/gis.column_type.json +34 -34
- package/module/gis/cls/gis.column_view_type.json +26 -26
- package/module/gis/cls/gis.filter_type.json +22 -22
- package/module/gis/cls/language.json +10 -10
- package/module/gis/cls/meta.service_type.json +42 -42
- package/module/gis/cls/ogc.service.json +21 -21
- package/module/gis/cls/service_type.json +42 -42
- package/module/gis/cls/source_type.json +10 -10
- package/module/gis/cls/standarts.json +6 -6
- package/module/gis/cls/topic_category.json +106 -106
- package/module/gis/cls/update_frequency.json +29 -29
- package/module/gis/cls/view.json +21 -21
- package/module/gis/form/feedback.answer.form.json +10 -10
- package/module/gis/form/gis.cartocss.form.json +64 -64
- package/module/gis/form/gis.group_list.form.json +17 -17
- package/module/gis/form/gis.maps.form.json +61 -61
- package/module/gis/form/gis.metadata.form.json +240 -240
- package/module/gis/form/gis.metadata_new.form.json +195 -195
- package/module/gis/form/gis.ogc_service.form.json +51 -51
- package/module/gis/form/gis.rasters.form.json +76 -76
- package/module/gis/form/gis.registers.form.json +127 -127
- package/module/gis/form/gis.registers_column.form.json +84 -84
- package/module/gis/form/gis.registers_filter.form.json +65 -65
- package/module/gis/form/gis.services.form.json +111 -111
- package/module/gis/form/gis.services_attributes.form.json +75 -75
- package/module/gis/form/gis.services_filter.form.json +65 -65
- package/module/gis/menu.json +43 -43
- package/module/gis/select/admin.cls.name.sql +9 -9
- package/module/gis/select/layer_list.sql +7 -7
- package/module/gis/select/layer_list_text.sql +7 -7
- package/module/gis/select/pg.column_name.geometry.sql +12 -12
- package/module/gis/select/pg.columns.parent.sql +6 -6
- package/module/gis/select/pg.table_name.sql +17 -17
- package/module/gis/select/service_id.sql +1 -1
- package/module/gis/select/srid.sql +7 -7
- package/module/gis/table/gis.cartocss.table.json +74 -74
- package/module/gis/table/gis.group_list.table.json +58 -58
- package/module/gis/table/gis.maps.table.json +110 -110
- package/module/gis/table/gis.metadata.table.json +70 -70
- package/module/gis/table/gis.metadata_new.table.json +137 -137
- package/module/gis/table/gis.ogc_service.table.json +100 -100
- package/module/gis/table/gis.rasters.table.json +113 -113
- package/module/gis/table/gis.registers.table.json +143 -143
- package/module/gis/table/gis.services.table.json +120 -120
- package/module/gis/table/site.gis.registers.table.json +88 -88
- package/module/gis/table/site.gis.services.table.json +106 -106
- package/module/gis/templates/ISO19136_2017_gml_template.xml +330 -330
- package/module/gis/tokens.yml +5 -5
- package/module/permissions/form/permissions.users.form.json +151 -151
- package/module/permissions/table/gis.permissions.table.json +97 -97
- package/module/test/map/addr.json +302 -302
- package/module/test/map/addr1.yml +23 -23
- package/module/test/select/address.str_id.sql +3 -3
- package/package.json +79 -79
- package/plugin.js +64 -64
- package/server/migrations/array_intersect.sql +13 -13
- package/server/migrations/cartocss.sql +27 -27
- package/server/migrations/group_list.sql +74 -74
- package/server/migrations/maps.sql +30 -30
- package/server/migrations/metadata.sql +415 -415
- package/server/migrations/ogc.sql +106 -106
- package/server/migrations/rasters.sql +265 -265
- package/server/migrations/services.sql +248 -248
- package/server/migrations/services_users_rel.sql +22 -22
- package/server/migrations/widgets.sql +20 -20
- package/server/plugins/crons.js +21 -21
- package/server/plugins/mapnik/funcs/checkRasterFile.js +107 -107
- package/server/plugins/mapnik/funcs/mapnik.js +151 -151
- package/server/plugins/mapnik/funcs/rootFolder.mjs +8 -8
- package/server/plugins/mapnik/map.proto +437 -437
- package/server/plugins/vite.js +74 -74
- package/server/routes/cartocss/cartocss.route.js +31 -31
- package/server/routes/cartocss/cartocss.route.test.js +78 -78
- package/server/routes/cartocss/cartocss.schema.js +76 -76
- package/server/routes/cartocss/functions/add.cartocss.js +39 -39
- package/server/routes/cartocss/functions/get.cartocss.js +57 -57
- package/server/routes/gis/functions/checkXML.js +107 -107
- package/server/routes/gis/functions/dashboard.js +24 -24
- package/server/routes/gis/gis.route.js +26 -26
- package/server/routes/gis/gis.route.test.js +33 -33
- package/server/routes/map/fonts/index.js +1933 -1933
- package/server/routes/map/functions/clearTiles.js +35 -35
- package/server/routes/map/functions/geojson.js +187 -187
- package/server/routes/map/functions/gis.convert.js +65 -65
- package/server/routes/map/functions/gis.icon.js +327 -327
- package/server/routes/map/functions/jsonData.js +206 -206
- package/server/routes/map/functions/layerList.js +144 -144
- package/server/routes/map/functions/map.js +122 -122
- package/server/routes/map/functions/mapCatalog.js +72 -72
- package/server/routes/map/functions/mapCatalogAttribute.js +53 -53
- package/server/routes/map/functions/mapFeatures.js +133 -133
- package/server/routes/map/functions/mapFormat.js +246 -246
- package/server/routes/map/functions/mapTiles.js +162 -162
- package/server/routes/map/functions/maps.js +15 -15
- package/server/routes/map/functions/marker_icon.js +43 -43
- package/server/routes/map/functions/parseGeoFile.js +85 -85
- package/server/routes/map/functions/pointHeight.js +51 -51
- package/server/routes/map/functions/vtile.js +174 -174
- package/server/routes/map/functions/vtile1.js +191 -191
- package/server/routes/map/map.route.js +283 -283
- package/server/routes/map/map.route.test.js +693 -693
- package/server/routes/map/map.schema.js +356 -356
- package/server/routes/map/pin/circle1.svg +76 -76
- package/server/routes/map/pin/circle2.svg +18 -18
- package/server/routes/map/pin/none.svg +12 -12
- package/server/routes/map/pin/pin.svg +14 -14
- package/server/routes/map/pin/pin1.svg +16 -16
- package/server/routes/map/pin/pin2.svg +7 -7
- package/server/routes/map/pin/pin3.svg +34 -34
- package/server/routes/map/pin/pin4.svg +14 -14
- package/server/routes/map/pin/pin5.svg +7 -7
- package/server/routes/map/pin/pin6.svg +12 -12
- package/server/routes/map/pin/pin7.svg +12 -12
- package/server/routes/mapnik/functions/cartoBounds.js +32 -32
- package/server/routes/mapnik/functions/clearTiles.js +103 -103
- package/server/routes/mapnik/functions/convertBoundToTile.js +27 -27
- package/server/routes/mapnik/functions/createXml.js +76 -76
- package/server/routes/mapnik/functions/createXmlMulti.js +72 -72
- package/server/routes/mapnik/functions/fileSearch.js +35 -35
- package/server/routes/mapnik/functions/fileStat.js +28 -28
- package/server/routes/mapnik/functions/pretile.js +138 -138
- package/server/routes/mapnik/functions/rasterInfo.js +66 -66
- package/server/routes/mapnik/functions/readDir.js +22 -22
- package/server/routes/mapnik/functions/rtile.js +164 -164
- package/server/routes/mapnik/functions/uploadRaster.js +152 -152
- package/server/routes/mapnik/functions/uploadXML.js +140 -140
- package/server/routes/mapnik/mapnik.route.js +102 -102
- package/server/routes/mapnik/mapnik.route.test.js +370 -370
- package/server/routes/mapnik/mapnik.schema.mjs +195 -195
- package/server/routes/mapnik.prefixless/functions/mapnikLogger.js +31 -31
- package/server/routes/mapnik.prefixless/functions/mapnikStat.js +21 -21
- package/server/routes/mapnik.prefixless/mapnik.prefixless.route.js +20 -20
- package/server/routes/mapnik.prefixless/mapnik.prefixless.route.test.js +91 -91
- package/server/routes/mapnik.prefixless/mapnik.prefixless.schema.mjs +27 -27
- package/server/routes/maps/functions/add.map.js +42 -42
- package/server/routes/maps/functions/del.map.js +17 -17
- package/server/routes/maps/functions/get.map.js +119 -119
- package/server/routes/maps/maps.route.js +58 -58
- package/server/routes/maps/maps.route.test.js +231 -231
- package/server/routes/maps/maps.schema.js +9 -9
- package/server/routes/metadata/functions/editMetadata.js +35 -35
- package/server/routes/metadata/functions/metadataXML.js +19 -19
- package/server/routes/metadata/metadata.route.js +26 -26
- package/server/routes/metadata/metadata.route.test.js +46 -46
- package/server/routes/metadata/metadata.schema.js +53 -53
- package/server/routes/ogc/functions/map.info.point.js +117 -117
- package/server/routes/ogc/ogc.route.js +21 -21
- package/server/routes/ogc/ogc.route.test.js +53 -53
- package/server/routes/ogc/ogc.schema.js +16 -16
- package/server/routes/permissions/functions/catalog.permissions.edit.js +20 -20
- package/server/routes/permissions/functions/catalog.permissions.js +12 -12
- package/server/routes/permissions/functions/get.permissions.js +59 -59
- package/server/routes/permissions/functions/gis.catalog.js +80 -80
- package/server/routes/permissions/permissions.route.js +35 -35
- package/server/routes/permissions/permissions.route.test.js +35 -35
- package/server/routes/permissions/permissions.schema.js +43 -43
- package/server/routes/registers/functions/add.registry.js +37 -37
- package/server/routes/registers/functions/classifiers.js +100 -100
- package/server/routes/registers/functions/columns.js +3 -3
- package/server/routes/registers/functions/content.type.js +9 -9
- package/server/routes/registers/functions/del.registry.js +14 -14
- package/server/routes/registers/functions/get.info.js +93 -93
- package/server/routes/registers/functions/getFilterSQLParametrized/formatValue.js +328 -328
- package/server/routes/registers/functions/getFilterSQLParametrized/getFilterQuery.js +126 -126
- package/server/routes/registers/functions/getFilterSQLParametrized/getRangeQuery.js +353 -353
- package/server/routes/registers/functions/getFilterSQLParametrized/index.js +81 -81
- package/server/routes/registers/functions/getTableColumnMeta.js +148 -148
- package/server/routes/registers/functions/gis.export.js +159 -159
- package/server/routes/registers/functions/gis.registry.js +71 -71
- package/server/routes/registers/functions/gis.registry.list.js +61 -61
- package/server/routes/registers/functions/gis.suggest.js +310 -310
- package/server/routes/registers/functions/handleRegistryRequest.js +178 -178
- package/server/routes/registers/functions/map.registry.js +87 -87
- package/server/routes/registers/registers.route.js +92 -92
- package/server/routes/registers/registers.route.test.js +173 -173
- package/server/routes/registers/registers.schema.js +276 -276
- package/server/routes/root.mjs +3 -3
- package/server/routes/services/functions/add.service.js +62 -62
- package/server/routes/services/functions/create.route.js +93 -93
- package/server/routes/services/functions/del.service.js +12 -12
- package/server/routes/services/functions/get.layer.geom.js +27 -27
- package/server/routes/services/functions/get.service.users.js +76 -76
- package/server/routes/services/functions/get.services.col.js +34 -34
- package/server/routes/services/functions/get.services.js +124 -124
- package/server/routes/services/functions/legend.auto.js +77 -77
- package/server/routes/services/services.route.js +76 -76
- package/server/routes/services/services.route.test.js +171 -171
- package/server/routes/services/services.schema.js +459 -459
- package/server/routes/widgets/functions/add.widget.js +38 -38
- package/server/routes/widgets/functions/del.widget.js +17 -17
- package/server/routes/widgets/functions/get.widget.js +41 -41
- package/server/routes/widgets/widgets.route.js +61 -61
- package/server/routes/widgets/widgets.route.test.js +144 -144
- package/server/routes/widgets/widgets.schema.js +24 -24
- package/server/shared/listFiles.js +14 -14
- package/utils.js +13 -13
- package/dist/service-JN82JN81.js +0 -3676
|
@@ -1,191 +1,191 @@
|
|
|
1
|
-
const headers = {
|
|
2
|
-
'Content-Type': 'application/x-protobuf',
|
|
3
|
-
'Cache-Control': '86400',
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
import path from 'node:path';
|
|
7
|
-
import { existsSync } from 'node:fs';
|
|
8
|
-
import { createHash } from 'node:crypto';
|
|
9
|
-
import {
|
|
10
|
-
readFile, stat, mkdir, writeFile,
|
|
11
|
-
} from 'node:fs/promises';
|
|
12
|
-
|
|
13
|
-
import { load } from 'js-yaml';
|
|
14
|
-
|
|
15
|
-
import { SphericalMercator } from '@mapbox/sphericalmercator';
|
|
16
|
-
|
|
17
|
-
const mercator = new SphericalMercator({ size: 256 });
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
getColumnCLS, getTemplate, getMeta, getSelect, pgClients
|
|
21
|
-
} from '@opengis/fastify-table/utils.js';
|
|
22
|
-
|
|
23
|
-
import rootFolder from '../../../plugins/mapnik/funcs/rootFolder.mjs';
|
|
24
|
-
import { NotFoundError } from '@opengis/fastify-table/errors.js';
|
|
25
|
-
|
|
26
|
-
import getFilterSQLParametrized from '../../registers/functions/getFilterSQLParametrized/index.js';
|
|
27
|
-
|
|
28
|
-
const propsCache = {};
|
|
29
|
-
|
|
30
|
-
const types = { point: 'ST_Point' /* ,ST_MultiPoint */, polygon: 'ST_Polygon,ST_MultiPolygon' };
|
|
31
|
-
|
|
32
|
-
export default async function vtile({
|
|
33
|
-
x, y, z, layer: paramsLayer, type, nocache, sql, clusterZoom, filter, nottl, uid, filter: queryFilter, all: queryAll, sql: querySql, pointZoom,
|
|
34
|
-
}, pg = pgClients.client) {
|
|
35
|
-
const hash = [type, clusterZoom, filter].join();
|
|
36
|
-
|
|
37
|
-
const filepath = path.join(
|
|
38
|
-
rootFolder,
|
|
39
|
-
`/vtile/${paramsLayer}/${hash ? `${createHash('md5').update(hash).digest('hex')}/` : ''}${z}/${x}/${y}.vmt`,
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
// layer
|
|
43
|
-
const [_, layer] = paramsLayer.includes(':') ? paramsLayer.split(':') : [null, paramsLayer];
|
|
44
|
-
|
|
45
|
-
// service
|
|
46
|
-
const data = await getTemplate('layer', layer) || await pg.query(`select * from gis.services where ${!uid ? 'is_public and is_active' : '1=1'} and service_id=$1`, [layer]).then(r => r.rows?.[0]);
|
|
47
|
-
|
|
48
|
-
if (!data) {
|
|
49
|
-
throw NotFoundError('layer not found');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const style = typeof data?.style === 'object' ? data?.style : load(data?.style);
|
|
53
|
-
|
|
54
|
-
const cacheZoom = style?.cacheZoom || 13;
|
|
55
|
-
const ttl = typeof style?.ttl === 'number' && style.ttl >= 0 ? style.ttl : 86400000;
|
|
56
|
-
|
|
57
|
-
if (existsSync(filepath) && +z < cacheZoom && !nocache && !sql && process.env.NODE_ENV !== 'test') {
|
|
58
|
-
const { birthtimeMs = Date.now() } = await stat(filepath);
|
|
59
|
-
const ageInMs = Date.now() - birthtimeMs;
|
|
60
|
-
if (ageInMs < ttl) {
|
|
61
|
-
const buffer = await readFile(filepath);
|
|
62
|
-
return { headers, data: buffer };
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const geom = data?.geometry_column || 'geom';
|
|
67
|
-
const table = data?.source_path;
|
|
68
|
-
const filterList = data?.filters;
|
|
69
|
-
const layerQuery = data?.query;
|
|
70
|
-
|
|
71
|
-
// bbox
|
|
72
|
-
const bbox = mercator.bbox(+y, +x, +z, false/* , '900913' */);
|
|
73
|
-
const bbox2d = `'BOX(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d`;
|
|
74
|
-
|
|
75
|
-
// meta
|
|
76
|
-
const metaTable = table ? await getMeta({ pg, table }) : {};
|
|
77
|
-
|
|
78
|
-
const filterData = table && queryFilter ? getFilterSQLParametrized({
|
|
79
|
-
table, fields: metaTable?.columns, filter: queryFilter, filterList,
|
|
80
|
-
}, pg) : { args: [] };
|
|
81
|
-
|
|
82
|
-
const { pk: pkey = data.key, columns = [] } = metaTable; // if view - specify via template
|
|
83
|
-
|
|
84
|
-
// props
|
|
85
|
-
const statusColumn = style?.colorAttr || style?.iconAttr ? columns.filter(col => col.name === style.colorAttr || style?.iconAttr) : [];
|
|
86
|
-
const props = !queryAll ? (statusColumn || []).concat(data.popup || []).concat(data.card || []) : columns.filter(el => !['json', 'geometry'].includes(pg.pgType[el.dataTypeID]) && !['uid', 'cdate', 'editor_id', 'editor_date'].includes(el.name));
|
|
87
|
-
|
|
88
|
-
const customClsList = await pg.queryCache('select array_agg(name) from admin.user_cls where parent is null and type=$1', { table: 'admin.user_cls', args: ['json'] }).then((r) => r.rows?.[0]?.array_agg || []);
|
|
89
|
-
|
|
90
|
-
const cache = layer + (queryAll ? '-all' : '') + JSON.stringify(data.popup || []).length;
|
|
91
|
-
const propsWithCls = propsCache[cache] ?? await Promise.all(props.map(async (col, idx) => {
|
|
92
|
-
const key = col.name;
|
|
93
|
-
const select = data.popup?.find(el => el.name === key);
|
|
94
|
-
// console.log(select);
|
|
95
|
-
const clsData = select?.data || col.data;
|
|
96
|
-
const cls = clsData ? await getSelect(clsData).then(el => {
|
|
97
|
-
if (!el) {
|
|
98
|
-
console.error('gis vtile: cls not found', clsData);
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return { name: clsData, type: el.arr ? 'cls' : 'select', sql: el.sql, custom: customClsList.includes(clsData) };
|
|
103
|
-
}) : null;
|
|
104
|
-
const { name, type: type1, sql: sql1, custom } = cls || getColumnCLS(table, col.data || key) || {};
|
|
105
|
-
// console.log({ name, type, sql });
|
|
106
|
-
if (name && type1 === 'cls' && custom) {
|
|
107
|
-
return `(select name from admin.user_cls where parent='${name.replace(/'/g, "")}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
108
|
-
}
|
|
109
|
-
if (name && type1 === 'cls') {
|
|
110
|
-
return `(select name from admin.cls where parent='${name.replace(/'/g, "")}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
111
|
-
}
|
|
112
|
-
if (type1 === 'select' && sql1) {
|
|
113
|
-
const { fields = [] } = await pg.query(`${sql1} limit 0`);
|
|
114
|
-
return `(select "${fields[1].name}" from (${sql1})s${idx} where "${fields[0].name}"::text=q."${key}"::text limit 1) as "${key}_text","${key}"::text`;
|
|
115
|
-
}
|
|
116
|
-
return `"${key}"::text`;
|
|
117
|
-
}));
|
|
118
|
-
propsCache[cache] = propsCache[cache] || propsWithCls;
|
|
119
|
-
|
|
120
|
-
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
121
|
-
|| (parseInt(z, 10) < parseInt(pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
|
|
122
|
-
|
|
123
|
-
const koef = {
|
|
124
|
-
10: 0.1, 11: 0.05, 12: 0.005, 13: 0.001, 14: 0.001, 15: 0.0005,
|
|
125
|
-
}[z] || 0.0005;
|
|
126
|
-
|
|
127
|
-
const clusterZoom1 = style?.clusterZoom || clusterZoom;
|
|
128
|
-
const queryMode = ((clusterZoom1 - z) > 2 ? 1 : null) || ((clusterZoom1 - z) > 0 ? 2 : null) || 3;
|
|
129
|
-
// console.log(z, clusterZoom, queryMode);
|
|
130
|
-
// const props1 = props.filter((el, idx, arr) => idx === arr.findIndex(item => item.name === el.name));
|
|
131
|
-
const q = {
|
|
132
|
-
1: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
133
|
-
FROM (
|
|
134
|
-
SELECT floor(random() * 100000 + 1)::int + row_number() over() as row, point_count, ST_AsMVTGeom(st_transform(geom,3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
135
|
-
FROM (
|
|
136
|
-
SELECT st_centroid(st_union(geom)) as geom, count(*) as point_count
|
|
137
|
-
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
138
|
-
)q`,
|
|
139
|
-
2: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
140
|
-
FROM (
|
|
141
|
-
SELECT floor(random() * 100000 + 1)::int +row_number() over() as row, count(*) as point_count,
|
|
142
|
-
ST_AsMVTGeom(st_transform(st_centroid(ST_Union(geom)),3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) as geom
|
|
143
|
-
FROM (
|
|
144
|
-
SELECT geom, ST_ClusterDBSCAN(geom,${koef},1) OVER () AS cluster
|
|
145
|
-
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
146
|
-
WHERE cluster IS NOT NULL
|
|
147
|
-
GROUP BY cluster
|
|
148
|
-
ORDER BY 1 DESC
|
|
149
|
-
)q`,
|
|
150
|
-
3: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
151
|
-
FROM (
|
|
152
|
-
SELECT
|
|
153
|
-
|
|
154
|
-
floor(random() * 100000 + 1)::int + row_number() over() as row,
|
|
155
|
-
|
|
156
|
-
${props?.length ? `${propsWithCls.join(',')},` : ''}
|
|
157
|
-
|
|
158
|
-
${pkey} as id,
|
|
159
|
-
ST_AsMVTGeom(st_transform(${geomCol},3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
160
|
-
|
|
161
|
-
FROM (select * from ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d}
|
|
162
|
-
|
|
163
|
-
and ${geom} is not null and st_srid(${geom}) >0
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
${types[type] ? ` and ST_GeometryType(geom) = any ('{ ${types[type]} }') ` : ''}
|
|
168
|
-
|
|
169
|
-
and ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${data?.filter || '1=1'}
|
|
170
|
-
|
|
171
|
-
${(style?.type || data?.geometry_type) === 'polygon' ? `order by st_area(st_transform(${geom},3857)) desc` : ''}
|
|
172
|
-
|
|
173
|
-
limit 3000)q
|
|
174
|
-
) q`,
|
|
175
|
-
}[queryMode];
|
|
176
|
-
|
|
177
|
-
if (querySql === 1) {
|
|
178
|
-
return q;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const args = filterData?.args || [];
|
|
182
|
-
const tile = await pg.query(q, args);
|
|
183
|
-
const buffer = Buffer.concat(tile.rows.map((el) => Buffer.from(el.tile)));
|
|
184
|
-
|
|
185
|
-
if (!nocache) {
|
|
186
|
-
await mkdir(path.dirname(filepath), { recursive: true });
|
|
187
|
-
await writeFile(filepath, buffer, 'binary');
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return { headers: { ...headers, 'Cache-Control': nocache || nottl ? 'no-cache' : headers['Cache-Control'] }, data: buffer };
|
|
191
|
-
}
|
|
1
|
+
const headers = {
|
|
2
|
+
'Content-Type': 'application/x-protobuf',
|
|
3
|
+
'Cache-Control': '86400',
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { existsSync } from 'node:fs';
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
import {
|
|
10
|
+
readFile, stat, mkdir, writeFile,
|
|
11
|
+
} from 'node:fs/promises';
|
|
12
|
+
|
|
13
|
+
import { load } from 'js-yaml';
|
|
14
|
+
|
|
15
|
+
import { SphericalMercator } from '@mapbox/sphericalmercator';
|
|
16
|
+
|
|
17
|
+
const mercator = new SphericalMercator({ size: 256 });
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
getColumnCLS, getTemplate, getMeta, getSelect, pgClients
|
|
21
|
+
} from '@opengis/fastify-table/utils.js';
|
|
22
|
+
|
|
23
|
+
import rootFolder from '../../../plugins/mapnik/funcs/rootFolder.mjs';
|
|
24
|
+
import { NotFoundError } from '@opengis/fastify-table/errors.js';
|
|
25
|
+
|
|
26
|
+
import getFilterSQLParametrized from '../../registers/functions/getFilterSQLParametrized/index.js';
|
|
27
|
+
|
|
28
|
+
const propsCache = {};
|
|
29
|
+
|
|
30
|
+
const types = { point: 'ST_Point' /* ,ST_MultiPoint */, polygon: 'ST_Polygon,ST_MultiPolygon' };
|
|
31
|
+
|
|
32
|
+
export default async function vtile({
|
|
33
|
+
x, y, z, layer: paramsLayer, type, nocache, sql, clusterZoom, filter, nottl, uid, filter: queryFilter, all: queryAll, sql: querySql, pointZoom,
|
|
34
|
+
}, pg = pgClients.client) {
|
|
35
|
+
const hash = [type, clusterZoom, filter].join();
|
|
36
|
+
|
|
37
|
+
const filepath = path.join(
|
|
38
|
+
rootFolder,
|
|
39
|
+
`/vtile/${paramsLayer}/${hash ? `${createHash('md5').update(hash).digest('hex')}/` : ''}${z}/${x}/${y}.vmt`,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// layer
|
|
43
|
+
const [_, layer] = paramsLayer.includes(':') ? paramsLayer.split(':') : [null, paramsLayer];
|
|
44
|
+
|
|
45
|
+
// service
|
|
46
|
+
const data = await getTemplate('layer', layer) || await pg.query(`select * from gis.services where ${!uid ? 'is_public and is_active' : '1=1'} and service_id=$1`, [layer]).then(r => r.rows?.[0]);
|
|
47
|
+
|
|
48
|
+
if (!data) {
|
|
49
|
+
throw NotFoundError('layer not found');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const style = typeof data?.style === 'object' ? data?.style : load(data?.style);
|
|
53
|
+
|
|
54
|
+
const cacheZoom = style?.cacheZoom || 13;
|
|
55
|
+
const ttl = typeof style?.ttl === 'number' && style.ttl >= 0 ? style.ttl : 86400000;
|
|
56
|
+
|
|
57
|
+
if (existsSync(filepath) && +z < cacheZoom && !nocache && !sql && process.env.NODE_ENV !== 'test') {
|
|
58
|
+
const { birthtimeMs = Date.now() } = await stat(filepath);
|
|
59
|
+
const ageInMs = Date.now() - birthtimeMs;
|
|
60
|
+
if (ageInMs < ttl) {
|
|
61
|
+
const buffer = await readFile(filepath);
|
|
62
|
+
return { headers, data: buffer };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const geom = data?.geometry_column || 'geom';
|
|
67
|
+
const table = data?.source_path;
|
|
68
|
+
const filterList = data?.filters;
|
|
69
|
+
const layerQuery = data?.query;
|
|
70
|
+
|
|
71
|
+
// bbox
|
|
72
|
+
const bbox = mercator.bbox(+y, +x, +z, false/* , '900913' */);
|
|
73
|
+
const bbox2d = `'BOX(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d`;
|
|
74
|
+
|
|
75
|
+
// meta
|
|
76
|
+
const metaTable = table ? await getMeta({ pg, table }) : {};
|
|
77
|
+
|
|
78
|
+
const filterData = table && queryFilter ? getFilterSQLParametrized({
|
|
79
|
+
table, fields: metaTable?.columns, filter: queryFilter, filterList,
|
|
80
|
+
}, pg) : { args: [] };
|
|
81
|
+
|
|
82
|
+
const { pk: pkey = data.key, columns = [] } = metaTable; // if view - specify via template
|
|
83
|
+
|
|
84
|
+
// props
|
|
85
|
+
const statusColumn = style?.colorAttr || style?.iconAttr ? columns.filter(col => col.name === style.colorAttr || style?.iconAttr) : [];
|
|
86
|
+
const props = !queryAll ? (statusColumn || []).concat(data.popup || []).concat(data.card || []) : columns.filter(el => !['json', 'geometry'].includes(pg.pgType[el.dataTypeID]) && !['uid', 'cdate', 'editor_id', 'editor_date'].includes(el.name));
|
|
87
|
+
|
|
88
|
+
const customClsList = await pg.queryCache('select array_agg(name) from admin.user_cls where parent is null and type=$1', { table: 'admin.user_cls', args: ['json'] }).then((r) => r.rows?.[0]?.array_agg || []);
|
|
89
|
+
|
|
90
|
+
const cache = layer + (queryAll ? '-all' : '') + JSON.stringify(data.popup || []).length;
|
|
91
|
+
const propsWithCls = propsCache[cache] ?? await Promise.all(props.map(async (col, idx) => {
|
|
92
|
+
const key = col.name;
|
|
93
|
+
const select = data.popup?.find(el => el.name === key);
|
|
94
|
+
// console.log(select);
|
|
95
|
+
const clsData = select?.data || col.data;
|
|
96
|
+
const cls = clsData ? await getSelect(clsData).then(el => {
|
|
97
|
+
if (!el) {
|
|
98
|
+
console.error('gis vtile: cls not found', clsData);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { name: clsData, type: el.arr ? 'cls' : 'select', sql: el.sql, custom: customClsList.includes(clsData) };
|
|
103
|
+
}) : null;
|
|
104
|
+
const { name, type: type1, sql: sql1, custom } = cls || getColumnCLS(table, col.data || key) || {};
|
|
105
|
+
// console.log({ name, type, sql });
|
|
106
|
+
if (name && type1 === 'cls' && custom) {
|
|
107
|
+
return `(select name from admin.user_cls where parent='${name.replace(/'/g, "")}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
108
|
+
}
|
|
109
|
+
if (name && type1 === 'cls') {
|
|
110
|
+
return `(select name from admin.cls where parent='${name.replace(/'/g, "")}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
111
|
+
}
|
|
112
|
+
if (type1 === 'select' && sql1) {
|
|
113
|
+
const { fields = [] } = await pg.query(`${sql1} limit 0`);
|
|
114
|
+
return `(select "${fields[1].name}" from (${sql1})s${idx} where "${fields[0].name}"::text=q."${key}"::text limit 1) as "${key}_text","${key}"::text`;
|
|
115
|
+
}
|
|
116
|
+
return `"${key}"::text`;
|
|
117
|
+
}));
|
|
118
|
+
propsCache[cache] = propsCache[cache] || propsWithCls;
|
|
119
|
+
|
|
120
|
+
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
121
|
+
|| (parseInt(z, 10) < parseInt(pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
|
|
122
|
+
|
|
123
|
+
const koef = {
|
|
124
|
+
10: 0.1, 11: 0.05, 12: 0.005, 13: 0.001, 14: 0.001, 15: 0.0005,
|
|
125
|
+
}[z] || 0.0005;
|
|
126
|
+
|
|
127
|
+
const clusterZoom1 = style?.clusterZoom || clusterZoom;
|
|
128
|
+
const queryMode = ((clusterZoom1 - z) > 2 ? 1 : null) || ((clusterZoom1 - z) > 0 ? 2 : null) || 3;
|
|
129
|
+
// console.log(z, clusterZoom, queryMode);
|
|
130
|
+
// const props1 = props.filter((el, idx, arr) => idx === arr.findIndex(item => item.name === el.name));
|
|
131
|
+
const q = {
|
|
132
|
+
1: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
133
|
+
FROM (
|
|
134
|
+
SELECT floor(random() * 100000 + 1)::int + row_number() over() as row, point_count, ST_AsMVTGeom(st_transform(geom,3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
135
|
+
FROM (
|
|
136
|
+
SELECT st_centroid(st_union(geom)) as geom, count(*) as point_count
|
|
137
|
+
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
138
|
+
)q`,
|
|
139
|
+
2: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
140
|
+
FROM (
|
|
141
|
+
SELECT floor(random() * 100000 + 1)::int +row_number() over() as row, count(*) as point_count,
|
|
142
|
+
ST_AsMVTGeom(st_transform(st_centroid(ST_Union(geom)),3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) as geom
|
|
143
|
+
FROM (
|
|
144
|
+
SELECT geom, ST_ClusterDBSCAN(geom,${koef},1) OVER () AS cluster
|
|
145
|
+
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
146
|
+
WHERE cluster IS NOT NULL
|
|
147
|
+
GROUP BY cluster
|
|
148
|
+
ORDER BY 1 DESC
|
|
149
|
+
)q`,
|
|
150
|
+
3: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
151
|
+
FROM (
|
|
152
|
+
SELECT
|
|
153
|
+
|
|
154
|
+
floor(random() * 100000 + 1)::int + row_number() over() as row,
|
|
155
|
+
|
|
156
|
+
${props?.length ? `${propsWithCls.join(',')},` : ''}
|
|
157
|
+
|
|
158
|
+
${pkey} as id,
|
|
159
|
+
ST_AsMVTGeom(st_transform(${geomCol},3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
160
|
+
|
|
161
|
+
FROM (select * from ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d}
|
|
162
|
+
|
|
163
|
+
and ${geom} is not null and st_srid(${geom}) >0
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
${types[type] ? ` and ST_GeometryType(geom) = any ('{ ${types[type]} }') ` : ''}
|
|
168
|
+
|
|
169
|
+
and ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${data?.filter || '1=1'}
|
|
170
|
+
|
|
171
|
+
${(style?.type || data?.geometry_type) === 'polygon' ? `order by st_area(st_transform(${geom},3857)) desc` : ''}
|
|
172
|
+
|
|
173
|
+
limit 3000)q
|
|
174
|
+
) q`,
|
|
175
|
+
}[queryMode];
|
|
176
|
+
|
|
177
|
+
if (querySql === 1) {
|
|
178
|
+
return q;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const args = filterData?.args || [];
|
|
182
|
+
const tile = await pg.query(q, args);
|
|
183
|
+
const buffer = Buffer.concat(tile.rows.map((el) => Buffer.from(el.tile)));
|
|
184
|
+
|
|
185
|
+
if (!nocache) {
|
|
186
|
+
await mkdir(path.dirname(filepath), { recursive: true });
|
|
187
|
+
await writeFile(filepath, buffer, 'binary');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { headers: { ...headers, 'Cache-Control': nocache || nottl ? 'no-cache' : headers['Cache-Control'] }, data: buffer };
|
|
191
|
+
}
|