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