缓冲区的单位将是您的数据集所在的任何单位。如果您在投影坐标系中,则单位可能以英尺或米为单位,具体取决于投影。如果您处于地理投影中,例如 WGS84,则度量单位为十进制度。
使用 DotSpatial 时,经常需要在地理单位和像素单位之间来回切换。对于命中测试,这里有一些涉及矩形和信封的示例代码,这些矩形和信封是正方形的,但提供了一种方便的方法来给出围绕坐标的近似区域以进行命中测试。
像上面的示例一样,使用 Buffer 主要用于更详细的几何计算,您可以在其中创建可以使用的永久几何图形,不仅可以用于相交,还可以用于缓冲区域的可视化。
几何图形都遵循 IGeometry 相关运算符,因此您使用的原始 Point 和 Buffer 操作的输出 LineString 都可以用于相交,但会比使用信封要慢。
这是一些使用像素和坐标的基本命中测试代码:
/// <summary>
/// This method creates a rectangular geographic envelope that is expanded by the
/// specified geographic amount in the original geographic units. Envelopes
/// are useful in that they are simpler than geographic shapes,
/// and so are much quicker to do intersection testing with.
/// </summary>
/// <param name="center">The center point in X and Y.</param>
/// <returns>A rectangular Envelope that contains the center point.</returns>
public static Envelope Extend(Coordinate center, double distance)
{
Envelope result = new Envelope(center);
result.ExpandBy(distance);
return result;
}
/// <summary>
/// Intersection testing with an envelope works the same as with the slower
/// and heavier geometries, but should be considerably faster.
/// </summary>
/// <param name="testPoint">The test point.</param>
/// <param name="box">The Envelope that has the box.</param>
/// <returns>Boolean, true if the box intersects with (contains, or touches) the
/// test point.</returns>
public static bool ContainsTest(Coordinate testPoint, Envelope box)
{
return box.Intersects(testPoint);
}
/// <summary>
/// To get a geographic envelope 10 pixels around an X, Y position of a pixel
/// coordinate on the map, in terms of the actual visible map component (and not
/// the possibly extended buffer of the map frame).
/// </summary>
/// <param name="center">The pixel position of the center on the map control</param>
/// <param name="map">The map control</param>
/// <returns>A Geographic envelope</returns>
public static Envelope Expand(Point center, Map map)
{
Rectangle rect = new Rectangle(center.X - 10, center.Y - 10, 20, 20);
// Get the geographic points
return map.PixelToProj(rect);
}
/// <summary>
/// To get a pixel coordinate bounding rectangle around a geographic point for
/// hit testing is similar.
/// </summary>
/// <param name="test">The geographic coordinate</param>
/// <param name="map">The map control</param>
/// <returns>A pixel coordinate rectangle for the specified coordinate</returns>
public static Rectangle Bounds(Coordinate test, Map map)
{
Envelope result = new Envelope(center);
result.ExpandBy(distance);
return map.ProjToPixel(result);
}