-
Notifications
You must be signed in to change notification settings - Fork 5
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
Antimeridian and bbox calc fix #1115
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6384b8
feat: set proper errors when a job fails (#1108)
aciddaute cc3d2fb
Fix Bbox and grid creation to ensure antimeridian is taken into account
aagm 45f2009
fix tile selection for Protected areas based on bbox
aagm a0a8afe
fixed bbox filtering based on antimeridian fix
aagm 4e9f7b4
format and moved bbox utils from geoprocessing to libs
aagm 2e5b37c
changed how bbox functions are imported and exported in the lib
aagm 20610a6
fixed filter
aagm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,8 @@ VALUES | |
('[email protected]', 'b', 'b', 'User B B', true, false, crypt('bbuserpassword', gen_salt('bf'))), | ||
('[email protected]', 'c', 'c', 'User C C', true, false, crypt('ccuserpassword', gen_salt('bf'))), | ||
('[email protected]', 'd', 'd', 'User D D', true, false, crypt('dduserpassword', gen_salt('bf'))); | ||
|
||
INSERT INTO organizations (name, created_by) | ||
VALUES | ||
('Example Org 1', (SELECT id FROM users WHERE email = '[email protected]')), | ||
('Example Org 2', (SELECT id FROM users WHERE email = '[email protected]')); |
72 changes: 72 additions & 0 deletions
72
...ing/src/migrations/geoprocessing/1653565019335-ModifyBboxCalculationTakingAntimeridian.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ModifyBboxCalculationTakingAntimeridian1653565019335 | ||
implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE OR REPLACE FUNCTION mx_bbox2json(geom geometry) | ||
aagm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
returns jsonb | ||
language plpgsql | ||
as | ||
$function$ | ||
DECLARE | ||
-- variable declaration | ||
both_hemispheres RECORD; | ||
BEGIN | ||
-- logic https://github.com/mapbox/carmen/blob/03fac2d7397ecdfcb4f0828fcfd9d8a54c845f21/lib/util/bbox.js#L59 | ||
-- json form of the bbox should be in Nominatim bbox [xmin, xmax, ymin, ymax] [W, E, S, N]. | ||
execute 'with region as (select st_intersection($1, geom) as the_geom, | ||
st_intersects($1, geom) intersects, pos | ||
from (values (ST_MakeEnvelope(-180, -90, 0, 90, 4326), ''west''), | ||
(ST_MakeEnvelope(0, -90, 180, 90, 4326), ''east'')) as t(geom, pos)), | ||
data as (select ST_XMax(the_geom), ST_XMin(the_geom), | ||
ST_YMax(the_geom),ST_YMin(the_geom), pos, intersects, | ||
ST_XMax(the_geom) + ABS(lag(ST_XMin(the_geom), 1) OVER ()) > | ||
(180 - ST_XMin(the_geom)) + (180 - ABS(lag(ST_XMax(the_geom), 1) OVER ())) as pm_am | ||
from region) | ||
select bool_and(intersects) and bool_and(pm_am) result, | ||
jsonb_build_array(max(st_xmax), min(st_xmin), max(st_ymax), min(st_ymin)) if_false, | ||
jsonb_build_array(min(st_xmax), max(st_xmin), max(st_ymax), min(st_ymin))if_true from data;' | ||
into both_hemispheres | ||
using geom; | ||
if both_hemispheres.result then | ||
return both_hemispheres.if_true; | ||
else | ||
return both_hemispheres.if_false; | ||
end if; | ||
end; | ||
$function$; | ||
|
||
CREATE OR REPLACE FUNCTION public.tr_getbbox() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
NEW.bbox := mx_bbox2json(NEW.the_geom); | ||
RETURN NEW; | ||
END; | ||
$function$; | ||
|
||
UPDATE admin_regions SET id = id; | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE OR REPLACE FUNCTION public.tr_getbbox() | ||
RETURNS trigger | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
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; | ||
$function$; | ||
|
||
Drop FUNCTION mx_bbox2json(geom geometry); | ||
|
||
UPDATE admin_regions SET id = id; | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import { IsArray, IsNumber, IsString, IsOptional } from 'class-validator'; | |
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { BBox } from 'geojson'; | ||
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils'; | ||
import { antimeridianBbox, nominatim2bbox } from '@marxan/utils/geo'; | ||
|
||
import { TileRequest } from '@marxan/tiles'; | ||
|
||
|
@@ -49,23 +49,32 @@ export class FeatureService { | |
let whereQuery = `feature_id = '${id}'`; | ||
|
||
if (bbox) { | ||
whereQuery += `AND st_intersects(ST_MakeEnvelope(${nominatim2bbox( | ||
bbox, | ||
)}, 4326), the_geom)`; | ||
const { westBbox, eastBbox } = antimeridianBbox(nominatim2bbox(bbox)); | ||
whereQuery += `AND | ||
(st_intersects( | ||
st_intersection(st_makeenvelope(${eastBbox}, 4326), | ||
ST_MakeEnvelope(0, -90, 180, 90, 4326)), | ||
the_geom | ||
) or st_intersects( | ||
st_intersection(st_makeenvelope(${westBbox}, 4326), | ||
ST_MakeEnvelope(-180, -90, 0, 90, 4326)), | ||
the_geom | ||
))`; | ||
} | ||
return whereQuery; | ||
} | ||
|
||
/** | ||
* @todo get attributes from Entity, based on user selection | ||
* @todo simplification level based on zoom level | ||
*/ | ||
public findTile( | ||
tileSpecification: TileSpecification, | ||
bbox?: BBox, | ||
): Promise<Buffer> { | ||
const { z, x, y, id } = tileSpecification; | ||
const attributes = 'feature_id, properties'; | ||
const table = `(select (st_dump(the_geom)).geom as the_geom, properties, feature_id from "${this.featuresRepository.metadata.tableName}")`; | ||
const table = `(select ST_RemoveRepeatedPoints((st_dump(the_geom)).geom, 0.1) as the_geom, properties, feature_id from "${this.featuresRepository.metadata.tableName}")`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 |
||
const customQuery = this.buildFeaturesWhereQuery(id, bbox); | ||
return this.tileService.getTile({ | ||
z, | ||
|
@@ -76,4 +85,5 @@ export class FeatureService { | |
attributes, | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to do anything in this PR about this, from my point of view, but these could be good candidates for a named constant that allows to avoid "magic numbers" that may take a little while for the inexperienced reader to figure out when they look at this code out of context (for example, not knowing that it was introduced while splitting semihemispheres)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes in the future we will need to revisit some of this, as well as at some point pay the debt in regarding sql formulation