原文链接及内容

运行界面

这里将从ArcGIS REST要素服务中加载要素,并允许添加新要素或更新现有要素。

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
import EsriJSON from 'ol/format/EsriJSON.js';
import Map from 'ol/Map.js';
import VectorSource from 'ol/source/Vector.js';
import View from 'ol/View.js';
import XYZ from 'ol/source/XYZ.js';
import {
Draw,
Modify,
Select,
defaults as defaultInteractions,
} from 'ol/interaction.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
import {createXYZ} from 'ol/tilegrid.js';
import {fromLonLat} from 'ol/proj.js';
import {tile as tileStrategy} from 'ol/loadingstrategy.js';

const serviceUrl =
'https://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/';
const layer = '2';

const esrijsonFormat = new EsriJSON();

const vectorSource = new VectorSource({
loader: function (extent, resolution, projection) {
const url =
serviceUrl +
layer +
'/query/?f=json&' +
'returnGeometry=true&spatialRel=esriSpatialRelIntersects&geometry=' +
encodeURIComponent(
'{"xmin":' +
extent[0] +
',"ymin":' +
extent[1] +
',"xmax":' +
extent[2] +
',"ymax":' +
extent[3] +
',"spatialReference":{"wkid":102100}}'
) +
'&geometryType=esriGeometryEnvelope&inSR=102100&outFields=*' +
'&outSR=102100';
$.ajax({
url: url,
dataType: 'jsonp',
success: function (response) {
if (response.error) {
alert(
response.error.message + '\n' + response.error.details.join('\n')
);
} else {
// dataProjection will be read from document
const features = esrijsonFormat.readFeatures(response, {
featureProjection: projection,
});
if (features.length > 0) {
vectorSource.addFeatures(features);
}
}
},
});
},
strategy: tileStrategy(
createXYZ({
tileSize: 512,
})
),
});

const vector = new VectorLayer({
source: vectorSource,
});

const raster = new TileLayer({
source: new XYZ({
attributions:
'Tiles © <a href="https://services.arcgisonline.com/ArcGIS/' +
'rest/services/World_Topo_Map/MapServer">ArcGIS</a>',
url:
'https://server.arcgisonline.com/ArcGIS/rest/services/' +
'World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
}),
});

const draw = new Draw({
source: vectorSource,
type: 'Polygon',
});

const select = new Select();
select.setActive(false);
const selected = select.getFeatures();

const modify = new Modify({
features: selected,
});
modify.setActive(false);

const map = new Map({
interactions: defaultInteractions().extend([draw, select, modify]),
layers: [raster, vector],
target: document.getElementById('map'),
view: new View({
center: fromLonLat([-110.875, 37.345]),
zoom: 5,
}),
});

const typeSelect = document.getElementById('type');

/**
* Let user change the interaction type.
*/
typeSelect.onchange = function () {
draw.setActive(typeSelect.value === 'DRAW');
select.setActive(typeSelect.value === 'MODIFY');
modify.setActive(typeSelect.value === 'MODIFY');
};

const dirty = {};

selected.on('add', function (evt) {
const feature = evt.element;
feature.on('change', function (evt) {
dirty[evt.target.get('objectid')] = true;
});
});

selected.on('remove', function (evt) {
const feature = evt.element;
const fid = feature.get('objectid');
if (dirty[fid] === true) {
const payload =
'[' +
esrijsonFormat.writeFeature(feature, {
featureProjection: map.getView().getProjection(),
}) +
']';
const url = serviceUrl + layer + '/updateFeatures';
$.post(url, {f: 'json', features: payload}).done(function (data) {
const result = typeof data === 'string' ? JSON.parse(data) : data;
if (result.updateResults && result.updateResults.length > 0) {
if (result.updateResults[0].success !== true) {
const error = result.updateResults[0].error;
alert(error.description + ' (' + error.code + ')');
} else {
delete dirty[fid];
}
}
});
}
});

draw.on('drawend', function (evt) {
const feature = evt.feature;
const payload =
'[' +
esrijsonFormat.writeFeature(feature, {
featureProjection: map.getView().getProjection(),
}) +
']';
const url = serviceUrl + layer + '/addFeatures';
$.post(url, {f: 'json', features: payload}).done(function (data) {
const result = typeof data === 'string' ? JSON.parse(data) : data;
if (result.addResults && result.addResults.length > 0) {
if (result.addResults[0].success === true) {
feature.set('objectid', result.addResults[0]['objectId']);
} else {
const error = result.addResults[0].error;
alert(error.description + ' (' + error.code + ')');
}
}
});
});

界面布局文件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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Editable ArcGIS REST Feature Service</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">Draw or modify &nbsp;</label>
<select id="type">
<option value="DRAW">Draw</option>
<option value="MODIFY">Modify</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 src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>