-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #904 from Vizzuality/feat/MARXAN-1318-export-proje…
…ct-custom-protected-areas feat: project custom protected areas piece exporter
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...eoprocessing/src/export/pieces-exporters/project-custom-protected-areas.piece-exporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
}; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api/libs/cloning/src/infrastructure/clone-piece-data/project-custom-protected-areas.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |