-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmeasurement-midpoint.html
140 lines (127 loc) · 3.79 KB
/
measurement-midpoint.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
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<link rel="stylesheet" href="assets/css/styles.css" type="text/css">
<script src="//cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
<script src="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js" type="text/javascript"></script>
<title>Turf and OpenLayers 3 - midpoint</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
// Declare a GeoJSON source
var geojson_source = new ol.source.Vector({
projection: 'EPSG:3857'
});
var x1 = -1.5741021;
var y1 = 47.1991929;
// Cranes of Nantes with properties defined
var pt1 = turf.point([x1, y1], {
name: 'Grue grise',
man_made: 'crane'
});
var x2 = -1.57007;
var y2 = 47.20507;
var pt2 = turf.point([x2, y2], {
name: 'Grue jaune',
man_made: 'crane'
});
var fc = turf.featureCollection([pt1, pt2]);
var midPoint = turf.midpoint(pt1, pt2);
// Declare a formatter to read GeoJSON
var format = new ol.format.GeoJSON();
// Declare a source
var vectorSource = new ol.source.Vector();
// When reading feature, reproject to EPSG 3857
var features = format.readFeatures(fc, {
featureProjection: 'EPSG:3857'
});
// Add a feature
vectorSource.addFeatures(features);
var pointsStyle = [new ol.style.Style({
image: new ol.style.Circle({
stroke: new ol.style.Stroke({
color: 'white'
}),
fill: new ol.style.Fill({
color: '#1f6b75'
}),
radius: 5
})
})];
var midPointStyle = [new ol.style.Style({
image: new ol.style.Circle({
stroke: new ol.style.Stroke({
color: 'white'
}),
fill: new ol.style.Fill({
color: 'black'
}),
radius: 5
})
})];
// Declare a vector layer with the already
// created source containing added features
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: pointsStyle
});
var vectorMidPoint = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(
midPoint, {
featureProjection: 'EPSG:3857'
}
),
projection: 'EPSG:3857'
}),
style: midPointStyle
});
var vectorLine = new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(
turf.lineString([[x1, y1], [x2, y2]], {}), {
featureProjection: 'EPSG:3857'
}
),
projection: 'EPSG:3857'
}),
style: [
new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'white',
width: 3
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 121, 88, 1],
width: 1
})
})
]
});
// Instanciate a map and add layers
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLine,
vectorLayer,
vectorMidPoint
],
view: new ol.View({
center: ol.proj.transform(
[-1.571456, 47.202561],
'EPSG:4326',
'EPSG:3857'
),
zoom: 15
})
});
</script>
</body>
</html>