@opengis/gis 0.2.176 → 0.2.178
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-c1Q2M3wQ.js → CardIcon-CMRD1LLh.js} +1 -1
- package/dist/{EntityTablePage-B5gpkH4S.js → EntityTablePage-XNNCltAN.js} +2 -2
- package/dist/{ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-DnGSvmMU.js → ExtentOutlineLayer.vue_vue_type_script_setup_true_lang-BqUjKTDY.js} +1 -1
- package/dist/{HeaderActions.vue_vue_type_script_setup_true_lang-C2B61JP6.js → HeaderActions.vue_vue_type_script_setup_true_lang-BXQ3hKwa.js} +1 -1
- package/dist/{MapSettings-6-Xru5Q8.js → MapSettings-P3h-vHn3.js} +2 -2
- package/dist/{RastersTablePage-CXNOcTy-.js → RastersTablePage-DnhGZLiH.js} +2 -2
- package/dist/{cartocss-BFVqWvfP.js → cartocss-BkrOeQg4.js} +6 -6
- package/dist/{import-utils-uxk4LdUJ.js → import-utils-BCATsbB5.js} +1 -1
- package/dist/{index-CahlV-Xs.js → index-cUagVqgA.js} +39 -39
- package/dist/index.css +1 -1
- package/dist/index.js +1 -1
- package/dist/index.umd.cjs +5 -5
- package/dist/{raster-CEi56f0P.js → raster-CP0tUp3j.js} +3 -3
- package/dist/{register-NFAGBNa3.js → register-Cr-uQcby.js} +3 -3
- package/dist/{service-BYspag8U.js → service-DwJaKrOd.js} +6 -6
- package/dist/{vs-datatable-CHcBr6zy.js → vs-datatable-B9WoVk-b.js} +2 -2
- package/module/gis/cls/service_type.json +42 -42
- package/module/gis/form/gis.registers.form.json +127 -127
- 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 +77 -77
- 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 +247 -247
- 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 +73 -73
- 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/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 +220 -220
- package/server/routes/map/functions/jsonData.js +206 -206
- package/server/routes/map/functions/layerList.js +102 -102
- 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 +232 -232
- 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 +185 -185
- package/server/routes/map/map.route.js +280 -280
- package/server/routes/map/map.route.test.js +503 -503
- 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 +312 -0
- package/server/routes/registers/functions/getFilterSQLParametrized/getFilterQuery.js +126 -0
- package/server/routes/registers/functions/getFilterSQLParametrized/getRangeQuery.js +353 -0
- package/server/routes/registers/functions/getFilterSQLParametrized/index.js +81 -0
- package/server/routes/registers/functions/getTableColumnMeta.js +125 -0
- 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 +275 -0
- package/server/routes/registers/functions/handleRegistryRequest.js +175 -147
- package/server/routes/registers/functions/map.registry.js +87 -87
- package/server/routes/registers/registers.route.js +91 -80
- package/server/routes/registers/registers.route.test.js +173 -144
- package/server/routes/registers/registers.schema.js +275 -246
- 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 +89 -89
- 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 +167 -167
- package/server/routes/services/services.schema.js +285 -278
- 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
|
@@ -1,185 +1,185 @@
|
|
|
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, getFilterSQL, getSelect,
|
|
21
|
-
pgClients,
|
|
22
|
-
getFilterSQLParametrized,
|
|
23
|
-
} from '@opengis/fastify-table/utils.js';
|
|
24
|
-
|
|
25
|
-
import rootFolder from '../../../plugins/mapnik/funcs/rootFolder.mjs';
|
|
26
|
-
import { NotFoundError } from '@opengis/fastify-table/errors.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
|
-
const filterData = queryFilter ? await getFilterSQLParametrized({
|
|
76
|
-
filter: queryFilter, table, filterList, pg,
|
|
77
|
-
}) : { args: [] };
|
|
78
|
-
|
|
79
|
-
// meta
|
|
80
|
-
const metaTable = table ? await getMeta({ pg, table }) : {};
|
|
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 cache = layer + (queryAll ? '-all' : '') + JSON.stringify(data.popup || []).length;
|
|
89
|
-
const propsWithCls = propsCache[cache] ?? await Promise.all(props.map(async (col, idx) => {
|
|
90
|
-
const key = col.name;
|
|
91
|
-
const select = data.popup?.find(el => el.name === key);
|
|
92
|
-
// console.log(select);
|
|
93
|
-
const clsData = select?.data || col.data;
|
|
94
|
-
const cls = clsData ? await getSelect(clsData).then(el => {
|
|
95
|
-
if (!el) {
|
|
96
|
-
console.error('gis vtile: cls not found', clsData);
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
return { name: clsData, type: el.arr ? 'cls' : 'select', sql: el.sql };
|
|
100
|
-
}) : null;
|
|
101
|
-
const { name, type: type1, sql: sql1 } = cls || getColumnCLS(table, col.data || key) || {};
|
|
102
|
-
// console.log({ name, type, sql });
|
|
103
|
-
if (name && type1 === 'cls') {
|
|
104
|
-
return `(select name from admin.cls where parent='${name}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
105
|
-
}
|
|
106
|
-
if (type1 === 'select' && sql1) {
|
|
107
|
-
const { fields = [] } = await pg.query(`${sql1} limit 0`);
|
|
108
|
-
return `(select "${fields[1].name}" from (${sql1})s${idx} where "${fields[0].name}"::text=q."${key}"::text limit 1) as "${key}_text","${key}"::text`;
|
|
109
|
-
}
|
|
110
|
-
return `"${key}"::text`;
|
|
111
|
-
}));
|
|
112
|
-
propsCache[cache] = propsCache[cache] || propsWithCls;
|
|
113
|
-
|
|
114
|
-
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
115
|
-
|| (parseInt(z, 10) < parseInt(pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
|
|
116
|
-
|
|
117
|
-
const koef = {
|
|
118
|
-
10: 0.1, 11: 0.05, 12: 0.005, 13: 0.001, 14: 0.001, 15: 0.0005,
|
|
119
|
-
}[z] || 0.0005;
|
|
120
|
-
|
|
121
|
-
const clusterZoom1 = style?.clusterZoom || clusterZoom;
|
|
122
|
-
const queryMode = ((clusterZoom1 - z) > 2 ? 1 : null) || ((clusterZoom1 - z) > 0 ? 2 : null) || 3;
|
|
123
|
-
// console.log(z, clusterZoom, queryMode);
|
|
124
|
-
// const props1 = props.filter((el, idx, arr) => idx === arr.findIndex(item => item.name === el.name));
|
|
125
|
-
const q = {
|
|
126
|
-
1: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
127
|
-
FROM (
|
|
128
|
-
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
|
|
129
|
-
FROM (
|
|
130
|
-
SELECT st_centroid(st_union(geom)) as geom, count(*) as point_count
|
|
131
|
-
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
132
|
-
)q`,
|
|
133
|
-
2: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
134
|
-
FROM (
|
|
135
|
-
SELECT floor(random() * 100000 + 1)::int +row_number() over() as row, count(*) as point_count,
|
|
136
|
-
ST_AsMVTGeom(st_transform(st_centroid(ST_Union(geom)),3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) as geom
|
|
137
|
-
FROM (
|
|
138
|
-
SELECT geom, ST_ClusterDBSCAN(geom,${koef},1) OVER () AS cluster
|
|
139
|
-
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
140
|
-
WHERE cluster IS NOT NULL
|
|
141
|
-
GROUP BY cluster
|
|
142
|
-
ORDER BY 1 DESC
|
|
143
|
-
)q`,
|
|
144
|
-
3: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
145
|
-
FROM (
|
|
146
|
-
SELECT
|
|
147
|
-
|
|
148
|
-
floor(random() * 100000 + 1)::int + row_number() over() as row,
|
|
149
|
-
|
|
150
|
-
${props?.length ? `${propsWithCls.join(',')},` : ''}
|
|
151
|
-
|
|
152
|
-
${pkey} as id,
|
|
153
|
-
ST_AsMVTGeom(st_transform(${geomCol},3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
154
|
-
|
|
155
|
-
FROM (select * from ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d}
|
|
156
|
-
|
|
157
|
-
and ${geom} is not null and st_srid(${geom}) >0
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
${types[type] ? ` and ST_GeometryType(geom) = any ('{ ${types[type]} }') ` : ''}
|
|
162
|
-
|
|
163
|
-
and ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${data?.filter || '1=1'}
|
|
164
|
-
|
|
165
|
-
${(style?.type || data?.geometry_type) === 'polygon' ? `order by st_area(st_transform(${geom},3857)) desc` : ''}
|
|
166
|
-
|
|
167
|
-
limit 3000)q
|
|
168
|
-
) q`,
|
|
169
|
-
}[queryMode];
|
|
170
|
-
|
|
171
|
-
if (querySql === 1) {
|
|
172
|
-
return q;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const args = filterData?.args || [];
|
|
176
|
-
const tile = await pg.query(q, args);
|
|
177
|
-
const buffer = Buffer.concat(tile.rows.map((el) => Buffer.from(el.tile)));
|
|
178
|
-
|
|
179
|
-
if (!nocache) {
|
|
180
|
-
await mkdir(path.dirname(filepath), { recursive: true });
|
|
181
|
-
await writeFile(filepath, buffer, 'binary');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return { headers: { ...headers, 'Cache-Control': nocache || nottl ? 'no-cache' : headers['Cache-Control'] }, data: buffer };
|
|
185
|
-
}
|
|
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, getFilterSQL, getSelect,
|
|
21
|
+
pgClients,
|
|
22
|
+
getFilterSQLParametrized,
|
|
23
|
+
} from '@opengis/fastify-table/utils.js';
|
|
24
|
+
|
|
25
|
+
import rootFolder from '../../../plugins/mapnik/funcs/rootFolder.mjs';
|
|
26
|
+
import { NotFoundError } from '@opengis/fastify-table/errors.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
|
+
const filterData = queryFilter ? await getFilterSQLParametrized({
|
|
76
|
+
filter: queryFilter, table, filterList, pg,
|
|
77
|
+
}) : { args: [] };
|
|
78
|
+
|
|
79
|
+
// meta
|
|
80
|
+
const metaTable = table ? await getMeta({ pg, table }) : {};
|
|
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 cache = layer + (queryAll ? '-all' : '') + JSON.stringify(data.popup || []).length;
|
|
89
|
+
const propsWithCls = propsCache[cache] ?? await Promise.all(props.map(async (col, idx) => {
|
|
90
|
+
const key = col.name;
|
|
91
|
+
const select = data.popup?.find(el => el.name === key);
|
|
92
|
+
// console.log(select);
|
|
93
|
+
const clsData = select?.data || col.data;
|
|
94
|
+
const cls = clsData ? await getSelect(clsData).then(el => {
|
|
95
|
+
if (!el) {
|
|
96
|
+
console.error('gis vtile: cls not found', clsData);
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
return { name: clsData, type: el.arr ? 'cls' : 'select', sql: el.sql };
|
|
100
|
+
}) : null;
|
|
101
|
+
const { name, type: type1, sql: sql1 } = cls || getColumnCLS(table, col.data || key) || {};
|
|
102
|
+
// console.log({ name, type, sql });
|
|
103
|
+
if (name && type1 === 'cls') {
|
|
104
|
+
return `(select name from admin.cls where parent='${name}' and code::text=q.${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
105
|
+
}
|
|
106
|
+
if (type1 === 'select' && sql1) {
|
|
107
|
+
const { fields = [] } = await pg.query(`${sql1} limit 0`);
|
|
108
|
+
return `(select "${fields[1].name}" from (${sql1})s${idx} where "${fields[0].name}"::text=q."${key}"::text limit 1) as "${key}_text","${key}"::text`;
|
|
109
|
+
}
|
|
110
|
+
return `"${key}"::text`;
|
|
111
|
+
}));
|
|
112
|
+
propsCache[cache] = propsCache[cache] || propsWithCls;
|
|
113
|
+
|
|
114
|
+
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
115
|
+
|| (parseInt(z, 10) < parseInt(pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
|
|
116
|
+
|
|
117
|
+
const koef = {
|
|
118
|
+
10: 0.1, 11: 0.05, 12: 0.005, 13: 0.001, 14: 0.001, 15: 0.0005,
|
|
119
|
+
}[z] || 0.0005;
|
|
120
|
+
|
|
121
|
+
const clusterZoom1 = style?.clusterZoom || clusterZoom;
|
|
122
|
+
const queryMode = ((clusterZoom1 - z) > 2 ? 1 : null) || ((clusterZoom1 - z) > 0 ? 2 : null) || 3;
|
|
123
|
+
// console.log(z, clusterZoom, queryMode);
|
|
124
|
+
// const props1 = props.filter((el, idx, arr) => idx === arr.findIndex(item => item.name === el.name));
|
|
125
|
+
const q = {
|
|
126
|
+
1: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
127
|
+
FROM (
|
|
128
|
+
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
|
|
129
|
+
FROM (
|
|
130
|
+
SELECT st_centroid(st_union(geom)) as geom, count(*) as point_count
|
|
131
|
+
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
132
|
+
)q`,
|
|
133
|
+
2: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
134
|
+
FROM (
|
|
135
|
+
SELECT floor(random() * 100000 + 1)::int +row_number() over() as row, count(*) as point_count,
|
|
136
|
+
ST_AsMVTGeom(st_transform(st_centroid(ST_Union(geom)),3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) as geom
|
|
137
|
+
FROM (
|
|
138
|
+
SELECT geom, ST_ClusterDBSCAN(geom,${koef},1) OVER () AS cluster
|
|
139
|
+
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
140
|
+
WHERE cluster IS NOT NULL
|
|
141
|
+
GROUP BY cluster
|
|
142
|
+
ORDER BY 1 DESC
|
|
143
|
+
)q`,
|
|
144
|
+
3: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
145
|
+
FROM (
|
|
146
|
+
SELECT
|
|
147
|
+
|
|
148
|
+
floor(random() * 100000 + 1)::int + row_number() over() as row,
|
|
149
|
+
|
|
150
|
+
${props?.length ? `${propsWithCls.join(',')},` : ''}
|
|
151
|
+
|
|
152
|
+
${pkey} as id,
|
|
153
|
+
ST_AsMVTGeom(st_transform(${geomCol},3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
154
|
+
|
|
155
|
+
FROM (select * from ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d}
|
|
156
|
+
|
|
157
|
+
and ${geom} is not null and st_srid(${geom}) >0
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
${types[type] ? ` and ST_GeometryType(geom) = any ('{ ${types[type]} }') ` : ''}
|
|
162
|
+
|
|
163
|
+
and ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${data?.filter || '1=1'}
|
|
164
|
+
|
|
165
|
+
${(style?.type || data?.geometry_type) === 'polygon' ? `order by st_area(st_transform(${geom},3857)) desc` : ''}
|
|
166
|
+
|
|
167
|
+
limit 3000)q
|
|
168
|
+
) q`,
|
|
169
|
+
}[queryMode];
|
|
170
|
+
|
|
171
|
+
if (querySql === 1) {
|
|
172
|
+
return q;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const args = filterData?.args || [];
|
|
176
|
+
const tile = await pg.query(q, args);
|
|
177
|
+
const buffer = Buffer.concat(tile.rows.map((el) => Buffer.from(el.tile)));
|
|
178
|
+
|
|
179
|
+
if (!nocache) {
|
|
180
|
+
await mkdir(path.dirname(filepath), { recursive: true });
|
|
181
|
+
await writeFile(filepath, buffer, 'binary');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { headers: { ...headers, 'Cache-Control': nocache || nottl ? 'no-cache' : headers['Cache-Control'] }, data: buffer };
|
|
185
|
+
}
|