[76] | 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/LinearRing.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Geometry.Polygon |
---|
| 13 | * Polygon is a collection of Geometry.LinearRings. |
---|
| 14 | * |
---|
| 15 | * Inherits from: |
---|
| 16 | * - <OpenLayers.Geometry.Collection> |
---|
| 17 | * - <OpenLayers.Geometry> |
---|
| 18 | */ |
---|
| 19 | OpenLayers.Geometry.Polygon = OpenLayers.Class( |
---|
| 20 | OpenLayers.Geometry.Collection, { |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Property: componentTypes |
---|
| 24 | * {Array(String)} An array of class names representing the types of |
---|
| 25 | * components that the collection can include. A null value means the |
---|
| 26 | * component types are not restricted. |
---|
| 27 | */ |
---|
| 28 | componentTypes: ["OpenLayers.Geometry.LinearRing"], |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Constructor: OpenLayers.Geometry.Polygon |
---|
| 32 | * Constructor for a Polygon geometry. |
---|
| 33 | * The first ring (this.component[0])is the outer bounds of the polygon and |
---|
| 34 | * all subsequent rings (this.component[1-n]) are internal holes. |
---|
| 35 | * |
---|
| 36 | * |
---|
| 37 | * Parameters: |
---|
| 38 | * components - {Array(<OpenLayers.Geometry.LinearRing>)} |
---|
| 39 | */ |
---|
| 40 | initialize: function(components) { |
---|
| 41 | OpenLayers.Geometry.Collection.prototype.initialize.apply(this, |
---|
| 42 | arguments); |
---|
| 43 | }, |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * APIMethod: getArea |
---|
| 47 | * Calculated by subtracting the areas of the internal holes from the |
---|
| 48 | * area of the outer hole. |
---|
| 49 | * |
---|
| 50 | * Returns: |
---|
| 51 | * {float} The area of the geometry |
---|
| 52 | */ |
---|
| 53 | getArea: function() { |
---|
| 54 | var area = 0.0; |
---|
| 55 | if ( this.components && (this.components.length > 0)) { |
---|
| 56 | area += Math.abs(this.components[0].getArea()); |
---|
| 57 | for (var i=1, len=this.components.length; i<len; i++) { |
---|
| 58 | area -= Math.abs(this.components[i].getArea()); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | return area; |
---|
| 62 | }, |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * APIMethod: getGeodesicArea |
---|
| 66 | * Calculate the approximate area of the polygon were it projected onto |
---|
| 67 | * the earth. |
---|
| 68 | * |
---|
| 69 | * Parameters: |
---|
| 70 | * projection - {<OpenLayers.Projection>} The spatial reference system |
---|
| 71 | * for the geometry coordinates. If not provided, Geographic/WGS84 is |
---|
| 72 | * assumed. |
---|
| 73 | * |
---|
| 74 | * Reference: |
---|
| 75 | * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for |
---|
| 76 | * Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion |
---|
| 77 | * Laboratory, Pasadena, CA, June 2007 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409 |
---|
| 78 | * |
---|
| 79 | * Returns: |
---|
| 80 | * {float} The approximate geodesic area of the polygon in square meters. |
---|
| 81 | */ |
---|
| 82 | getGeodesicArea: function(projection) { |
---|
| 83 | var area = 0.0; |
---|
| 84 | if(this.components && (this.components.length > 0)) { |
---|
| 85 | area += Math.abs(this.components[0].getGeodesicArea(projection)); |
---|
| 86 | for(var i=1, len=this.components.length; i<len; i++) { |
---|
| 87 | area -= Math.abs(this.components[i].getGeodesicArea(projection)); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | return area; |
---|
| 91 | }, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Method: containsPoint |
---|
| 95 | * Test if a point is inside a polygon. Points on a polygon edge are |
---|
| 96 | * considered inside. |
---|
| 97 | * |
---|
| 98 | * Parameters: |
---|
| 99 | * point - {<OpenLayers.Geometry.Point>} |
---|
| 100 | * |
---|
| 101 | * Returns: |
---|
| 102 | * {Boolean | Number} The point is inside the polygon. Returns 1 if the |
---|
| 103 | * point is on an edge. Returns boolean otherwise. |
---|
| 104 | */ |
---|
| 105 | containsPoint: function(point) { |
---|
| 106 | var numRings = this.components.length; |
---|
| 107 | var contained = false; |
---|
| 108 | if(numRings > 0) { |
---|
| 109 | // check exterior ring - 1 means on edge, boolean otherwise |
---|
| 110 | contained = this.components[0].containsPoint(point); |
---|
| 111 | if(contained !== 1) { |
---|
| 112 | if(contained && numRings > 1) { |
---|
| 113 | // check interior rings |
---|
| 114 | var hole; |
---|
| 115 | for(var i=1; i<numRings; ++i) { |
---|
| 116 | hole = this.components[i].containsPoint(point); |
---|
| 117 | if(hole) { |
---|
| 118 | if(hole === 1) { |
---|
| 119 | // on edge |
---|
| 120 | contained = 1; |
---|
| 121 | } else { |
---|
| 122 | // in hole |
---|
| 123 | contained = false; |
---|
| 124 | } |
---|
| 125 | break; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | return contained; |
---|
| 132 | }, |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * APIMethod: intersects |
---|
| 136 | * Determine if the input geometry intersects this one. |
---|
| 137 | * |
---|
| 138 | * Parameters: |
---|
| 139 | * geometry - {<OpenLayers.Geometry>} Any type of geometry. |
---|
| 140 | * |
---|
| 141 | * Returns: |
---|
| 142 | * {Boolean} The input geometry intersects this one. |
---|
| 143 | */ |
---|
| 144 | intersects: function(geometry) { |
---|
| 145 | var intersect = false; |
---|
| 146 | var i, len; |
---|
| 147 | if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 148 | intersect = this.containsPoint(geometry); |
---|
| 149 | } else if(geometry.CLASS_NAME == "OpenLayers.Geometry.LineString" || |
---|
| 150 | geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") { |
---|
| 151 | // check if rings/linestrings intersect |
---|
| 152 | for(i=0, len=this.components.length; i<len; ++i) { |
---|
| 153 | intersect = geometry.intersects(this.components[i]); |
---|
| 154 | if(intersect) { |
---|
| 155 | break; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | if(!intersect) { |
---|
| 159 | // check if this poly contains points of the ring/linestring |
---|
| 160 | for(i=0, len=geometry.components.length; i<len; ++i) { |
---|
| 161 | intersect = this.containsPoint(geometry.components[i]); |
---|
| 162 | if(intersect) { |
---|
| 163 | break; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } else { |
---|
| 168 | for(i=0, len=geometry.components.length; i<len; ++ i) { |
---|
| 169 | intersect = this.intersects(geometry.components[i]); |
---|
| 170 | if(intersect) { |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | // check case where this poly is wholly contained by another |
---|
| 176 | if(!intersect && geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") { |
---|
| 177 | // exterior ring points will be contained in the other geometry |
---|
| 178 | var ring = this.components[0]; |
---|
| 179 | for(i=0, len=ring.components.length; i<len; ++i) { |
---|
| 180 | intersect = geometry.containsPoint(ring.components[i]); |
---|
| 181 | if(intersect) { |
---|
| 182 | break; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | return intersect; |
---|
| 187 | }, |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | * APIMethod: distanceTo |
---|
| 191 | * Calculate the closest distance between two geometries (on the x-y plane). |
---|
| 192 | * |
---|
| 193 | * Parameters: |
---|
| 194 | * geometry - {<OpenLayers.Geometry>} The target geometry. |
---|
| 195 | * options - {Object} Optional properties for configuring the distance |
---|
| 196 | * calculation. |
---|
| 197 | * |
---|
| 198 | * Valid options: |
---|
| 199 | * details - {Boolean} Return details from the distance calculation. |
---|
| 200 | * Default is false. |
---|
| 201 | * edge - {Boolean} Calculate the distance from this geometry to the |
---|
| 202 | * nearest edge of the target geometry. Default is true. If true, |
---|
| 203 | * calling distanceTo from a geometry that is wholly contained within |
---|
| 204 | * the target will result in a non-zero distance. If false, whenever |
---|
| 205 | * geometries intersect, calling distanceTo will return 0. If false, |
---|
| 206 | * details cannot be returned. |
---|
| 207 | * |
---|
| 208 | * Returns: |
---|
| 209 | * {Number | Object} The distance between this geometry and the target. |
---|
| 210 | * If details is true, the return will be an object with distance, |
---|
| 211 | * x0, y0, x1, and y1 properties. The x0 and y0 properties represent |
---|
| 212 | * the coordinates of the closest point on this geometry. The x1 and y1 |
---|
| 213 | * properties represent the coordinates of the closest point on the |
---|
| 214 | * target geometry. |
---|
| 215 | */ |
---|
| 216 | distanceTo: function(geometry, options) { |
---|
| 217 | var edge = !(options && options.edge === false); |
---|
| 218 | var result; |
---|
| 219 | // this is the case where we might not be looking for distance to edge |
---|
| 220 | if(!edge && this.intersects(geometry)) { |
---|
| 221 | result = 0; |
---|
| 222 | } else { |
---|
| 223 | result = OpenLayers.Geometry.Collection.prototype.distanceTo.apply( |
---|
| 224 | this, [geometry, options] |
---|
| 225 | ); |
---|
| 226 | } |
---|
| 227 | return result; |
---|
| 228 | }, |
---|
| 229 | |
---|
| 230 | CLASS_NAME: "OpenLayers.Geometry.Polygon" |
---|
| 231 | }); |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * APIMethod: createRegularPolygon |
---|
| 235 | * Create a regular polygon around a radius. Useful for creating circles |
---|
| 236 | * and the like. |
---|
| 237 | * |
---|
| 238 | * Parameters: |
---|
| 239 | * origin - {<OpenLayers.Geometry.Point>} center of polygon. |
---|
| 240 | * radius - {Float} distance to vertex, in map units. |
---|
| 241 | * sides - {Integer} Number of sides. 20 approximates a circle. |
---|
| 242 | * rotation - {Float} original angle of rotation, in degrees. |
---|
| 243 | */ |
---|
| 244 | OpenLayers.Geometry.Polygon.createRegularPolygon = function(origin, radius, sides, rotation) { |
---|
| 245 | var angle = Math.PI * ((1/sides) - (1/2)); |
---|
| 246 | if(rotation) { |
---|
| 247 | angle += (rotation / 180) * Math.PI; |
---|
| 248 | } |
---|
| 249 | var rotatedAngle, x, y; |
---|
| 250 | var points = []; |
---|
| 251 | for(var i=0; i<sides; ++i) { |
---|
| 252 | rotatedAngle = angle + (i * 2 * Math.PI / sides); |
---|
| 253 | x = origin.x + (radius * Math.cos(rotatedAngle)); |
---|
| 254 | y = origin.y + (radius * Math.sin(rotatedAngle)); |
---|
| 255 | points.push(new OpenLayers.Geometry.Point(x, y)); |
---|
| 256 | } |
---|
| 257 | var ring = new OpenLayers.Geometry.LinearRing(points); |
---|
| 258 | return new OpenLayers.Geometry.Polygon([ring]); |
---|
| 259 | }; |
---|