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(scenarios): include protected by default flag #471

Merged
merged 3 commits into from
Aug 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const validDataWithGivenPuIds = (
puids.map((id, index) => ({
lockStatus: LockStatus.Unstated,
planningUnitMarxanId: index++,
protectedByDefault: false,
scenarioId,
puGeometryId: v4(),
id: id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { IUCNCategory, ProtectedArea } from '../protected-area.geo.entity';
import { ProtectedArea } from '../protected-area.geo.entity';
import { IUCNCategory } from '@marxan/iucn';

export class IUCNProtectedAreaCategoryDTO extends PickType(ProtectedArea, [
'iucnCategory',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { IUCNCategory } from '@marxan/iucn';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

export enum IUCNCategory {
Ia = 'Ia',
Ib = 'Ib',
II = 'II',
III = 'III',
IV = 'IV',
V = 'V',
VI = 'VI',
NotApplicable = 'Not Applicable',
NotAssigned = 'Not Assigned',
NotReported = 'Not Reported',
}

@Entity('wdpa')
export class ProtectedArea {
@ApiProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppInfoDTO } from '@marxan-api/dto/info.dto';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { CreateProtectedAreaDTO } from './dto/create.protected-area.dto';
import { UpdateProtectedAreaDTO } from './dto/update.protected-area.dto';
import { IUCNCategory, ProtectedArea } from './protected-area.geo.entity';
import { ProtectedArea } from './protected-area.geo.entity';
import * as JSONAPISerializer from 'jsonapi-serializer';

import {
Expand All @@ -23,6 +23,7 @@ import { AdminAreasService } from '@marxan-api/modules/admin-areas/admin-areas.s
import { IsBoolean, IsOptional, IsUUID } from 'class-validator';
import { apiConnections } from '../../ormconfig';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { IUCNCategory } from '@marxan/iucn';

const protectedAreaFilterKeyNames = [
'fullName',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IUCNCategory } from '@marxan/iucn';
import { UpdateScenarioDTO } from '../dto/update.scenario.dto';
import { IUCNCategory } from '../../protected-areas/protected-area.geo.entity';

export const emptyWatchedChangeSet = (): UpdateScenarioDTO => ({
customProtectedAreaIds: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
Max,
Min,
} from 'class-validator';
import { IUCNCategory } from '@marxan-api/modules/protected-areas/protected-area.geo.entity';
import { JobStatus, ScenarioType } from '../scenario.api.entity';
import { ScenarioMetadataDto } from './scenario-metadata.dto';
import { IUCNCategory } from '@marxan/iucn';

export class CreateScenarioDTO {
@ApiProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class ScenarioPlanningUnitSerializer {
units.map((unit) => ({
id: unit.id,
inclusionStatus: unit.lockStatus,
defaultStatus: LockStatus.Unstated, // TODO https://vizzuality.atlassian.net/browse/MARXAN-584
defaultStatus: unit.protectedByDefault
? LockStatus.LockedIn
: LockStatus.Unstated,
})),
);
}
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion api/apps/api/src/modules/scenarios/scenario.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { IUCNCategory } from '@marxan/iucn';
import { User } from '../users/user.api.entity';
import { IsArray, IsOptional } from 'class-validator';
import { TimeUserEntityMetadata } from '../../types/time-user-entity-metadata';
import { BaseServiceResource } from '../../types/resource.interface';
import { IUCNCategory } from '../protected-areas/protected-area.geo.entity';
import { GeoFeatureSetSpecification } from '../geo-features/dto/geo-feature-set-specification.dto';

export const scenarioResource: BaseServiceResource = {
Expand Down
4 changes: 2 additions & 2 deletions api/apps/api/src/modules/scenarios/scenarios.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { ScenarioPlanningUnitSerializer } from './dto/scenario-planning-unit.ser
import { ScenarioPlanningUnitsService } from './planning-units/scenario-planning-units.service';
import { ScenarioPlanningUnitsLinkerService } from './planning-units/scenario-planning-units-linker-service';
import { AdminAreasModule } from '../admin-areas/admin-areas.module';
import { ScenarioPlanningUnitsProtectedStatusCalculatorService } from './planning-units/scenario-planning-units-protection-status-calculator-service';
import { ProtectionStatusModule } from '@marxan/scenarios-planning-unit';

import { SpecificationModule } from './specification';

Expand All @@ -57,6 +57,7 @@ import { SpecificationModule } from './specification';
[ScenariosPuOutputGeoEntity, ScenariosPlanningUnitGeoEntity],
DbConnections.geoprocessingDB,
),
ProtectionStatusModule.for(DbConnections.geoprocessingDB),
UsersModule,
ScenarioFeaturesModule,
AnalysisModule,
Expand All @@ -76,7 +77,6 @@ import { SpecificationModule } from './specification';
WdpaAreaCalculationService,
ScenarioPlanningUnitsService,
ScenarioPlanningUnitsLinkerService,
ScenarioPlanningUnitsProtectedStatusCalculatorService,
ScenarioSerializer,
ScenarioFeatureSerializer,
SolutionResultCrudService,
Expand Down
2 changes: 1 addition & 1 deletion api/apps/api/src/modules/scenarios/scenarios.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import { notFound, RunService } from './marxan-run';
import { GeoFeatureSetSpecification } from '../geo-features/dto/geo-feature-set-specification.dto';
import { SimpleJobStatus } from './scenario.api.entity';
import { assertDefined } from '@marxan/utils';
import { ScenarioPlanningUnitsProtectedStatusCalculatorService } from '@marxan/scenarios-planning-unit';
import { GeoFeaturePropertySetService } from '../geo-features/geo-feature-property-sets.service';
import { ScenarioPlanningUnitsService } from './planning-units/scenario-planning-units.service';
import { ScenarioPlanningUnitsLinkerService } from './planning-units/scenario-planning-units-linker-service';
import { ScenarioPlanningUnitsProtectedStatusCalculatorService } from './planning-units/scenario-planning-units-protection-status-calculator-service';
import { CreateGeoFeatureSetDTO } from '../geo-features/dto/create.geo-feature-set.dto';
import { SpecificationService } from './specification';

Expand Down
2 changes: 1 addition & 1 deletion api/apps/api/test/e2e.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@marxan-api/modules/scenarios/scenario.api.entity';
import { CreateUserDTO } from '@marxan-api/modules/users/dto/create.user.dto';
import { UpdateUserDTO } from '@marxan-api/modules/users/dto/update.user.dto';
import { IUCNCategory } from '@marxan-api/modules/protected-areas/protected-area.geo.entity';
import { IUCNCategory } from '@marxan/iucn';

interface CountryCodeInput {
countryCode?: string;
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 @@ -62,6 +62,8 @@
"@marxan/features/(.*)": "<rootDir>/../../../libs/features/src/$1",
"@marxan/features": "<rootDir>/../../../libs/features/src",
"@marxan/geofeature-calculations/(.*)": "<rootDir>/../../../libs/geofeature-calculations/src/$1",
"@marxan/geofeature-calculations": "<rootDir>/../../../libs/geofeature-calculations/src"
"@marxan/geofeature-calculations": "<rootDir>/../../../libs/geofeature-calculations/src",
"@marxan/iucn/(.*)": "<rootDir>/../../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../../libs/iucn/src"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import * as request from 'supertest';
import { E2E_CONFIG } from '../e2e.config';
import { CreateScenarioDTO } from '@marxan-api/modules/scenarios/dto/create.scenario.dto';
import { IUCNProtectedAreaCategoryDTO } from '@marxan-api/modules/protected-areas/dto/iucn-protected-area-category.dto';
import {
IUCNCategory,
ProtectedArea,
} from '@marxan-api/modules/protected-areas/protected-area.geo.entity';
import { ProtectedArea } from '@marxan-api/modules/protected-areas/protected-area.geo.entity';
import * as JSONAPISerializer from 'jsonapi-serializer';
import { Organization } from '@marxan-api/modules/organizations/organization.api.entity';
import { Project } from '@marxan-api/modules/projects/project.api.entity';
Expand All @@ -18,6 +15,7 @@ import { GivenUserIsLoggedIn } from '../steps/given-user-is-logged-in';
import { ProjectsTestUtils } from '../utils/projects.test.utils';
import { OrganizationsTestUtils } from '../utils/organizations.test.utils';
import { ScenariosTestUtils } from '../utils/scenarios.test.utils';
import { IUCNCategory } from '@marxan/iucn';

afterAll(async () => {
await tearDown();
Expand Down
4 changes: 2 additions & 2 deletions api/apps/api/test/proxy.vector-tiles.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpStatus, INestApplication, Logger } from '@nestjs/common';
import { HttpStatus, INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import * as JSONAPISerializer from 'jsonapi-serializer';
import { tearDown } from './utils/tear-down';
Expand All @@ -9,11 +9,11 @@ import { E2E_CONFIG } from './e2e.config';
import { OrganizationsTestUtils } from './utils/organizations.test.utils';
import { ProjectsTestUtils } from './utils/projects.test.utils';
import { ScenariosTestUtils } from './utils/scenarios.test.utils';
import { IUCNCategory } from '@marxan-api/modules/protected-areas/protected-area.geo.entity';
import { GivenUserIsLoggedIn } from './steps/given-user-is-logged-in';
import { bootstrapApplication } from './utils/api-application';
import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity';
import { FeaturesTestUtils } from './utils/features.test.utils';
import { IUCNCategory } from '@marxan/iucn';

afterAll(async () => {
await tearDown();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class ScenarioPuDefaultInclusion1629699295404
implements MigrationInterface {
name = 'ScenarioPuDefaultInclusion1629699295404';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "scenarios_pu_data"
ADD "protected_by_default" boolean NOT NULL DEFAULT false`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "scenarios_pu_data" DROP COLUMN "protected_by_default"`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,9 @@ import { ApiProperty } from '@nestjs/swagger';
import { Check, Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
import { MultiPolygon } from 'geojson';
import { defaultSrid } from '@marxan/utils/geo';
import { IUCNCategory } from '@marxan/iucn';
import { TimeUserEntityMetadata } from '../../types/time-user-entity-metadata';

export enum IUCNCategory {
Ia = 'Ia',
Ib = 'Ib',
II = 'II',
III = 'III',
IV = 'IV',
V = 'V',
VI = 'VI',
NotApplicable = 'Not Applicable',
NotAssigned = 'Not Assigned',
NotReported = 'Not Reported',
}

@Entity('wdpa')
export class ProtectedArea extends TimeUserEntityMetadata {
@ApiProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export type AreaUnitSampleGeometryProps = {
[k in ForCase]: {
shouldBeExcluded: boolean;
shouldBeIncluded: boolean;
protectedByDefault?: boolean;
};
};

Expand Down Expand Up @@ -495,6 +496,7 @@ export const areaUnitsSample = (forCase: string): AreaUnitSampleGeometry => ({
singleFeature: {
shouldBeExcluded: false,
shouldBeIncluded: false,
protectedByDefault: true,
},
multipleFeatures: {
shouldBeExcluded: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,23 @@ export class ScenarioPlanningUnitsInclusionProcessor
await this.scenarioPlanningUnitsRepo.update(
{
scenarioId,
protectedByDefault: false,
},
{
lockStatus: LockStatus.Unstated,
},
);

await this.scenarioPlanningUnitsRepo.update(
{
scenarioId,
protectedByDefault: true,
},
{
lockStatus: LockStatus.LockedIn,
},
);

await this.scenarioPlanningUnitsRepo.update(
{
scenarioId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,26 @@ export const createWorld = async (app: INestApplication) => {
const toInclude = await insertPuGeometryFromGeoJson(
puGeometryRepo,
planningUnits.features.filter(
(f) => f.properties[forCase].shouldBeIncluded,
(f) =>
f.properties[forCase].shouldBeIncluded ||
f.properties[forCase].protectedByDefault,
),
);
const toExclude = await insertPuGeometryFromGeoJson(
puGeometryRepo,
planningUnits.features.filter(
(f) => f.properties[forCase].shouldBeExcluded,
(f) =>
f.properties[forCase].shouldBeExcluded &&
!f.properties[forCase].protectedByDefault,
),
);
const untouched = await insertPuGeometryFromGeoJson(
puGeometryRepo,
planningUnits.features.filter(
(f) =>
!f.properties[forCase].shouldBeExcluded &&
!f.properties[forCase].shouldBeIncluded,
!f.properties[forCase].shouldBeIncluded &&
!f.properties[forCase].protectedByDefault,
),
);

Expand Down Expand Up @@ -109,6 +114,7 @@ export const createWorld = async (app: INestApplication) => {
puGeometryId: id,
scenarioId,
planningUnitMarxanId: index++,
protectedByDefault: true,
}),
),
)
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 @@ -35,6 +35,8 @@
"@marxan/features/(.*)": "<rootDir>/../../libs/features/src/$1",
"@marxan/features": "<rootDir>/../../libs/features/src",
"@marxan/geofeature-calculations/(.*)": "<rootDir>/../../libs/geofeature-calculations/src/$1",
"@marxan/geofeature-calculations": "<rootDir>/../../libs/geofeature-calculations/src"
"@marxan/geofeature-calculations": "<rootDir>/../../libs/geofeature-calculations/src",
"@marxan/iucn/(.*)": "<rootDir>/../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../libs/iucn/src"
}
}
1 change: 1 addition & 0 deletions api/libs/iucn/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { IUCNCategory } from './iucn-category';
Loading