2013-06-29 84 views
1

我想只包含位於給定邊界框內的shapefile的地理特徵。我無法找到類似於Matlab的Shaperead函數的函數和BoundingBox選項[1],因此我試圖從圖層的線串中刪除不相關的點,但我不確定如何做到這一點(我預計會有與addPoint [2]相反)。我到目前爲止的代碼是:從GDAL中刪除點LineString

 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 
     } 

任何想法?

回答

0

鑑於你想要做什麼,也許你可以在OGR中使用Intersection()方法。請參閱參考文獻the GDAL docs。只需將您的邊界框創建爲實際的幾何對象,然後調用poClippedLine = poLineString-> Intersection(poBoundingBox) - 和poClippedLine將是你需要的。

但是,請注意,此方法將在剪裁的邊框的「邊框」處創建新的部分,您可能會也可能不想要。

否則: - 是的,我也沒有找到RemovePoint方法。所以你可能只需要複製並創建一個新的lineString和你想包含的頂點的子集。