Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
#3 retrieve the request parameters from the data package
Browse files Browse the repository at this point in the history
  • Loading branch information
therter committed Apr 24, 2019
1 parent 3483157 commit 2801d87
Show file tree
Hide file tree
Showing 8 changed files with 5,092 additions and 4,796 deletions.
52 changes: 5 additions & 47 deletions dist/gen/js/src/components/AdaptationOptionsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,9 @@ export default class AdaptationOptionsTable extends React.Component {
fetch(data.data[0].relationships.field_data_package.links.related.href.replace('http://', obj.protocol), { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
if (data.data.relationships.field_resources.links.related != null) {
var includes = 'include=field_analysis_context.field_field_eu_gl_methodology';
var separator = data.data.relationships.field_resources.links.related.href.indexOf('?') === -1 ? '?' : '&';

fetch(data.data.relationships.field_resources.links.related.href.replace('http://', obj.protocol) + '?' + includes, { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
fetch(data.data.relationships.field_resources.links.related.href.replace('http://', obj.protocol) + separator + includes, { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
obj.convertDataFromServer(data, 'eu-gl:adaptation-options:identification');
}).catch(function (error) {
console.log(JSON.stringify(error));
Expand All @@ -781,11 +782,11 @@ export default class AdaptationOptionsTable extends React.Component {
const resource = resourceArray[i];

if (resource.relationships.field_analysis_context != null && resource.relationships.field_analysis_context.data != null) {
var analysisContext = this.getInculdedObject(resource.relationships.field_analysis_context.data.type, resource.relationships.field_analysis_context.data.id, originData.included);
var analysisContext = this.getIncludedObject(resource.relationships.field_analysis_context.data.type, resource.relationships.field_analysis_context.data.id, originData.included);

if (analysisContext != null) {
if (analysisContext.relationships.field_field_eu_gl_methodology != null && analysisContext.relationships.field_field_eu_gl_methodology.data != null) {
var mythodologyData = this.getInculdedObject(analysisContext.relationships.field_field_eu_gl_methodology.data[0].type, analysisContext.relationships.field_field_eu_gl_methodology.data[0].id, originData.included);
var mythodologyData = this.getIncludedObject(analysisContext.relationships.field_field_eu_gl_methodology.data[0].type, analysisContext.relationships.field_field_eu_gl_methodology.data[0].id, originData.included);
console.log(mythodologyData.attributes.field_eu_gl_taxonomy_id.value);

if (mythodologyData.attributes.field_eu_gl_taxonomy_id.value == mapType) {
Expand All @@ -806,7 +807,7 @@ export default class AdaptationOptionsTable extends React.Component {
}
}

getInculdedObject(type, id, includedArray) {
getIncludedObject(type, id, includedArray) {
if (type != null && id != null) {
for (let i = 0; i < includedArray.length; ++i) {
if (includedArray[i].type === type && includedArray[i].id === id) {
Expand All @@ -818,49 +819,6 @@ export default class AdaptationOptionsTable extends React.Component {
return null;
}

// convertDataFromServer(originData, mapType) {
// var resourceArray = originData.data;
// const thisObj = this;

// for (var i = 0; i < resourceArray.length; ++i) {
// const resource = resourceArray[i];

// fetch(resource.relationships.field_analysis_context.links.related.href.replace('http://', thisObj.protocol), {credentials: 'include'})
// .then((resp) => resp.json())
// .then(function(data) {
// if (data.data.relationships.field_field_eu_gl_methodology.links.related.href != null) {
// fetch(data.data.relationships.field_field_eu_gl_methodology.links.related.href.replace('http://', thisObj.protocol), {credentials: 'include'})
// .then((resp) => resp.json())
// .then(function(data) {
// console.log(data.data[0].attributes.field_eu_gl_taxonomy_id.value);
// if (data.data[0].attributes.field_eu_gl_taxonomy_id.value === mapType) {
// if (resource.attributes.field_url != null) {
// fetch(resource.attributes.field_url)
// .then((resp) => resp.json())
// .then(function(data) {
// thisObj.setState({
// data: thisObj.convertData(data),
// loading: false
// });
// })
// .catch(function(error) {
// console.log(JSON.stringify(error));
// });

// }
// }
// })
// .catch(function(error) {
// console.log(JSON.stringify(error));
// });
// }
// })
// .catch(function(error) {
// console.log(JSON.stringify(error));
// });
// }
// }

removeNameSpace(val) {
if (val != null) {
if (val.lastIndexOf(":") !== -1) {
Expand Down
5 changes: 0 additions & 5 deletions dist/gen/js/src/components/CharacteriseHazardTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import turf from 'turf';
import Wkt from 'wicket';
import proj4 from 'proj4';

//const CharacteriseHazardTable = () => {
// return (<img width={1058} height={578} src='../../../../../../modules/custom/map-component/src/img/01-HC-03-Table.png' />);
//};


export default class CharacteriseHazardTable extends React.Component {
constructor(props) {
super(props);
Expand Down
171 changes: 156 additions & 15 deletions dist/gen/js/src/components/ExposureTable.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from "react";
import ReactDOM from 'react-dom';
import TableComponent from './commons/TableComponent';

//const ExposureTable = () => {
// return (<img width={1058} height={578} src='../../../../../../modules/custom/map-component/src/img/03-EE-03-table.png' />);
//};

import turf from 'turf';
import Wkt from 'wicket';
import proj4 from 'proj4';

export default class ExposureTable extends React.Component {
constructor(props) {
super(props);
this.protocol = "https://";
this.layerParams = ['COVERAGEID', 'layers'];
this.request = {
"type": "eu-gl:exposure-evaluation",
"epsg": "EPSG:3035",
Expand Down Expand Up @@ -167,11 +167,11 @@ export default class ExposureTable extends React.Component {
});
}

initServer(server) {
this.loadDataFromServer(server, this.request);
initServer(server, id) {
this.loadDataFromServer(server, id, this.request);
}

loadDataFromServer(server, requestData) {
sendRequestToRestApi(requestData) {
const obj = this;

fetch("https://clarity.meteogrid.com/api/request_exposure", { method: 'POST', body: JSON.stringify(requestData), headers: { 'Content-Type': 'application/json' } }).then(resp => resp.json()).then(function (data) {
Expand All @@ -189,9 +189,154 @@ export default class ExposureTable extends React.Component {
});
}

loadDataFromServer(server, id, request) {
const obj = this;

fetch(server + '/jsonapi/group/study?filter[id][condition][path]=id&filter[id][condition][operator]=%3D&filter[id][condition][value]=' + id, { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
var wktVar = new Wkt.Wkt();
if (data.data[0].attributes.field_area != null && data.data[0].attributes.field_area.value != null) {
wktVar.read(data.data[0].attributes.field_area.value);
var studyAreaBbox = obj.getBoundsFromArea(wktVar.toJson());
request.bbox = [studyAreaBbox[0][0], studyAreaBbox[0][1], studyAreaBbox[1][0], studyAreaBbox[1][1]];
}
if (data != null && data.data[0] != null && data.data[0].relationships.field_data_package.links.related != null) {
fetch(data.data[0].relationships.field_data_package.links.related.href.replace('http://', obj.protocol), { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
if (data.data.relationships.field_resources.links.related != null) {
var includes = 'include=field_analysis_context.field_field_eu_gl_methodology,field_analysis_context.field_hazard,field_analysis_context.field_exposure_category,field_analysis_context.field_vulnerability_classes';
var separator = data.data.relationships.field_resources.links.related.href.indexOf('?') === -1 ? '?' : '&';

fetch(data.data.relationships.field_resources.links.related.href.replace('http://', obj.protocol) + separator + includes, { credentials: 'include' }).then(resp => resp.json()).then(function (data) {
obj.convertDataFromServer(data, 'eu-gl:exposure-evaluation');
}).catch(function (error) {
console.log(JSON.stringify(error));
});
}
}).catch(function (error) {
console.log(JSON.stringify(error));
});
}
}).catch(function (error) {
console.log(JSON.stringify(error));
});
}

getBoundsFromArea(area) {
const bboxArray = turf.bbox(area);
const targetProj = 'PROJCS["ETRS89 / LAEA Europe", GEOGCS["ETRS89", DATUM["European_Terrestrial_Reference_System_1989", SPHEROID["GRS 1980",6378137,298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0,0,0,0,0,0,0], AUTHORITY["EPSG","6258"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4258"]], PROJECTION["Lambert_Azimuthal_Equal_Area"], PARAMETER["latitude_of_center",52], PARAMETER["longitude_of_center",10], PARAMETER["false_easting",4321000], PARAMETER["false_northing",3210000], UNIT["metre",1, AUTHORITY["EPSG","9001"]], AUTHORITY["EPSG","3035"]]';
const corner1 = proj4(targetProj, [bboxArray[0], bboxArray[1]]);
const corner2 = proj4(targetProj, [bboxArray[2], bboxArray[3]]);
var bounds = [corner1, corner2];

return bounds;
}

convertDataFromServer(originData, mapType) {
var resourceArray = originData.data;
const thisObj = this;
var dataFromDP = [];

for (var i = 0; i < resourceArray.length; ++i) {
const resource = resourceArray[i];

if (resource.relationships.field_analysis_context != null && resource.relationships.field_analysis_context.data != null) {
//analysisContext = eu_fg type
var analysisContext = this.getIncludedObject(resource.relationships.field_analysis_context.data.type, resource.relationships.field_analysis_context.data.id, originData.included);

if (analysisContext != null) {
if (analysisContext.relationships.field_field_eu_gl_methodology != null && analysisContext.relationships.field_field_eu_gl_methodology.data != null) {
var mythodologyData = this.getIncludedObject(analysisContext.relationships.field_field_eu_gl_methodology.data[0].type, analysisContext.relationships.field_field_eu_gl_methodology.data[0].id, originData.included);
console.log(mythodologyData.attributes.field_eu_gl_taxonomy_id.value);

if (mythodologyData.attributes.field_eu_gl_taxonomy_id.value == mapType) {
if (resource.attributes.field_url != null) {
let obj = {};

//get hazards
if (analysisContext.relationships.field_hazard != null && analysisContext.relationships.field_hazard.data != null && analysisContext.relationships.field_hazard.data.length > 0) {
obj.hazard = "";
for (let i = 0; i < analysisContext.relationships.field_hazard.data.length; ++i) {
var hazard = this.getIncludedObject(analysisContext.relationships.field_hazard.data[i].type, analysisContext.relationships.field_hazard.data[i].id, originData.included);
if (hazard != null) {
if (obj.hazard === "") {
obj.hazard = hazard.attributes.name;
} else {
obj.hazard = obj.hazard + ', ' + hazard.attributes.name;
}
}
}
}

//get elements at risk
if (analysisContext.relationships.field_exposure_category != null && analysisContext.relationships.field_exposure_category.data != null && analysisContext.relationships.field_exposure_category.data.length > 0) {
obj.elementAtRisk = "";
for (let i = 0; i < analysisContext.relationships.field_exposure_category.data.length; ++i) {
var elAtRisk = this.getIncludedObject(analysisContext.relationships.field_exposure_category.data[i].type, analysisContext.relationships.field_exposure_category.data[i].id, originData.included);
if (elAtRisk != null) {
if (obj.elementAtRisk === "") {
obj.elementAtRisk = elAtRisk.attributes.name;
} else {
obj.elementAtRisk = obj.elementAtRisk + ', ' + elAtRisk.attributes.name;
}
}
}
}

//get vulnerability classes
if (analysisContext.relationships.field_vulnerability_classes != null && analysisContext.relationships.field_vulnerability_classes.data != null && analysisContext.relationships.field_vulnerability_classes.data.length > 0) {
obj.vulnerabilityClasses = "";
for (let i = 0; i < analysisContext.relationships.field_vulnerability_classes.data.length; ++i) {
var vulClasses = this.getIncludedObject(analysisContext.relationships.field_vulnerability_classes.data[i].type, analysisContext.relationships.field_vulnerability_classes.data[i].id, originData.included);
if (vulClasses != null) {
if (obj.vulnerabilityClasses === "") {
obj.vulnerabilityClasses = vulClasses.attributes.name;
} else {
obj.vulnerabilityClasses = obj.vulnerabilityClasses + ', ' + vulClasses.attributes.name;
}
}
}
}

obj.layer = this.extractLayers(resource.attributes.field_url);
obj.unit = 'pop//km2';
dataFromDP.push(obj);
}
}
}
}
}
}

this.request.data = dataFromDP;
this.sendRequestToRestApi(this.request);
}

getIncludedObject(type, id, includedArray) {
if (type != null && id != null) {
for (let i = 0; i < includedArray.length; ++i) {
if (includedArray[i].type === type && includedArray[i].id === id) {
return includedArray[i];
}
}
}

return null;
}

extractLayers(url) {
for (let i = 0; i < this.layerParams.length; ++i) {
let paramKey = this.layerParams[i];

if (url.indexOf(paramKey + '=') != -1) {
var layerParam = url.substring(url.indexOf(paramKey + '=') + paramKey.length + 1);
return layerParam.indexOf('&') !== -1 ? layerParam.substring(0, layerParam.indexOf('&')) : layerParam;
}
}

return url;
}

render() {
window.tableCom = this;
// const header = "The following table and the associated chart show the development of different categories' elements for several scenarios. There are always 3 scenarios considered: 1) the current today's rate development, 2) low rate development and 3) high rate development for the selected time period. The values will be used in assessing the vulnarability, risk and impact in the next steps.";
return React.createElement(
'div',
null,
Expand All @@ -210,19 +355,15 @@ export default class ExposureTable extends React.Component {
React.createElement(TableComponent, {
data: this.state.data,
columns: this.state.columns,
loading: this.state.loading
// header={header}
, pivotBy: ["Hazards", "ElementAtRisk"]
// expanded={["Hazards", "ElementAtRisk"]}
loading: this.state.loading,
pivotBy: ["Hazards", "ElementAtRisk"]
})
)
);
}

}

//export default ExposureTable;

if (document.getElementById('exposure-table-container') != null) {
ReactDOM.render(React.createElement(ExposureTable, null), document.getElementById('exposure-table-container'));
document.getElementById('exposure-table-container').style.width = "100%";
Expand Down
Loading

0 comments on commit 2801d87

Please sign in to comment.