@opengis/gis 0.2.2 → 0.2.4
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/dist/index.css +1 -1
- package/dist/index.js +3277 -3229
- package/dist/index.umd.cjs +51 -36
- package/module/test/layer/individual.yml +54 -0
- package/package.json +1 -1
- package/plugin.js +4 -2
- package/server/routes/gis/services/get.services.col.js +19 -18
- package/server/routes/gis/services/get.services.js +60 -55
- package/server/routes/map/controllers/layerList.js +42 -32
- package/server/routes/map/controllers/mapFormat.js +76 -116
- package/server/routes/map/index.mjs +9 -9
- package/server/routes/map/maps/get.map.js +54 -50
- package/server/routes/map/vtile1.js +76 -81
|
@@ -1,70 +1,74 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import yaml from 'js-yaml';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getMeta, getTemplate, pgClients, getTemplates, getTemplateSync,
|
|
5
|
+
} from '@opengis/fastify-table/utils.js';
|
|
4
6
|
|
|
5
7
|
const table = 'gis.maps';
|
|
6
8
|
|
|
7
9
|
const columnType = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
text: 'text',
|
|
11
|
+
date: 'date',
|
|
12
|
+
bool: 'yes/no',
|
|
13
|
+
numeric: 'number',
|
|
14
|
+
integer: 'number',
|
|
15
|
+
'timestamp without time zone': 'date',
|
|
16
|
+
'timestamp with time zone': 'date',
|
|
15
17
|
};
|
|
16
18
|
|
|
17
19
|
export default async function getMap({ params = {}, pg = pgClients.client }, reply) {
|
|
18
|
-
|
|
20
|
+
const { columns = [] } = await getMeta({ pg, table }) || {};
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
const fields = columns.map(({ name, dataTypeID, title }) => ({ name, type: columnType[pg.pgType?.[dataTypeID] || 'text'], label: title || name }));
|
|
23
|
+
const mapList = getTemplates('map').map(el => el[0]);
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const rows = pg.pk?.['gis.maps'] ? await pg.query(
|
|
26
|
+
`SELECT map_id as id, * FROM gis.maps where ${params.id ? 'map_id=$1' : '1=1'}`,
|
|
27
|
+
[params.id].filter(Boolean),
|
|
28
|
+
).then(el => el.rows || []) : [];
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
if (params.id && !mapList?.includes(params.id) && !rows[0]) {
|
|
31
|
+
return { status: 404, message: 'map not found' };
|
|
32
|
+
}
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (params.id) {
|
|
39
|
-
const map = rows[0] || {};
|
|
40
|
-
const loadTemplate = await getTemplate('map', params.id) || await getTemplate('map', map.map_key);
|
|
34
|
+
const totals = pg.queryCache ? await pg.queryCache('select json_object_agg(oid::regclass, reltuples) from pg_class')
|
|
35
|
+
.then(el => el.rows?.[0]?.json_object_agg || {}) : {};
|
|
36
|
+
const maps = pg.pk?.['gis.maps'] ? await pg.query(
|
|
37
|
+
'SELECT map_id as slug, name FROM gis.maps',
|
|
38
|
+
).then(el => el.rows || []) : [];
|
|
41
39
|
|
|
42
|
-
|
|
40
|
+
if (params.id) {
|
|
41
|
+
const map = rows[0] || {};
|
|
42
|
+
const loadTemplate = await getTemplate('map', params.id) || await getTemplate('map', map.map_key);
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
const layers = await Promise.all(obj.layers.map(async (layer) => {
|
|
46
|
-
const layerData = await getTemplate('layer', layer);
|
|
47
|
-
const serviceData = layerData || await pg.query('select service_id as id, *, st_asgeojson(bbox)::json as bbox, st_asgeojson(center)::json as center from gis.services where service_id=$1', [layer]).then(el => el.rows?.[0]);
|
|
48
|
-
Object.assign(serviceData, { count: totals[serviceData.source_path || ''] || 0 });
|
|
49
|
-
serviceData.style = typeof serviceData.style ==='object'?serviceData.style: yaml.load(serviceData.style);
|
|
50
|
-
return { ...serviceData, id: layer, visible: true };
|
|
51
|
-
}));
|
|
52
|
-
return { maps,...rows[0], ...obj, layers };
|
|
53
|
-
}
|
|
44
|
+
const obj = loadTemplate;
|
|
54
45
|
|
|
55
|
-
|
|
46
|
+
if (Array.isArray(obj?.layers) && typeof obj?.layers?.[0] === 'string') {
|
|
47
|
+
const layers = await Promise.all(obj.layers.map(async (layer) => {
|
|
48
|
+
const layerData = await getTemplate('layer', layer);
|
|
49
|
+
const serviceData = layerData || await pg.query('select service_id as id, *, st_asgeojson(bbox)::json as bbox, st_asgeojson(center)::json as center from gis.services where service_id=$1', [layer]).then(el => el.rows?.[0]);
|
|
50
|
+
Object.assign(serviceData, { count: totals[serviceData.source_path || ''] || 0 });
|
|
51
|
+
serviceData.style = typeof serviceData.style === 'object' ? serviceData.style : yaml.load(serviceData.style);
|
|
52
|
+
return { ...serviceData, id: layer, visible: true };
|
|
53
|
+
}));
|
|
54
|
+
return {
|
|
55
|
+
maps, ...rows[0], ...obj, layers,
|
|
56
|
+
};
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
59
|
+
return reply.status(200).send({ maps, ...rows[0], ...loadTemplate });
|
|
60
|
+
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
if (params.id && !rows.length) {
|
|
63
|
+
return reply.status(404).send('map not found');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (params.id) {
|
|
67
|
+
return reply.status(200).send({ ...rows[0], fields });
|
|
68
|
+
}
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
70
|
+
return reply.status(200).send({
|
|
71
|
+
rows,
|
|
72
|
+
fields,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -1,94 +1,89 @@
|
|
|
1
1
|
const headers = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
'Content-Type': 'application/x-protobuf',
|
|
3
|
+
'Cache-Control': 'no-cache',
|
|
4
4
|
};
|
|
5
5
|
import Sphericalmercator from '@mapbox/sphericalmercator';
|
|
6
|
+
|
|
6
7
|
const mercator = new Sphericalmercator({ size: 256 });
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
getColumnCLS, getTemplate, getMeta, getFilterSQL, getSelect,
|
|
10
|
+
} from '@opengis/fastify-table/utils.js';
|
|
8
11
|
import yaml from 'js-yaml';
|
|
9
12
|
|
|
10
13
|
const propsCache = {};
|
|
11
14
|
|
|
12
|
-
export default async function vtile({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return `(select "${fields[1].name}" from (${clsdata.sql})s${idx} where "${fields[0].name}"="${key}" limit 1) as "${key}_text","${key}"::text`
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return `"${key}"::text`;
|
|
70
|
-
}));
|
|
71
|
-
if (!propsCache[layer]) { propsCache[layer] = propsWithCls; }
|
|
72
|
-
|
|
73
|
-
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
15
|
+
export default async function vtile({
|
|
16
|
+
params = {}, query, pg, user,
|
|
17
|
+
}, reply) {
|
|
18
|
+
const { y, z } = params;
|
|
19
|
+
const x = params.x.split('.')[0] - 0;
|
|
20
|
+
|
|
21
|
+
// layer
|
|
22
|
+
const [map, layer] = params.layer.includes(':') ? params.layer.split(':') : [null, params.layer];
|
|
23
|
+
|
|
24
|
+
// service
|
|
25
|
+
const data = await getTemplate('layer', layer) || await pg.query(`select * from gis.services where is_active and ${!user?.uid ? 'is_public' : '1=1'} and service_id=$1`, [layer]).then(el => el.rows?.[0]);
|
|
26
|
+
|
|
27
|
+
const geom = data?.geometry_column || 'geom';
|
|
28
|
+
const table = data?.source_path;
|
|
29
|
+
const style = typeof data?.style === 'object' ? data?.style : yaml.load(data?.style);
|
|
30
|
+
const filterList = data?.filters;
|
|
31
|
+
const layerQuery = data?.query;
|
|
32
|
+
|
|
33
|
+
// bbox
|
|
34
|
+
const bbox = mercator.bbox(+y, +x, +z, false/* , '900913' */);
|
|
35
|
+
const bbox2d = `'BOX(${bbox[0]} ${bbox[1]},${bbox[2]} ${bbox[3]})'::box2d`;
|
|
36
|
+
|
|
37
|
+
const filterData = query.filter ? await getFilterSQL({
|
|
38
|
+
filter: query.filter, table, filterList,
|
|
39
|
+
}) : {};
|
|
40
|
+
|
|
41
|
+
// meta
|
|
42
|
+
const metaTable = await getMeta({ pg, table });
|
|
43
|
+
if (metaTable.error) { return metaTable; }
|
|
44
|
+
const { pk: pkey, columns } = metaTable;
|
|
45
|
+
|
|
46
|
+
// props
|
|
47
|
+
const props = !query.all ? data.popup || [] : columns.filter(el => !['json', 'geometry'].includes(pg.pgType[el.dataTypeID]) && !['uid', 'cdate', 'editor_id', 'editor_date'].includes(el.name));
|
|
48
|
+
const cache = layer + (query.all ? '-all' : '');
|
|
49
|
+
const propsWithCls = propsCache[cache] ?? await Promise.all(props.map(async (col, idx) => {
|
|
50
|
+
const key = col.name;
|
|
51
|
+
const select = data.popup?.find(el => el.name === key);
|
|
52
|
+
// console.log(select);
|
|
53
|
+
const clsData = select?.data || col.data;
|
|
54
|
+
const cls = clsData ? await getSelect(clsData).then(el => ({ name: clsData, type: el.arr ? 'cls' : 'select', sql: el.sql })) : null;
|
|
55
|
+
const { name, type, sql } = cls || getColumnCLS(table, col.data || key) || {};
|
|
56
|
+
// console.log({ name, type, sql });
|
|
57
|
+
if (name && type === 'cls') {
|
|
58
|
+
return `(select name from admin.cls where parent='${name}' and code::text=${key}::text limit 1) as "${key}_text","${key}"::text`;
|
|
59
|
+
}
|
|
60
|
+
if (type === 'select' && sql) {
|
|
61
|
+
const { fields = [] } = await pg.query(`${sql} limit 0`);
|
|
62
|
+
return `(select "${fields[1].name}" from (${sql})s${idx} where "${fields[0].name}"::text="${key}"::text limit 1) as "${key}_text","${key}"::text`;
|
|
63
|
+
}
|
|
64
|
+
return `"${key}"::text`;
|
|
65
|
+
}));
|
|
66
|
+
propsCache[cache] = propsCache[cache] || propsWithCls;
|
|
67
|
+
|
|
68
|
+
const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
|
|
74
69
|
|| (parseInt(z, 10) < parseInt(query.pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
|
|
75
70
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
const koef = {
|
|
72
|
+
10: 0.1, 11: 0.05, 12: 0.005, 13: 0.001, 14: 0.001, 15: 0.0005,
|
|
73
|
+
}[z] || 0.0005;
|
|
79
74
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
const { type } = query;
|
|
76
|
+
const types = { point: 'ST_Point' /* ,ST_MultiPoint */, polygon: 'ST_Polygon,ST_MultiPolygon' };
|
|
77
|
+
const sql = ((query.clusterZoom - z) > 2
|
|
78
|
+
? `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
84
79
|
FROM (
|
|
85
80
|
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
|
|
86
81
|
FROM (
|
|
87
82
|
SELECT st_centroid(st_union(geom)) as geom, count(*) as point_count
|
|
88
83
|
FROM ${table} where ${layerQuery || 'true'} and ${filterData?.q || 'true'} and ${geom} && ${bbox2d} ) j
|
|
89
|
-
)q
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
)q` : null
|
|
85
|
+
) || ((query.clusterZoom - z) > 0
|
|
86
|
+
? `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
92
87
|
FROM (
|
|
93
88
|
SELECT floor(random() * 100000 + 1)::int +row_number() over() as row, count(*) as point_count, ST_AsMVTGeom(st_transform(st_centroid(ST_Union(geom)),3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom
|
|
94
89
|
FROM (
|
|
@@ -99,7 +94,7 @@ export default async function vtile({ params = {}, query, pg, user }, reply) {
|
|
|
99
94
|
ORDER BY 1 DESC
|
|
100
95
|
)q`
|
|
101
96
|
|
|
102
|
-
|
|
97
|
+
: `SELECT ST_AsMVT(q, '${layer}', 4096, 'geom','row') as tile
|
|
103
98
|
FROM (
|
|
104
99
|
SELECT
|
|
105
100
|
|
|
@@ -125,8 +120,8 @@ export default async function vtile({ params = {}, query, pg, user }, reply) {
|
|
|
125
120
|
limit 3000)q
|
|
126
121
|
) q`);
|
|
127
122
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
123
|
+
if (query.sql === '1') { return sql; }
|
|
124
|
+
const tile = await pg.query(sql);
|
|
125
|
+
const buffer = Buffer.concat(tile.rows.map((el) => Buffer.from(el.tile)));
|
|
126
|
+
return reply.headers(headers).send(buffer);
|
|
127
|
+
}
|