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

feat: scenario features data piece exporter #951

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const getFixtures = async () => {
ClonePiece.ScenarioProtectedAreas,
ClonePiece.ScenarioPlanningUnitsData,
ClonePiece.ScenarioRunResults,
ClonePiece.ScenarioFeaturesData,
];
if (!projectExport) pieces.push(ClonePiece.ExportConfig);
return pieces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class ExportResourcePiecesAdapter implements ExportResourcePieces {
ExportComponent.newOne(id, ClonePiece.ScenarioProtectedAreas),
ExportComponent.newOne(id, ClonePiece.ScenarioPlanningUnitsData),
ExportComponent.newOne(id, ClonePiece.ScenarioRunResults),
ExportComponent.newOne(id, ClonePiece.ScenarioFeaturesData),
];

if (kind === ResourceKind.Scenario) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ScenarioFeaturesData } from '@marxan/features';
import { FileRepositoryModule } from '@marxan/files-repository';
import { OutputScenariosFeaturesDataGeoEntity } from '@marxan/marxan-output';
import { Logger, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ExportConfigProjectPieceExporter } from './export-config.project-piece-exporter';
Expand All @@ -12,6 +14,7 @@ import { PlanningUnitsGridPieceExporter } from './planning-units-grid.piece-expo
import { ProjectCustomFeaturesPieceExporter } from './project-custom-features.piece-exporter';
import { ProjectCustomProtectedAreasPieceExporter } from './project-custom-protected-areas.piece-exporter';
import { ProjectMetadataPieceExporter } from './project-metadata.piece-exporter';
import { ScenarioFeaturesDataPieceExporter } from './scenario-features-data.piece-exporter';
import { ScenarioMetadataPieceExporter } from './scenario-metadata.piece-exporter';
import { ScenarioPlanningUnitsDataPieceExporter } from './scenario-planning-units-data.piece-exporter';
import { ScenarioProtectedAreasPieceExporter } from './scenario-protected-areas.piece-exporter';
Expand All @@ -21,7 +24,10 @@ import { ScenarioRunResultsPieceExporter } from './scenario-run-results.piece-ex
imports: [
FileRepositoryModule,
TypeOrmModule.forFeature([], geoprocessingConnections.apiDB),
TypeOrmModule.forFeature([], geoprocessingConnections.default),
TypeOrmModule.forFeature(
[ScenarioFeaturesData, OutputScenariosFeaturesDataGeoEntity],
geoprocessingConnections.default,
),
],
providers: [
ProjectMetadataPieceExporter,
Expand All @@ -38,6 +44,7 @@ import { ScenarioRunResultsPieceExporter } from './scenario-run-results.piece-ex
ScenarioProtectedAreasPieceExporter,
ScenarioRunResultsPieceExporter,
ScenarioPlanningUnitsDataPieceExporter,
ScenarioFeaturesDataPieceExporter,
Logger,
],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { ClonePiece, ExportJobInput, ExportJobOutput } from '@marxan/cloning';
import { ClonePieceUrisResolver } from '@marxan/cloning/infrastructure/clone-piece-data';
import {
FeatureDataElement,
ScenarioFeaturesDataContent,
} from '@marxan/cloning/infrastructure/clone-piece-data/scenario-features-data';
import { ScenarioFeaturesData } from '@marxan/features';
import { FileRepository } from '@marxan/files-repository';
import { OutputScenariosFeaturesDataGeoEntity } from '@marxan/marxan-output';
import { Injectable, Logger } from '@nestjs/common';
import { InjectEntityManager } from '@nestjs/typeorm';
import { isLeft } from 'fp-ts/Either';
import { Readable } from 'stream';
import { EntityManager, In } from 'typeorm';
import {
ExportPieceProcessor,
PieceExportProvider,
} from '../pieces/export-piece-processor';

type FeaturesSelectResult = {
id: string;
feature_class_name: string;
is_custom: boolean;
};

type FeatureDataElementWithFeatureId = FeatureDataElement & {
sfdId: string;
apiFeatureId: string;
};

type FeatureDataElementWithIsCustom = FeatureDataElementWithFeatureId & {
isCustom: boolean;
featureClassName: string;
};

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

isSupported(piece: ClonePiece): boolean {
return piece === ClonePiece.ScenarioFeaturesData;
}

private parseScenarioFeaturesDataToFeatureDataElementWithFeatureId(
scenarioFeaturesData: ScenarioFeaturesData[],
): FeatureDataElementWithFeatureId[] {
return scenarioFeaturesData
.filter((sfd) => sfd.featureData.featureId)
.map((sfd) => ({
sfdId: sfd.id,
apiFeatureId: sfd.featureData.featureId!,
currentArea: sfd.currentArea,
featureDataHash: sfd.featureData.hash,
featureId: sfd.featureId,
specificationId: sfd.specificationId,
totalArea: sfd.totalArea,
fpf: sfd.fpf,
metadata: sfd.metadata,
prop: sfd.prop,
sepNum: sfd.sepNum,
target2: sfd.target2,
target: sfd.target,
targetocc: sfd.targetocc,
featureClassName: '',
outputFeaturesData: [],
}));
}

