@opengis/gis 0.2.0 → 0.2.1

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 (33) hide show
  1. package/dist/index.css +1 -1
  2. package/dist/index.js +4018 -2967
  3. package/dist/index.umd.cjs +48 -38
  4. package/module/cls.json +2 -1
  5. package/module/gis/form/gis.maps.form.json +2 -2
  6. package/module/gis/form/gis.registers.form.json +8 -199
  7. package/module/gis/form/gis.services.form.json +11 -200
  8. package/module/gis/table/gis.maps.table.json +5 -7
  9. package/module/gis/table/gis.ogc_service.table.json +14 -3
  10. package/module/gis/table/gis.registers.table.json +2 -3
  11. package/module/gis/table/gis.services.table.json +19 -6
  12. package/module/test/cls/ts.temp_status.json +18 -0
  13. package/module/test/cls/ts.temp_type.json +10 -0
  14. package/module/test/map/bp_myo.json +10 -6
  15. package/module/test/select/address_id.json +3 -0
  16. package/module/test/select/address_id.sql +8 -0
  17. package/module/test/table/data_bp_myo.bp.table.json +123 -0
  18. package/package.json +4 -4
  19. package/server/migrations/ogc.sql +107 -0
  20. package/server/routes/gis/index.mjs +16 -2
  21. package/server/routes/gis/ogc/map.info.point.js +120 -0
  22. package/server/routes/gis/registers/funcs/classifiers.js +6 -3
  23. package/server/routes/gis/registers/funcs/handleRegistryRequest.js +15 -22
  24. package/server/routes/gis/registers/gis.registry.js +3 -2
  25. package/server/routes/gis/services/add.service.js +5 -5
  26. package/server/routes/gis/services/get.services.col.js +19 -11
  27. package/server/routes/gis/services/get.services.js +25 -12
  28. package/server/routes/map/controllers/layerList.js +14 -2
  29. package/server/routes/map/controllers/mapFeatures.js +11 -2
  30. package/server/routes/map/controllers/mapFormat.js +13 -16
  31. package/server/routes/map/controllers/vtile.js +1 -1
  32. package/server/routes/map/maps/get.map.js +13 -11
  33. package/server/routes/map/vtile1.js +30 -2
@@ -1,24 +1,32 @@
1
- import { getMeta, pgClients, yml2json } from "@opengis/fastify-table/utils.js";
2
-
3
-
1
+ import { metaFormat, pgClients } from "@opengis/fastify-table/utils.js";
4
2
 
