0

I am looking for C or C++ code to identify whether the point (a co-ordinate) lies inside a KML polygon or not.

I have searched but all I got is javascript (google maps apis) which can achieve the same. I cannot even port it directly because I have a slight modified requirement:

  1. Identify whether given co-ordinate lies within polygon of KML (the code should exclude holes in the polygon too!).
  2. Informing the caller the distance to the nearest polygon edge if the point is outside the range.

As far as I have researched, the 2nd point has not been achieved anywhere yet. I would want to know how to achieve this in C or C++? Or, have I missed searching any existing sources?

Would like to have inputs.

4

2 回答 2

1

扩展了开源 libkml C++ 库并在其中实现了多边形中的点。这是源代码:

https://github.com/gumdal/libkml-pointinpolygon

我还没有达到上述问题的第二点。不过,在这件事上的任何线索都将不胜感激:)

于 2013-10-07T09:39:11.973 回答
1

我正在寻找 C/C++ 中的 KML 库并看到了您的帖子。看起来它已经 6 岁以上了,但如果其他人来这里寻找你问题的第二部分......

我不记得我在网上是在哪里挖到这个的,所以我不能给予/信任。

// *************************************************************************
//
//  Function:   distToLine
//
//  Usage:      Calculate the shortest distance to a line
//
//  Params:     x1      - Line start x
//              y1      - Line start y
//              x2      - Line end x
//              y2      - Line end y
//              ptX     - Observation Point x
//              ptY     - Observation Point y
//
//  Returns:    Distance
//
//  Notes:
//
// *************************************************************************

double distToLine(double x1, double y1, double x2, double y2, double ptX, double ptY)
{
    double  dx;
    float   dy;
    float   t;

    dx = x2 - x1;
    dy = y2 - y1;

    if ((dx == 0) && (dy == 0))
    {
        dx = ptX - x1;
        dy = ptY - y1;
        return sqrt(dx * dx + dy * dy);
    }

    t = ((ptX - x1) * dx + (ptY - y1) * dy) / (dx * dx + dy * dy);

    if (t < 0)          // point is nearest to the first point i.e x1,y1
    {        
        dx = ptX - x1;
        dy = ptY - y1;
    }
    else if (t > 1)     // point is nearest to the end point i.e x2,y2
    {        
        dx = ptX - x2;
        dy = ptY - y2;
    }
    else                // if perpendicular line intersect the line segment.
    {        
        dx = ptX - (x1 + t * dx);
        dy = ptY - (y1 + t * dy);
    }

    return sqrt(dx * dx + dy * dy);     // returning shortest distance
}
// distToLine()
于 2019-03-25T14:49:32.587 回答