ol-wfst 3.0.1 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ol-wfst.js CHANGED
@@ -93,6 +93,7 @@ const es = {
93
93
  capabilities: 'No se pudieron obtener las Capabilidades del GeoServer',
94
94
  wfst: 'El GeoServer no tiene soporte a Transacciones',
95
95
  layer: 'No se pudieron obtener datos de la capa',
96
+ layerNotFound: 'CApa no encontrada',
96
97
  noValidGeometry: 'No se encontraron geometrías válidas para agregar a esta capa',
97
98
  geoserver: 'No se pudieron obtener datos desde el GeoServer',
98
99
  badFormat: 'Formato no soportado',
@@ -131,20 +132,61 @@ const en = {
131
132
  capabilities: 'GeoServer Capabilities could not be downloaded.',
132
133
  wfst: 'The GeoServer does not support Transactions',
133
134
  layer: 'Could not get data from layer',
135
+ layerNotFound: 'Layer not found',
134
136
  noValidGeometry: 'No valid geometries found to add to this layer',
135
137
  geoserver: 'Could not get data from the GeoServer',
136
138
  badFormat: 'Unsupported format',
137
139
  badFile: 'Error reading items from file',
138
- lockFeature: 'No se pudieron bloquear elementos en el GeoServer. HTTP status:',
140
+ lockFeature: 'Could not lock items on the GeoServer. HTTP status:',
139
141
  transaction: 'Error when doing Transaction with GeoServer. HTTP status:',
140
142
  getFeatures: 'Error getting elements from GeoServer. HTTP status:'
141
143
  }
142
144
  };
143
145
 
146
+ const zh = {
147
+ labels: {
148
+ select: '选择',
149
+ addElement: '切换绘图类型',
150
+ editElement: '编辑元素',
151
+ save: '保存',
152
+ delete: '删除',
153
+ cancel: '取消',
154
+ apply: '确认并应用改变',
155
+ upload: '上传',
156
+ editMode: '编辑模式',
157
+ confirmDelete: '确认删除元素?',
158
+ geomTypeNotSupported: '图层不支持该几何',
159
+ editFields: '编辑区域',
160
+ editGeom: '编辑几何',
161
+ selectDrawType: '几何类型',
162
+ uploadToLayer: '通过文件上传图层',
163
+ uploadFeatures: '上传元素到图层',
164
+ validFeatures: '合法的几何类型',
165
+ invalidFeatures: '不合法',
166
+ loading: '加载中...',
167
+ toggleVisibility: '切换图层透明度',
168
+ close: '关闭'
169
+ },
170
+ errors: {
171
+ capabilities: '无法加载GeoServer服务所支持的能力.',
172
+ wfst: 'GeoServer不支持事务',
173
+ layer: '无法从图层获得数据',
174
+ layerNotFound: 'Layer not found',
175
+ noValidGeometry: '不支持的几何类型无法加载到图层',
176
+ geoserver: '无法从GeoServer获取数据',
177
+ badFormat: '不支持的格式',
178
+ badFile: '读取文件数据出错',
179
+ lockFeature: '无法锁定GeoServer上的元素. HTTP状态码:',
180
+ transaction: 'GeoServer处理事务出错. HTTP状态码:',
181
+ getFeatures: '从GeoServer获取元素出错. HTTP状态码:'
182
+ }
183
+ };
184
+
144
185
  var i18n = /*#__PURE__*/Object.freeze({
145
186
  __proto__: null,
146
187
  es: es,
147
- en: en
188
+ en: en,
189
+ zh: zh
148
190
  });
149
191
 
150
192
  // https://docs.geoserver.org/latest/en/user/services/wfs/axis_order.html
@@ -198,6 +240,7 @@ class Wfst extends Control {
198
240
  projection: DEFAULT_GEOSERVER_SRS
199
241
  },
200
242
  headers: {},
243
+ credentials: 'same-origin',
201
244
  layers: null,
202
245
  evtType: 'singleclick',
203
246
  active: true,
