Skip to content

Commit

Permalink
Merge pull request #199 from Vizzuality/feature/api/MARXAN-196-migrat…
Browse files Browse the repository at this point in the history
…e-for-bbox

Feature/api/marxan 196 migrate for bbox
  • Loading branch information
aagm authored May 24, 2021
2 parents a16d364 + e2d6846 commit 2fe5bdc
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 26 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ extract-geo-test-data:

generate-content-dumps: dump-api-data | dump-geodb-data
jq -n --arg dateName $$(date +%Y-%m-%d) '{"metadata":{"latest":{"name":$$dateName}}}' > data/data/processed/db_dumps/content.json

generate-export-shpfile:
-docker-compose exec -T postgresql-geo-api mkdir testdataoutput2
-docker-compose exec -T postgresql-geo-api pgsql2shp -f ./testdataoutput2/test.shp -h localhost -p 5432 -r -g the_geom -u ${_GEO_POSTGRES_USER} ${_GEO_POSTGRES_DB} "SELECT the_geom, pug.id as uid, 1 as cost FROM scenarios_pu_data spd inner join planning_units_geom pug on pug.id = spd.pu_geom_id ";
-mkdir data/data
-docker cp marxan-postgresql-geo-api:testdataoutput2 data/data
47 changes: 47 additions & 0 deletions api/src/migrations/api/1621439031072-AddBboxToProjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddBboxToProjects1621439031072 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
-----------------------------------------
-- Generates the new column we need
-----------------------------------------
ALTER TABLE projects ADD COLUMN bbox jsonb;
UPDATE projects SET bbox = jsonb_build_array(ST_XMax(extent), ST_XMin(extent), ST_YMax(extent), ST_YMin(extent)) where extent is not null;
-----------------------------------------
-- tr_GetBbox()
-- Utility func to populate bbox
-----------------------------------------
CREATE OR REPLACE FUNCTION tr_GetBbox()
RETURNS trigger AS $BODY$
BEGIN
IF NEW.extent IS NOT NULL THEN
NEW.bbox := jsonb_build_array(ST_XMax(NEW.extent), ST_XMin(NEW.extent), ST_YMax(NEW.extent), ST_YMin(NEW.extent));
ELSE
NEW.bbox := NULL;
END IF;
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
-----------------------------------------
-- Creates the trigger
-----------------------------------------
DROP TRIGGER IF EXISTS tr_projects_extent ON projects;
CREATE TRIGGER tr_projects_extent
BEFORE INSERT or UPDATE ON projects
FOR EACH ROW EXECUTE
PROCEDURE tr_GetBbox();
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP TRIGGER IF EXISTS tr_projects_extent ON projects;
ALTER TABLE projects
DROP COLUMN bbox;
`);
}
}
1 change: 1 addition & 0 deletions api/src/modules/admin-areas/admin-areas.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class AdminAreasService extends AppBaseService<
'gid2',
'name2',
'theGeom',
'bbox'
],
keyForAttribute: 'camelCase',
};
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/countries/countries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class CountriesService extends AppBaseService<
get serializerConfig(): JSONAPISerializerConfig<Country> {
return {
transform: (item: Country) => ({ ...item, id: item.gid0 }),
attributes: ['gid0', 'name0', 'theGeom'],
attributes: ['gid0', 'name0', 'theGeom', 'bbox'],
keyForAttribute: 'camelCase',
};
}
Expand Down
20 changes: 17 additions & 3 deletions api/src/modules/countries/country.geo.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Column, Entity, PrimaryColumn } from 'typeorm';
import { BBox, Geometry } from 'geojson';
import { Column, PrimaryColumn, ViewEntity } from 'typeorm';
import { BaseServiceResource } from 'types/resource.interface';

export const countryResource: BaseServiceResource = {
Expand All @@ -10,7 +11,13 @@ export const countryResource: BaseServiceResource = {
},
};

@Entity('countries')
@ViewEntity('countries', {
expression: `
SELECT id, gid_0, name_0, the_geom, level, iso3, bbox, created_at, created_by,
last_modified_at FROM admin_regions
WHERE gid_0 IS NOT NULL AND gid_1 IS NULL AND gid_2 IS NULL;
`,
})
export class Country {
@ApiProperty()
@PrimaryColumn()
Expand All @@ -36,7 +43,14 @@ export class Country {
*/
@ApiProperty()
@Column('geometry', { name: 'the_geom' })
theGeom: any;
theGeom!: Geometry;

/**
* Bbox.
*/
@ApiProperty()
@Column('jsonb', { name: 'bbox' })
bbox!: BBox;
}

export class JSONAPICountryData {
Expand Down
11 changes: 9 additions & 2 deletions api/src/modules/projects/project.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { Organization } from '../organizations/organization.api.entity';
import { TimeUserEntityMetadata } from '../../types/time-user-entity-metadata';
import { BaseServiceResource } from '../../types/resource.interface';

import { BBox } from 'geojson';
export const projectResource: BaseServiceResource = {
className: 'Project',
name: {
Expand Down Expand Up @@ -115,12 +115,19 @@ export class Project extends TimeUserEntityMetadata {
planningUnitAreakm2?: number;

/**
* Extent of the project
* custom extent of the project
*/
@ApiPropertyOptional()
@Column('geometry')
extent?: Record<string, unknown> | null;

/**
* Bbox of the custom extent
*/
@ApiProperty()
@Column('jsonb', { name: 'bbox' })
bbox!: BBox;

/**
* JSONB storage for non-relational attributes
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddBboxToAdminAreasAndProjects1621431520169
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
-----------------------------------------
-- Generates the new column we need
-----------------------------------------
ALTER TABLE admin_regions ADD COLUMN bbox jsonb;
UPDATE admin_regions SET bbox = jsonb_build_array(ST_XMax(the_geom), ST_XMin(the_geom), ST_YMax(the_geom), ST_YMin(the_geom));
-----------------------------------------
-- tr_GetBbox()
-- Utility func to populate bbox
-----------------------------------------
CREATE OR REPLACE FUNCTION tr_GetBbox()
RETURNS trigger AS $BODY$
BEGIN
NEW.bbox := jsonb_build_array(ST_XMax(NEW.the_geom), ST_XMin(NEW.the_geom), ST_YMax(NEW.the_geom), ST_YMin(NEW.the_geom));
RETURN NEW;
END;
$BODY$ LANGUAGE plpgsql;
-----------------------------------------
-- Creates the trigger
-----------------------------------------
DROP TRIGGER IF EXISTS tr_adminRegions_the_geom ON admin_regions;
CREATE TRIGGER tr_adminRegions_the_geom
BEFORE INSERT or UPDATE ON admin_regions
FOR EACH ROW EXECUTE
PROCEDURE tr_GetBbox();
-----------------------------------------
-- Recreates the countries view
-----------------------------------------
DROP VIEW countries;
CREATE VIEW countries AS (
SELECT
id,
gid_0,
name_0,
the_geom,
level,
iso3,
created_at,
created_by,
last_modified_at,
bbox
FROM admin_regions
WHERE gid_0 IS NOT NULL AND gid_1 IS NULL AND gid_2 IS NULL
);
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP TRIGGER IF EXISTS tr_adminRegions_the_geom ON admin_regions;
DROP VIEW countries;
CREATE VIEW countries AS (
SELECT
id,
gid_0,
name_0,
the_geom,
level,
iso3,
created_at,
created_by,
last_modified_at
FROM admin_regions
WHERE gid_0 IS NOT NULL AND gid_1 IS NULL AND gid_2 IS NULL
);
ALTER TABLE admin_regions DROP COLUMN bbox;
`);
}
}
23 changes: 3 additions & 20 deletions geoprocessing/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,10 @@
'minzoom': 1,
'maxzoom': 12,
'tiles': [
"http://localhost:3040/api/v1/geodata/protected-areas/preview/tiles/{z}/{x}/{y}.mvt?id=b469939b-2cbb-4a66-9507-87ab7270e6b2",
]
},
'postgis-tilesfeatures': {
'type': 'vector',
'minzoom': 1,
'maxzoom': 12,
'tiles': [
"http://localhost:3040/api/v1/geodata/features/f01903fe-2e41-423e-8b64-176c77f10be6/preview/tiles/{z}/{x}/{y}.mvt?bbox=[0.12084960937499999, 44.11914151643737, 2.2412109375, 44.11914151643737]",
"http://localhost:3040/api/v1/geodata/protected-areas/preview/tiles/{z}/{x}/{y}.mvt",
]
},

'postgis-tilesPU': {
'type': 'vector',
'minzoom': 1,
Expand All @@ -88,17 +81,7 @@
'minzoom': 1,
'maxzoom': 22
},
{
'id': 'postgis-tiles-layer-features',
'type': 'fill',
'source': 'postgis-tilesfeatures',
'source-layer': 'layer0',
'paint': {
'fill-outline-color': 'red',
'fill-color': 'blue',
'fill-opacity': 1
}
},

{
'id': 'postgis-tiles-layer',
'type': 'fill',
Expand Down

0 comments on commit 2fe5bdc

Please sign in to comment.