Skip to content

Commit

Permalink
Merge pull request #161 from Plant-for-the-Planet-org/feature/expand-…
Browse files Browse the repository at this point in the history
…slice-4and5

New Slice Convention
  • Loading branch information
dhakalaashish authored Mar 29, 2024
2 parents 10608f8 + ee8ee0d commit 453316c
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 204 deletions.
315 changes: 206 additions & 109 deletions apps/server/src/utils/slices.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,219 @@
const slices = {
'1': {
name: 'Slice 1',
'10': {
name: 'Slice 10',
bbox: '-180,-90,180,-30',
coordinates: [
[
[-180, -90],
[180, -90],
[180, -30],
[-180, -30],
[-180, -90],
],
],
description:
'Covers the Southernmost regions of the world including the majority of Antarctica and parts of Southern South America.',
},
'2': {
name: 'Slice 2',
bbox: '-180,-30,180,-15',
coordinates: [
[
[-180, -30],
[180, -30],
[180, -15],
[-180, -15],
[-180, -30],
],
],
description:
'Covers parts of Southern South America, South Africa and Australia.',
},
'3': {
name: 'Slice 3',
bbox: '-180,-15,180,0',
coordinates: [
[
[-180, -15],
[180, -15],
[180, 0],
[-180, 0],
[-180, -15],
],
],
description: 'Covers parts of South America, Africa and Australia.',
},
'4': {
name: 'Slice 4',
bbox: '-180,0,180,15',
coordinates: [
[
[-180, 0],
[180, 0],
[180, 15],
[-180, 15],
[-180, 0],
],
],
description:
'Covers parts of South America, Africa, Asia and northern Australia.',
},
'5': {
name: 'Slice 5',
bbox: '-180,15,180,30',
coordinates: [
[
[-180, 15],
[180, 15],
[180, 30],
[-180, 30],
[-180, 15],
],
],
description:
'Covers parts of United States, Europe, Asia, North Africa and the northern tip of Australia.',
},
'6': {
name: 'Slice 6',
bbox: '-180,30,180,45',
coordinates: [
[
[-180, 30],
[180, 30],
[180, 45],
[-180, 45],
[-180, 30],
],
],
description:
'Covers parts of United States, Europe, Asia and North Africa.',
},
'7': {
name: 'Slice 7',
[-180, -90],
[180, -90],
[180, -30],
[-180, -30],
[-180, -90]
],
description: 'Covers the Southernmost regions of the world including the majority of Antarctica and parts of Southern South America.',
},
'21': {
name: 'Slice 21',
bbox: '-180,-30,-60,-15',
coordinates: [
[-180, -30],
[-60, -30],
[-60, -15],
[-180, -15],
[-180, -30]
],
description: 'Covers left part of mid-lower South America.',
},
'22': {
name: 'Slice 22',
bbox: '-60,-30,30,-15',
coordinates: [
[-60, -30],
[30, -30],
[30, -15],
[-60, -15],
[-60, -30]
],
description: 'Covers right part of mid-lower South America, and left part of Lower Africa.',
},
'23': {
name: 'Slice 23',
bbox: '30,-30,180,-15',
coordinates: [
[30, -30],
[180, -30],
[180, -15],
[30, -15],
[30, -30]
],
description: 'Covers right part of Lower Africa, and upper part of Australia.',
},
'31': {
name: 'Slice 31',
bbox: '-180,-15,-60,0',
coordinates: [
[-180, -15],
[-60, -15],
[-60, 0],
[-180, 0],
[-180, -15]
],
description: 'Covers left part of Mid-upper South America.',
},
'32': {
name: 'Slice 32',
bbox: '-60,-15,30,0',
coordinates: [
[-60, -15],
[30, -15],
[30, 0],
[-60, 0],
[-60, -15]
],
description: 'Covers right part of Mid-upper South America, and left part of lower-mid Africa.',
},
'33': {
name: 'Slice 33',
bbox: '30,-15,180,0',
coordinates: [
[30, -15],
[180, -15],
[180, 0],
[30, 0],
[30, -15]
],
description: 'Covers right part of lower-mid Africa, and Papua New Guinea and parts of Indonesia.',
},
'41': {
name: 'Slice 41',
bbox: '-180,0,-60,15',
coordinates: [
[-180, 0],
[-60, 0],
[-60, 15],
[-180, 15],
[-180, 0]
],
description: 'Covers left part of Upper South America.',
},
'42': {
name: 'Slice 42',
bbox: '-60,0,30,15',
coordinates: [
[-60, 0],
[30, 0],
[30, 15],
[-60, 15],
[-60, 0]
],
description: 'Covers right part of Upper South America and left part of Upper-mid Africa.',
},
'43': {
name: 'Slice 43',
bbox: '30,0,180,15',
coordinates: [
[30, 0],
[180, 0],
[180, 15],
[30, 15],
[30, 0]
],
description: 'Covers right part of Upper-mid Africa, and Lower South Asia.',
},
'51': {
name: 'Slice 51',
bbox: '-180,15,-60,30',
coordinates: [
[-180, 15],
[-60, 15],
[-60, 30],
[-180, 30],
[-180, 15]
],
description: 'Covers Lower North America, containing Mexico and Cuba.',
},
'52': {
name: 'Slice 52',
bbox: '-60,15,30,30',
coordinates: [
[-60, 15],
[30, 15],
[30, 30],
[-60, 30],
[-60, 15]
],
description: 'Covers left part of Upper Africa.',
},
'53': {
name: 'Slice 53',
bbox: '30,15,180,30',
coordinates: [
[30, 15],
[180, 15],
[180, 30],
[30, 30],
[30, 15]
],
description: 'Covers right part of Upper Africa, lower part of Middle East, and Upper South Asia.',
},
'61': {
name: 'Slice 61',
bbox: '-180,30,-60,45',
coordinates: [
[-180, 30],
[-60, 30],
[-60, 45],
[-180, 45],
[-180, 30]
],
description: 'Covers Mid North America, covering most of the United States.',
},
'62': {
name: 'Slice 62',
bbox: '-60,30,30,45',
coordinates: [
[-60, 30],
[30, 30],
[30, 45],
[-60, 45],
[-60, 30]
],
description: 'Covers lower western Europe.',
},
'63': {
name: 'Slice 63',
bbox: '30,30,180,45',
coordinates: [
[30, 30],
[180, 30],
[180, 45],
[30, 45],
[30, 30]
],
description: 'Covers upper part of Middle East, and Middle Asia.',
},
'70': {
name: 'Slice 70',
bbox: '-180,45,180,60',
coordinates: [
[
[-180, 45],
[180, 45],
[180, 60],
[-180, 60],
[-180, 45],
],
[-180, 45],
[180, 45],
[180, 60],
[-180, 60],
[-180, 45]
],
description:
'Covers Northern parts of United States, Europe, Asia, and the majority of Russia.',
description: 'Covers Northern parts of United States, Europe, Asia, and the majority of Russia.',
},
'8': {
name: 'Slice 8',
'80': {
name: 'Slice 80',
bbox: '-180,60,180,90',
coordinates: [
[
[-180, 60],
[180, 60],
[180, 90],
[-180, 90],
[-180, 60],
],
[-180, 60],
[180, 60],
[180, 90],
[-180, 90],
[-180, 60]
],
description:
'Covers the Northernmost regions of the world including Greenland, Arctic Ocean, and Northern Russia.',
description: 'Covers the Northernmost regions of the world including Greenland, Arctic Ocean, and Northern Russia.',
},
};

Expand Down
39 changes: 21 additions & 18 deletions docs/create-postgis-triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,11 @@ BEGIN
END LOOP;

NEW."geometry" = jsonb_set(NEW."geometry", '{properties}', jsonb_build_object('detection_geometry', to_jsonb(detectionGeometryHex)));

-- Calculate detectionGeometry for MultiPolygon as a whole
NEW."detectionGeometry" = ST_Collect(ARRAY(
SELECT ST_GeomFromEWKB(decode(dg_elem, 'hex'))
FROM jsonb_array_elements_text(NEW."geometry"->'detection_geometry') AS dg_elem
));
ELSE
-- Handle Point and Polygon as before
NEW."originalGeometry" = ST_Transform(ST_Transform(ST_GeomFromGeoJSON(NEW."geometry"::text), 3857), 4326);
NEW."detectionGeometry" = ST_Transform(ST_Buffer(ST_Transform(ST_GeomFromGeoJSON(NEW."geometry"::text), 3857), NEW."radius"), 4326);
END IF;

NEW."originalGeometry" = ST_Transform(ST_Transform(ST_GeomFromGeoJSON(NEW."geometry"::text), 3857), 4326);
NEW."detectionGeometry" = ST_Transform(ST_Buffer(ST_Transform(ST_GeomFromGeoJSON(NEW."geometry"::text), 3857), NEW."radius"), 4326);

-- Calculate detection area
NEW."detectionArea" := ST_Area(
ST_Transform(
Expand All @@ -73,14 +66,24 @@ BEGIN
ST_GeomFromText('POLYGON((' || bbox || '))', 4326) AS slice_geometry
FROM (
VALUES
('1', '-180 -90, 180 -90, 180 -30, -180 -30, -180 -90'),
('2', '-180 -30, 180 -30, 180 -15, -180 -15, -180 -30'),
('3', '-180 -15, 180 -15, 180 0, -180 0, -180 -15'),
('4', '-180 0, 180 0, 180 15, -180 15, -180 0'),
('5', '-180 15, 180 15, 180 30, -180 30, -180 15'),
('6', '-180 30, 180 30, 180 45, -180 45, -180 30'),
('7', '-180 45, 180 45, 180 60, -180 60, -180 45'),
('8', '-180 60, 180 60, 180 90, -180 90, -180 60')
('10', '-180 -90, 180 -90, 180 -30, -180 -30, -180 -90'),
('21', '-180 -30, -60 -30, -60 -15, -180 -15, -180 -30'),
('22', '-60 -30, 30 -30, 30 -15, -60 -15, -60 -30'),
('23', '30 -30, 180 -30, 180 -15, 30 -15, 30 -30'),
('31', '-180 -15, -60 -15, -60 0, -180 0, -180 -15'),
('32', '-60 -15, 30 -15, 30 0, -60 0, -60 -15'),
('33', '30 -15, 180 -15, 180 0, 30 0, 30 -15'),
('41', '-180 0, -60 0, -60 15, -180 15, -180 0'),
('42', '-60 0, 30 0, 30 15, -60 15, -60 0'),
('43', '30 0, 180 0, 180 15, 30 15, 30 0'),
('51', '-180 15, -60 15, -60 30, -180 30, -180 15'),
('52', '-60 15, 30 15, 30 30, -60 30, -60 15'),
('53', '30 15, 180 15, 180 30, 30 30, 30 15'),
('61', '-180 30, -60 30, -60 45, -180 45, -180 30'),
('62', '-60 30, 30 30, 30 45, -60 45, -60 30'),
('63', '30 30, 180 30, 180 45, 30 45, 30 30'),
('70', '-180 45, 180 45, 180 60, -180 60, -180 45'),
('80', '-180 60, 180 60, 180 90, -180 90, -180 60')
) AS slices(slice_key, bbox)
)
SELECT INTO sliceKeys ARRAY_AGG(slice_key)
Expand Down
Loading

0 comments on commit 453316c

Please sign in to comment.