Merge branch 'master' of ssh://apples.lambdacomplex.org/git/bus
[bus.git] / openlayers / examples / snapping.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Snapping</title>
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <style type="text/css">
        .olControlEditingToolbar .olControlModifyFeatureItemInactive { 
            background-position: -1px 0px ;                                                                   
        }
        .olControlEditingToolbar .olControlModifyFeatureItemActive { 
            background-position: -1px -23px ;                                                                   
        }
        table {
            padding: 1em 0 1em;
        }
        td {
            padding: 0.5em 1em;
            border: 1px solid #ddd;
        }
        tr.head td {
            text-align: center;
            font-weight: bold;
        }
    </style>
    <script src="../lib/Firebug/firebug.js"></script>
    <script src="../lib/OpenLayers.js"></script>
    <script type="text/javascript">
        OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
 
        function init() {
            initMap();
            initUI();
        }
        
        var map, draw, modify, snap, point, line, poly;
        function initMap() {
 
            map = new OpenLayers.Map('map');
            var styles = new OpenLayers.StyleMap({
                "default": new OpenLayers.Style(null, {
                    rules: [
                        new OpenLayers.Rule({
                            symbolizer: {
                                "Point": {
                                    pointRadius: 5,
                                    graphicName: "square",
                                    fillColor: "white",
                                    fillOpacity: 0.25,
                                    strokeWidth: 1,
                                    strokeOpacity: 1,
                                    strokeColor: "#3333aa"
                                },
                                "Line": {
                                    strokeWidth: 3,
                                    strokeOpacity: 1,
                                    strokeColor: "#6666aa"
                                },
                                "Polygon": {
                                    strokeWidth: 1,
                                    strokeOpacity: 1,
                                    fillColor: "#9999aa",
                                    strokeColor: "#6666aa"
                                }
                            }
                        })
                    ]
                }),
                "select": new OpenLayers.Style(null, {
                    rules: [
                        new OpenLayers.Rule({
                            symbolizer: {
                                "Point": {
                                    pointRadius: 5,
                                    graphicName: "square",
                                    fillColor: "white",
                                    fillOpacity: 0.25,
                                    strokeWidth: 2,
                                    strokeOpacity: 1,
                                    strokeColor: "#0000ff"
                                },
                                "Line": {
                                    strokeWidth: 3,
                                    strokeOpacity: 1,
                                    strokeColor: "#0000ff"
                                },
                                "Polygon": {
                                    strokeWidth: 2,
                                    strokeOpacity: 1,
                                    fillColor: "#0000ff",
                                    strokeColor: "#0000ff"
                                }
                            }
                        })
                    ]
                }),
                "temporary": new OpenLayers.Style(null, {
                    rules: [
                        new OpenLayers.Rule({
                            symbolizer: {
                                "Point": {
                                    graphicName: "square",
                                    pointRadius: 5,
                                    fillColor: "white",
                                    fillOpacity: 0.25,
                                    strokeWidth: 2,
                                    strokeColor: "#0000ff"
                                },
                                "Line": {
                                    strokeWidth: 3,
                                    strokeOpacity: 1,
                                    strokeColor: "#0000ff"
                                },
                                "Polygon": {
                                    strokeWidth: 2,
                                    strokeOpacity: 1,
                                    strokeColor: "#0000ff",
                                    fillColor: "#0000ff"
                                }
                            }
                        })
                    ]
                })
            });
 
            // create three vector layers
            poly = new OpenLayers.Layer.Vector("polygons", {
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/poly.json",
                    format: new OpenLayers.Format.GeoJSON()
                }),
                styleMap: styles,
                isBaseLayer: true
            });
            line = new OpenLayers.Layer.Vector("lines", {
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/line.json",
                    format: new OpenLayers.Format.GeoJSON()
                }),
                styleMap: styles
            });
            point = new OpenLayers.Layer.Vector("points", {
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/point.json",
                    format: new OpenLayers.Format.GeoJSON()
                }),
                styleMap: styles
            });
            map.addLayers([poly, line, point]);
            
            // configure the snapping agent
            snap = new OpenLayers.Control.Snapping({
                layer: poly,
                targets: [point, line, poly],
                greedy: false
            });
            snap.activate();
 
            // add some editing tools to a panel
            var panel = new OpenLayers.Control.Panel({
                displayClass: "olControlEditingToolbar"
            });
            draw = new OpenLayers.Control.DrawFeature(
                poly, OpenLayers.Handler.Polygon,
                {displayClass: "olControlDrawFeaturePoint", title: "Draw Features"}
            );
            modify = new OpenLayers.Control.ModifyFeature(
                poly, {displayClass: "olControlModifyFeature", title: "Modify Features"}
            );            
            panel.addControls([
                new OpenLayers.Control.Navigation({title: "Navigate"}),
                draw, modify
            ]);
            map.addControl(panel);
            
            // give the map a location
            map.setCenter(new OpenLayers.LonLat(0, 0), 1);
        }
        
        /**
         * Add behavior to page elements.  This basically lets us set snapping
         * target properties with the checkboxes and text inputs.  The checkboxes
         * toggle the target node, vertex, or edge (boolean) values.  The
         * text inputs set the nodeTolerance, vertexTolerance, or edgeTolerance
         * property values.
         */
        function initUI() {
            var check = $("snapping");
            check.checked = true;
            check.onclick = function() {
                if(check.checked) {
                    snap.activate();
                } else {
                    snap.deactivate();
                }
            };
            
            var sel = $("editable");
            sel.value = "poly";
            sel.onchange = function() {
                updateEditable(sel.value);
            }
            
            var target, type, tog, tol;
            var types = ["node", "vertex", "edge"];
            for(var i=0; i<snap.targets.length; ++i) {
                target = snap.targets[i];
                for(var j=0; j<types.length; ++j) {
                    type = types[j];
                    tog = $(i + "_" + type);
                    tog.checked = target[type];
                    tog.onclick = (function(tog, type, target) {
                        return function() {target[type] = tog.checked;}
                    })(tog, type, target);
                    tol = $(i + "_" + type + "Tolerance");
                    tol.value = target[type + "Tolerance"];
                    tol.onchange = (function(tol, type, target) {
                        return function() {
                            target[type + "Tolerance"] = Number(tol.value) || 0;
                        }
                    })(tol, type, target);
                }
            }
            
        }
        
        // this function allows the editable layer to be changed
        // for the snapping control, this amounts to calling setLayer
        function updateEditable(name) {
 
            layer = window[name];
            
            // update the editable layer for the snapping control (nice)
            snap.setLayer(layer);
 
            // update the editable layer for the modify control (ugly)
            var modActive = modify.active;
            if(modActive) {
                modify.deactivate();
            }
            modify.layer = layer;
            modify.selectControl.layer = layer;
            modify.selectControl.handlers.feature.layer = layer;
            modify.dragControl.layer = layer;
            modify.dragControl.handlers.drag.layer = layer;
            modify.dragControl.handlers.feature.layer = layer;
            if(modActive) {
                modify.activate();
            }
            
            // update the editable layer for the draw control (very ugly)
            var drawActive = draw.active;
            if(drawActive) {
                draw.deactivate();
            }
            draw.layer = layer;
            var handler = ({
                point: OpenLayers.Handler.Point,
                line: OpenLayers.Handler.Path,
                poly: OpenLayers.Handler.Polygon
            })[name];
            draw.handler = new handler(draw, draw.callbacks, draw.handlerOptions);
            if(drawActive) {
                draw.activate();
            }
 
        }
 
    </script>
  </head>
  <body onload="init()">
    <h1 id="title">Snapping Example</h1>
    <div id="shortdesc">A demonstration snapping while editing vector features.</div>
    <div id="map" class="smallmap"></div>
    <br/>
    <label for="editable">Editable Layer:</label>
    <select id="editable" name="editable">
        <option value="poly">polygons</option>
        <option value="line">lines</option>
        <option value="point">points</option>
    </select>
    <label for="snapping">Enable Snapping</label>
    <input type="checkbox" name="snapping" id="snapping" checked="checked" />
    <table>
        <tbody>
            <tr class="head">
                <td>targets</td><td>node</td><td>vertex</td><td>edge</td>
            </tr>
            <tr>
                <td>points</td>
                <td><input type="checkbox" id="0_node" /><input id="0_nodeTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="0_vertex" /><input id="0_vertexTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="0_edge" /><input id="0_edgeTolerance" type="text" size="3" /></td>
            </tr>
            <tr>
                <td>lines</td>
                <td><input type="checkbox" id="1_node" /><input id="1_nodeTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="1_vertex" /><input id="1_vertexTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="1_edge" /><input id="1_edgeTolerance" type="text" size="3" /></td>
            </tr>
            <tr>
                <td>polygons</td>
                <td><input type="checkbox" id="2_node" /><input id="2_nodeTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="2_vertex" /><input id="2_vertexTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="2_edge" /><input id="2_edgeTolerance" type="text" size="3" /></td>
            </tr>
        </tbody>
    </table>
    <p>Though all snapping types are shown here for all target layers, not all are sensible.
    Points don't have edges, for example.</p>
  </body>
</html>