# 基于GDAL的OGRPolygon网格化

1.获取多边形的外环

``OGRLinearRing *poLR = mPolygon->getExteriorRing();``

2.分别获取横纵坐标列表

``````QList<double> x;
QList<double> y;

for (int i = 0; i < poLR->getNumPoints(); i++)
{
OGRPoint tempPoint;
poLR->getPoint(i, &tempPoint);
x.append(tempPoint.getX());
y.append(tempPoint.getY());
}``````

3.给横纵坐标列表进行排序，然后获取最大和最小值

``````        qSort(x.begin(), x.end());
qSort(y.begin(), y.end());

double Xmin = x.first();
double Ymin = y.first();

double Xmax = x.last();
double Ymax = y.last();``````

``````       //网格化最大矩形
QList<double> girdX;
QList<double> girdY;

double gx, gy;
for (gx = Xmin; gx <= Xmax; gx += girdWidth)
{
girdX.append(gx);
}
gx -= girdWidth;
if (gx < Xmax) girdX.append(Xmax);

for (gy = Ymin; gy <= Ymax; gy += girdHeight)
{
girdY.append(gy);
}

gy -= girdHeight;
if (gy < Ymax) girdY.append(Ymax);``````

``````        for (int xIndex = 1; xIndex < girdX.size(); xIndex++)
{
for (int yIndex = 1; yIndex < girdY.size(); yIndex++)
{
//OGRPoint minPoint(girdX[xIndex-1],girdY[yIndex-1]);
//OGRPoint maxPoint(girdX[xIndex],girdY[yIndex]);
//qDebug() << xIndex - 1 << yIndex - 1 << xIndex << yIndex;

OGRPoint lbottomPoint(girdX[xIndex - 1], girdY[yIndex - 1]);
OGRPoint ltopPoint(girdX[xIndex - 1], girdY[yIndex]);
OGRPoint rtopPoint(girdX[xIndex], girdY[yIndex]);
OGRPoint rbottomPoint(girdX[xIndex], girdY[yIndex-1]);

OGRPolygon  *tempPolygon = new OGRPolygon;  //多边形
OGRLinearRing *mLineRing = new OGRLinearRing;  //多边形中的ring
tempPolygon->closeRings();

//筛选网格，求交叉
OGRPolygon *iPolygon =(OGRPolygon*)mPolygon->Intersection(tempPolygon);
OGRLinearRing *plRing = iPolygon->getExteriorRing();
if (plRing != nullptr)
{
double mArea = iPolygon->get_Area();
qDebug() << mArea;
int num = plRing->getNumPoints();
QPolygonF mPolygonF;
for (int i = 0; i < num; i++)
{
OGRPoint mPoint;

plRing->getPoint(i, &mPoint);
QPointF mPointf(mPoint.getX(), mPoint.getY());
mPolygonF.append(mPointf);
}
tempGirdList.append(mPolygonF);
//qDebug() << mPolygonF;
}
}
}``````

``````QList<QPolygonF> LayerOper::girdPolygon(OGRPolygon *mPolygon, double girdWidth, double girdHeight)
{
//获取最小和最大坐标，建立最大矩形
QList<QPolygonF> tempGirdList;
OGRLinearRing *poLR = mPolygon->getExteriorRing();

QList<double> x;
QList<double> y;

for (int i = 0; i < poLR->getNumPoints(); i++)
{
OGRPoint tempPoint;
poLR->getPoint(i, &tempPoint);
x.append(tempPoint.getX());
y.append(tempPoint.getY());
}

qSort(x.begin(), x.end());
qSort(y.begin(), y.end());

double Xmin = x.first();
double Ymin = y.first();

double Xmax = x.last();
double Ymax = y.last();

//网格化最大矩形
QList<double> girdX;
QList<double> girdY;

double gx, gy;
for (gx = Xmin; gx <= Xmax; gx += girdWidth)
{
girdX.append(gx);
}
gx -= girdWidth;
if (gx < Xmax) girdX.append(Xmax);

for (gy = Ymin; gy <= Ymax; gy += girdHeight)
{
girdY.append(gy);
}

gy -= girdHeight;
if (gy < Ymax) girdY.append(Ymax);

for (int xIndex = 1; xIndex < girdX.size(); xIndex++)
{
for (int yIndex = 1; yIndex < girdY.size(); yIndex++)
{
//OGRPoint minPoint(girdX[xIndex-1],girdY[yIndex-1]);
//OGRPoint maxPoint(girdX[xIndex],girdY[yIndex]);
//qDebug() << xIndex - 1 << yIndex - 1 << xIndex << yIndex;

OGRPoint lbottomPoint(girdX[xIndex - 1], girdY[yIndex - 1]);
OGRPoint ltopPoint(girdX[xIndex - 1], girdY[yIndex]);
OGRPoint rtopPoint(girdX[xIndex], girdY[yIndex]);
OGRPoint rbottomPoint(girdX[xIndex], girdY[yIndex-1]);

OGRPolygon  *tempPolygon = new OGRPolygon;  //多边形
OGRLinearRing *mLineRing = new OGRLinearRing;  //多边形中的ring
tempPolygon->closeRings();

//筛选网格，求交叉
OGRPolygon *iPolygon =(OGRPolygon*)mPolygon->Intersection(tempPolygon);
OGRLinearRing *plRing = iPolygon->getExteriorRing();
if (plRing != nullptr)
{
double mArea = iPolygon->get_Area();
qDebug() << mArea;
int num = plRing->getNumPoints();
QPolygonF mPolygonF;
for (int i = 0; i < num; i++)
{
OGRPoint mPoint;

plRing->getPoint(i, &mPoint);
QPointF mPointf(mPoint.getX(), mPoint.getY());
mPolygonF.append(mPointf);
}
tempGirdList.append(mPolygonF);
//qDebug() << mPolygonF;
}
}
}
return tempGirdList;
}``````

``````OGRGeometry *pRet = mPolygon->Intersection(tempPolygon);
if (pRet != NULL)
{
OGRwkbGeometryType pGeoType = pRet->getGeometryType();
if (pGeoType == wkbPolygon)//这里就是多边形判断
{
OGRPolygon *rdPolygon = (OGRPolygon*)pRet->clone();
QPolygonF mPolygonF;
bool ret = ogrPolygon2qpolygonf(rdPolygon, mPolygonF);
if (ret)
{
tempGirdList.append(mPolygonF);
}
}
else if (pGeoType == wkbMultiPolygon)  //这里是多个相交区域获取方法
{
OGRMultiPolygon * multiPolygon = (OGRMultiPolygon *)pRet;
int num = multiPolygon->getNumGeometries();
for (int i = 0; i < num; i++)
{
OGRPolygon *mOGRPolygon = (OGRPolygon *)multiPolygon->getGeometryRef(i);
QPolygonF mPolygonF;
bool ret = ogrPolygon2qpolygonf(mOGRPolygon, mPolygonF);
if (ret)
{
tempGirdList.append(mPolygonF);
}
}
}
}``````

``````bool LayerOper::ogrPolygon2qpolygonf(OGRPolygon *mPolygon, QPolygonF &targetPolygon)
{
OGRLinearRing *plRing = mPolygon->getExteriorRing();
if (plRing != nullptr)
{
int num = plRing->getNumPoints();
for (int i = 0; i < num; i++)
{
OGRPoint mPoint;

plRing->getPoint(i, &mPoint);
QPointF mPointf(mPoint.getX(), mPoint.getY());
targetPolygon.append(mPointf);
}
return true;
}
else
{
return false;
}
}``````