forked from 3liz/lizmap-javascript-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activateSnapWhenEditing.js
135 lines (113 loc) · 4.59 KB
/
activateSnapWhenEditing.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
lizSnapEdition = function(){
// Options
var snapLayerName = '';
var snapLayers = {
'Quartiers' : {
'layers': [
'SousQuartiers'
],
snapToNode: true,
snapToEdge: false,
snapToVertex: true
},
'SousQuartiers' : {
'layers': [
'Quartiers'
],
snapToNode: true,
snapToEdge: false,
snapToVertex: true
}
};
var snapRestrictToMapExtent = true;
var snapMaxFeatures = 1000;
var snapTolerance = 40;
var snapToNode = true;
var snapToVertex = true;
var snapToEdge = false;
var snapColor = 'red';
lizMap.events.on({
'uicreated':function(evt){
// Create layer to store snap features
lizMap.map.addLayer(new OpenLayers.Layer.Vector('snaplayer',{
styleMap: new OpenLayers.StyleMap({
pointRadius: 2,
fill: false,
stroke: true,
strokeWidth: 3,
strokeColor: snapColor,
strokeOpacity: 0.8
})
}));
var snapLayer = lizMap.map.getLayersByName('snaplayer')[0];
var editionLayer = lizMap.map.getLayersByName('editLayer')[0];
var snapControl = new OpenLayers.Control.Snapping({
layer: editionLayer,
targets: [{
layer: snapLayer,
snapTolerance: 15,
node: snapToNode,
vertex: snapToVertex,
edge: snapToEdge
}]
});
lizMap.map.addControls([snapControl]);
lizMap.controls['snapControl'] = snapControl;
},
'lizmapeditionformdisplayed': function(evt){
// Get editing layer config
var getLayerConfig = lizMap.getLayerConfigById(evt['layerId']);
// verifiying config
if ( !getLayerConfig )
return true;
var layerConfig = getLayerConfig[1];
var featureType = getLayerConfig[0];
// verifying editing layer has snap config
if ( !(featureType in snapLayers) )
return true;
var getSnapLayer = snapLayers[featureType]['layers'];
// Get features for the current extent
// Max 1000 features
var snapLayer = lizMap.map.getLayersByName('snaplayer')[0];
snapLayer.destroyFeatures();
for(var i=0; i<getSnapLayer.length; i++){
snapLayerName = getSnapLayer[i];
lizMap.getFeatureData(snapLayerName, null, null, 'geom', snapRestrictToMapExtent, null, snapMaxFeatures,
function(fName, fFilter, fFeatures, fAliases) {
// Transform features
var snapLayerConfig = lizMap.config.layers[snapLayerName];
var snapLayerCrs = snapLayerConfig['featureCrs'];
if(!snapLayerCrs)
snapLayerCrs = snapLayerConfig['crs'];
var gFormat = new OpenLayers.Format.GeoJSON({
ignoreExtraDims: true,
externalProjection: snapLayerCrs,
internalProjection: lizMap.map.getProjection()
});
var tfeatures = gFormat.read( {
type: 'FeatureCollection',
features: fFeatures
} );
// Add features
var snapLayer = lizMap.map.getLayersByName('snaplayer')[0];
snapLayer.addFeatures( tfeatures );
// Activate snapping
var snapControl = lizMap.controls.snapControl;
snapControl.deactivate();
snapControl.targets[0].edge = snapLayers[featureType].snapToEdge;
snapControl.targets[0].node = snapLayers[featureType].snapToNode;
snapControl.targets[0].vertex = snapLayers[featureType].snapToVertex;
snapControl.activate();
return false;
});
}
},
'lizmapeditionformclosed': function(evt){
var snapControl = lizMap.map.getControlsByClass('OpenLayers.Control.Snapping')[0];
snapControl.deactivate();
var snapLayer = lizMap.map.getLayersByName('snaplayer')[0];
snapLayer.destroyFeatures();
}
});
return true;
}();