Skip to content
Snippets Groups Projects
Commit f9cd1292 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Check that selected point is covered by forecast source

parent d0aa7f90
Branches
No related tags found
2 merge requests!13Saddlegallmidge form idec 372,!12feat: Add test page (spatial) with mapserver layer in openlayers map
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
/** /**
* Dependencies: * Dependencies:
* momentJS * momentJS
* turf v6
*/ */
const ipmdDSSApiURL = "https://platform.ipmdecisions.net/api/dss/"; const ipmdDSSApiURL = "https://platform.ipmdecisions.net/api/dss/";
...@@ -190,6 +191,26 @@ function getPragmaticWeatherParameterList(requestedParameters, availableParamete ...@@ -190,6 +191,26 @@ function getPragmaticWeatherParameterList(requestedParameters, availableParamete
} }
async function isPointCoveredByDatasource(coordinate, datasource)
{
// If the geoJson is {"type":"Sphere"}, return true
if(datasource.spatial.geoJSON != null && JSON.parse(datasource.spatial.geoJSON).type.toLowerCase() == "sphere")
{
return true;
}
let geoJson = await getDatasourceFeatures(JSON.parse(datasource.spatial.geoJSON), datasource.spatial.countries);
console.info(geoJson);
let retVal = false;
for(let i=0; i<geoJson.features.length;i++)
{
if(turf.booleanPointInPolygon(coordinate, geoJson.features[i]))
{
retVal = true;
}
}
return retVal;
}
/** /**
* Displays the weather data in a table * Displays the weather data in a table
* @param {Object} weatherData the weatherData in IPM Decisions format * @param {Object} weatherData the weatherData in IPM Decisions format
...@@ -439,25 +460,14 @@ async function initDataSourceMap(containerId, geoJson, countryCodeList, featureC ...@@ -439,25 +460,14 @@ async function initDataSourceMap(containerId, geoJson, countryCodeList, featureC
let features = new ol.Collection(); let features = new ol.Collection();
let format = new ol.format.GeoJSON(); let format = new ol.format.GeoJSON();
let drawnFeatures = undefined;
let drawnFeatures = await format.readFeatures(
// If we have geoJson available, we display that await getDatasourceFeatures(geoJson, countryCodeList),
if (geoJson != null && geoJson.features.length > 0) { {
drawnFeatures = format.readFeatures(geoJson, {
dataProjection: 'EPSG:4326',
featureProjection: map.getView().getProjection().getCode()
});
}
// If not, we have to get geoJson mapped to the countries
else if (countryCodeList != undefined && countryCodeList != null && countryCodeList.length > 0) {
let countryBoundaries = await getCountryBoundaries(countryCodeList);
//console.info(countryBoundaries);
drawnFeatures = await format.readFeatures(countryBoundaries, {
dataProjection: 'EPSG:4326', dataProjection: 'EPSG:4326',
featureProjection: map.getView().getProjection().getCode() featureProjection: map.getView().getProjection().getCode()
}); }
} );
let featureOverlay = undefined; let featureOverlay = undefined;
...@@ -513,6 +523,22 @@ async function initDataSourceMap(containerId, geoJson, countryCodeList, featureC ...@@ -513,6 +523,22 @@ async function initDataSourceMap(containerId, geoJson, countryCodeList, featureC
}); });
} }
/**
* Get "complete" spatial info from weather datasource, using either provided GeoJson or
* inferred from list of countries
* @param {JSON} geoJson
* @param {Array} countryCodeList
* @returns {Json} GeoJson
*/
async function getDatasourceFeatures(geoJson, countryCodeList)
{
// If we have geoJson available, we display that
if (geoJson != null && geoJson.features !== undefined && geoJson.features.length > 0) {
return geoJson;
}
return await getCountryBoundaries(countryCodeList);
}
const styleUnselected = new ol.style.Style({ const styleUnselected = new ol.style.Style({
fill: new ol.style.Fill({ fill: new ol.style.Fill({
color: 'rgba(173, 173, 173, 0.5)' color: 'rgba(173, 173, 173, 0.5)'
...@@ -580,6 +606,10 @@ function getDecimalDegrees(map, coordinate) ...@@ -580,6 +606,10 @@ function getDecimalDegrees(map, coordinate)
async function getCountryBoundaries(countryCodeList) async function getCountryBoundaries(countryCodeList)
{ {
if(countryCodeList == undefined || countryCodeList == null)
{
return {};
}
const response = await fetch(ipmdWeatherApiURL + "rest/country/" + countryCodeList.join(",")); const response = await fetch(ipmdWeatherApiURL + "rest/country/" + countryCodeList.join(","));
return await response.json(); return await response.json();
} }
......
...@@ -84,6 +84,7 @@ ...@@ -84,6 +84,7 @@
{% block customJS %} {% block customJS %}
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<script type="text/javascript" src="{% static "js/3rdparty/moment.min.js" %}"></script> <script type="text/javascript" src="{% static "js/3rdparty/moment.min.js" %}"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment"></script> <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment"></script>
<script type="text/javascript" src="{% static "js/3rdparty/ol-debug.js" %}"></script> <script type="text/javascript" src="{% static "js/3rdparty/ol-debug.js" %}"></script>
...@@ -203,7 +204,6 @@ ...@@ -203,7 +204,6 @@
function handleHistoricDatasourceMapClicked(id, coordinate) function handleHistoricDatasourceMapClicked(id, coordinate)
{ {
//console.info("Map clicked, station=" + id +", coordinate=" + coordinate + ". TODO: select station and/or populate lat/lon fields"); //console.info("Map clicked, station=" + id +", coordinate=" + coordinate + ". TODO: select station and/or populate lat/lon fields");
// id != null => station. Pull station coordinates from source list to avoid inacurracies in user's map click coordinate // id != null => station. Pull station coordinates from source list to avoid inacurracies in user's map click coordinate
if(id !== null) if(id !== null)
...@@ -234,7 +234,7 @@ ...@@ -234,7 +234,7 @@
} }
} }
function handleForecastSourceSelected(forecastSourceSelectList) async function handleForecastSourceSelected(forecastSourceSelectList)
{ {
currentForecastWeatherDatasource = getWeatherDatasource( currentForecastWeatherDatasource = getWeatherDatasource(
weatherDatasources, weatherDatasources,
...@@ -248,10 +248,12 @@ ...@@ -248,10 +248,12 @@
return; return;
} }
let geoJson = JSON.parse(currentForecastWeatherDatasource.spatial.geoJSON);
// Display map // Display map
initDataSourceMap( initDataSourceMap(
"forecastDatasourceMap", "forecastDatasourceMap",
JSON.parse(currentForecastWeatherDatasource.spatial.geoJSON), geoJson,
currentForecastWeatherDatasource.spatial.countries, currentForecastWeatherDatasource.spatial.countries,
function(){} // We don't do nothing, right? function(){} // We don't do nothing, right?
); );
...@@ -259,6 +261,10 @@ ...@@ -259,6 +261,10 @@
let sourceInfo = document.getElementById("forecastSourceInfo"); let sourceInfo = document.getElementById("forecastSourceInfo");
sourceInfo.innerHTML = currentForecastWeatherDatasource.description; sourceInfo.innerHTML = currentForecastWeatherDatasource.description;
sourceInfoPanel.style.display="block"; sourceInfoPanel.style.display="block";
// Does the forecast data source cover the point in question?
console.info("In geoJson? " + await isPointCoveredByDatasource(getLatLon(), currentForecastWeatherDatasource));
} }
function handleWeatherStationSelected(weatherStationSelectList) function handleWeatherStationSelected(weatherStationSelectList)
...@@ -275,6 +281,11 @@ ...@@ -275,6 +281,11 @@
document.getElementById("latitude").value = coordinate[1]; document.getElementById("latitude").value = coordinate[1];
} }
function getLatLon()
{
return [document.getElementById("longitude").value, document.getElementById("latitude").value];
}
async function submitData(){ async function submitData(){
//console.info("submitData!"); //console.info("submitData!");
let inputData = editor.getValue(); let inputData = editor.getValue();
...@@ -321,10 +332,11 @@ ...@@ -321,10 +332,11 @@
} }
else else
{ {
coordinate = getLatLon();
weatherData = await getLocationWeatherData( weatherData = await getLocationWeatherData(
getWeatherDatasourceEndpoint(currentWeatherDatasource), getWeatherDatasourceEndpoint(currentWeatherDatasource),
document.getElementById("longitude").value, coordinate[0],
document.getElementById("latitude").value, coordinate[1],
getPragmaticWeatherParameterList( getPragmaticWeatherParameterList(
function (){ function (){
let parameterList = [] let parameterList = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment