Hello,<div><br></div><div>I am sharing bellow with you a function that I made for a problem there I step on it. </div><div>Using the DrawPolygon feature of OpenLayers, I had a restriction to the polygon never be twisted. Twisted, we call when a line of a polygon cross another line at the same polygon, creating something that appears to be like 2 or more polygons with ends adjacents, but they are only one. </div>
<div><br></div><div>There is the .intersects() function in OpenLayers to check if two Geometries are intersecting each other, but not apply to this case. So I had to deconstruct the polygon in lines, to check line by line. </div>
<div><br></div><div>The function code:</div><div><br></div><div><font face="'courier new', monospace"><div> </div><div> /**</div><div> * Author: Fernando Norte (<a href="http://fnorte.org">http://fnorte.org</a>)</div>
<div> * description: check if the lines of a polygon are intersecting</div><div> * Returns: TRUE if intersects | FALSE don't </div><div> */</div><div> </div><div> // Variables are globals to debug at firebug.console</div>
<div> var fauxpoint, point, line, vi, vj;</div><div> </div><div> function checkPolygonIntersect(){</div><div> // Get the polygon (in my case there is only one, but u can put the 'led' as parameter)</div>
<div> led = map.layers[1].features[0];</div><div> strng = led.geometry.toString(); // Convert to string</div><div> coord = strng.split(','); // split the coordinates</div><div> coord[0] = coord[0].substr(9); // remove the 'Polygon((' from the 1st coord</div>
<div> coord[coord.length-1] = coord[coord.length-1].substr(0,coord[coord.length-1].length-2); // Remove the '))' from the last coord</div><div> </div><div> //convert to lines</div><div> for (i=0;i<coord.length;i++) {</div>
<div> lonlat = coord[i].split(' '); // split coord in lonlat.array</div><div> fauxpoint[i] = new OpenLayers.Geometry.Point(lonlat[0], lonlat[1]); // convert to a point</div><div> if (i>0) {</div>
<div> // create an array with the 2 last points</div><div> point[0] = fauxpoint[i-1];</div><div> point[1] = fauxpoint[i]; </div><div> </div><div> line[i] = new OpenLayers.Geometry.LineString(point); // create the line</div>
<div> </div><div> }</div><div> }</div><div> var bool = false; // boolean variable</div><div> </div><div> // Check every line against every line</div><div> for (var i=1;i<line.length;i++) { </div>
<div> for(var j=1;j<line.length;j++){</div><div> var vi = line[i].getVertices(); // get points of the I line in an array</div><div> var vj = line[j].getVertices(); // get points of the J line in an array</div>
<div> </div><div> /*</div><div> * / the lines must be differents and not adjacents.</div><div> * // The end or start point of an adjacent line will be intersect, </div>
<div> * // and adjacent lines never intersect in other point than the ends.</div><div> */ </div><div> if (i!=j && </div><div> vi[1].toString() != vj[0].toString() && </div>
<div> vi[0].toString() != vj[1].toString()){</div><div> // the intersect check</div><div> if (line[i].intersects(line[j])) bool = true;</div><div> }</div>
<div> </div><div> }</div><div> }</div><div> </div><div> return bool;</div><div> }</div><div><br></div></font></div><div>Enjoy,</div><div><br clear="all">Fernando G. Norte<br>
BHte - MG - Brasil<br>cel: +55 31 9119 8814<br>-------------------------<br>MSN e Gtalk # <a href="mailto:fnorte@gmail.com">fnorte@gmail.com</a><br>
</div>