Newer
Older
import config from 'ui/config';
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
* Returns the sensor things datastream URL used to fetch the datastream. Add the filter for parameter code if not the null string
* @param {String} agencySiteNumberCode - combines the Agency Code with the Site Number for example USGS-01646500
* @param {String} parameterCode - five digit code that uniquely identifies the observed property (parameter)
Bucknell, Mary S.
committed
* @return {String} Sensor Things datastream URL for the site with parameter code.
Bucknell, Mary S.
committed
const getSiteDatastreamURL = function(agencySiteNumberCode, parameterCode) {
const parameterQuery = parameterCode ? `ParameterCode eq '${parameterCode}'` : '';
const queryString = parameterQuery ? `?$filter=properties/${encodeURIComponent(parameterQuery)}` : '';
return `${config.SENSOR_THINGS_ENDPOINT}Things('${agencySiteNumberCode}')/Datastreams${queryString}`;
Bucknell, Mary S.
committed
* Does the work of contacting the Sensor Things API and returning the datastream
* @param {String} agencySiteNumberCode - combines the Agency Code with the Site Number for example USGS-01646500
* @param {String} parameterCode - five digit code that uniquely identifies the observed property
* @return {Promise} resolves to an empty object or one containing threshold information (and other secondary information)
*/
Bucknell, Mary S.
committed
export const fetchSiteDatastream = async function(agencySiteNumberCode, parameterCode) {
const url = getSiteDatastreamURL(agencySiteNumberCode, parameterCode);
try {
const response = await fetch(url, {
method: 'GET'
});
if (response.status === 200) {
return await response.json();
} else {
console.error(`Received bad status, ${response.status} at ${url}`);
return {};
}
} catch (error) {
console.error(`Failed fetch for ${url}`);
return {};
}
};
/**
* Fetch the sites that are within the polygon and return the geojson.
* @param {String} - Well Known Text Polygon string, for example 'Polygon((-92 44, -89 44, -89 43, -92 43, -92 44))'
* @returns {Promise} - resolves to a json object. If the call fails or returns an unexpected status the json object
* will be empty
*/
export const fetchSitesGeoJson = async function(wktPolygon) {
const expandQuery = encodeURIComponent('Locations($select=id,location)');
const locationQuery = encodeURIComponent(`st_within(Location/location, geography'${wktPolygon}')`);
const url =
`${config.SENSOR_THINGS_ENDPOINT}/Things?$expand=${expandQuery}&$filter=${locationQuery}&$resultFormat=GeoJSON&$top=1000`;
Bucknell, Mary S.
committed
try {
const response = await fetch(url, {
method: 'GET'
if (response.status === 200) {
return await response.json();
} else {
console.error(`Received bad status, ${response.status} at ${url}`);
return {};
}
console.error(`Failed fetch for ${url}`);
return {};
}