从GDAL LineString中删除点

Remove point from GDAL LineString

本文关键字:删除 LineString GDAL      更新时间:2023-10-16

我试图只包含位于给定边界框内的形状文件中的地理特征。我找不到一个类似于带有BoundingBox选项[1]的Matlab的shapehead函数的函数,所以我试图从层的行字符串中删除不相关的点,但我不确定如何做到这一点(我有点期待会有与addPoint[2]相反的结果)。到目前为止,我的代码是:

        OGRLineString *poLineString = (OGRLineString *) poGeometry;
        int numPoints = poLineString->getNumPoints();
        cout << "num points" << poLineString->getNumPoints() << endl;
        //for each feature check if its in the bounding box
        for (int i=0; i<numPoints; i++)
        {
            // start off assuming we are including everything
            bool xInclude, yInclude = 1;
            OGRPoint* poPoint;
            poLineString->getPoint(i, poPoint);
            double ptX = poPoint->getX();
            double ptY = poPoint->getY();
            cout << "ptX " << ptX << " ptY " << ptY <<endl;
            //tlE, tlN, maxE, maxN are eastings/northings coordinates
            if((ptX<tlE)||(ptX>maxE))
                xInclude=0;
            if((ptY<minN)||(ptY>tlN))
                yInclude=0;
            if(!(xInclude && yInclude))
               //poLineString->setPoint(i,0,0);
               REMOVE POINT HERE
        }

有什么想法吗?

如果你想做什么,也许你可以在OGR中使用Intersection()方法。请参阅GDAL文档中的参考资料。只需将边界框创建为实际的Geometry对象,然后调用例如poClippedLine=poLineString->Intersection(poBoundingBox),poClippedRine将是您所需要的。

但是,请注意,此方法将在剪裁边界框的"边界"处创建新的部分,您可能需要也可能不需要。

否则:-是的,我也没有找到RemovePoint方法。因此,您可能只需要复制并创建一个包含要包含的顶点子集的新lineString。