forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeostore.js
94 lines (85 loc) · 2.99 KB
/
geostore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import WRISerializer from 'wri-json-api-serializer';
// utils
import { WRIAPI } from 'utils/axios';
import { logger } from 'utils/logs';
/**
* Fetches Geostore
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#obtain-a-geostore|here}
* @param {String} id - geostore ID.
* @returns {Object} serialized geostore object.
*/
export const fetchGeostore = (id) => {
logger.info(`Fetch geostore ${id}`);
return WRIAPI.get(`geostore/${id}`)
.then((response) => {
const { status, statusText, data } = response;
if (status >= 300) {
logger.error('Error fetching geostore:', `${status}: ${statusText}`);
throw new Error(statusText);
}
return WRISerializer(data);
})
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error fetching geostore ${id}: ${status}: ${statusText}`);
throw new Error(`Error fetching geostore ${id}: ${status}: ${statusText}`);
});
};
/**
* Create a Geostore
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#create-geostore|here}
* @param {Object} geojson Geojson with your geometry
*/
export const createGeostore = (geojson) => {
logger.info('Create geostore');
return WRIAPI.post('geostore', geojson)
.then(response => response.data.data)
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error creating geostore: ${status}: ${statusText}`);
throw new Error(`Error creating geostore: ${status}: ${statusText}`);
});
};
/**
* Fetch countries
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#geostore|here}
* @returns {Object[]}
*/
export const fetchCountries = () => {
logger.info('Fetch countries');
return WRIAPI.get('geostore/admin/list')
.then(array =>
array.data.data.sort((a, b) => {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
// eslint-disable-line no-else-return
return 0;
}))
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching countries: ${status}: ${statusText}`);
throw new Error(`Error fetching countries: ${status}: ${statusText}`);
});
};
/**
* Get country
* @param {String} iso
*/
export const fetchCountry = (iso) => {
logger.info(`Fetch country: ${iso}`);
return WRIAPI.get(`query/134caa0a-21f7-451d-a7fe-30db31a424aa?sql=SELECT name_engli as label, st_asgeojson(the_geom_simple) as geojson, bbox as bounds from gadm28_countries WHERE iso = '${iso}'`)
.catch((response) => {
const { status, statusText } = response;
logger.error(`Error fetching country ${iso}: ${status}: ${statusText}`);
throw new Error(`Error fetching country ${iso}: ${status}: ${statusText}`);
});
};
export default {
createGeostore,
fetchGeostore,
fetchCountries,
fetchCountry
};