-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
234 lines (213 loc) · 8.1 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html>
<head>
<title>Draw features example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script>
var raster;
var source;
var vector;
var map;
var typeSelect;
var draw;
var features = new ol.Collection();
var format = new ol.format.WKT();
var current_shape = "point";
var fill = new ol.style.Fill({
color: 'rgba(210, 122, 167,0.2)'
});
var stroke = new ol.style.Stroke({
color: '#B40404',
width: 2
});
var styles = [
new ol.style.Style({
image: new ol.style.Circle({
fill: fill,
stroke: stroke,
radius: 5
}),
fill: fill,
stroke: stroke
})
];
function addInteraction(shape) {
draw = new ol.interaction.Draw({
features: features,
type: /** @type {ol.geom.GeometryType} */ shape
});
map.addInteraction(draw);
}
/**
* Let user change the geometry type.
* @param {Event} e Change event.
*/
function createVector() {
vector = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: styles
});
}
function toEPSG4326(element, index, array) {
element = element.getGeometry().transform('EPSG:3857', 'EPSG:4326');
}
function toEPSG3857(element, index, array) {
element = element.getGeometry().transform('EPSG:4326', 'EPSG:3857');
}
function selectGeom(shape) {
current_shape = shape;
map.removeInteraction(draw);
addInteraction(shape);
}
function init() {
// document.getElementById("missing_wkt").style.display = "block";
createVector();
raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
features.on("add", function (e) {
restoreDefaultColors();
features.forEach(toEPSG4326);
document.getElementById('wktStringTextArea').value = format.writeFeatures(features.getArray(), { rightHanded: true });
features.forEach(toEPSG3857);
});
map = new ol.Map({
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
plotWKT();
selectGeom('Point');
}
function restoreDefaultColors() {
document.getElementById("wktStringTextArea").style.borderColor = "";
document.getElementById("wktStringTextArea").style.backgroundColor = "";
};
// Plot wkt string on map
function plotWKT() {
var new_feature;
wkt_string = document.getElementById("wktStringTextArea").value;
if (wkt_string == "") {
document.getElementById("wktStringTextArea").style.borderColor = "red";
document.getElementById("wktStringTextArea").style.backgroundColor = "#F7E8F3";
return;
} else {
try {
new_feature = format.readFeature(wkt_string);
} catch (err) {
}
}
if (!new_feature) {
document.getElementById("wktStringTextArea").style.borderColor = "red";
document.getElementById("wktStringTextArea").style.backgroundColor = "#F7E8F3";
return;
} else {
map.removeLayer(vector);
features.clear();
new_feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
features.push(new_feature);
}
vector = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: styles
});
selectGeom(current_shape);
map.addLayer(vector);
derived_feature = features.getArray()[0];
extent = derived_feature.getGeometry().getExtent();
minx = derived_feature.getGeometry().getExtent()[0];
miny = derived_feature.getGeometry().getExtent()[1];
maxx = derived_feature.getGeometry().getExtent()[2];
maxy = derived_feature.getGeometry().getExtent()[3];
centerx = (minx + maxx) / 2;
centery = (miny + maxy) / 2;
map.setView(new ol.View({
center: [centerx, centery],
zoom: 8
}));
map.getView().fit(extent, map.getSize());
}
function clearMap() {
map.removeLayer(vector);
features.clear();
vector = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: styles
});
selectGeom(current_shape);
map.addLayer(vector);
document.getElementById("wktStringTextArea").value = "";
restoreDefaultColors();
}
</script>
<!-- Place this tag in your head or just before your close body tag. -->
<script async defer src="https://buttons.github.io/buttons.js"></script>
<style>
@media only screen and (min-width: 1366px) {
.map { height: 400px !important}
}
@media only screen and (min-width: 1440px) {
.map { height: 500px !important}
}
@media only screen and (min-width: 1680px) {
.map { height: 650px !important}
}
@media only screen and (min-width: 1920px) {
.map { height: 700px !important}
}
@media only screen and (min-width: 2560px) {
.map { height: 1050px !important}
}
</style>
</head>
<body onload="init()">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark" style="height: 40px;line-height: 0.5">
<a class="navbar-brand" href="#">WKT Playground</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="https://github.com/xlxxcc/xlxxcc">Code</a>
</li>
<li class="nav-item">
<!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/xlxxcc/xlxxcc" data-icon="octicon-star" data-size="large" data-show-count="true"
aria-label="Star xlxxcc/xlxxcc on GitHub">Star</a>
</li>
</ul>
</nav>
<div class="container-fluid" style="padding: 0px; border:0px">
<div class="span12">
<div id="overlay" class="row align-items-center justify-content-center" style="background-color: transparent; z-index:1; position: absolute; top:280px; right:26px">
<div class="btn-group-vertical ">
<button type="button" class="btn btn-primary" onclick="selectGeom('Point')">Point</button>
<button type="button" class="btn btn-primary" onclick="selectGeom('LineString')">Line String</button>
<button type="button" class="btn btn-primary" onclick="selectGeom('Polygon')">Polygon</button>
</div>
</div>
<div id="map" class="map" style="z-index: 0"></div>
</div>
<div class="span12" style="padding: 10px; border:0px">
<textarea id="wktStringTextArea" class="form-control" rows="4" onclick="restoreDefaultColors()">
MULTIPOLYGON(((-108.72070312499997 34.99400375757577,-100.01953124999997 46.58906908309183,-90.79101562499996 34.92197103616377,-108.72070312499997
34.99400375757577),(-100.10742187499997 41.47566020027821,-102.91992187499996 37.61423141542416,-96.85546874999996
37.54457732085582,-100.10742187499997 41.47566020027821)),((-85.16601562499999 34.84987503195417,-80.771484375 28.497660832963476,-76.904296875
34.92197103616377,-85.16601562499999 34.84987503195417)))
</textarea>
<div class="btn-group btn-group-md" style="padding: 10px; position: absolute; right:0px">
<button type="button" class="btn btn-primary" onclick="clearMap()">Clear</button>
<button type="button" class="btn btn-primary" onclick="plotWKT()">Plot Shape</button>
</div>
</div>
</div>
</body>
</html>