-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.js
115 lines (93 loc) · 3.3 KB
/
transformer.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
import { arcgisToGeoJSON, geojsonToArcGIS } from '@terraformer/arcgis'
import { wktToGeoJSON, geojsonToWKT } from "@terraformer/wkt"
const mainTxtArea = document.getElementById('inWkt');
const transformerModal = document.getElementById("transformerModal");
const openTransformBtn = document.getElementById("openTransforBtn");
const closeTransformSpan = document.getElementsByClassName("close-transformer-modal")[0];
const txtAreaWkt = document.getElementById('inTransformWkt');
const txtAreaGeoJson = document.getElementById('inTransformGeoJson');
const txtAreaArcGisJson = document.getElementById('inTransformArcGisJson');
const lblModalInfo = document.getElementById('lblModalInfo');
// When the user clicks the button, open the modal
openTransformBtn.onclick = function () {
transformerModal.style.display = "block";
if (txtAreaWkt.value.length === 0) {
txtAreaWkt.value = mainTxtArea.value;
transformFromWkt();
}
}
// When the user clicks on <span> (x), close the modal
closeTransformSpan.onclick = () => {
transformerModal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = (event) => {
if (event.target == transformerModal) {
transformerModal.style.display = "none";
}
}
txtAreaWkt.addEventListener('input', () => {
transformFromWkt();
}, false);
txtAreaGeoJson.addEventListener('input', () => {
transformFromGeoJson();
}, false);
txtAreaArcGisJson.addEventListener('input', () => {
transformFromArcGisJson();
}, false);
function transformFromWkt() {
const wkt = txtAreaWkt.value;
if (wkt.length === 0) {
lblModalInfo.innerText = 'nothing';
return;
}
try {
const geoJson = wktToGeoJSON(wkt);
txtAreaGeoJson.value = JSON.stringify(geoJson, undefined, 2);
const arcgisJson = geojsonToArcGIS(geoJson);
txtAreaArcGisJson.value = JSON.stringify(arcgisJson, undefined, 2);
lblModalInfo.innerText = 'WKT transformed';
}
catch (x) {
lblModalInfo.innerText = 'could not parse';
console.error(x);
}
}
function transformFromGeoJson() {
const geoJson = txtAreaGeoJson.value;
if (geoJson.length === 0) {
lblModalInfo.innerText = 'nothing';
return;
}
try {
const geoJsonParsed = JSON.parse(geoJson);
const wkt = geojsonToWKT(geoJsonParsed);
txtAreaWkt.value = wkt;
const arcgisJson = geojsonToArcGIS(geoJsonParsed);
txtAreaArcGisJson.value = JSON.stringify(arcgisJson, undefined, 2);
lblModalInfo.innerText = 'GeoJSON transformed';
}
catch (x) {
lblModalInfo.innerText = 'could not parse';
console.error(x);
}
}
function transformFromArcGisJson() {
const arcgisJson = txtAreaArcGisJson.value;
if (arcgisJson.length === 0) {
lblModalInfo.innerText = 'nothing';
return;
}
try {
const arcGisJsonParsed = JSON.parse(arcgisJson);
const geoJson = arcgisToGeoJSON(arcGisJsonParsed);
txtAreaGeoJson.value = JSON.stringify(geoJson, undefined, 2);
const wkt = geojsonToWKT(geoJson);
txtAreaWkt.value = wkt;
lblModalInfo.innerText = 'ArcGIS JSON transformed';
}
catch (x) {
lblModalInfo.innerText = 'could not parse';
console.error(x);
}
}