-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
211 lines (181 loc) · 6.61 KB
/
index.html
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
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Geometry Conversor</title>
<style>
#container {
max-width: 800px;
margin: 2rem auto 0;
}
.btn-menu{
display: flex;
}
textarea{
height: 300px;
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
padding: 1rem;
}
.padding{
padding-left: 1rem;
padding-right: 1rem;
}
#results{
margin-top: 1rem
}
.footer{
text-align: center;
font-size: .9rem;
margin-top: 2rem;
opacity: .4;
transition: opacity .5s;
}
.footer:hover{
opacity: 1;
cursor: pointer;
}
</style>
<script type="module" src="https://js.arcgis.com/calcite-components/1.0.0-beta.52/calcite.esm.js"></script>
<script nomodule="" src="https://js.arcgis.com/calcite-components/1.0.0-beta.52/calcite.js"></script>
<link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/1.0.0-beta.52/calcite.css" />
<link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/themes/light/main.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"></script>
<!-- and it's easy to individually load additional languages -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/languages/go.min.js"></script>
<script src="json-stringify-pretty-compact.js"></script>
</head>
<body>
<div id="container">
<h1 class="padding">ArcGIS geomeries converter</h1>
<div class='row'>
<div class='column'>
Sample Geometry in Mercator units (3857)
<pre><code class="javascript">{
"xmin": -13648495.058561934,
"ymin": 4534651.565967717,
"xmax": -13609168.207509262,
"ymax": 4557353.363368384,
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
}</code></pre>
</div>
<div class='column'>
Sample Geometry in Geographic units (4326)
<pre><code class="javascript">{
"xmin": -122.6065171633595,
"ymin": 37.68236684303815,
"xmax": -122.25323804959042,
"ymax": 37.84358670736482,
"spatialReference": {
"wkid": 4326
}
}
</code></pre>
</div>
</div>
<div class="padding">
<calcite-label>Input geometry
<calcite-input placeholder="Placeholder" type="textarea" id="geometry" value='{ "spatialReference": { "wkid": 102100 }, "rings": [ [ [ 6118187.462239532, 2882908.6239332743 ], [ 6120593.989399323, 2884450.844186426 ], [ 6126378.015927959, 2881537.2209938993 ], [ 6129812.9002211755, 2874778.576282998 ], [ 6123548.3319766475, 2867449.987249655 ], [ 6112623.96019846, 2873542.873095356 ], [ 6118187.462239532, 2882908.6239332743 ] ] ] }'></calcite-input></calcite-label>
<p>Convert geometry from:</p>
</div>
<div class="btn-menu">
<div class='row'>
<div class='column'>
<calcite-button icon-start="deep-learning-project" id="mercator2geo">Web Mercator -> Geographic</calcite-button>
</div>
<div class='column'>
<calcite-button icon-start="deep-learning-project" id="geo2Mercator">Geographic -> Web Mercator</calcite-button>
</div>
</div>
</div>
<div style="display:none" id="results" class="padding">
Converted geometry:
<pre><code id="output" class="javascript"></code></pre>
</div>
<p class="footer">Made using <a href="http://js.arcgis.com/">API for JavaScript</a> - <a href="https://github.com/hhkaos/mercator-geographic-converter">Fork me on Github</a></p>
<script src="https://js.arcgis.com/4.19/"></script>
<script>
require(["esri/geometry/support/webMercatorUtils", "esri/geometry/Geometry"], (webMercatorUtils, Geometry) => {
hljs.highlightAll();
const reducePrecision = (num) => {
if(num % 1 !== 0){
// Number has decimals
return num.toFixed(6);
}else{
return num;
}
}
const reducePrecisionLoop = (obj) => {
for (const key in obj) {
const objType = typeof obj[key];
if(objType === "object"){
if(obj[key].constructor === Array){
obj[key] = obj[key].map(el => {
if(typeof el === "object"){
return reducePrecisionLoop(el);
}else{
return reducePrecision(el);
}
});
}else{
if(obj[key].toJSON){
reducePrecisionLoop(obj[key].toJSON());
}else{
obj[key]= reducePrecisionLoop(obj[key]);
}
}
}else if(objType === "number"){
obj[key] = reducePrecision(obj[key]);
}
}
return obj;
};
const updateOutput = (transformation) =>{
let transformationFunction = null;
switch(transformation){
case "geo2Mercator":
transformationFunction = webMercatorUtils.geographicToWebMercator;
break;
case "mercator2geo":
transformationFunction = webMercatorUtils.webMercatorToGeographic;
}
try{
const obj = JSON.parse(document.querySelector(`#geometry textarea`).value);
let geom = transformationFunction(obj);
// document.getElementById('output').innerText = JSON.stringify(geom, null, 2);
geom = reducePrecisionLoop(geom);
document.getElementById('output').innerText = stringify(geom);
hljs.highlightAll();
}catch(err){
document.getElementById('output').innerText = err;
}
document.getElementById("results").style.display = "block";
};
document.getElementById("geo2Mercator").addEventListener('click', (evt) => {
evt.preventDefault();
updateOutput(evt.target.id);
return false;
});
document.getElementById("mercator2geo").addEventListener('click', (evt) => {
evt.preventDefault();
updateOutput(evt.target.id);
return false;
});
});
</script>
</body>
</html>