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

Cost Surface - shapefile converting & extracting PU cost/id #219

Merged
merged 4 commits into from
Jun 1, 2021
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
2 changes: 1 addition & 1 deletion api/apps/api/src/decorators/shapefile.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDefined } from '@marxan/utils';
import { applyDecorators } from '@nestjs/common';
import {
ApiBody,
Expand All @@ -7,7 +8,6 @@ import {
} from '@nestjs/swagger';

import { ShapefileGeoJSONResponseDTO } from '@marxan-api/modules/scenarios/dto/shapefile.geojson.response.dto';
import { isDefined } from '../utils/is-defined';

export function ApiConsumesShapefile(withGeoJsonResponse = true) {
return applyDecorators(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@nestjs/common';
import { isDefined } from '@marxan/utils';
import { differenceWith } from 'lodash';
import { ArePuidsAllowedPort } from '../are-puids-allowed.port';

import { ScenariosPlanningUnitService } from '../../../../scenarios-planning-unit/scenarios-planning-unit.service';
import { isDefined } from '../../../../../utils/is-defined';

@Injectable()
export class ArePuidsAllowedAdapter
Expand Down
4 changes: 3 additions & 1 deletion api/apps/api/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"@marxan-api/(.*)": "<rootDir>/../src/$1"
"@marxan-api/(.*)": "<rootDir>/../src/$1",
"^@marxan/utils": ["<rootDir>/../../../libs/utils/src"],
"^@marxan/utils/(.*)$": ["<rootDir>/../../..//libs/utils/src/$1"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Test } from '@nestjs/testing';
import { PromiseType } from 'utility-types';
import {
getGeoJson,
getGeoJsonWithMissingCost,
getGeometryMultiPolygon,
} from '../application/__mocks__/geojson';
import { PuCostExtractor } from './pu-cost-extractor';

let sut: PuCostExtractor;
let fixtures: PromiseType<ReturnType<typeof getFixtures>>;

beforeEach(async () => {
fixtures = await getFixtures();
sut = fixtures.getService();
});

describe(`when features miss cost`, () => {
it(`throws exception`, () => {
expect(() => sut.extract(fixtures.geoFeaturesWithoutCost())).toThrow(
/missing cost/,
);
});
});

describe(`when given GeoJson isn't a FeatureCollection`, () => {
it(`throws exception`, () => {
expect(() => sut.extract(fixtures.simpleGeometry())).toThrow(
/Only FeatureCollection is supported/,
);
});
});

describe(`when given GeoJson has pu costs`, () => {
it(`resolves them`, () => {
expect(sut.extract(fixtures.geoFeaturesWithData())).toMatchInlineSnapshot(`
Array [
Object {
"cost": 200,
"planningUnitId": "uuid-1",
},
Object {
"cost": 100,
"planningUnitId": "uuid-2",
},
]
`);
});
});

const getFixtures = async () => {
const sandbox = await Test.createTestingModule({
providers: [PuCostExtractor],
}).compile();

return {
getService: () => sandbox.get(PuCostExtractor),
geoFeaturesWithoutCost: () => getGeoJsonWithMissingCost(),
geoFeaturesWithData: () => getGeoJson(),
simpleGeometry: () => getGeometryMultiPolygon(),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { isDefined } from '@marxan/utils';
import { MaybeProperties } from '@marxan/utils/types';
import { FeatureCollection, GeoJSON, Geometry } from 'geojson';
import { PlanningUnitCost } from '../ports/planning-unit-cost';
import { PuExtractorPort } from '../ports/pu-extractor/pu-extractor.port';

type Properties = {
cost: number;
planningUnitId: string;
};

type MaybeCost = MaybeProperties<Properties>;

export class PuCostExtractor implements PuExtractorPort {
extract(geo: GeoJSON): PlanningUnitCost[] {
if (!this.isFeatureCollection(geo)) {
throw new Error('Only FeatureCollection is supported.');
}
const input: FeatureCollection<Geometry, MaybeCost> = geo;

const puCosts = input.features
.map((feature) => feature.properties)
.filter(this.hasCostValues);

if (puCosts.length !== input.features.length) {
throw new Error(
`Some of the Features are missing cost and/or planning unit id.`,
);
}

return puCosts.map((puCost) => ({
planningUnitId: puCost.planningUnitId,
cost: puCost.cost,
}));
}

private isFeatureCollection(geo: GeoJSON): geo is FeatureCollection {
return geo.type === 'FeatureCollection';
}

private hasCostValues(properties: MaybeCost): properties is Properties {
return (
isDefined(properties) &&
isDefined(properties.cost) &&
isDefined(properties.planningUnitId)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GeoJSON } from 'geojson';
import { ShapefileService } from '../../shapefiles/shapefiles.service';

import { CostSurfaceJobInput } from '../cost-surface-job-input';
import { ShapefileConverterPort } from '../ports/shapefile-converter/shapefile-converter.port';

export class ShapefileConverter implements ShapefileConverterPort {
constructor(private readonly converter: ShapefileService) {}

async convert(file: CostSurfaceJobInput['shapefile']): Promise<GeoJSON> {
return (await this.converter.getGeoJson(file)).data;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature, FeatureCollection, MultiPolygon, Polygon } from 'geojson';
import { FeatureCollection, MultiPolygon, Polygon } from 'geojson';
import { PlanningUnitCost } from '../../ports/planning-unit-cost';

export const getGeoJson = (): FeatureCollection<
Expand Down Expand Up @@ -56,3 +56,8 @@ export const getGeoJsonWithMissingCost = (): FeatureCollection<
},
],
});

export const getGeometryMultiPolygon = (): MultiPolygon => ({
type: 'MultiPolygon',
coordinates: [],
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { WorkerModule, WorkerProcessor } from '../worker';
import { ShapefilesModule } from '../shapefiles/shapefiles.module';

import { SurfaceCostProcessor } from './application/surface-cost-processor';
import { SurfaceCostWorker } from './application/surface-cost-worker';
Expand All @@ -11,12 +12,15 @@ import { ArePuidsAllowedPort } from './ports/pu-validator/are-puuids-allowed.por
import { ShapefileConverterPort } from './ports/shapefile-converter/shapefile-converter.port';

import { TypeormCostSurface } from './adapters/typeorm-cost-surface';
import { ShapefileConverter } from './adapters/shapefile-converter';
import { ScenariosPuCostDataGeo } from '../scenarios/scenarios-pu-cost-data.geo.entity';
import { ScenariosPlanningUnitGeoEntity } from '../scenarios/scenarios-planning-unit.geo.entity';
import { PuCostExtractor } from './adapters/pu-cost-extractor';

@Module({
imports: [
WorkerModule,
ShapefilesModule,
TypeOrmModule.forFeature([
ScenariosPuCostDataGeo,
ScenariosPlanningUnitGeoEntity, // not used but has to imported somewhere
Expand All @@ -38,11 +42,11 @@ import { ScenariosPlanningUnitGeoEntity } from '../scenarios/scenarios-planning-
},
{
provide: PuExtractorPort,
useValue: {},
useClass: PuCostExtractor,
},
{
provide: ShapefileConverterPort,
useValue: {},
useClass: ShapefileConverter,
},
],
})
Expand Down
4 changes: 3 additions & 1 deletion api/apps/geoprocessing/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"^.+\\.(t|j)s$": "ts-jest"
},
"moduleNameMapper": {
"@marxan-geoprocessing/(.*)": "<rootDir>/src/$1"
"@marxan-geoprocessing/(.*)": "<rootDir>/src/$1",
"^@marxan/utils": ["<rootDir>/../../libs/utils/src"],
"^@marxan/utils/(.*)$": ["<rootDir>/../..//libs/utils/src/$1"]
}
}
1 change: 1 addition & 0 deletions api/libs/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { isDefined } from './is-defined';
File renamed without changes.
1 change: 1 addition & 0 deletions api/libs/utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MaybeProperties } from './maybe-properties';
1 change: 1 addition & 0 deletions api/libs/utils/src/types/maybe-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MaybeProperties<T> = Partial<T> | undefined | null;
9 changes: 9 additions & 0 deletions api/libs/utils/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/utils"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
11 changes: 10 additions & 1 deletion api/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
"compilerOptions": {
"tsConfigPath": "apps/geoprocessing/tsconfig.app.json"
}
},
"utils": {
"type": "library",
"root": "libs/utils",
"entryFile": "index",
"sourceRoot": "libs/utils/src",
"compilerOptions": {
"tsConfigPath": "libs/utils/tsconfig.lib.json"
}
}
}
}
}
5 changes: 3 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
"unzipper": "^0.10.11",
"uuid": "8.3.2",
"swagger-ui-express": "^4.1.6"

},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
Expand Down Expand Up @@ -145,7 +144,9 @@
],
"moduleNameMapper": {
"@marxan-api/(.*)": "<rootDir>/apps/api/src/$1",
"@marxan-geoprocessing/(.*)": "<rootDir>/apps/geoprocessing/src/$1"
"@marxan-geoprocessing/(.*)": "<rootDir>/apps/geoprocessing/src/$1",
"@marxan/utils/(.*)": "<rootDir>/libs/utils/src/$1",
"@marxan/utils": "<rootDir>/libs/utils/src"
}
}
}
8 changes: 7 additions & 1 deletion api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
],
"@marxan-api/*": [
"apps/api/src/*"
],
"@marxan/utils": [
"libs/utils/src"
],
"@marxan/utils/*": [
"libs/utils/src/*"
]
}
},
"exclude": [
"node_modules",
"dist"
]
}
}