aboutsummaryrefslogtreecommitdiffstats
path: root/index.html
blob: 7be2880aba3136553b3ac7708c5330bb92d81d4c (plain)
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
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Geo Feature Explorer</title>

	<link rel="stylesheet" href="https://openlayers.org/en/v4.1.0/css/ol.css" type="text/css">
	<style>
		textarea {
			width: 500px;
			height: 200px;
			font: 12px Monaco, Courier, monospace;
		}
	</style>
</head>
<body>
	<textarea class="js-wkt"></textarea>
	<textarea class="js-selected-feature"></textarea>

	<div id="map"></div>

	<script src="https://openlayers.org/en/v4.1.0/build/ol.js"></script>
	<script>
		var input_el = document.getElementsByClassName('js-wkt')[0];
		var output_el = document.getElementsByClassName('js-selected-feature')[0];

		var raster = new ol.layer.Tile({
			source: new ol.source.OSM()
		});

		var format = new ol.format.WKT();

		var vector_style = new ol.style.Style({
			stroke: new ol.style.Stroke({
				color: '#0091ff',
				width: 5
			})
		});

		var map = new ol.Map({
			layers: [raster],
			target: 'map',
			view: new ol.View({
				center: ol.proj.fromLonLat([2.3475075, 48.869163]),
				zoom: 16
			})
		});

		var select_click = new ol.interaction.Select({
			condition: ol.events.condition.click
		});
		select_click.on('select', function(e) {
			if (e.selected.length) {
				var feature = e.selected[0];
				output_el.textContent = format.writeFeature(feature, {
					dataProjection: 'EPSG:4326',
					featureProjection: 'EPSG:3857'
				});
			}
		});

		map.addInteraction(select_click);

		var wkt_layer;
		input_el.onblur = function(e) {
			// Remove old WKT layer
			if (wkt_layer) {
				map.removeLayer(wkt_layer);
			}

			var wkt_features = e.target.value.split('\n');
			wkt_features = wkt_features.map(function(wkt) {
				return format.readFeature(wkt, {
					dataProjection: 'EPSG:4326',
					featureProjection: 'EPSG:3857'
				});
			});

			wkt_layer = new ol.layer.Vector({
				source: new ol.source.Vector({
					features: wkt_features
				}),
				style: vector_style
			});
			map.addLayer(wkt_layer);
		};
	</script>
</body>
</html>