summaryrefslogtreecommitdiff
path: root/misc/openlayers/examples/snap-split.html
blob: 13306cceab27e7693091f26cf876d305eedddb5c (plain)
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
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>Snapping &amp; Splitting</title>
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
    <!--[if lte IE 6]>
        <link rel="stylesheet" href="../theme/default/ie6-style.css" type="text/css" />
    <![endif]-->
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        .olControlEditingToolbar .olControlModifyFeatureItemInactive { 
            background-position: -1px -1px;
        }
        .olControlEditingToolbar .olControlModifyFeatureItemActive { 
            background-position: -1px -24px;
        }
        label.head {
            font-weight: bold;
            padding: 1em 0 0.1em 0;
            border-bottom: 1px solid gray;
        }
        td {
            padding: 0.25em 1em;
        }
        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, split, vectors;
        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: "#333333"
                                },
                                "Line": {
                                    strokeWidth: 3,
                                    strokeOpacity: 1,
                                    strokeColor: "#666666"
                                }
                            }
                        })
                    ]
                }),
                "select": new OpenLayers.Style({
                    strokeColor: "#00ccff",
                    strokeWidth: 4
                }),
                "temporary": new OpenLayers.Style(null, {
                    rules: [
                        new OpenLayers.Rule({
                            symbolizer: {
                                "Point": {
                                    pointRadius: 5,
                                    graphicName: "square",
                                    fillColor: "white",
                                    fillOpacity: 0.25,
                                    strokeWidth: 1,
                                    strokeOpacity: 1,
                                    strokeColor: "#333333"
                                },
                                "Line": {
                                    strokeWidth: 3,
                                    strokeOpacity: 1,
                                    strokeColor: "#00ccff"
                                }
                            }
                        })
                    ]
                })
            });

            // allow testing of specific renderers via "?renderer=Canvas", etc
            var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
            renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;

            // create three vector layers
            vectors = new OpenLayers.Layer.Vector("Lines", {
                isBaseLayer: true,
                strategies: [new OpenLayers.Strategy.Fixed()],                
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "data/roads.json",
                    format: new OpenLayers.Format.GeoJSON()
                }),
                styleMap: styles,
                maxExtent: new OpenLayers.Bounds(
                    1549471.9221, 6403610.94, 1550001.32545, 6404015.8
                ),
                renderers: renderer
            });
            map.addLayer(vectors);
            
            // configure the snapping agent
            snap = new OpenLayers.Control.Snapping({layer: vectors});
            map.addControl(snap);
            snap.activate();
            
            // configure split agent
            split = new OpenLayers.Control.Split({
                layer: vectors,
                source: vectors,
                tolerance: 0.0001,
                eventListeners: {
                    aftersplit: function(event) {
                        flashFeatures(event.features);
                    }
                }
            });
            map.addControl(split);
            split.activate();

            // add some editing tools to a panel
            var panel = new OpenLayers.Control.Panel({
                displayClass: "olControlEditingToolbar"
            });
            draw = new OpenLayers.Control.DrawFeature(
                vectors, OpenLayers.Handler.Path,
                {displayClass: "olControlDrawFeaturePoint", title: "Draw Features"}
            );
            modify = new OpenLayers.Control.ModifyFeature(
                vectors, {displayClass: "olControlModifyFeature", title: "Modify Features"}
            );
            panel.addControls([
                new OpenLayers.Control.Navigation({title: "Navigate"}),
                draw, modify
            ]);
            map.addControl(panel);
            
            map.addControl(new OpenLayers.Control.MousePosition());
            
            map.zoomToMaxExtent();
        }

        function flashFeatures(features, index) {
            if(!index) {
                index = 0;
            }
            var current = features[index];
            if(current && current.layer === vectors) {
                vectors.drawFeature(features[index], "select");
            }
            var prev = features[index-1];
            if(prev && prev.layer === vectors) {
                vectors.drawFeature(prev, "default");
            }
            ++index;
            if(index <= features.length) {
                window.setTimeout(function() {flashFeatures(features, index)}, 75);
            }
        }

        /**
         * 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() {
            // add behavior to snap elements
            var snapCheck = document.getElementById("snap_toggle");
            snapCheck.checked = true;
            snapCheck.onclick = function() {
                if(snapCheck.checked) {
                    snap.activate();
                    document.getElementById("snap_options").style.display = "block";
                } else {
                    snap.deactivate();
                    document.getElementById("snap_options").style.display = "none";
                }
            };
            var target, type, tog, tol;
            var types = ["node", "vertex", "edge"];
            var target = snap.targets[0];
            for(var j=0; j<types.length; ++j) {
                type = types[j];
                tog = document.getElementById("target_" + type);
                tog.checked = target[type];
                tog.onclick = (function(tog, type, target) {
                    return function() {target[type] = tog.checked;}
                })(tog, type, target);
                tol = document.getElementById("target_" + 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);
            }

            // add behavior to split elements
            var splitCheck = document.getElementById("split_toggle");
            splitCheck.checked = true;
            splitCheck.onclick = function() {
                if(splitCheck.checked) {
                    split.activate();
                    document.getElementById("split_options").style.display = "block";
                } else {
                    split.deactivate();
                    document.getElementById("split_options").style.display = "none";
                }
            };
            var edgeCheck = document.getElementById("edge_toggle");
            edgeCheck.checked = split.edge;
            edgeCheck.onclick = function() {
                split.edge = edgeCheck.checked;
            };
            
            document.getElementById("clear").onclick = function() {
                modify.deactivate();
                vectors.destroyFeatures();
            };
            
        }
    
    </script>
  </head>
  <body onload="init()">
    <h1 id="title">Snapping & Splitting Example</h1>
    <div id="tags">
        vector, feature, splitting, snapping, stylemap, advanced
    </div>    
    <div id="shortdesc">A demonstration snapping and splitting while editing vector features.</div>
    <div id="map" class="smallmap"></div>
    <br>
    <input type="checkbox" id="snap_toggle" />
    <label for="snap_toggle" class="head">Enable Snapping</label>
    <table id="snap_options">
        <tbody>
            <tr class="head">
                <td>target</td><td>node</td><td>vertex</td><td>edge</td>
            </tr>
            <tr>
                <td>roads</td>
                <td><input type="checkbox" id="target_node" /><input id="target_nodeTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="target_vertex" /><input id="target_vertexTolerance" type="text" size="3" /></td>
                <td><input type="checkbox" id="target_edge" /><input id="target_edgeTolerance" type="text" size="3" /></td>
            </tr>
        </tbody>
    </table>
    <br>
    <input type="checkbox" id="split_toggle" />
    <label for="split_toggle" class="head">Enable Splitting</label>
    <table id="split_options">
        <tbody>
            <tr>
                <td><label for="edge_toggle">edges split</label></td>
                <td><input type="checkbox" id="edge_toggle" /></td>
            </tr>
        </tbody>
    </table>
    <br>
    <button id="clear">clear</button> Clear all features.
  </body>
</html>