5
3
  export default async function getServicesCol({ params = {}, pg = pgClients.client }, reply) {
6
-
7
-
8
4
  const row = await pg.query(`
9
5
  SELECT
10
- service_id, source_path
6
+ service_id, source_path, attributes
11
7
  FROM gis.services where ${params.id ? 'service_id=$1' : '1=1'}
12
8
  `, [params.id].filter(Boolean)).then(el => el.rows[0] || {});
13
9
 
14
- if (!row.source_path) return { status: 404, message: 'source not found' }
10
+ if (!row.source_path) {
11
+ return reply.status(404).send({ code: 404, message: 'source not found' });
12
+ }
13
+
15
14
  const fields = await pg.query(` SELECT * FROM ${row.source_path} limit 0`).then(el => el.fields);
16
15
  const field = fields.find(el => el.name == params.col);
17
- if (!field) return {
18
- status: 404, message: { error: 'col not found', fields }
16
+
17
+ if (!field) {
18
+ return reply.status(404).send({ message: { error: 'column not found', fields }, code: 404 });
19
19
  }
20
20
 
21
- const rows = await pg.query(`SELECT ${params.col} as id, count(*) FROM ${row.source_path} where ${params.col} is not null group by ${params.col} order by count(*) desc limit 10`).then(el => el.rows);
21
+ const rows = await pg.query(`SELECT ${params.col}::text as id, ${params.col}, count(*) FROM ${row.source_path}
22
+ where ${params.col} is not null group by ${params.col} order by count(*) desc limit 10`
23
+ ).then(el => el.rows || []);
24
+
25
+ const cls = row.attributes?.filter?.((col) => col.name && col.data && ["select", "badge", "tags"].includes(col.format)).reduce((acc, curr) => ({ ...acc, [curr.name]: curr.data }), {}) || {};
26
+
27
+ await metaFormat({ rows, table: row.source_path, cls });
28
+
29
+ const rows1 = rows.map(row => ({ ...row[`${params.col}_data`], ...row, [`${params.col}_data`]: undefined, [params.col]: undefined }));
22
30
 
23
- return { rows, field, fields }
31
+ return reply.status(200).send({ rows: rows1, field, fields });
24
32
  }
@@ -1,7 +1,5 @@
1
1
  import { dataUpdate, getMeta, pgClients, yml2json } from "@opengis/fastify-table/utils.js";
2
2
 
3
- const table = 'gis.services';
4
-
5
3
  const columnType = {
6
4
  text: 'text',
7
5
  date: 'date',
@@ -13,22 +11,34 @@ const columnType = {
13
11
  };
14
12
 
15
13
  export default async function getServices({ params = {}, pg = pgClients.client }, reply) {
16
-
17
-
18
14
  const rows = await pg.query(`
19
15
  SELECT
20
- service_id, service_key, name, description, keywords, category, holder, group_id, service_type,
16
+ service_id, service_key, name, description, keywords, category, holder, group_id, b.group_name, service_type,
21
17
  source_type, service_url, source_path, query, geom_type, geometry_column, sql_list, attributes, filters,
22
18
  popup, style, legend, card, srid, bbox::box2d as extent, st_asgeojson(bbox)::json as bbox, st_asgeojson(center)::json as center, is_active as enabled, is_public, is_active, is_downloadable, metadata,
23
19
  metadata_url, thumbnail_url, created_by, updated_by, updated_at, created_at, template
24
- FROM gis.services where ${params.id ? 'service_id=$1' : '1=1'}
20
+ FROM gis.services a
21
+ left join lateral (select group_name from gis.group_list where group_id=a.group_id)b on 1=1
22
+ where ${params.id ? 'service_id=$1' : '1=1'}
25
23
  `, [params.id].filter(Boolean)).then(el => el.rows || []);
26
24
 
27
- if (params.id && rows[0]?.template) {
25
+ if (params.id && !rows.length) {
26
+ return reply.status(404).send('service not found');
27
+ }
28
+
29
+
30
+ const totals = pg.queryCache ? await pg.queryCache(`select json_object_agg(oid::regclass, reltuples) from pg_class`)
31
+ .then(el => el.rows?.[0]?.json_object_agg || {}) : {};
32
+
33
+ if (params.id) {
28
34
  const html = await pg.query('select body from admin.templates where template_id=$1', [params.id]).then(el => el.rows?.[0]?.body);
29
35
  Object.assign(rows[0], { html });
30
36
  }
31
37
 
38
+ rows.filter(row => row.source_path).forEach(row => {
39
+ Object.assign(row, { count: totals[row.source_path] || 0 });
40
+ });
41
+
32
42
  const { columns = [] } = await getMeta({ pg, table: rows[0].source_path }) || {};
33
43
 
34
44
  const fields = columns.map(({ name, dataTypeID, title }) => ({ name, type: columnType[pg.pgType?.[dataTypeID] || 'text'], label: title || name }));
@@ -43,17 +53,20 @@ export default async function getServices({ params = {}, pg = pgClients.client }
43
53
  Object.assign(row, { center: centers[row.service_id] });
44
54
  }));
45
55
 
46
- rows.forEach(row => Object.assign(row, { style: row.style ? yml2json(row.style) : undefined }));
47
- rows.forEach(row => Object.assign(row, { center: row.center?.coordinates }));
56
+ rows.forEach(row => Object.assign(row, {
57
+ center: row.center?.coordinates,
58
+ style: row.style ? yml2json(row.style) : undefined
59
+ }));
48
60
  rows.filter(row => row.extent).forEach(row => Object.assign(row, {
49
61
  extent: row.extent.match(/BOX\(([^)]+)\)/)?.[1]
50
62
  ?.replace?.(/ /g, ",")
51
63
  ?.split?.(",")
52
64
  }));
53
65
 
54
- if (params.id && !rows.length) {
55
- return reply.status(404).send('service not found');
56
- }
66
+
67
+ rows.filter(row => row.filters).forEach(row => Object.assign(row, {
68
+ filters: row.filters.map(filter => ({ ...filter, api: '/api/suggest/' + row.source_path + ':' + filter.name + (filter.data && filter.name !== filter.data ? `?sel=${filter.data}` : '') }))
69
+ }));
57
70
 
58
71
  if (params.id) {
59
72
  return reply.status(200).send({ ...rows[0], fields });
@@ -10,15 +10,27 @@ export default async function layerList({
10
10
  }
11
11
 
12
12
  const q = `
13
- select service_id as id, name, category, style, bbox::box2d as extent, st_asgeojson(bbox)::json as geom, null as url, coalesce(service_type, 'vtile') as service from gis.services where is_active and ${!user.uid ? 'is_public' : '1=1'}
13
+ select service_id as id, name, category, style, bbox::box2d as extent, st_asgeojson(bbox)::json as geom, null as url, coalesce(service_type, 'vtile') as service, group_id,
14
+ popup, card, filters, style, source_path
15
+ from gis.services where is_active and ${!user.uid ? 'is_public' : '1=1'}
14
16
  union all
15
- select ogc_service_id as id, name, category, null as style, geom::box2d as extent, st_asgeojson(geom)::json as geom, url, 'ogc' as service from gis.ogc_service where enabled and ${!user.uid ? 'ispublic' : '1=1'}
17
+ select ogc_service_id as id, name, category, null as style, geom::box2d as extent, st_asgeojson(geom)::json as geom, url, 'ogc' as service, group_id,
18
+ null as popup, null as card, null as filters, null as style, table_name as source_path
19
+ from gis.ogc_service where enabled and ${!user.uid ? 'ispublic' : '1=1'}
16
20
  `;
17
21
 
18
22
  if (user.uid && sql) return q;
19
23
 
20
24
  const rows = await pg.query(q).then(el => el.rows || []);
21
25
 
26
+ const totals = pg.queryCache ? await pg.queryCache(`select json_object_agg(oid::regclass, reltuples) from pg_class`)
27
+ .then(el => el.rows?.[0]?.json_object_agg || {}) : {};
28
+ rows.filter(row => row.source_path).forEach(row => Object.assign(row, { count: totals[row.source_path] || 0 }));
29
+
30
+ const groupIds = rows.map(el => el.group_id).filter(Boolean);
31
+ const groupNames = groupIds.length ? await pg.query('select json_object_agg(group_id,group_name) from gis.group_list where group_id=any($1)', [groupIds]).then(el => el.rows?.[0]?.json_object_agg || {}) : {};
32
+ rows.filter(el => el.group_id).forEach(row => Object.assign(row, { group_name: groupNames[row.group_id] }));
33
+
22
34
  if (!rows.length) {
23
35
  return reply.status(200).send([]);
24
36
  }
@@ -1,4 +1,4 @@
1
- import { config, getMeta, getFilterSQL, getTemplate, metaFormat, pgClients } from '@opengis/fastify-table/utils.js';
1
+ import { getMeta, getFilterSQL, getTemplate, metaFormat, pgClients, yml2json } from '@opengis/fastify-table/utils.js';
2
2
 
3
3
  const layerMetaColumns = ['id', 'name', 'label', 'source', 'table', 'pk', 'geomColumn', 'srid', 'cls', 'htmls'];
4
4
 
@@ -22,10 +22,19 @@ export default async function mapFeaturesPoint({ pg = pgClients.client, params =
22
22
 
23
23
  const index = loadTemplate?.find?.(el => el[0].split('.').shift() === 'index')?.[1];
24
24
 
25
- const layers = !index
25
+ const layers1 = !index
26
26
  ? loadTemplate?.layers
27
27
  : loadTemplate?.filter?.(el => el[0].split('.').shift() !== 'index')?.map(el => ({ ...el[1], id: el[0].replace('.yml', '').replace('.json', '') }));
28
28
 
29
+ const serviceLayers = await Promise.all(layers1.filter(el => typeof el === 'string').map(async (layer) => {
30
+ const layerData = await getTemplate('layer', layer);
31
+ 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]);
32
+ serviceData.style = yml2json(serviceData.style);
33
+ return { ...serviceData, id: layer, visible: true };
34
+ }));
35
+
36
+ const layers = layers1.filter(el => el && typeof el === 'object').concat(serviceLayers);
37
+
29
38
  if (!layers?.length) {
30
39
  return reply.status(404).send('map layers not found');
31
40
  }
@@ -1,10 +1,7 @@
1
- import { pgClients, getTemplate, handlebars } from "@opengis/fastify-table/utils.js";
2
- import { attachClassifiers } from '../../gis/registers/funcs/classifiers.js';
3
-
4
- const pg = pgClients.client;
1
+ import { pgClients, getTemplate, handlebars, metaFormat } from "@opengis/fastify-table/utils.js";
5
2
 
6
3
  export default async function mapFormat(req, reply) {
7
- const { query } = req;
4
+ const { pg = pgClients.client, query = {} } = req;
8
5
  const { layer, id, map } = query;
9
6
  const time = Date.now();
10
7
 
@@ -65,30 +62,30 @@ export default async function mapFormat(req, reply) {
65
62
  const excluded = ["created_by", "created_at", "updated_by", "geom", "id", "uid", "updated_at", "cdate", "editor_date", "editor_id"];
66
63
  const filteredColumns = columnNames.filter(name => !excluded.includes(name));
67
64
 
68
- const {
69
- rows: [row]
70
- } = await pg.query(
65
+ const rows = await pg.query(
71
66
  `SELECT ${pk} as "id", geom::json, ${filteredColumns.map(n => `"${n}"`).join(", ")} FROM ${source_path} WHERE ${pk} = $1`,
72
67
  [id]
73
- );
68
+ ).then(el => el.rows || []);
74
69
 
75
- if (!row) return reply.status(404).send('object not found');
70
+ if (!rows.length) return reply.status(404).send('object not found');
76
71
 
77
72
  const classifiers = columns
78
73
  .filter(col => col.data && ["select", "badge", "tags"].includes(col.format))
79
- .map(col => ({ name: col.name, classifier: col.data }));
74
+ .reduce((acc, curr) => ({ ...acc, [curr.name]: curr.data }), {});
75
+
76
+ await metaFormat({ rows, table: source_path, cls: classifiers, sufix: false });
77
+ const fullRow = rows[0];
80
78
 
81
- const fullRow = await attachClassifiers(row, classifiers);
82
79
  const result = [];
83
80
  for await (const col of columns) {
84
81
  if (excluded.includes(col.name)) continue;
85
82
  if (col.hidden_card === true) continue;
86
83
 
87
- const name = col.label || col.ua || col.name;
84
+ const name = col.label || col.ua || col.name;
88
85
  let value;
89
86
  value = fullRow[`${col.name}_data`]?.text
90
87
  || fullRow[`${col.name}_text`]
91
- || fullRow[col.name] ;
88
+ || fullRow[col.name];
92
89
 
93
90
  if (col.format === 'date' && fullRow[col.name]) {
94
91
  // const dt = formatDate(fullRow[col.name], { hash: { format: 'dd.mm.yy' } });
@@ -98,12 +95,12 @@ export default async function mapFormat(req, reply) {
98
95
  if (!value) continue;
99
96
  result.push(`<div class="grid grid-cols-1 gap-1 py-3 sm:grid-cols-3 sm:gap-4 even:bg-gray-50 text-[12px]">
100
97
  <dt class="text-gray-900">${name}</dt>
101
- <dd class="text-gray-700 sm:col-span-2">${value|| '-'}</dd>
98
+ <dd class="text-gray-700 sm:col-span-2">${value || '-'}</dd>
102
99
  </div>`);
103
100
  }
104
101
 
105
102
  const htmlTemplate = pg?.pk?.['admin.template']
106
- ? await pg.query('select body from admin.templates where name=$1 limit 1').then(el => el.rows?.[0]?.body)
103
+ ? await pg.query('select body from admin.templates where name=$1 limit 1', [template || id]).then(el => el.rows?.[0]?.body)
107
104
  : null;
108
105
 
109
106
  const html1 = await handlebars.compile(htmlTemplate || 'template not found')(fullRow);
@@ -80,7 +80,7 @@ export default async function vtile({ pg = pgClients.client, params = {}, query
80
80
  return reply.status(404).send('layer table not found');
81
81
  }
82
82
 
83
- const cls = await getColumnCLS(table);
83
+ const cls = getColumnCLS(table);
84
84
 
85
85
  const meta = await getMeta({ pg, table });
86
86
  const { columns = [] } = meta || {};
@@ -19,30 +19,34 @@ export default async function getMap({ params = {}, pg = pgClients.client }, rep
19
19
 
20
20
  const fields = columns.map(({ name, dataTypeID, title }) => ({ name, type: columnType[pg.pgType?.[dataTypeID] || 'text'], label: title || name }));
21
21
  const mapList = getTemplates('map').map(el => el[0]);
22
-
22
+
23
23
  const rows = pg.pk?.['gis.maps'] ? await pg.query(
24
24
  `SELECT map_id as id, * FROM gis.maps where ${params.id ? 'map_id=$1' : '1=1'}`,
25
25
  [params.id].filter(Boolean),
26
26
  ).then(el => el.rows || []) : [];
27
27
 
28
- if (params.id && !mapList.includes(params.id) && !rows[0]) {
29
- return { status: 404, message: 'map not found'}
28
+ if (params.id && !mapList.includes(params.id) && !rows[0]) {
29
+ return { status: 404, message: 'map not found' }
30
30
  }
31
-
31
+
32
+ const totals = pg.queryCache ? await pg.queryCache(`select json_object_agg(oid::regclass, reltuples) from pg_class`)
33
+ .then(el => el.rows?.[0]?.json_object_agg || {}) : {};
34
+
32
35
  if (params.id) {
33
36
  const map = rows[0] || {};
34
- const loadTemplate = await getTemplate('map', params.id) || await getTemplate('map', map.map_key);
35
-
37
+ const loadTemplate = await getTemplate('map', params.id) || await getTemplate('map', map.map_key);
38
+
36
39
  const obj = loadTemplate;
37
40
 
38
41
  if (Array.isArray(obj?.layers) && typeof obj?.layers?.[0] === 'string') {
39
42
  const layers = await Promise.all(obj.layers.map(async (layer) => {
40
43
  const layerData = await getTemplate('layer', layer);
41
- const serviceData = layerData || await pg.query('select service_id as id, * from gis.services where service_id=$1', [layer]).then(el => el.rows?.[0]);
44
+ 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]);
45
+ Object.assign(serviceData, { count: totals[serviceData.source_path || ''] || 0 });
42
46
  serviceData.style = yaml.load(serviceData.style);
43
- return { ...serviceData, id: layer,visible:true };
47
+ return { ...serviceData, id: layer, visible: true };
44
48
  }));
45
- return { ...rows[0],...obj, layers };
49
+ return { ...rows[0], ...obj, layers };
46
50
  }
47
51
 
48
52
  return reply.status(200).send({ ...rows[0], ...loadTemplate });
@@ -56,9 +60,7 @@ export default async function getMap({ params = {}, pg = pgClients.client }, rep
56
60
  return reply.status(200).send({ ...rows[0], fields });
57
61
  }
58
62
 
59
- maps.filter(el => Object.keys(el).length > 1).forEach(el => rows.push(el));
60
63
  return reply.status(200).send({
61
- template,
62
64
  rows,
63
65
  fields,
64
66
  });
@@ -4,9 +4,11 @@ const headers = {
4
4
  };
5
5
  import Sphericalmercator from '@mapbox/sphericalmercator';
6
6
  const mercator = new Sphericalmercator({ size: 256 });
7
- import { getTemplate, getMeta, getFilterSQL } from '@opengis/fastify-table/utils.js';
7
+ import { getColumnCLS, getTemplate, getMeta, getFilterSQL } from '@opengis/fastify-table/utils.js';
8
8
  import yaml from 'js-yaml';
9
9
 
10
+ const propsCache = {};
11
+
10
12
  export default async function vtile({ params = {}, query, pg, user }, reply) {
11
13
  const { y, z } = params;
12
14
  const x = params.x.split('.')[0] - 0;
@@ -55,6 +57,32 @@ export default async function vtile({ params = {}, query, pg, user }, reply) {
55
57
  return el.type?.[0] === '_' ? `${el.name}[1] as ${el.name}` : `${el.name}${['int4'].includes(el.type) ? '::text' : ''}`;
56
58
  });
57
59
 
60
+ const obj = (data1.attributes || []).concat(data1.card || []).concat(data1.popup || []).filter(el => el.data).reduce((acc, curr) => ({ ...acc, [curr.name]: curr.data }), {});
61
+
62
+ const propsWithCls = propsCache[layer] ?? await Promise.all(props.map(async (key, idx) => {
63
+ const { name, type, sql } = getColumnCLS(table, key) || {};
64
+ if (name && type === 'cls') {
65
+ return `(select name from admin.cls where parent='${name}' and code::text="${key}"::text limit 1) as "${key}_text","${key}"::text`;
66
+ }
67
+ if (type === 'select' && sql) {
68
+ const { fields = [] } = await pg.query(sql + ' limit 0');
69
+ return `(select "${fields[1].name}" from (${sql})s${idx} where "${fields[0].name}"="${key}" limit 1) as "${key}_text","${key}"::text`
70
+ }
71
+
72
+ if (obj[key]) {
73
+ const clsdata = await pg.query('select name, type, data as sql from admin.cls where name=$1', [obj[key]]).then(el => el.rows?.[0] || {});
74
+ if (clsdata.name && clsdata.type === 'json') {
75
+ return `(select name from admin.cls where parent='${clsdata.name}' and code::text="${key}"::text limit 1) as "${key}_text","${key}"::text`;
76
+ }
77
+ if (clsdata.type === 'sql' && clsdata.sql) {
78
+ const { fields = [] } = await pg.query(clsdata.sql + ' limit 0');
79
+ return `(select "${fields[1].name}" from (${clsdata.sql})s${idx} where "${fields[0].name}"="${key}" limit 1) as "${key}_text","${key}"::text`
80
+ }
81
+ }
82
+ return `"${key}"::text`;
83
+ }));
84
+ if (!propsCache[layer]) { propsCache[layer] = propsWithCls; }
85
+
58
86
  const geomCol = (style?.type === 'point' ? `ST_Centroid(${geom})` : null)
59
87
  || (parseInt(z, 10) < parseInt(query.pointZoom || style?.pointZoom || style?.iconZoom || '0', 10) ? `ST_Centroid(${geom})` : geom);
60
88
 
@@ -90,7 +118,7 @@ export default async function vtile({ params = {}, query, pg, user }, reply) {
90
118
 
91
119
  floor(random() * 100000 + 1)::int + row_number() over() as row,
92
120
 
93
- ${props?.length ? `${props.map(el => `"${el}"::text`).join(',')},` : ''}
121
+ ${props?.length ? `${propsWithCls.join(',')},` : ''}
94
122
 
95
123
  ${pkey} as id,
96
124
  ST_AsMVTGeom(st_transform(${geomCol},3857),ST_TileEnvelope(${z},${y},${x})::box2d,4096,256,false) geom