Geos库学习之(四)——几何对象空间关系判断实例

本文通过程序实例详细介绍了如何使用GEOS库来判断几何对象之间的空间关系,包括不同几何对象的关系验证,提供了具体的代码展示。
摘要由CSDN通过智能技术生成

上篇我们主要对几何对象之间的关系做了简单的说明,在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库学习之(三)——几何对象空间关系

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尘海折柳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值