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 all 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
38 changes: 35 additions & 3 deletions src/createPackage/models/createPackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ 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,
featureCollection as createFeatureCollection,
} 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 @@ -141,16 +151,21 @@ export class CreatePackageManager {
}

public async createJsonMetadata(fullGpkgPath: string, job: JobResponse): Promise<void> {
this.logger.info(`Creating metadata.json file for gpkg in path "${fullGpkgPath}"`);
this.logger.info(`Creating metadata.json file for gpkg in path "${fullGpkgPath}" for jobId ${job.id}`);
const record = await this.rasterCatalogManager.findLayer(job.internalId as string);

const parsedPath = parsePath(fullGpkgPath);
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 +292,21 @@ 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.debug(`Extracting layerPolygonParts from original record that intersects with sanitized bbox`);
const newFeatures: Feature[] = [];

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

const newPolygonLarts = createFeatureCollection(newFeatures, { bbox: sanitizedBboxPolygonzied.bbox });

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