1
1
import area from "@turf/area" ;
2
+ import axios from "axios" ;
2
3
import bbox from "@turf/bbox" ;
3
4
import React , { Component } from "react" ;
4
5
import { Col , Nav , Panel , Row } from "react-bootstrap" ;
@@ -8,7 +9,6 @@ import { Redirect, Route, Switch } from "react-router";
8
9
import { NavLink } from "react-router-dom" ;
9
10
import { Fields , formValueSelector , reduxForm } from "redux-form" ;
10
11
import { pointToTile } from "tilebelt" ;
11
-
12
12
import ChooseFormats from "./ChooseFormats" ;
13
13
import DescribeExport from "./DescribeExport" ;
14
14
import ExportAOIField from "./ExportAOIField" ;
@@ -128,6 +128,86 @@ export class ExportForm extends Component {
128
128
} ;
129
129
}
130
130
131
+ async fetchData ( geometry ) {
132
+ const url = window . RAW_DATA_API_URL + "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
+ if ( ! this . props . formValues . the_geom ) return null ;
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 : "10px" } } >
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
+
131
211
componentWillMount ( ) {
132
212
const { getConfigurations, getOverpassTimestamp, getGalaxyTimestamp} = this . props ;
133
213
@@ -281,10 +361,11 @@ export class ExportForm extends Component {
281
361
/> }
282
362
/>
283
363
</ Switch >
364
+ { this . renderFetchedInfo ( ) }
284
365
< Panel style = { { marginTop : "20px" } } >
285
366
< FormattedMessage
286
367
id = "ui.overpass_last_updated"
287
- defaultMessage = "Img/pbf/obf/ updated {overpassLastUpdated}, Rest of other formats updated {galaxyLastUpdated} "
368
+ defaultMessage = "Img/pbf/obf updated {overpassLastUpdated}, Rest of other formats updated {galaxyLastUpdated} "
288
369
values = { { overpassLastUpdated, galaxyLastUpdated } }
289
370
/>
290
371
</ Panel >
0 commit comments