上篇我们主要对几何对象之间的关系做了简单的说明,在GEOS中的方法使做了大概的了解,本篇我们以程序实例对这几种关系进行简单的验证说明,直接上代码:
// GeoTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include "geos.h"
using namespace std;
using namespace geos;
using namespace geos::geom;
static const GeometryFactory* g_factory = geos::geom::GeometryFactory::getDefaultInstance(); //全局对象,所有的图形都由此对象创建
//单点的创建
Point* createGeosPoint(double x, double y)
{
Coordinate pt(x, y); //坐标
Point* p = g_factory->createPoint(pt);
return p;
}
//多点的创建
MultiPoint* createGeosMultiPoint(double x, double y, double offset)
{
CoordinateArraySequence *cas = new CoordinateArraySequence(); //构建点序列
std::vector<Coordinate> points;
points.push_back(Coordinate(x, y));
points.push_back(Coordinate(x + offset, y));
points.push_back(Coordinate(x + 2 * offset, y + offset));
points.push_back(Coordinate(x + 3 * offset, y + 2 * offset));
MultiPoint* Mp = g_factory->createMultiPoint(points);
return Mp;
}
//非闭合线
LineString* createGeosLine(double x, double y, double offset)
{
CoordinateArraySequence *cas = new CoordinateArraySequence(); //构建点序列
cas->add(Coordinate(x, y));
cas->add(Coordinate(x, y + offset));
cas->add(Coordinate(x + offset, y + offset));
cas->add(Coordinate(x + offset, y + 2 * offset));
cas->add(Coordinate(x + 2 * offset, y + 2 * offset));
LineString *ls = g_factory->createLineString(cas);
return ls;
}
//创建一条环线,与线的区别就是环线是闭合的。即第一个点和最后一点重合
LinearRing* createGeosRing(double x, double y, double offset)
{
CoordinateArraySequence *cas = new CoordinateArraySequence(); //构建点序列
cas->add(Coordinate(x, y));
cas->add(Coordinate(x, y + offset));
cas->add(Coordinate(x + offset, y + offset));
cas->add(Coordinate(x + offset, y + 2 * offset));
cas->add(Coordinate(x + 2 * offset, y + 2 * offset));
cas->add(Coordinate(x + 2 * offset, y));
cas->add(Coordinate(x, y)); //与第一个点相等
LinearRing *lr = g_factory->createLinearRing(cas);
return lr;
}
//创建一个多边形,如果多边形内部没有孔洞实际上与环线是一样的
Polygon* createGeosPolygon(double x, double y, double offset)
{
LinearRing *lr = createGeosRing(x, y, offset);
Polygon *poly = g_factory->createPolygon(lr, NULL); //如果多边形中间没有孔洞,第二个参数设为NULL
return poly;
}
int main()
{
Point* point = createGeosPoint(10, 20);
std::string typeStr = point->getGeometryType();
cout << "创建的点类型:" << typeStr << endl;
MultiPoint* multiPoint = createGeosMultiPoint(10, 20,5);
int count = multiPoint->getNumPoints();
std::unique_ptr<IntersectionMatrix> pointIm = multiPoint->relate(point);
cout << "多点和点之前的关系: " << *pointIm << " ";
if (multiPoint->disjoint(point))
cout << "不相交" << endl;
else
{
if (multiPoint->touches(point))
cout << "接触" << endl;
else if (multiPoint->overlaps(point))
cout << "部分重叠" << endl;
else if (multiPoint->covers(point))
cout << "覆盖" << endl;
else
cout << *pointIm << endl;
}
cout << "**************************************************" << endl << endl;
LineString *ls = createGeosRing(10, 10, 5);
cout << "线条点数:" << ls->getNumPoints() << " 线条长度:" << ls->getLength() << endl;
std::unique_ptr<IntersectionMatrix> lineIm = ls->relate(point);
cout << "线和点之前的关系: " << *lineIm << " ";
if (ls->disjoint(point))
cout << "不相交" << endl;
else
{
if (ls->touches(point))
cout << "接触" << endl;
else if (ls->overlaps(point))
cout << "部分重叠" << endl;
else if (ls->covers(point))
cout << "覆盖" << endl;
else
cout << *lineIm << endl;
}
cout << "**************************************************" << endl << endl;
Polygon *poly = createGeosPolygon(10, 10, 5);
cout << "多边形面积:" << poly->getArea() << endl;
std::unique_ptr<IntersectionMatrix> polygonIm = poly->relate(ls);
cout << "面和线之前的关系: " << *polygonIm << " ";
if (poly->disjoint(ls))
cout << "不相交" << endl;
else
{
if (poly->touches(ls))
cout << "接触" << endl;
else if (poly->overlaps(ls))
cout << "部分重叠" << endl;
else if (poly->covers(ls))
cout << "覆盖" << endl;
else
cout << *polygonIm << endl;
}
cout << "**************************************************" << endl << endl;
Polygon *p1 = createGeosPolygon(12, 12, 5); //创建一个多边形
for (int i = 0;i <= 20;i++)
{
cout << "第" << i << "个面和面之间的关系 " << ": ";
Polygon *pPolygon = createGeosPolygon(0, 0, i); //创建第二个多边形
std::unique_ptr<IntersectionMatrix> im = pPolygon->relate(p1);
cout << *im << " "; //返回DE-9IM交叉矩阵
if (pPolygon->disjoint(p1))
cout << "不相交" << endl;
else
{
if (pPolygon->touches(p1))
cout << "接触" << endl;
else if (pPolygon->overlaps(p1))
cout << "部分重叠" << endl;
else if (pPolygon->covers(p1))
cout << "覆盖" << endl;
else
cout << *im << endl;
}
}
system("pause");
return 0;
}
输出结果如下:
其他几何对象之间的关系判断与此类似。
【上一篇:】Geos库学习之(三)——几何对象空间关系