Skip to content

Commit 88c48c4

Browse files
Added data completeness stats info
1 parent f0aba75 commit 88c48c4

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

ui/app/components/ExportForm.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import area from "@turf/area";
2+
import axios from "axios";
23
import bbox from "@turf/bbox";
34
import React, { Component } from "react";
45
import { Col, Nav, Panel, Row } from "react-bootstrap";
@@ -8,7 +9,6 @@ import { Redirect, Route, Switch } from "react-router";
89
import { NavLink } from "react-router-dom";
910
import { Fields, formValueSelector, reduxForm } from "redux-form";
1011
import { pointToTile } from "tilebelt";
11-
1212
import ChooseFormats from "./ChooseFormats";
1313
import DescribeExport from "./DescribeExport";
1414
import ExportAOIField from "./ExportAOIField";
@@ -128,6 +128,86 @@ export class ExportForm extends Component {
128128
};
129129
}
130130

131+
async fetchData(geometry) {
132+
const url = "https://api-prod.raw-data.hotosm.org/v1/stats/polygon/";
133+
try {
134+
const response = await axios.post(url, {
135+
geometry: geometry
136+
}, {
137+
headers: {"Content-Type": "application/json"}
138+
});
139+
140+
if (response.data) {
141+
142+
this.setState({ fetchedInfo: response.data });
143+
}
144+
} catch (error) {
145+
console.error("Failed to fetch summary data", error);
146+
147+
}
148+
}
149+
150+
componentDidUpdate(prevProps) {
151+
if (this.props.formValues.the_geom !== prevProps.formValues.the_geom) {
152+
this.fetchData(this.props.formValues.the_geom);
153+
}
154+
}
155+
156+
renderFetchedInfo() {
157+
const { fetchedInfo } = this.state;
158+
159+
if (!fetchedInfo) return null;
160+
161+
// Function to trigger the download of the raw data as a JSON file
162+
const downloadRawData = () => {
163+
const filename = "raw_region_summary.json";
164+
const jsonStr = JSON.stringify(fetchedInfo, null, 4);
165+
const element = document.createElement('a');
166+
element.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(jsonStr));
167+
element.setAttribute('download', filename);
168+
element.style.display = 'none';
169+
document.body.appendChild(element);
170+
element.click();
171+
document.body.removeChild(element);
172+
};
173+
174+
return (
175+
<Panel style={{ marginTop: "5px" }}>
176+
<div>
177+
<div>
178+
<strong style={{ fontSize: "smaller" }}>Buildings:</strong>
179+
<p style={{ fontSize: "smaller", textAlign: "justify", margin: "10px 0" }}>
180+
<FormattedMessage
181+
id="export.dc.stats.buildings"
182+
defaultMessage="{buildings}"
183+
values={{ buildings: fetchedInfo.summary.buildings }}
184+
/>
185+
</p>
186+
</div>
187+
<div>
188+
<strong style={{ fontSize: "smaller" }}>Roads:</strong>
189+
<p style={{ fontSize: "smaller", textAlign: "justify", margin: "10px 0" }}>
190+
<FormattedMessage
191+
id="export.dc.stats.roads"
192+
defaultMessage="{roads}"
193+
values={{ roads: fetchedInfo.summary.roads }}
194+
/>
195+
</p>
196+
</div>
197+
<div style={{ fontSize: "smaller", marginTop: "10px" }}>
198+
More info:
199+
<a href="#" onClick={downloadRawData} style={{ marginLeft: "5px" }}>Download</a>,
200+
<a href={fetchedInfo.meta.indicators} target="_blank" rel="noopener noreferrer" style={{ marginLeft: "5px" }}>Indicators</a>,
201+
<a href={fetchedInfo.meta.metrics} target="_blank" rel="noopener noreferrer" style={{ marginLeft: "5px" }}>Metrics</a>
202+
</div>
203+
</div>
204+
</Panel>
205+
);
206+
}
207+
208+
209+
210+
131211
componentWillMount() {
132212
const { getConfigurations, getOverpassTimestamp, getGalaxyTimestamp} = this.props;
133213

@@ -281,6 +361,7 @@ export class ExportForm extends Component {
281361
/>}
282362
/>
283363
</Switch>
364+
{this.renderFetchedInfo()}
284365
<Panel style={{ marginTop: "20px" }}>
285366
<FormattedMessage
286367
id="ui.overpass_last_updated"

0 commit comments

Comments
 (0)