comparison src/djol/WFSSource.js @ 24:f750c89976d3

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