Skip to content

Commit

Permalink
bbox utilities used in vl bbox filter
Browse files Browse the repository at this point in the history
  • Loading branch information
aagm committed Jun 9, 2021
1 parent c4ae1ac commit 3669193
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { Transform } from 'class-transformer';
import { BBox } from 'geojson';
import { AdminArea } from '@marxan/admin-regions';
import { BboxUtils } from '@marxan-geoprocessing/utils/bbox.utils';
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils';

export class TileSpecification extends TileRequest {
@ApiProperty()
Expand Down Expand Up @@ -52,8 +52,6 @@ export class AdminAreasService {
private readonly adminAreasRepository: Repository<AdminArea>,
@Inject(TileService)
private readonly tileService: TileService,
@Inject(BboxUtils)
private readonly bboxUtils: BboxUtils,
) {}

buildAdminAreaWhereQuery(level: number, filters?: AdminAreasFilters): string {
Expand All @@ -74,7 +72,7 @@ export class AdminAreasService {
whereQuery += ` AND gid_${level-1} = '${filters?.guid}'`;
}
if (filters?.bbox) {
whereQuery += ` AND the_geom && ST_MakeEnvelope(${this.bboxUtils.nominatim2bbox(filters?.bbox)}, 4326)`;
whereQuery += ` AND the_geom && ST_MakeEnvelope(${nominatim2bbox(filters?.bbox)}, 4326)`;
}

return whereQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IsArray, IsNumber, IsString, IsOptional } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import { BBox } from 'geojson';
import { BboxUtils } from '@marxan-geoprocessing/utils/bbox.utils';
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils';

export class TileSpecification extends TileRequest {
@ApiProperty()
Expand Down Expand Up @@ -38,8 +38,6 @@ export class FeatureService {
private readonly featuresRepository: Repository<GeoFeatureGeometry>,
@Inject(TileService)
private readonly tileService: TileService,
@Inject(BboxUtils)
private readonly bboxUtils: BboxUtils,
) {}

/**
Expand All @@ -52,7 +50,7 @@ export class FeatureService {
let whereQuery = `feature_id = '${id}'`;

if (bbox) {
whereQuery += `AND st_intersects(ST_MakeEnvelope(${this.bboxUtils.nominatim2bbox(bbox)}, 4326), the_geom)`;
whereQuery += `AND st_intersects(ST_MakeEnvelope(${nominatim2bbox(bbox)}, 4326), the_geom)`;
}
return whereQuery;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BBox } from 'geojson';
import { Transform } from 'class-transformer';

import { PlanningUnitsGeom } from '@marxan-geoprocessing/modules/planning-units/planning-units.geo.entity';
import { BboxUtils } from '@marxan-geoprocessing/utils/bbox.utils';
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils';

export class tileSpecification extends TileRequest {
@ApiProperty()
Expand Down Expand Up @@ -44,8 +44,6 @@ export class PlanningUnitsService {
private readonly planningUnitsRepository: Repository<PlanningUnitsGeom>,
@Inject(TileService)
private readonly tileService: TileService,
@Inject(BboxUtils)
private readonly bboxUtils: BboxUtils,
) {}

/**
Expand Down Expand Up @@ -116,8 +114,7 @@ export class PlanningUnitsService {
let whereQuery = ``;

if (filters?.bbox) {
this.logger.debug('Im a bbox')
whereQuery =`st_intersects(ST_Transform(ST_MakeEnvelope(${this.bboxUtils.nominatim2bbox(filters.bbox)}, 4326), 3857) ,the_geom)`;
whereQuery =`st_intersects(ST_Transform(ST_MakeEnvelope(${nominatim2bbox(filters.bbox)}, 4326), 3857) ,the_geom)`;
}
return whereQuery;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator'
import { ProtectedArea } from '@marxan-geoprocessing/modules/protected-areas/protected-areas.geo.entity';
import { BBox } from 'geojson';
import { Transform } from 'class-transformer';
import { BboxUtils } from '@marxan-geoprocessing/utils/bbox.utils';
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils';

export class ProtectedAreasFilters {
@IsOptional()
Expand All @@ -31,8 +31,6 @@ export class ProtectedAreasService {
private readonly protectedAreasRepository: Repository<ProtectedArea>,
@Inject(TileService)
private readonly tileService: TileService,
@Inject(BboxUtils)
private readonly bboxUtils: BboxUtils,
) {}

/**
Expand All @@ -44,7 +42,7 @@ export class ProtectedAreasService {
let whereQuery = undefined;
whereQuery = filters?.id ? ` id = '${filters?.id}'` : undefined;
if (filters?.bbox) {
const bboxIntersect =`st_intersects(ST_MakeEnvelope(${this.bboxUtils.nominatim2bbox(filters.bbox)}, 4326), the_geom)`;
const bboxIntersect =`st_intersects(ST_MakeEnvelope(${nominatim2bbox(filters.bbox)}, 4326), the_geom)`;
whereQuery = whereQuery ? `${whereQuery} and ${bboxIntersect}`: bboxIntersect;
}
return whereQuery;
Expand Down
10 changes: 4 additions & 6 deletions api/apps/geoprocessing/src/modules/tile/tile.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// to-do: work on cache later
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { getConnection } from 'typeorm';
import * as zlib from 'zlib';
import { Transform } from 'class-transformer';
Expand Down Expand Up @@ -124,20 +124,18 @@ export class TileService {
`ST_Intersects(ST_Transform(ST_TileEnvelope(:z, :x, :y), ${inputProjection}), ${geometry} )`,
{ z, x, y },
);
this.logger.debug(customQuery)
if (customQuery) {
subQuery.andWhere(customQuery);
}
return subQuery;
}, 'tile');
this.logger.debug(query.getSql())
const result = await query.getRawMany();

if (result) {
return result;
} else {
this.logger.debug(query.getSql());
throw new Error("Property 'mvt' does not exist in res.rows[0]");
this.logger.error(query.getSql());
throw new NotFoundException("Property 'mvt' does not exist in res.rows[0]");
}
}

Expand Down Expand Up @@ -174,7 +172,7 @@ export class TileService {
>[] = await this.fetchTileFromDatabase(tileInput);
// zip data
data = await this.zip(queryResult[0].mvt);
} catch (error) {
} catch (error: any) {
this.logger.error(`Database error: ${error.message}`);
throw new BadRequestException(error.message);
}
Expand Down
13 changes: 6 additions & 7 deletions api/apps/geoprocessing/src/utils/bbox.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { BBox } from 'geojson';
*
* @debt This should be moved to a self-standing
*/
export class BboxUtils {

/**
* conversion operation between bbox [xmin, ymin, xmax, ymax]
* to Nominatim bbox [xmin, xmax, ymin, ymax].
*
*/
public bbox2Nominatim(bbox: BBox): BBox {
return[bbox[0],bbox[2],bbox[1],bbox[3]]
}
public nominatim2bbox(nominatim: BBox): BBox {
return[nominatim[0], nominatim[2], nominatim[1], nominatim[3]]
}
export function bbox2Nominatim(bbox: BBox): BBox {
return[bbox[0],bbox[2],bbox[1],bbox[3]]
}
export function nominatim2bbox(nominatim: BBox): BBox {
return[nominatim[0], nominatim[2], nominatim[1], nominatim[3]]
}

0 comments on commit 3669193

Please sign in to comment.