-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dashboard.js
469 lines (434 loc) · 13.8 KB
/
Dashboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import React, { createRef } from "react";
import {
Map,
TileLayer,
FeatureGroup,
Polygon,
GeoJSON,
Marker,
Popup,
} from "react-leaflet";
import Metrics from "./Metrics";
import "leaflet/dist/leaflet.css";
import { EditControl } from "react-leaflet-draw";
import london_postcodes from "../Files/london_postcodes.json";
import geojson from "../Files/geojson.json";
import points from "../Files/points.json";
import "../../leaflet.filelayer";
import { connect } from "react-redux";
import { saveData } from "../../store/actions/authActions";
import { saveAs } from "file-saver";
import "../dashboard/GeojsonLayer.css";
import ReactLeafletSearch from "react-leaflet-search";
import "./Map.css";
import L from "leaflet";
import { storage } from "../../firebase/index";
import { format } from "date-fns";
import LocateControl from "./LocateControl";
import FullscreenControl from "react-leaflet-fullscreen";
import "react-leaflet-fullscreen/dist/styles.css";
import { popupContent, popupHead } from "./popupStyles";
import { Typography } from "@material-ui/core";
import { withTranslation } from "react-i18next";
import { NavLink } from "react-router-dom";
import LockIcon from "@material-ui/icons/Lock";
import AllLayers from "./AllLayers";
import "./filelayer.js";
import LocationOnIcon from "@material-ui/icons/LocationOn";
// import CustomWMSLayer from "./CustomWMSLayer";
import styles from "./Dashboard.module.css";
// import Legend from "./Legend";
import KmlUpload from "./KmlUpload";
import "../dashboard/styles.css";
L.Icon.Default.imagePath = "https://unpkg.com/leaflet@1.5.0/dist/images/";
//Hardcoded coordinates of polygons
const polygon = [
[42.696295, 23.303643],
[42.699295, 23.303643],
[42.699295, 23.313643],
[42.679295, 23.313643],
];
export class Dashboard extends React.Component {
constructor(props) {
super(props);
this.state = {
downloadURLs: [],
area: null,
coordinates: [],
coordinatesCenter: [42.696295, 23.303643],
zoom: 10,
showMarkers: false,
showPolygons: false,
geojsonvisible: false,
viewToolbar: false,
keyMAP: Math.random(),
kmlNameDescription: "",
};
}
//Set location when the map is visualized
mapRef = createRef();
handleClick = () => {
const map = this.mapRef.current;
if (map != null) {
map.leafletElement.locate();
}
console.log("CatchMouseClick", Polygon.latlng);
};
onCreate = (e) => {
var layer = e.layer;
console.log("Polygon Coordinates", layer.getLatLngs());
console.log("Log_Create_Shape: ", e);
//get coordinates to geojson format
var drawedCord = layer.toGeoJSON().geometry.coordinates;
console.log("shape1", drawedCord);
//Create kml file
var data = layer.toGeoJSON();
var tokml = require("tokml");
var kml = tokml(data);
console.log("KMLLLLLLLL", kml);
this.setState({
kmlNameDescription: tokml(data, {
name: "name",
description: "description",
}),
});
var convertedData = JSON.stringify(data);
console.log(convertedData);
// var FileSaver = require("file-saver");
var blob = new Blob([this.state.kmlNameDescription], {
type: "text/plain;charset=utf-8",
});
// FileSaver.saveAs(blob, "coordinates.kml");
//Save kml file with formated filename
var date = new Date();
var formattedDate = format(date, "dd-mm-yyyy_h:mma");
console.log(formattedDate);
const filename = formattedDate + "_" + this.props.profile.firstName;
console.log(filename);
storage.ref(`KmlFiles/${filename}.kml`).put(blob);
//Save arean and coordinates
var Area = L.GeometryUtil.geodesicArea(layer.getLatLngs()[0]);
var areaFormatted = L.GeometryUtil.formattedNumber(Area * 0.001) + " m²";
console.log(areaFormatted);
this.setState({
area: areaFormatted,
});
this.props.areaHandler(areaFormatted);
for (const result of drawedCord) this.state.coordinates.push(result);
console.log(this.state.coordinates[0]);
this.props.coordinatesHandler(this.state.coordinates);
};
saveToFile = (e) => {
var FileSaver = require("file-saver");
const { kmlNameDescription } = this.state;
var blob = new Blob([kmlNameDescription], {
type: "text/plain;charset=utf-8",
});
FileSaver.saveAs(blob, "coordinates.kml");
};
//for future upgrade
saveToFile1(content, filename) {
var file = filename + ".geojson";
saveAs(
new File([JSON.stringify(content)], file, {
type: "text/plain;charset=utf-8",
}),
file
);
}
onGeojsonPolygons = (e) => {
this.setState({
showPolygons: e.currentTarget.checked,
});
};
onGeojsonMarkers = (e) => {
this.setState({
showMarkers: e.currentTarget.checked,
});
};
onGeojsonToggleButton = (e) => {
this.setState({
geojsonvisible: e.currentTarget.click,
});
};
// Function to send data to Blitline
sendHttpPostToBlitline(job_data) {
var http = require("http");
var options = {
host: "api.blitline.com",
port: 80,
method: "POST",
path: "/job",
};
var req = http
.request(options, function (res) {
res.on("data", function (chunk) {
console.log("Data=" + chunk);
});
})
.on("error", function (e) {
console.log("Got error: " + e.message);
});
req.write("json=" + JSON.stringify(job_data));
req.end();
}
processHardcodedImageUrl() {
var job_data = {
application_id: "8k9BXHJtK_sKYGltk_Fiobw",
src:
"https://cdn3.iconfinder.com/data/icons/basicolor-arrows-checks/24/149_check_ok-512.png",
functions: [
{
name: "sketch",
save: { image_identifier: "MY_CLIENT_ID" },
},
],
};
this.sendHttpPostToBlitline(job_data);
}
componentDidMount() {}
//same for polygon
onEachFeaturePoint(feature, layer) {
console.log("feature: ", feature);
console.log("layer: ", layer);
var popupContent =
feature.properties.Name + " " + feature.properties.Crops;
console.log(feature.properties.coordinates);
this.setState({
coordinatesCenter: feature.properties.coordinates,
});
layer.bindPopup(popupContent);
layer.on({
click: function (e) {
console.log("e: ", e);
console.log("click");
},
});
}
pointToLayer(feature, latlng) {
return L.circleMarker(latlng, null); // Change marker to circle
// return L.marker(latlng, { icon: {}}); // Change the icon to a custom icon
}
onButtonClick = (coords) => {
// const map = this.mapRef.current;
// var group = new L.featureGroup([L.marker(coords)]);
// if (map) map.leafletElement.fitBounds(group.getBounds());
this.setState({
coordinatesCenter: coords,
});
};
resetMap = (e) => {
this.setState({ keyMAP: Math.random(), area: null });
var areaReset = null;
this.props.areaHandler(areaReset);
};
moveTo = () => {
this.setState({ coordinatesCenter: [42.5035355, 24.1737867] });
};
render() {
//Here we shorten the area and the coordinated definition
const { viewMap } = this.props;
//
const locateOptions = {
position: "bottomleft",
strings: {
title: "Your location ",
},
onActivate: () => {}, // callback before engine starts retrieving locations
};
const position = this.state.coordinatesCenter;
const { profile } = this.props;
//This is for the translation
const { t } = this.props;
if (profile.role === "User" || profile.role === "Admin") {
return (
<div id="map" className="dashboard container">
<br />
<br />
<hr />
<Map
key={this.state.keyMAP}
style={{ height: "65vh" }}
center={position}
zoom={13}
onClick={this.handleClick}
onCreate={this.onCreate}
onLocationfound={this.handleLocationFound}
useFlyTo={false}
>
{/* startDirectly */}
<FullscreenControl position="topleft" />
<LocateControl options={locateOptions}>
<span className="fa fa-map-marker"></span>
</LocateControl>
<AllLayers />
<Metrics />
<ReactLeafletSearch />
<div className="geojson-toggle">
<label>Show Polygons </label>
<inputpointer
style={{ opacity: 1, Events: "auto" }}
type="checkbox"
name="layertoggle"
id="layertoggle"
value={this.state.showPolygons}
onChange={this.onGeojsonPolygons}
/>
</div>
{this.state.showPolygons && (
<GeoJSON
data={geojson}
style={this.geoJSONStyle}
value={this.state.showPolygons}
/>
)}
<div className="geojson-toggle1">
<label>Show Markers </label>
<input
style={{ opacity: 1, pointerEvents: "auto" }}
type="checkbox"
name="layertoggle"
id="layertoggle"
value={this.state.showMarkers}
onChange={this.onGeojsonMarkers}
/>
</div>
{this.state.showMarkers && (
<GeoJSON
data={points}
style={this.geoJSONStyle}
value={this.state.showMarkers}
onEachFeature={this.onEachFeaturePoint.bind(this)}
/>
)}
{/* /////////////////////////////////////// */}
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
layers="NDVI"
// baseUrl="https://services.sentinel-hub.com/ogc/wms/bb1c8a2f-5b11-42bb-8ce4-dbf7f5300663"
/>
<KmlUpload />
{/* <Legend /> */}
<Marker position={position}>
<Popup className="request-popup">
<div style={popupContent}>
<img
src="https://cdn3.iconfinder.com/data/icons/basicolor-arrows-checks/24/149_check_ok-512.png"
width="150"
height="150"
alt="no img"
/>
<div className="m-2" style={popupHead}>
Success!
</div>
</div>
</Popup>
</Marker>
{this.state.geojsonvisible && (
<GeoJSON
data={points}
onEachFeature={this.onEachFeaturePoint.bind(this)}
// pointToLayer={this.pointToLayer.bind(this)}
/>
)}
<FeatureGroup>
{viewMap && (
<EditControl
position="topleft"
onEdited={this._onEditPath}
onCreated={this.onCreate}
onDeleted={this._onDeleted}
onMounted={this._mounted}
onEditStart={this._onEditStart}
onEditStop={this._onEditStop}
onDeleteStart={this._onDeleteStart}
onDeleteStop={this._onDeleteStop}
draw={{
polyline: false,
rectangle: false,
marker: false,
circleMarker: false,
circle: false,
circlemarker: false,
}}
/>
)}
</FeatureGroup>
<Polygon color="purple" positions={polygon} />
<GeoJSON
data={london_postcodes}
style={this.geoJSONStyle}
onEachFeature={this.onEachFeature}
/>
</Map>
<Typography>
Click on the{" "}
<LocationOnIcon className={styles.centered}></LocationOnIcon>
icon to use your current location.
<Typography variant="caption">
{" "}
Device location must be enabled
</Typography>
</Typography>
<div className={styles.whiteSpace15}>
<button
className="waves-effect waves-light btn"
onClick={this.saveToFile}
>
Download drawed shape in kml File
</button>
<br />
{/* uncoment to see image processing job
<button
className="waves-effect waves-light btn"
onClick={this.processHardcodedImageUrl()}
>
Process hardcoded Image Url
</button> */}
<br />
<button
className="waves-effect waves-light btn"
onClick={this.resetMap}
>
Clear map
</button>
<button
className="waves-effect waves-light btn"
onClick={this.moveTo}
>
Move to another location
</button>
</div>
</div>
);
} else {
return (
<div className={styles.container}>
<NavLink to="/signin">
<Typography>
<LockIcon className={styles.centered}></LockIcon>
{t("Please login to continue")}
</Typography>
</NavLink>
</div>
);
}
}
}
const mapStateToProps = (state) => {
return {
auth: state.firebase.auth.uid,
profile: state.firebase.profile,
};
};
//at the moment this function is not used
const mapDispatchToProps = (dispatch) => {
return {
saveData: (coordinates) => dispatch(saveData(coordinates)),
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(withTranslation()(Dashboard));