【GEOS】GEOS库学习之简单几何图形的创建

一、GEOS库介绍

GEOS的前⾝是JTS,JTS提供了全功能的,强⼤的空间操作和空间判断。 后来PostGIS缺少⼀套完整的空间查询操作,于是就将JTS移植成为C++版本,正式命名为GEOS

GEOS为开源库,它包括了完整的空间查询和⼀⼤部分空间操作,是从事图形操作和GIS⾏业开发⼈员经常接触的开发库。较为知名的使⽤GEOS的GIS软件就有QGIS,QGIS使⽤GEOS的c接⼝,c接⼝函数名称不会经常发⽣更改,具有更多的稳定性。

简单得说,GEOS就是判断两个几何形状之间关系和对两个几何形状进行操作以形成新的几何形状的库

二、基础概念

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

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

  • 坐标:Coordinate
  • 点:PointMultiPoint
  • 线:LineStringMultiLineString(多条线)、LinearRing(环线)
  • 面:PolygonMultiPolygon
  • 集合:GeometryCollection

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

所有的几何图形的创建,都是由类GeometryFactory来完成。因此,在创建几何图形前,可以先创建一个全局的GeometryFactory对象:

GeometryFactory factory; //全局对象,所有的图形都由此对象创建
1. 点的创建
Point* createGeosPoint(double x,double y)
{
    Coordinate pt(x,y);  
    Point* p=factory.createPoint(pt);
    return p;
}
2. 非闭合线条的创建
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=factory.createLineString(cas);
    return ls;
}
3. 闭合线条的创建
//创建一条环线,与线的区别就是环线是闭合的。即第一个点和最后一点重合
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=factory.createLinearRing(cas);
    return lr;
}

除了用add的方法来构建点序列,也可以用另外一种方法setAt

LinearRing* createGeosRing(double x,double y,double offset)
{
     CoordinateArraySequenceFactory csf; 
    CoordinateSequence* cs = csf.create(7,2);
    cs->setAt(Coordinate(x,y),0);
    cs->setAt(Coordinate(x,y+offset),1);
    cs->setAt(Coordinate(x+offset,y+offset),2);
    cs->setAt(Coordinate(x+offset,y+2*offset),3);
    cs->setAt(Coordinate(x+2*offset,y+2*offset),4);
    cs->setAt(Coordinate(x+2*offset,y),5);
    cs->setAt(Coordinate(x,y),6); //与第一个点相等
    LinearRing *lr=factory.createLinearRing(cs);
    return lr;
}
4. 多边形的创建
//创建一个多边形,如果多边形内部没有孔洞实际上与环线是一样的
Polygon* createGeosPolygon(double x,double y,double offset)
{
    LinearRing *lr=createGeosRing(x,y,offset);
    Polygon *poly=factory.createPolygon(lr,NULL); //如果多边形中间没有孔洞,第二个参数设为NULL
    return poly;
}
测试
#include "geos.h"
int main()
{
       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 1;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值