有时候我们需要获得矢量边界的空间坐标,然后把它转化为行列坐标,例如使用GDALWarp实现裁剪的时候。下面的代码就是把空间坐标下的矢量多边形转化为行列坐标的矢量多边形。
OGRPolygon* aoiPolygon = (OGRPolygon*)aoiGeometry;
OGRLinearRing* boundary = aoiPolygon->getExteriorRing();
OGRPolygon* pPolygon = (OGRPolygon*)OGRGeometryFactory::createGeometry(wkbPolygon);
int pointcount = boundary->getNumPoints();
OGRLinearRing* pRing = (OGRLinearRing*)OGRGeometryFactory::createGeometry(wkbLinearRing);
for (int i = 0; i < pointcount; i++)
{
double gx = boundary->getX(i);
double gy = boundary->getY(i);
int row = static_cast((gy - srcGeotransform[3]) / srcGeotransform[5]);
int col = static_cast((gx - srcGeotransform[0]) / srcGeotransform[1]);
OGRPoint pt;
pt.setX(col); pt.setY(row);
pRing->addPoint(&pt);
}
pRing->closeRings();
pPolygon->addRing(pRing);
OGRGeometry* cutGeometry = (OGRGeometry*)pPolygon;
注:aoiGeometry是原始多边形