Skip to content

Commit

Permalink
Merge pull request #96 from AplinkosMinisterija/save-and-show-real-geoms
Browse files Browse the repository at this point in the history
Show & save real geometries
  • Loading branch information
ambrazasp authored Jun 6, 2024
2 parents 173e60b + 3786fd0 commit 587ac95
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 121 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@moleculer/database": "github:ambrazasp/moleculerjs-database",
"@r2d2bzh/moleculer-cron": "^0.1.4",
"@sentry/node": "^7.114.0",
"@turf/point-on-feature": "^6.5.0",
"biip-auth-nodejs": "github:DadPatch/biip-auth-nodejs",
"date-fns": "^3.3.1",
"dotenv": "^16.0.0",
Expand Down
20 changes: 3 additions & 17 deletions services/integrations.lumbering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import Cron from '@r2d2bzh/moleculer-cron';
import unzipper from 'unzipper';
import stream from 'node:stream';

import pointOnFeature from '@turf/point-on-feature';
import transformation from 'transform-coordinates';
import { getFeatureCollection } from 'geojsonjs';
import { Event, toEventBodyMarkdown } from './events.service';

@Service({
Expand Down Expand Up @@ -115,19 +112,8 @@ export default class IntegrationsLumberingService extends moleculer.Service {
? geojson.features.splice(0, ctx.params.limit)
: geojson.features;

const transform = transformation('EPSG:4326', '3346');
for (const feature of features) {
const pointOnPolygon = pointOnFeature(feature);

const transformedCoordinates = transform.forward([
pointOnPolygon.geometry.coordinates[0],
pointOnPolygon.geometry.coordinates[1],
]);

const geom = getFeatureCollection({
type: 'Point',
coordinates: transformedCoordinates,
});
feature.geometry.crs = 'EPSG:4326';

const bodyJSON = [
{ title: 'VĮ VMU padalinys', value: `${feature.properties.padalinys} RP` },
Expand All @@ -138,7 +124,7 @@ export default class IntegrationsLumberingService extends moleculer.Service {
},
{ title: 'Kvartalas', value: feature.properties.kvartalas },
{ title: 'Sklypas', value: feature.properties.sklypas },
{ title: 'Kertamas plotas', value: feature.properties.kertamas_plotas },
{ title: 'Kertamas plotas', value: `${feature.properties.kertamas_plotas || '-'} ha` },
{ title: 'Kirtimo rūšis', value: feature.properties.kirtimo_rusis },
{ title: 'Vyraujantys medžiai', value: feature.properties.vyraujantys_medziai },
{ title: 'Atkūrimo būdas', value: feature.properties.atkurimo_budas },
Expand All @@ -149,7 +135,7 @@ export default class IntegrationsLumberingService extends moleculer.Service {
body: toEventBodyMarkdown(bodyJSON),
startAt: new Date(feature.properties.galioja_nuo),
endAt: new Date(feature.properties.galioja_iki),
geom,
geom: feature,
app: app.id,
isFullDay: true,
externalId: feature.properties.id,
Expand Down
75 changes: 59 additions & 16 deletions services/tiles.events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import Supercluster from 'supercluster';
// @ts-ignore
import vtpbf from 'vt-pbf';
import _ from 'lodash';
import { parseToJsonIfNeeded } from '../utils';
import { LKS_SRID, parseToJsonIfNeeded } from '../utils';
import { applyEventsQueryBySubscriptions } from './events.service';
import { Subscription } from './subscriptions.service';
import { Knex } from 'knex';

interface Fields extends CommonFields {
name: string;
Expand Down Expand Up @@ -167,17 +168,24 @@ export default class TilesEventsService extends moleculer.Service {
ctx.params.query = parseToJsonIfNeeded(ctx.params.query);
ctx.meta.$responseType = 'application/x-protobuf';

const supercluster: Supercluster = await this.getSupercluster(ctx);
// make clusters
if (z <= 12) {
const supercluster: Supercluster = await this.getSupercluster(ctx);

const tileEvents = supercluster.getTile(z, x, y);

const tileEvents = supercluster.getTile(z, x, y);
const layers: any = {};

const layers: any = {};
if (tileEvents) {
layers.events = tileEvents;
}

if (tileEvents) {
layers.events = tileEvents;
return Buffer.from(vtpbf.fromGeojsonVt(layers, { extent: superclusterOpts.extent }));
}

return Buffer.from(vtpbf.fromGeojsonVt(layers, { extent: superclusterOpts.extent }));
// show real geometries
const tileData = await this.getMVTTiles(ctx);
return tileData.tile;
}

@Action({
Expand Down Expand Up @@ -235,30 +243,55 @@ export default class TilesEventsService extends moleculer.Service {
});
}

@Method
async getMVTTiles(ctx: Context<{ query: any; x: number; y: number; z: number }>) {
const adapter = await this.getAdapter(ctx);
const table = adapter.getTable();
const knex: Knex = adapter.client;

const query = await this.getComputedQuery(ctx);

const fields = ['id'];
const { x, y, z } = ctx.params;

const WM_SRID = 3857;
const envelopeQuery = `ST_TileEnvelope(${z}, ${x}, ${y})`;
const transformedEnvelopeQuery = `ST_Transform(${envelopeQuery}, ${LKS_SRID})`;
const transformedGeomQuery = `ST_Transform(ST_CurveToLine("geom"), ${WM_SRID})`;

const asMvtGeomQuery = adapter
.computeQuery(table, query)
.whereRaw(`ST_Intersects(events.geom, ${transformedEnvelopeQuery})`)
.select(
...fields,
knex.raw(`ST_AsMVTGeom(${transformedGeomQuery}, ${envelopeQuery}, 4096, 64, true) AS geom`),
);

const tileQuery = knex
.select(knex.raw(`ST_AsMVT(tile, 'events', 4096, 'geom') as tile`))
.from(asMvtGeomQuery.as('tile'))
.whereNotNull('geom');

return tileQuery.first();
}

@Action({
timeout: 0,
})
async getEventsFeatureCollection(ctx: Context<{ query: any }>) {
let { params } = ctx;
params = this.sanitizeParams(params);
params = await this._applyScopes(params, ctx);
params = this.paramsFieldNameConversion(params);

const adapter = await this.getAdapter(ctx);
const table = adapter.getTable();
const knex = adapter.client;

const query = parseToJsonIfNeeded(params.query) || {};

const query = await this.getComputedQuery(ctx);
const fields = ['id'];

const eventsQuery = adapter
.computeQuery(table, query)
.select(...fields, knex.raw(`ST_Transform(ST_Centroid(geom), ${WGS_SRID}) as geom`));
.select(...fields, knex.raw(`ST_Transform(ST_PointOnSurface(geom), ${WGS_SRID}) as geom`));

const res = await knex
.select(knex.raw(`ST_AsGeoJSON(e)::json as feature`))

.from(eventsQuery.as('e'));

return {
Expand All @@ -267,6 +300,16 @@ export default class TilesEventsService extends moleculer.Service {
};
}

@Method
async getComputedQuery(ctx: Context<{ query: any }>) {
let { params } = ctx;
params = this.sanitizeParams(params);
params = await this._applyScopes(params, ctx);
params = this.paramsFieldNameConversion(params);

return parseToJsonIfNeeded(params.query) || {};
}

@Method
async getSupercluster(ctx: Context<{ query: any }>) {
const hash = getSuperclusterHash(ctx.params.query);
Expand Down
87 changes: 0 additions & 87 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -277,93 +277,6 @@
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.4.tgz#0b92dcc0cc1c81f6f306a381f28e31b1a56536e9"
integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==

"@turf/bbox@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/bbox/-/bbox-6.5.0.tgz#bec30a744019eae420dac9ea46fb75caa44d8dc5"
integrity sha512-RBbLaao5hXTYyyg577iuMtDB8ehxMlUqHEJiMs8jT1GHkFhr6sYre3lmLsPeYEi/ZKj5TP5tt7fkzNdJ4GIVyw==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/boolean-point-in-polygon@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-6.5.0.tgz#6d2e9c89de4cd2e4365004c1e51490b7795a63cf"
integrity sha512-DtSuVFB26SI+hj0SjrvXowGTUCHlgevPAIsukssW6BG5MlNSBQAo70wpICBNJL6RjukXg8d2eXaAWuD/CqL00A==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"

"@turf/center@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/center/-/center-6.5.0.tgz#3bcb6bffcb8ba147430cfea84aabaed5dbdd4f07"
integrity sha512-T8KtMTfSATWcAX088rEDKjyvQCBkUsLnK/Txb6/8WUXIeOZyHu42G7MkdkHRoHtwieLdduDdmPLFyTdG5/e7ZQ==
dependencies:
"@turf/bbox" "^6.5.0"
"@turf/helpers" "^6.5.0"

"@turf/clone@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/clone/-/clone-6.5.0.tgz#895860573881ae10a02dfff95f274388b1cda51a"
integrity sha512-mzVtTFj/QycXOn6ig+annKrM6ZlimreKYz6f/GSERytOpgzodbQyOgkfwru100O1KQhhjSudKK4DsQ0oyi9cTw==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/distance@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/distance/-/distance-6.5.0.tgz#21f04d5f86e864d54e2abde16f35c15b4f36149a"
integrity sha512-xzykSLfoURec5qvQJcfifw/1mJa+5UwByZZ5TZ8iaqjGYN0vomhV9aiSLeYdUGtYRESZ+DYC/OzY+4RclZYgMg==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/invariant" "^6.5.0"

"@turf/explode@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/explode/-/explode-6.5.0.tgz#02c292cc143dd629643da5b70bb5b19b9f0f1c6b"
integrity sha512-6cSvMrnHm2qAsace6pw9cDmK2buAlw8+tjeJVXMfMyY+w7ZUi1rprWMsY92J7s2Dar63Bv09n56/1V7+tcj52Q==
dependencies:
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/helpers@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/helpers/-/helpers-6.5.0.tgz#f79af094bd6b8ce7ed2bd3e089a8493ee6cae82e"
integrity sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==

"@turf/invariant@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/invariant/-/invariant-6.5.0.tgz#970afc988023e39c7ccab2341bd06979ddc7463f"
integrity sha512-Wv8PRNCtPD31UVbdJE/KVAWKe7l6US+lJItRR/HOEW3eh+U/JwRCSUl/KZ7bmjM/C+zLNoreM2TU6OoLACs4eg==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/meta@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/meta/-/meta-6.5.0.tgz#b725c3653c9f432133eaa04d3421f7e51e0418ca"
integrity sha512-RrArvtsV0vdsCBegoBtOalgdSOfkBrTJ07VkpiCnq/491W67hnMWmDu7e6Ztw0C3WldRYTXkg3SumfdzZxLBHA==
dependencies:
"@turf/helpers" "^6.5.0"

"@turf/nearest-point@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/nearest-point/-/nearest-point-6.5.0.tgz#2f1781c26ff3f054005d4ff352042973318b92f1"
integrity sha512-fguV09QxilZv/p94s8SMsXILIAMiaXI5PATq9d7YWijLxWUj6Q/r43kxyoi78Zmwwh1Zfqz9w+bCYUAxZ5+euA==
dependencies:
"@turf/clone" "^6.5.0"
"@turf/distance" "^6.5.0"
"@turf/helpers" "^6.5.0"
"@turf/meta" "^6.5.0"

"@turf/point-on-feature@^6.5.0":
version "6.5.0"
resolved "https://registry.yarnpkg.com/@turf/point-on-feature/-/point-on-feature-6.5.0.tgz#37d07afeb31896e53c0833aa404993ba7d500f0c"
integrity sha512-bDpuIlvugJhfcF/0awAQ+QI6Om1Y1FFYE8Y/YdxGRongivix850dTeXCo0mDylFdWFPGDo7Mmh9Vo4VxNwW/TA==
dependencies:
"@turf/boolean-point-in-polygon" "^6.5.0"
"@turf/center" "^6.5.0"
"@turf/explode" "^6.5.0"
"@turf/helpers" "^6.5.0"
"@turf/nearest-point" "^6.5.0"

"@types/cacheable-request@^6.0.1":
version "6.0.3"
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.3.tgz#a430b3260466ca7b5ca5bfd735693b36e7a9d183"
Expand Down

0 comments on commit 587ac95

Please sign in to comment.