Adding distanceTo method to all geometry types. This calculates the shortest distance between any two geometries. For geometries that are entirely contained within polygons, the distance returned will be the distance to the nearest edge. Set the edge option to false to return 0 in cases where one geometry contains another. Details about the distance calculation can be returned by setting the details option true. Details include information about endpoints of the shortest segment between the two geometries. r=ahocevar (closes #1907)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@8836 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -156,6 +156,47 @@ OpenLayers.Geometry.Polygon = OpenLayers.Class(
|
||||
return intersect;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: distanceTo
|
||||
* Calculate the closest distance between two geometries.
|
||||
*
|
||||
* Parameters:
|
||||
* geometry - {<OpenLayers.Geometry>} The target geometry.
|
||||
* options - {Object} Optional properties for configuring the distance
|
||||
* calculation.
|
||||
*
|
||||
* Valid options:
|
||||
* details - {Boolean} Return details from the distance calculation.
|
||||
* Default is false.
|
||||
* edge - {Boolean} Calculate the distance from this geometry to the
|
||||
* nearest edge of the target geometry. Default is true. If true,
|
||||
* calling distanceTo from a geometry that is wholly contained within
|
||||
* the target will result in a non-zero distance. If false, whenever
|
||||
* geometries intersect, calling distanceTo will return 0. If false,
|
||||
* details cannot be returned.
|
||||
*
|
||||
* Returns:
|
||||
* {Number | Object} The distance between this geometry and the target.
|
||||
* If details is true, the return will be an object with distance,
|
||||
* x0, y0, x1, and y1 properties. The x0 and y0 properties represent
|
||||
* the coordinates of the closest point on this geometry. The x1 and y1
|
||||
* properties represent the coordinates of the closest point on the
|
||||
* target geometry.
|
||||
*/
|
||||
distanceTo: function(geometry, options) {
|
||||
var edge = !(options && options.edge === false);
|
||||
var result;
|
||||
// this is the case where we might not be looking for distance to edge
|
||||
if(!edge && this.intersects(geometry)) {
|
||||
result = 0;
|
||||
} else {
|
||||
result = OpenLayers.Geometry.Collection.prototype.distanceTo.apply(
|
||||
this, [geometry, options]
|
||||
);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Geometry.Polygon"
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user