Geos库学习之(二)——使用Geos库创建简单的几何对象

目录

一.基础概念

二.使用geos创建简单的几何对象


一.基础概念

几何图形(Geometry)是geos里面基本的操作对象,因此Geometry类就是最重要的一个类

几何图形中主要有三个要素:点,线,面。坐标构成点,多点构成线,环线构成面,点线面混合构成几何集合。对应的在GEOS里面的几个类为:

坐标:Coordinate(横纵坐标)

点:Point(单点)、MultiPoint(多点)

线:LineString(单条线)、MultiLineString(多条线)、LinearRing(环线)

面:Polygon(单面)、MultiPolygon(多面)

集合:GeometryCollection

在geos中,最小的组成单位是坐标,由Coordinate类来创建,如:Coordinate(2,3),表示一个点,横坐标是2,纵坐标是3。

如图所示:

在GEOS中每个类之间的关系如下图所示:

二.使用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();
	cout << "多点的个数:" << count << endl;

	LineString *ls = createGeosRing(10, 10, 5);
	cout << "线条点数:" << ls->getNumPoints() << " 线条长度:" << ls->getLength() << endl;

	Polygon *poly = createGeosPolygon(10, 10, 5);
	cout << "多边形面积:" << poly->getArea() << endl;

	system("pause");
    return 0;
}

输出结果如下:

其他几何对象创建方法与此类似

【上一篇:】Geos库学习之(一)——Geos库介绍和编译

【下一篇:】Geos库学习之(三)——空间关系

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

尘海折柳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值