2

I am trying to just include geographic features from a shapefile that lie within a given bounding box. I couldn't find a function similar to Matlab's shaperead function with a BoundingBox option [1] and so I am trying to remove non-relevant points from a layer's linestring however I am unsure how to do this (I kind of expected there would be the opposite of addPoint [2]). The code I have so far is:

        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
        }

Any ideas?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
tjp
  • 203
  • 1
  • 4
  • 9

1 Answers1

1

Given what you want to do, perhaps you can use the Intersection() method in OGR. See reference in the GDAL docs. Just create your bounding box as an actual Geometry object, then call e.g. poClippedLine = poLineString->Intersection(poBoundingBox) - and poClippedLine will be what you need.

However, be aware this method will create new sections at the 'borders' of the clipped bounding box, which you may or may not want.

Otherwise:- yes, I also haven't found a RemovePoint method. So you might just need to copy and create a new lineString with the subset of vertices you want to include.

patdevelop
  • 41
  • 1