@@ -309,7 +352,8 @@ class Wfst extends Control {
309
352
  const url_fetch = this._options.geoServerUrl + '?' + params.toString();
310
353
  try {
311
354
  const response = yield fetch(url_fetch, {
312
- headers: this._options.headers
355
+ headers: this._options.headers,
356
+ credentials: this._options.credentials
313
357
  });
314
358
  if (!response.ok) {
315
359
  throw new Error('');
@@ -375,7 +419,8 @@ class Wfst extends Control {
375
419
  });
376
420
  const url_fetch = geoServerUrl + '?' + params.toString();
377
421
  const response = yield fetch(url_fetch, {
378
- headers: this._options.headers
422
+ headers: this._options.headers,
423
+ credentials: this._options.credentials
379
424
  });
380
425
  if (!response.ok) {
381
426
  throw new Error('');
@@ -464,7 +509,8 @@ class Wfst extends Control {
464
509
  tileLoadFunction: (tile, src) => __awaiter(this, void 0, void 0, function* () {
465
510
  try {
466
511
  const response = yield fetch(src, {
467
- headers: this._options.headers
512
+ headers: this._options.headers,
513
+ credentials: this._options.credentials
468
514
  });
469
515
  if (!response.ok) {
470
516
  throw new Error('');
@@ -539,7 +585,8 @@ class Wfst extends Control {
539
585
  '?' +
540
586
  params.toString();
541
587
  const response = yield fetch(url_fetch, {
542
- headers: this._options.headers
588
+ headers: this._options.headers,
589
+ credentials: this._options.credentials
543
590
  });
544
591
  if (!response.ok) {
545
592
  throw new Error('');
@@ -720,7 +767,8 @@ class Wfst extends Control {
720
767
  });
721
768
  try {
722
769
  const response = yield fetch(url, {
723
- headers: this._options.headers
770
+ headers: this._options.headers,
771
+ credentials: this._options.credentials
724
772
  });
725
773
  if (!response.ok) {
726
774
  throw new Error(this._i18n.errors.getFeatures +
@@ -1088,7 +1136,8 @@ class Wfst extends Control {
1088
1136
  const url_fetch = this._options.geoServerUrl + '?' + params.toString();
1089
1137
  try {
1090
1138
  const response = yield fetch(url_fetch, {
1091
- headers: this._options.headers
1139
+ headers: this._options.headers,
1140
+ credentials: this._options.credentials
1092
1141
  });
1093
1142
  if (!response.ok) {
1094
1143
  throw new Error(this._i18n.errors.lockFeature);
@@ -1217,7 +1266,8 @@ class Wfst extends Control {
1217
1266
  }
1218
1267
  }
1219
1268
  if (!clonedFeatures.length) {
1220
- return this._showError(this._i18n.errors.noValidGeometry);
1269
+ this._showError(this._i18n.errors.noValidGeometry);
1270
+ return false;
1221
1271
  }
1222
1272
  switch (action) {
1223
1273
  case 'insert':
@@ -1241,95 +1291,111 @@ class Wfst extends Control {
1241
1291
  }
1242
1292
  this._countRequests++;
1243
1293
  const numberRequest = this._countRequests;
1244
- setTimeout(() => __awaiter(this, void 0, void 0, function* () {
1245
- try {
1246
- // Prevent fire multiples times
1247
- if (numberRequest !== this._countRequests) {
1248
- return;
1249
- }
1250
- let srs = this._view.getProjection().getCode();
1251
- // Force latitude/longitude order on transactions
1252
- // EPSG:4326 is longitude/latitude (assumption) and is not managed correctly by GML3
1253
- srs = srs === 'EPSG:4326' ? 'urn:x-ogc:def:crs:EPSG:4326' : srs;
1254
- const options = {
1255
- featureNS: this._geoServerData[layerName].namespace,
1256
- featureType: layerName,
1257
- srsName: srs,
1258
- featurePrefix: null,
1259
- nativeElements: null,
1260
- version: this._options.geoServerAdvanced
1261
- .wfsTransactionVersion
1262
- };
1263
- const transaction = this._formatWFS.writeTransaction(this._insertFeatures, this._updateFeatures, this._deleteFeatures, options);
1264
- let payload = this._xs.serializeToString(transaction);
1265
- const geomType = this._geoServerData[layerName].geomType;
1266
- const geomField = this._geoServerData[layerName].geomField;
1267
- // Ugly fix to support GeometryCollection on GML
1268
- // See https://github.com/openlayers/openlayers/issues/4220
1269
- if (geomType === GeometryType.GeometryCollection) {
1294
+ return new Promise((resolve, reject) => {
1295
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
1296
+ try {
1297
+ // Prevent fire multiples times
1298
+ if (numberRequest !== this._countRequests) {
1299
+ return;
1300
+ }
1301
+ let srs = this._view.getProjection().getCode();
1302
+ // Force latitude/longitude order on transactions
1303
+ // EPSG:4326 is longitude/latitude (assumption) and is not managed correctly by GML3
1304
+ srs =
1305
+ srs === 'EPSG:4326'
1306
+ ? 'urn:x-ogc:def:crs:EPSG:4326'
1307
+ : srs;
1308
+ const geoLayer = this._geoServerData[layerName];
1309
+ if (!geoLayer) {
1310
+ throw new Error(this._i18n.errors.layerNotFound);
1311
+ }
1312
+ const options = {
1313
+ featureNS: geoLayer.namespace,
1314
+ featureType: layerName,
1315
+ srsName: srs,
1316
+ featurePrefix: null,
1317
+ nativeElements: null,
1318
+ version: this._options.geoServerAdvanced
1319
+ .wfsTransactionVersion
1320
+ };
1321
+ const transaction = this._formatWFS.writeTransaction(this._insertFeatures, this._updateFeatures, this._deleteFeatures, options);
1322
+ let payload = this._xs.serializeToString(transaction);
1323
+ const geomType = geoLayer.geomType;
1324
+ const geomField = geoLayer.geomField;
1325
+ // Ugly fix to support GeometryCollection on GML
1326
+ // See https://github.com/openlayers/openlayers/issues/4220
1327
+ if (geomType === GeometryType.GeometryCollection) {
1328
+ if (action === 'insert') {
1329
+ payload = payload.replace(/<geometry>/g, `<geometry><MultiGeometry xmlns="http://www.opengis.net/gml" srsName="${srs}"><geometryMember>`);
1330
+ payload = payload.replace(/<\/geometry>/g, `</geometryMember></MultiGeometry></geometry>`);
1331
+ }
1332
+ else if (action === 'update') {
1333
+ const gmemberIn = `<MultiGeometry xmlns="http://www.opengis.net/gml" srsName="${srs}"><geometryMember>`;
1334
+ const gmemberOut = `</geometryMember></MultiGeometry>`;
1335
+ payload = payload.replace(/(.*)(<Name>geometry<\/Name><Value>)(.*?)(<\/Value>)(.*)/g, `$1$2${gmemberIn}$3${gmemberOut}$4$5`);
1336
+ }
1337
+ }
1338
+ // Fixes geometry name, weird bug with GML:
1339
+ // The property for the geometry column is always named "geometry"
1270
1340
  if (action === 'insert') {
1271
- payload = payload.replace(/<geometry>/g, `<geometry><MultiGeometry xmlns="http://www.opengis.net/gml" srsName="${srs}"><geometryMember>`);
1272
- payload = payload.replace(/<\/geometry>/g, `</geometryMember></MultiGeometry></geometry>`);
1341
+ payload = payload.replace(/<(\/?)\bgeometry\b>/g, `<$1${geomField}>`);
1273
1342
  }
1274
- else if (action === 'update') {
1275
- const gmemberIn = `<MultiGeometry xmlns="http://www.opengis.net/gml" srsName="${srs}"><geometryMember>`;
1276
- const gmemberOut = `</geometryMember></MultiGeometry>`;
1277
- payload = payload.replace(/(.*)(<Name>geometry<\/Name><Value>)(.*?)(<\/Value>)(.*)/g, `$1$2${gmemberIn}$3${gmemberOut}$4$5`);
1343
+ else {
1344
+ payload = payload.replace(/<Name>geometry<\/Name>/g, `<Name>${geomField}</Name>`);
1278
1345
  }
1279
- }
1280
- // Fixes geometry name, weird bug with GML:
1281
- // The property for the geometry column is always named "geometry"
1282
- if (action === 'insert') {
1283
- payload = payload.replace(/<(\/?)\bgeometry\b>/g, `<$1${geomField}>`);
1284
- }
1285
- else {
1286
- payload = payload.replace(/<Name>geometry<\/Name>/g, `<Name>${geomField}</Name>`);
1287
- }
1288
- // Add default LockId value
1289
- if (this._hasLockFeature &&
1290
- this._useLockFeature &&
1291
- action !== 'insert') {
1292
- payload = payload.replace(`</Transaction>`, `<LockId>GeoServer</LockId></Transaction>`);
1293
- }
1294
- const headers = Object.assign({ 'Content-Type': 'text/xml' }, this._options.headers);
1295
- const response = yield fetch(this._options.geoServerUrl, {
1296
- method: 'POST',
1297
- body: payload,
1298
- headers: headers
1299
- });
1300
- if (!response.ok) {
1301
- throw new Error(this._i18n.errors.transaction + ' ' + response.status);
1302
- }
1303
- const parseResponse = this._formatWFS.readTransactionResponse(response);
1304
- if (!Object.keys(parseResponse).length) {
1305
- const responseStr = yield response.text();
1306
- const findError = String(responseStr).match(/<ows:ExceptionText>([\s\S]*?)<\/ows:ExceptionText>/);
1307
- if (findError) {
1308
- this._showError(findError[1]);
1346
+ // Add default LockId value
1347
+ if (this._hasLockFeature &&
1348
+ this._useLockFeature &&
1349
+ action !== 'insert') {
1350
+ payload = payload.replace(`</Transaction>`, `<LockId>GeoServer</LockId></Transaction>`);
1309
1351
  }
1310
- }
1311
- if (action !== 'delete') {
1312
- for (const feature of features) {
1313
- this._editLayer.getSource().removeFeature(feature);
1352
+ const headers = Object.assign({ 'Content-Type': 'text/xml' }, this._options.headers);
1353
+ const response = yield fetch(this._options.geoServerUrl, {
1354
+ method: 'POST',
1355
+ body: payload,
1356
+ headers: headers,
1357
+ credentials: this._options.credentials
1358
+ });
1359
+ if (!response.ok) {
1360
+ throw new Error(this._i18n.errors.transaction +
1361
+ ' ' +
1362
+ response.status);
1314
1363
  }
1364
+ const parseResponse = this._formatWFS.readTransactionResponse(response);
1365
+ if (!Object.keys(parseResponse).length) {
1366
+ const responseStr = yield response.text();
1367
+ const findError = String(responseStr).match(/<ows:ExceptionText>([\s\S]*?)<\/ows:ExceptionText>/);
1368
+ if (findError) {
1369
+ throw new Error(findError[1]);
1370
+ }
1371
+ }
1372
+ if (action !== 'delete') {
1373
+ for (const feature of features) {
1374
+ this._editLayer.getSource().removeFeature(feature);
1375
+ }
1376
+ }
1377
+ const { mode } = this._options.layers.find((layer) => layer.name === layerName);
1378
+ if (mode === 'wfs') {
1379
+ refreshWfsLayer(this._mapLayers[layerName]);
1380
+ }
1381
+ else if (mode === 'wms') {
1382
+ refreshWmsLayer(this._mapLayers[layerName]);
1383
+ }
1384
+ this._hideLoading();
1385
+ this._insertFeatures = [];
1386
+ this._updateFeatures = [];
1387
+ this._deleteFeatures = [];
1388
+ this._countRequests = 0;
1389
+ resolve(true);
1315
1390
  }
1316
- const { mode } = this._options.layers.find((layer) => layer.name === layerName);
1317
- if (mode === 'wfs') {
1318
- refreshWfsLayer(this._mapLayers[layerName]);
1319
- }
1320
- else if (mode === 'wms') {
1321
- refreshWmsLayer(this._mapLayers[layerName]);
1391
+ catch (err) {
1392
+ this._showError(err.message, err);
1393
+ this._hideLoading();
1394
+ this._countRequests = 0;
1395
+ reject();
1322
1396
  }
1323
- this._hideLoading();
1324
- }
1325
- catch (err) {
1326
- this._showError(err.message, err);
1327
- }
1328
- this._insertFeatures = [];
1329
- this._updateFeatures = [];
1330
- this._deleteFeatures = [];
1331
- this._countRequests = 0;
1332
- }), 0);
1397
+ }), 0);
1398
+ });
1333
1399
  });
1334
1400
  }
1335
1401
  /**
@@ -2059,15 +2125,19 @@ class Wfst extends Control {
2059
2125
  this.interactionWfsSelect.setActive(bool);
2060
2126
  }
2061
2127
  /**
2128
+ *
2062
2129
  * Add features directly to the geoserver, in a custom layer
2063
2130
  * without checking geometry or showing modal to confirm.
2131
+ * Returns `true` if features are inserted correctly
2064
2132
  *
2065
2133
  * @param layerName
2066
2134
  * @param features
2067
- * @public
2135
+ * @returns
2068
2136
  */
2069
2137
  insertFeaturesTo(layerName, features) {
2070
- this._transactWFS('insert', features, layerName);
2138
+ return __awaiter(this, void 0, void 0, function* () {
2139
+ return yield this._transactWFS('insert', features, layerName);
2140
+ });
2071
2141
  }
2072
2142
  /**
2073
2143
  * Shows a fields form in a modal window to allow changes in the properties of the feature.