private getScenarioFeaturesDataWithIsCustom(
featuresDataWithFeatureId: FeatureDataElementWithFeatureId[],
features: FeaturesSelectResult[],
): FeatureDataElementWithIsCustom[] {
const featurePropertiesById: Record<
string,
Omit<FeaturesSelectResult, 'id'>
> = {};
features.forEach(({ id, feature_class_name, is_custom }) => {
featurePropertiesById[id] = { feature_class_name, is_custom };
});

return featuresDataWithFeatureId.map((el) => {
const featureProperties = featurePropertiesById[el.apiFeatureId];

if (!featureProperties)
throw new Error(
`Feature properties not found for feature with id ${el.apiFeatureId}`,
);

return {
...el,
isCustom: featureProperties.is_custom,
featureClassName: featureProperties.feature_class_name,
};
});
}

private getFileContent(
scenarioFeaturesDataWithIsCustom: FeatureDataElementWithIsCustom[],
outputScenariosFeaturesData: OutputScenariosFeaturesDataGeoEntity[],
): ScenarioFeaturesDataContent {
const customFeaturesData: FeatureDataElement[] = [];
const platformFeaturesData: FeatureDataElement[] = [];

scenarioFeaturesDataWithIsCustom.forEach(
({ sfdId, isCustom, apiFeatureId, ...sfd }) => {
const outputData = outputScenariosFeaturesData.filter(
(el) => el.featureScenarioId === sfdId,
);

const array = isCustom ? customFeaturesData : platformFeaturesData;
array.push({
...sfd,
outputFeaturesData: outputData.map(
({ id, featureScenarioId, ...rest }) => ({
...rest,
}),
),
});
},
);

return {
customFeaturesData,
platformFeaturesData,
};
}

async run(input: ExportJobInput): Promise<ExportJobOutput> {
const scenarioFeaturesData = await this.geoprocessingEntityManager
.getRepository(ScenarioFeaturesData)
.find({
where: { scenarioId: input.resourceId },
relations: ['featureData'],
});
const outputScenariosFeaturesData = await this.geoprocessingEntityManager
.getRepository(OutputScenariosFeaturesDataGeoEntity)
.find({
where: {
featureScenarioId: In(scenarioFeaturesData.map((el) => el.id)),
},
});

const scenarioFeaturesDataWithFeatureId = this.parseScenarioFeaturesDataToFeatureDataElementWithFeatureId(
scenarioFeaturesData,
);
const featuresIds = [
...new Set<string>(
scenarioFeaturesDataWithFeatureId.map((sfd) => sfd.apiFeatureId),
),
];

let features: FeaturesSelectResult[] = [];

if (featuresIds.length > 0) {
features = await this.apiEntityManager
.createQueryBuilder()
.select('id, feature_class_name, is_custom')
.from('features', 'f')
.where('id IN (:...featuresIds)', {
featuresIds,
})
.execute();
}

const scenarioFeaturesDataWithIsCustom = this.getScenarioFeaturesDataWithIsCustom(
scenarioFeaturesDataWithFeatureId,
features,
);

const fileContent = this.getFileContent(
scenarioFeaturesDataWithIsCustom,
outputScenariosFeaturesData,
);

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

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

return {
...input,
uris: ClonePieceUrisResolver.resolveFor(
ClonePiece.ScenarioFeaturesData,
outputFile.right,
{
kind: input.resourceKind,
scenarioId: input.resourceId,
},
),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,35 @@ export const scenarioFeaturesDataRelativePath = {
projectImport: (oldScenarioId: string) =>
`scenarios/${oldScenarioId}/scenario-features-data.json`,
};

export type OutputFeatureDataElement = {
runId: number;
amount?: number;
totalArea?: number;
occurrences?: number;
separation?: number;
target?: boolean;
mpm?: number;
};

export type FeatureDataElement = {
featureClassName: string;
featureDataHash: string;
totalArea: number;
currentArea: number;
fpf?: number;
target?: number;
prop?: number;
target2?: number;
targetocc?: number;
sepNum?: number;
metadata?: Record<'sepdistance', number | string>;
featureId: number;
specificationId: string;
outputFeaturesData: OutputFeatureDataElement[];
};

export type ScenarioFeaturesDataContent = {
customFeaturesData: FeatureDataElement[];
platformFeaturesData: FeatureDataElement[];
};
Loading