Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: layer polygon parts on metadata json #45

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/createPackage/models/createPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fsPromise } from 'fs';
import { sep, join, parse as parsePath } from 'path';
import config from 'config';
import { Logger } from '@map-colonies/js-logger';
import { Polygon, MultiPolygon, BBox, bbox as PolygonBbox, intersect, bboxPolygon } from '@turf/turf';
import { Polygon, MultiPolygon, BBox, bbox as PolygonBbox, intersect, bboxPolygon, FeatureCollection, Feature } from '@turf/turf';
import { inject, injectable } from 'tsyringe';
import { degreesPerPixelToZoomLevel, ITileRange, snapBBoxToTileGrid } from '@map-colonies/mc-utils';
import { IJobResponse, OperationStatus } from '@map-colonies/mc-priority-queue';
Expand Down Expand Up @@ -148,9 +148,14 @@ export class CreatePackageManager {
const directoryName = parsedPath.dir;
const metadataFileName = parsedPath.name.concat(METADATA_JSON_FILE_EXTENSION);
const metadataFilePath = join(directoryName, metadataFileName);
const sanitizedBboxToPolygon = bboxPolygon(job.parameters.sanitizedBbox);

record.metadata.footprint = bboxPolygon(job.parameters.sanitizedBbox);
record.metadata.footprint = sanitizedBboxToPolygon;
record.metadata.maxResolutionDeg = job.parameters.targetResolution;
(record.metadata.layerPolygonParts as FeatureCollection) = this.extractPolygonParts(
record.metadata.layerPolygonParts as FeatureCollection,
sanitizedBboxToPolygon
);

const recordMetadata = JSON.stringify(record.metadata);
await fsPromise.writeFile(metadataFilePath, recordMetadata);
Expand Down Expand Up @@ -277,4 +282,22 @@ export class CreatePackageManager {
const bboxToString = bbox.map((val) => String(val.toFixed(numberOfDecimals)).replace('.', '_').replace(/-/g, 'm')).join('');
return `${productType}_${productId}_${productVersion}_${zoomLevel}_${bboxToString}.gpkg`;
}

private extractPolygonParts(layerPolygonParts: FeatureCollection, sanitizedBboxPolygonzied: Feature<Polygon>): FeatureCollection {
this.logger.info(`Extracting layerPolygonParts from original record that intersects with sanitized bbox`);
asafMasa marked this conversation as resolved.
Show resolved Hide resolved
let newPolygonLarts = layerPolygonParts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should create empty feature connection

const newFeatures: Feature[] = [];

newPolygonLarts.features.forEach((feature) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

layerPolygonParts.features.forEach((feature) => {

const intersection = intersect(feature.geometry as Polygon, sanitizedBboxPolygonzied);
if (intersection !== null) {
intersection.properties = feature.properties;
newFeatures.push(intersection);
}
});

newPolygonLarts = { ...newPolygonLarts, bbox: sanitizedBboxPolygonzied.bbox, features: newFeatures };

return newPolygonLarts;
}
}
38 changes: 22 additions & 16 deletions tests/mocks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,47 @@ const layerMetadata = {
],
},
layerPolygonParts: {
bbox: [0, 0, 0, 0],
bbox: [0, 0, 67.5, 22.5],
type: 'FeatureCollection',
features: [
{
bbox: [0, 0, 0, 0],
type: 'Feature',
properties: {},
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
},
type: 'Polygon',
coordinates: [
[
[0, 0],
[67.5, 0],
[67.5, 22.5],
[0, 22.5],
[0, 0],
],
],
},
properties: {},
},
],
},
includedInBests: [],
rawProductData: {
bbox: [0, 0, 0, 0],
bbox: [0, 0, 67.5, 22.5],
type: 'FeatureCollection',
features: [
{
bbox: [0, 0, 0, 0],
type: 'Feature',
properties: {},
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
},
type: 'Polygon',
coordinates: [
[
[0, 0],
[67.5, 0],
[67.5, 22.5],
[0, 22.5],
[0, 0],
],
],
},
properties: {},
},
],
},
Expand Down