Mercurial > pub > ImplabJs
comparison src/djol/WFSSource.js @ 34:27e8e9e38e07 default tip
Слияние
author | nickolay |
---|---|
date | Wed, 05 Jun 2019 20:44:15 +0300 |
parents | 8af8e840dd49 1dc2fd263b90 |
children |
comparison
equal
deleted
inserted
replaced
33:8af8e840dd49 | 34:27e8e9e38e07 |
---|---|
1 define([ | |
2 "ol", | |
3 "dojo/request", | |
4 "dojo/_base/array", | |
5 "implab/safe", | |
6 "implab/log/trace!" | |
7 ], function (ol, | |
8 request, | |
9 array, | |
10 safe, | |
11 trace | |
12 ) { | |
13 return function (wfs, featurePrefix, featureType, featureNS, queryArgs) { | |
14 if (arguments.length == 1) { | |
15 featurePrefix = wfs.featurePrefix; | |
16 featureType = wfs.featureType; | |
17 featureNS = wfs.featureNS; | |
18 wfs = wfs.wfsURL; | |
19 queryArgs = wfs.queryArgs; | |
20 } | |
21 | |
22 safe.argumentNotNull(wfs, "wfsURL"); | |
23 safe.argumentNotEmptyString(featurePrefix, "featurePrefix"); | |
24 safe.argumentNotEmptyString(featureNS, "featureNS"); | |
25 safe.argumentNotEmptyString(featureType, "featureType"); | |
26 | |
27 var format = new ol.format.WFS({ | |
28 featureNS: featureNS, | |
29 featureType: featureType | |
30 }); | |
31 | |
32 var layerName = featurePrefix + ":" + featureType; | |
33 | |
34 function loader(extent, resolution, projection) { | |
35 var query = { | |
36 service: 'WFS', | |
37 version: '1.1.0', | |
38 request: 'GetFeature', | |
39 typename: layerName, | |
40 srsname: projection.getCode() | |
41 }; | |
42 safe.mixin(query, queryArgs); | |
43 | |
44 if (extent && isFinite(extent[0])) | |
45 query.bbox = extent.join(',') + "," + projection.getCode(); | |
46 | |
47 return request(wfs, { | |
48 query: query, | |
49 handleAs: 'xml' | |
50 }).then(function (data) { | |
51 // в загрузчике нельзя вызывать метод source.clear() поскольку | |
52 // это приводит к рекурсии | |
53 var features = format.readFeatures(data); | |
54 | |
55 var map = {}, | |
56 del = [], | |
57 add = []; | |
58 | |
59 array.forEach(features, function (x) { | |
60 // HACK исправляем идентификаторы, чтобы они совпадали с | |
61 // реальными | |
62 | |
63 var id = x.get("id"); | |
64 if (id) | |
65 x.setId(id); | |
66 else | |
67 id = x.getId(); | |
68 | |
69 map[id] = x; | |
70 | |
71 // нужно проверить, была ли фича на карте | |
72 var prev = source.getFeatureById(id); | |
73 if (prev) { | |
74 // если да, то обновить ее. | |
75 var data = x.getProperties(); | |
76 prev.setProperties(data); | |
77 } else { | |
78 // иначе добавить | |
79 add.push(x); | |
80 } | |
81 }); | |
82 | |
83 source.forEachFeatureInExtent(extent, function (x) { | |
84 if (!(x.getId() in map)) | |
85 del.push(x); | |
86 }); | |
87 | |
88 source.addFeatures(add); | |
89 | |
90 array.forEach(del, function (x) { | |
91 source.removeFeature(x); | |
92 }); | |
93 | |
94 //revision = revision + 1; | |
95 | |
96 source.set("revision", ++revision); | |
97 | |
98 }); | |
99 } | |
100 | |
101 var cls = ol.source.ServerVector || ol.source.Vector; | |
102 var revision = 0; | |
103 var source = new cls({ | |
104 loader: loader, | |
105 //revision: revision | |
106 wrapX: false | |
107 // , | |
108 // strategy : options.strategy || ol.loadingstrategy.all, | |
109 // projection : options.projection | |
110 }); | |
111 source.set("revision", revision); | |
112 source.reload = function (extent, resolution, projection, q) { | |
113 if (arguments.length >= 4) | |
114 queryArgs = q; | |
115 if (!extent) | |
116 extent = [-Infinity, -Infinity, Infinity, Infinity]; | |
117 return loader(extent, resolution, projection); | |
118 }; | |
119 | |
120 trace.log("{0} -> {1}", layerName, wfs); | |
121 return source; | |
122 }; | |
123 }); |