原文链接及内容

运行界面

使用ol/interaction/Draw与自定义几何函数进行绘制交互,以及使用ol/interaction/Modify修改交互来修改测地线(大地线)圆(ol/geom/Polygon#circular多边形表示地球球体表面上的圆)的示例。多边形则是利用ol/geom/GeometryCollectionol/geom/Point来绘制的,以方便使用修改交互来调整圆的中心和半径。自定义样式函数确保最终能够正确显示几何图形。除此之外,还可以绘制和修改ol/geom/Circle投影(平面)几何图形。当测地线(大地线)圆和投影圆的中心在Web墨卡托投影的南北纬度之间移动时,可以看到测地线(大地线)圆和投影圆之间的差异。我们可以记住ol/interaction/Snap捕捉交互来创建同心圆。

main.js代码如下:

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
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js';
import {Draw, Modify, Snap} from 'ol/interaction.js';
import {GeometryCollection, Point, Polygon} from 'ol/geom.js';
import {OSM, Vector as VectorSource} from 'ol/source.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
import {circular} from 'ol/geom/Polygon.js';
import {getDistance} from 'ol/sphere.js';
import {transform} from 'ol/proj.js';

const raster = new TileLayer({
source: new OSM(),
});

const source = new VectorSource();

const style = new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.2)',
}),
stroke: new Stroke({
color: '#33cc33',
width: 2,
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: '#ffcc33',
}),
}),
});

const geodesicStyle = new Style({
geometry: function (feature) {
return feature.get('modifyGeometry') || feature.getGeometry();
},
fill: new Fill({
color: 'rgba(255, 255, 255, 0.2)',
}),
stroke: new Stroke({
color: '#ff3333',
width: 2,
}),
image: new CircleStyle({
radius: 7,
fill: new Fill({
color: 'rgba(0, 0, 0, 0)',
}),
}),
});

const vector = new VectorLayer({
source: source,
style: function (feature) {
const geometry = feature.getGeometry();
return geometry.getType() === 'GeometryCollection' ? geodesicStyle : style;
},
});

const map = new Map({
layers: [raster, vector],
target: 'map',
view: new View({
center: [-11000000, 6600000],
zoom: 3,
}),
});

const defaultStyle = new Modify({source: source})
.getOverlay()
.getStyleFunction();

const modify = new Modify({
source: source,
style: function (feature) {
feature.get('features').forEach(function (modifyFeature) {
const modifyGeometry = modifyFeature.get('modifyGeometry');
if (modifyGeometry) {
const modifyPoint = feature.getGeometry().getCoordinates();
const geometries = modifyFeature.getGeometry().getGeometries();
const polygon = geometries[0].getCoordinates()[0];
const center = geometries[1].getCoordinates();
const projection = map.getView().getProjection();
let first, last, radius;
if (modifyPoint[0] === center[0] && modifyPoint[1] === center[1]) {
// center is being modified
// get unchanged radius from diameter between polygon vertices
first = transform(polygon[0], projection, 'EPSG:4326');
last = transform(
polygon[(polygon.length - 1) / 2],
projection,
'EPSG:4326'
);
radius = getDistance(first, last) / 2;
} else {
// radius is being modified
first = transform(center, projection, 'EPSG:4326');
last = transform(modifyPoint, projection, 'EPSG:4326');
radius = getDistance(first, last);
}
// update the polygon using new center or radius
const circle = circular(
transform(center, projection, 'EPSG:4326'),
radius,
128
);
circle.transform('EPSG:4326', projection);
geometries[0].setCoordinates(circle.getCoordinates());
// save changes to be applied at the end of the interaction
modifyGeometry.setGeometries(geometries);
}
});
return defaultStyle(feature);
},
});

modify.on('modifystart', function (event) {
event.features.forEach(function (feature) {
const geometry = feature.getGeometry();
if (geometry.getType() === 'GeometryCollection') {
feature.set('modifyGeometry', geometry.clone(), true);
}
});
});

modify.on('modifyend', function (event) {
event.features.forEach(function (feature) {
const modifyGeometry = feature.get('modifyGeometry');
if (modifyGeometry) {
feature.setGeometry(modifyGeometry);
feature.unset('modifyGeometry', true);
}
});
});

map.addInteraction(modify);

let draw, snap; // global so we can remove them later
const typeSelect = document.getElementById('type');

function addInteractions() {
let value = typeSelect.value;
let geometryFunction;
if (value === 'Geodesic') {
value = 'Circle';
geometryFunction = function (coordinates, geometry, projection) {
if (!geometry) {
geometry = new GeometryCollection([
new Polygon([]),
new Point(coordinates[0]),
]);
}
const geometries = geometry.getGeometries();
const center = transform(coordinates[0], projection, 'EPSG:4326');
const last = transform(coordinates[1], projection, 'EPSG:4326');
const radius = getDistance(center, last);
const circle = circular(center, radius, 128);
circle.transform('EPSG:4326', projection);
geometries[0].setCoordinates(circle.getCoordinates());
geometry.setGeometries(geometries);
return geometry;
};
}
draw = new Draw({
source: source,
type: value,
geometryFunction: geometryFunction,
});
map.addInteraction(draw);
snap = new Snap({source: source});
map.addInteraction(snap);
}

/**
* Handle change event.
*/
typeSelect.onchange = function () {
map.removeInteraction(draw);
map.removeInteraction(snap);
addInteractions();
};

addInteractions();

界面布局文件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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Draw and Modify Geodesic Circles</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<form>
<label for="type">Geometry type &nbsp;</label>
<select id="type">
<option value="Point">Point</option>
<option value="LineString">LineString</option>
<option value="Polygon">Polygon</option>
<option value="Circle">Circle Geometry</option>
<option value="Geodesic" selected>Geodesic Circle</option>
</select>
</form>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>