-
Notifications
You must be signed in to change notification settings - Fork 12
/
measurement-pointOnSurface.html
129 lines (122 loc) · 4 KB
/
measurement-pointOnSurface.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
<!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 - Point on surface</title>
</head>
<body>
<div id="map" class="map"></div>
<script type="text/javascript">
// A building in Nantes, France
var coordinates = [
[
[-1.5587178, 47.2172349],
[-1.5588193, 47.2171524],
[-1.5586966, 47.2170781],
[-1.5586852, 47.217087],
[-1.5586665, 47.2170754],
[-1.5586529, 47.2170844],
[-1.558591, 47.2170477],
[-1.5586034, 47.2170364],
[-1.5585939, 47.2170291],
[-1.5585793, 47.2170404],
[-1.5585595, 47.2170289],
[-1.5585627, 47.2170011],
[-1.5585961, 47.2170028],
[-1.5586105, 47.2168861],
[-1.5587366, 47.2168588],
[-1.5587301, 47.2168345],
[-1.558791, 47.216822],
[-1.5587989, 47.2168363],
[-1.5589123, 47.2169065],
[-1.5590038, 47.2168367],
[-1.5590088, 47.2168311],
[-1.5588502, 47.2167374],
[-1.5585378, 47.2168006],
[-1.5584812, 47.216812],
[-1.558452, 47.2170663],
[-1.5585381, 47.2171282],
[-1.5587178, 47.2172349]
]
];
var polygon = turf.polygon(coordinates, {
"@id": "way/85440995",
"building": "yes",
"source": "cadastre-dgi-fr source : Direction Générale des Impôts - \
Cadastre. Mise à jour : 2010"
});
// 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 feature = format.readFeature(polygon, {
featureProjection: 'EPSG:3857'
});
// Add a feature
vectorSource.addFeature(feature);
// Create function to style points
function setPointStyle(color, radius) {
return new ol.style.Style({
image: new ol.style.Circle({
stroke: new ol.style.Stroke({
color: 'white'
}),
fill: new ol.style.Fill({
color: color
}),
radius: radius
})
})
}
// Declare a vector layer with the already
// created source containing added feature
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: [
new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 121, 88, 1],
width: 2
})
})
]
});
// Instanciate a map and add layers
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer,
// Centroid
new ol.layer.Vector({
source: new ol.source.Vector({
projection: 'EPSG:3857',
object: turf.centroid(polygon)
}),
style: [setPointStyle('black', 5)]
}),
// Point on surface
new ol.layer.Vector({
source: new ol.source.Vector({
projection: 'EPSG:3857',
features: (new ol.format.GeoJSON()).readFeatures(turf.pointOnSurface(polygon), {
featureProjection: 'EPSG:3857'
})
}),
style: [setPointStyle('orange', 5)]
})
],
view: new ol.View({
center: ol.proj.fromLonLat([-1.558567, 47.216947]),
zoom: 19
})
});
</script>
</body>
</html>