Domanda

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.

È stato utile?

Soluzione

Extended the opensource libkml C++ library and implemented point in polygon in it. This is the source code:

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

I am yet to achieve the 2nd point in my question above. Any leads in that matter would be appreciated though :)

Altri suggerimenti

I was looking for a KML Library in C/C++ and came across your post. Looks like it is 6yrs+ old, but if someone else gets here looking for the 2nd part of your question...

I can't recall where I dug this up on the web, so I can't give/take credit.

// *************************************************************************
//
//  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()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top