目录
一.基础概念
几何图形(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库学习之(三)——空间关系