1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Geometry/Collection.js |
---|
8 | * @requires OpenLayers/Geometry/LineString.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Geometry.MultiLineString |
---|
13 | * A MultiLineString is a geometry with multiple <OpenLayers.Geometry.LineString> |
---|
14 | * components. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Geometry.Collection> |
---|
18 | * - <OpenLayers.Geometry> |
---|
19 | */ |
---|
20 | OpenLayers.Geometry.MultiLineString = OpenLayers.Class( |
---|
21 | OpenLayers.Geometry.Collection, { |
---|
22 | |
---|
23 | /** |
---|
24 | * Property: componentTypes |
---|
25 | * {Array(String)} An array of class names representing the types of |
---|
26 | * components that the collection can include. A null value means the |
---|
27 | * component types are not restricted. |
---|
28 | */ |
---|
29 | componentTypes: ["OpenLayers.Geometry.LineString"], |
---|
30 | |
---|
31 | /** |
---|
32 | * Constructor: OpenLayers.Geometry.MultiLineString |
---|
33 | * Constructor for a MultiLineString Geometry. |
---|
34 | * |
---|
35 | * Parameters: |
---|
36 | * components - {Array(<OpenLayers.Geometry.LineString>)} |
---|
37 | * |
---|
38 | */ |
---|
39 | initialize: function(components) { |
---|
40 | OpenLayers.Geometry.Collection.prototype.initialize.apply(this, |
---|
41 | arguments); |
---|
42 | }, |
---|
43 | |
---|
44 | /** |
---|
45 | * Method: split |
---|
46 | * Use this geometry (the source) to attempt to split a target geometry. |
---|
47 | * |
---|
48 | * Parameters: |
---|
49 | * target - {<OpenLayers.Geometry>} The target geometry. |
---|
50 | * options - {Object} Properties of this object will be used to determine |
---|
51 | * how the split is conducted. |
---|
52 | * |
---|
53 | * Valid options: |
---|
54 | * mutual - {Boolean} Split the source geometry in addition to the target |
---|
55 | * geometry. Default is false. |
---|
56 | * edge - {Boolean} Allow splitting when only edges intersect. Default is |
---|
57 | * true. If false, a vertex on the source must be within the tolerance |
---|
58 | * distance of the intersection to be considered a split. |
---|
59 | * tolerance - {Number} If a non-null value is provided, intersections |
---|
60 | * within the tolerance distance of an existing vertex on the source |
---|
61 | * will be assumed to occur at the vertex. |
---|
62 | * |
---|
63 | * Returns: |
---|
64 | * {Array} A list of geometries (of this same type as the target) that |
---|
65 | * result from splitting the target with the source geometry. The |
---|
66 | * source and target geometry will remain unmodified. If no split |
---|
67 | * results, null will be returned. If mutual is true and a split |
---|
68 | * results, return will be an array of two arrays - the first will be |
---|
69 | * all geometries that result from splitting the source geometry and |
---|
70 | * the second will be all geometries that result from splitting the |
---|
71 | * target geometry. |
---|
72 | */ |
---|
73 | split: function(geometry, options) { |
---|
74 | var results = null; |
---|
75 | var mutual = options && options.mutual; |
---|
76 | var splits, sourceLine, sourceLines, sourceSplit, targetSplit; |
---|
77 | var sourceParts = []; |
---|
78 | var targetParts = [geometry]; |
---|
79 | for(var i=0, len=this.components.length; i<len; ++i) { |
---|
80 | sourceLine = this.components[i]; |
---|
81 | sourceSplit = false; |
---|
82 | for(var j=0; j < targetParts.length; ++j) { |
---|
83 | splits = sourceLine.split(targetParts[j], options); |
---|
84 | if(splits) { |
---|
85 | if(mutual) { |
---|
86 | sourceLines = splits[0]; |
---|
87 | for(var k=0, klen=sourceLines.length; k<klen; ++k) { |
---|
88 | if(k===0 && sourceParts.length) { |
---|
89 | sourceParts[sourceParts.length-1].addComponent( |
---|
90 | sourceLines[k] |
---|
91 | ); |
---|
92 | } else { |
---|
93 | sourceParts.push( |
---|
94 | new OpenLayers.Geometry.MultiLineString([ |
---|
95 | sourceLines[k] |
---|
96 | ]) |
---|
97 | ); |
---|
98 | } |
---|
99 | } |
---|
100 | sourceSplit = true; |
---|
101 | splits = splits[1]; |
---|
102 | } |
---|
103 | if(splits.length) { |
---|
104 | // splice in new target parts |
---|
105 | splits.unshift(j, 1); |
---|
106 | Array.prototype.splice.apply(targetParts, splits); |
---|
107 | break; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | if(!sourceSplit) { |
---|
112 | // source line was not hit |
---|
113 | if(sourceParts.length) { |
---|
114 | // add line to existing multi |
---|
115 | sourceParts[sourceParts.length-1].addComponent( |
---|
116 | sourceLine.clone() |
---|
117 | ); |
---|
118 | } else { |
---|
119 | // create a fresh multi |
---|
120 | sourceParts = [ |
---|
121 | new OpenLayers.Geometry.MultiLineString( |
---|
122 | sourceLine.clone() |
---|
123 | ) |
---|
124 | ]; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | if(sourceParts && sourceParts.length > 1) { |
---|
129 | sourceSplit = true; |
---|
130 | } else { |
---|
131 | sourceParts = []; |
---|
132 | } |
---|
133 | if(targetParts && targetParts.length > 1) { |
---|
134 | targetSplit = true; |
---|
135 | } else { |
---|
136 | targetParts = []; |
---|
137 | } |
---|
138 | if(sourceSplit || targetSplit) { |
---|
139 | if(mutual) { |
---|
140 | results = [sourceParts, targetParts]; |
---|
141 | } else { |
---|
142 | results = targetParts; |
---|
143 | } |
---|
144 | } |
---|
145 | return results; |
---|
146 | }, |
---|
147 | |
---|
148 | /** |
---|
149 | * Method: splitWith |
---|
150 | * Split this geometry (the target) with the given geometry (the source). |
---|
151 | * |
---|
152 | * Parameters: |
---|
153 | * geometry - {<OpenLayers.Geometry>} A geometry used to split this |
---|
154 | * geometry (the source). |
---|
155 | * options - {Object} Properties of this object will be used to determine |
---|
156 | * how the split is conducted. |
---|
157 | * |
---|
158 | * Valid options: |
---|
159 | * mutual - {Boolean} Split the source geometry in addition to the target |
---|
160 | * geometry. Default is false. |
---|
161 | * edge - {Boolean} Allow splitting when only edges intersect. Default is |
---|
162 | * true. If false, a vertex on the source must be within the tolerance |
---|
163 | * distance of the intersection to be considered a split. |
---|
164 | * tolerance - {Number} If a non-null value is provided, intersections |
---|
165 | * within the tolerance distance of an existing vertex on the source |
---|
166 | * will be assumed to occur at the vertex. |
---|
167 | * |
---|
168 | * Returns: |
---|
169 | * {Array} A list of geometries (of this same type as the target) that |
---|
170 | * result from splitting the target with the source geometry. The |
---|
171 | * source and target geometry will remain unmodified. If no split |
---|
172 | * results, null will be returned. If mutual is true and a split |
---|
173 | * results, return will be an array of two arrays - the first will be |
---|
174 | * all geometries that result from splitting the source geometry and |
---|
175 | * the second will be all geometries that result from splitting the |
---|
176 | * target geometry. |
---|
177 | */ |
---|
178 | splitWith: function(geometry, options) { |
---|
179 | var results = null; |
---|
180 | var mutual = options && options.mutual; |
---|
181 | var splits, targetLine, sourceLines, sourceSplit, targetSplit, sourceParts, targetParts; |
---|
182 | if(geometry instanceof OpenLayers.Geometry.LineString) { |
---|
183 | targetParts = []; |
---|
184 | sourceParts = [geometry]; |
---|
185 | for(var i=0, len=this.components.length; i<len; ++i) { |
---|
186 | targetSplit = false; |
---|
187 | targetLine = this.components[i]; |
---|
188 | for(var j=0; j<sourceParts.length; ++j) { |
---|
189 | splits = sourceParts[j].split(targetLine, options); |
---|
190 | if(splits) { |
---|
191 | if(mutual) { |
---|
192 | sourceLines = splits[0]; |
---|
193 | if(sourceLines.length) { |
---|
194 | // splice in new source parts |
---|
195 | sourceLines.unshift(j, 1); |
---|
196 | Array.prototype.splice.apply(sourceParts, sourceLines); |
---|
197 | j += sourceLines.length - 2; |
---|
198 | } |
---|
199 | splits = splits[1]; |
---|
200 | if(splits.length === 0) { |
---|
201 | splits = [targetLine.clone()]; |
---|
202 | } |
---|
203 | } |
---|
204 | for(var k=0, klen=splits.length; k<klen; ++k) { |
---|
205 | if(k===0 && targetParts.length) { |
---|
206 | targetParts[targetParts.length-1].addComponent( |
---|
207 | splits[k] |
---|
208 | ); |
---|
209 | } else { |
---|
210 | targetParts.push( |
---|
211 | new OpenLayers.Geometry.MultiLineString([ |
---|
212 | splits[k] |
---|
213 | ]) |
---|
214 | ); |
---|
215 | } |
---|
216 | } |
---|
217 | targetSplit = true; |
---|
218 | } |
---|
219 | } |
---|
220 | if(!targetSplit) { |
---|
221 | // target component was not hit |
---|
222 | if(targetParts.length) { |
---|
223 | // add it to any existing multi-line |
---|
224 | targetParts[targetParts.length-1].addComponent( |
---|
225 | targetLine.clone() |
---|
226 | ); |
---|
227 | } else { |
---|
228 | // or start with a fresh multi-line |
---|
229 | targetParts = [ |
---|
230 | new OpenLayers.Geometry.MultiLineString([ |
---|
231 | targetLine.clone() |
---|
232 | ]) |
---|
233 | ]; |
---|
234 | } |
---|
235 | |
---|
236 | } |
---|
237 | } |
---|
238 | } else { |
---|
239 | results = geometry.split(this); |
---|
240 | } |
---|
241 | if(sourceParts && sourceParts.length > 1) { |
---|
242 | sourceSplit = true; |
---|
243 | } else { |
---|
244 | sourceParts = []; |
---|
245 | } |
---|
246 | if(targetParts && targetParts.length > 1) { |
---|
247 | targetSplit = true; |
---|
248 | } else { |
---|
249 | targetParts = []; |
---|
250 | } |
---|
251 | if(sourceSplit || targetSplit) { |
---|
252 | if(mutual) { |
---|
253 | results = [sourceParts, targetParts]; |
---|
254 | } else { |
---|
255 | results = targetParts; |
---|
256 | } |
---|
257 | } |
---|
258 | return results; |
---|
259 | }, |
---|
260 | |
---|
261 | CLASS_NAME: "OpenLayers.Geometry.MultiLineString" |
---|
262 | }); |
---|