MultiLineString.js 10 KB

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