Skip to content

Commit

Permalink
Merge pull request #904 from Vizzuality/feat/MARXAN-1318-export-proje…
Browse files Browse the repository at this point in the history
…ct-custom-protected-areas

feat: project custom protected areas piece exporter
  • Loading branch information
angelhigueraacid authored Mar 15, 2022
2 parents a6035a2 + e0f3221 commit d143676
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const getFixtures = async () => {
projectWithCustomPlanningArea
? ClonePiece.PlanningAreaCustom
: ClonePiece.PlanningAreaGAdm,
ClonePiece.ProjectCustomProtectedAreas,
];

const expectedScenarioPieces = (projectExport: boolean) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class ExportResourcePiecesAdapter implements ExportResourcePieces {
customPlanningArea
? ExportComponent.newOne(id, ClonePiece.PlanningAreaCustom)
: ExportComponent.newOne(id, ClonePiece.PlanningAreaGAdm),
ExportComponent.newOne(id, ClonePiece.ProjectCustomProtectedAreas),
...scenarioPieces.flat(),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PlanningAreaCustomPieceExporter } from './planning-area-custom.piece-ex
import { PlanningAreaGadmPieceExporter } from './planning-area-gadm.piece-exporter';
import { PlanningUnitsGridGeojsonPieceExporter } from './planning-units-grid-geojson.piece-exporter';
import { PlanningUnitsGridPieceExporter } from './planning-units-grid.piece-exporter';
import { ProjectCustomProtectedAreasPieceExporter } from './project-custom-protected-areas.piece-exporter';
import { ProjectMetadataPieceExporter } from './project-metadata.piece-exporter';
import { ScenarioMetadataPieceExporter } from './scenario-metadata.piece-exporter';

Expand All @@ -27,6 +28,7 @@ import { ScenarioMetadataPieceExporter } from './scenario-metadata.piece-exporte
PlanningAreaCustomGeojsonPieceExporter,
PlanningUnitsGridPieceExporter,
PlanningUnitsGridGeojsonPieceExporter,
ProjectCustomProtectedAreasPieceExporter,
ScenarioMetadataPieceExporter,
Logger,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ClonePiece, ExportJobInput, ExportJobOutput } from '@marxan/cloning';
import { ResourceKind } from '@marxan/cloning/domain';
import { FileRepository } from '@marxan/files-repository';
import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
import { EntityManager } from 'typeorm';
import {
ExportPieceProcessor,
PieceExportProvider,
} from '../pieces/export-piece-processor';
import { Readable } from 'stream';
import { isLeft } from 'fp-ts/lib/Either';
import { ClonePieceUrisResolver } from '@marxan/cloning/infrastructure/clone-piece-data';
import { ProjectCustomProtectedAreasContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-custom-protected-areas';
import { ProtectedArea } from '@marxan/protected-areas';

interface ProjectCustomProtectedAreasSelectResult {
fullName: string;
ewkb: Buffer;
}

@Injectable()
@PieceExportProvider()
export class ProjectCustomProtectedAreasPieceExporter
implements ExportPieceProcessor {
constructor(
private readonly fileRepository: FileRepository,
@InjectEntityManager(geoprocessingConnections.default)
private readonly geoprocessingEntityManager: EntityManager,
private readonly logger: Logger,
) {
this.logger.setContext(ProjectCustomProtectedAreasPieceExporter.name);
}

isSupported(piece: ClonePiece, kind: ResourceKind): boolean {
return (
piece === ClonePiece.ProjectCustomProtectedAreas &&
kind === ResourceKind.Project
);
}

async run(input: ExportJobInput): Promise<ExportJobOutput> {
const customProtectedAreas: ProjectCustomProtectedAreasSelectResult[] = await this.geoprocessingEntityManager
.createQueryBuilder()
.select('ST_AsEWKB(wdpa.the_geom)', 'ewkb')
.addSelect('full_name', 'fullName')
.from(ProtectedArea, 'wdpa')
.where('project_id = :projectId', { projectId: input.resourceId })
.execute();

const content = customProtectedAreas.map<ProjectCustomProtectedAreasContent>(
(protectedArea) => {
return {
fullName: protectedArea.fullName,
ewkb: protectedArea.ewkb.toJSON().data,
};
},
);

const outputFile = await this.fileRepository.save(
Readable.from(JSON.stringify(content)),
`json`,
);

if (isLeft(outputFile)) {
const errorMessage = `${ProjectCustomProtectedAreasPieceExporter.name} - Project Custom Protected Areas - couldn't save file - ${outputFile.left.description}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}

return {
...input,
uris: ClonePieceUrisResolver.resolveFor(
ClonePiece.ProjectCustomProtectedAreas,
outputFile.right,
),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export const projectCustomProtectedAreasRelativePath =
'project-custom-protected-areas.json';

export interface ProjectCustomProtectedAreasContent {
fullName: string;
ewkb: number[];
}

0 comments on commit d143676

Please sign in to comment.