1.Geos库的安装

背景:项目需要判断多边形ROI区域和识别目标Bounding Box是否重叠。作者是在Jetson nano开发板上编译Geos,因为deepstream使用的是c语言,因此我使用Geos_c库。

1.1 下载 geos-3.8.0.tar.bz2

geos官网

1.2 安装

cd geos-3.6.2
./configue  //或选择安装的目录./configure --prefix=/usr/geos
make
make install

我这里建议(Jetson环境下)自定义安装目录,方便在gcc编译的时候,添加:
头文件“-I/home/lieryang/geos/include”,
库文件地址-L/home/lieryang/geos/lib","-lgeos_c"

2.计算多边形是否相交

2.1 C语言实现

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>#include "geos_c.h"void
notice(const char *fmt, ...) {va_list ap;fprintf( stdout, "NOTICE: ");va_start (ap, fmt);vfprintf( stdout, fmt, ap);va_end(ap);fprintf( stdout, "\n" );
}void
log_and_exit(const char *fmt, ...) {va_list ap;fprintf( stdout, "ERROR: ");va_start (ap, fmt);vfprintf( stdout, fmt, ap);va_end(ap);fprintf( stdout, "\n" );exit(1);
}int
main(int argc, char **argv)
{int i, n=1;initGEOS(notice, log_and_exit);printf("GEOS version %s\n", GEOSversion());GEOSGeometry* g1;GEOSGeometry* g2;GEOSGeometry* shell;const GEOSGeometry* gtmp;GEOSCoordSequence* cs;unsigned int npoints, ndims;g1 = GEOSGeomFromWKT("POLYGON((0 0, 0  10,10 10,0 0 ))");g2 = GEOSGeomFromWKT("POLYGON((0 11,20 20,20 11,0 11))");gtmp = GEOSGetExteriorRing(g1);cs = GEOSCoordSeq_clone(GEOSGeom_getCoordSeq(gtmp));g1 = GEOSGeom_createLinearRing(cs);gtmp = GEOSGetExteriorRing(g2);cs = GEOSCoordSeq_clone(GEOSGeom_getCoordSeq(gtmp));//if(GEOSCoordSeq_setXY(cs, 2, 20, 9)){printf("ok\n");};g2 = GEOSGeom_createLinearRing(cs);npoints = GEOSGetNumCoordinates(g1);/*坐标个数*/ndims = GEOSGeom_getDimensions(g1);/*坐标维度*/printf("Geometry coordinates: %dx%d\n", npoints, ndims);if ( GEOSIntersects(g1, g2) ) printf("Intersect\n");if ( GEOSDisjoint(g1, g2) ) printf("Disjoint\n");if ( GEOSTouches(g1, g2) ) printf("Touches\n");if ( GEOSCrosses(g1, g2) ) printf("Crosses\n");if ( GEOSWithin(g1, g2) ) printf("Within\n");if ( GEOSContains(g1, g2) ) printf("Contains\n");if ( GEOSOverlaps(g1, g2) ) printf("Overlaps\n");finishGEOS();return EXIT_SUCCESS;
}

2.2 C++实现

#include <iostream>
#include <stdlib.h>
#include <ctime>#include "geos.h"using namespace std;
using namespace geos;
using namespace geos::geom;static const GeometryFactory* g_factory = geos::geom::GeometryFactory::getDefaultInstance(); //全局对象,所有的图形都由此对象创建
#if 0
//单点的创建
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;
}#endif//创建一条环线,与线的区别就是环线是闭合的。即第一个点和最后一点重合
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 + offset));cas->add(Coordinate(x + 2, y + offset));cas->add(Coordinate(x + offset + 5, y + offset + 5));cas->add(Coordinate(x + 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); //如果多边形中间没有孔洞,第二个参数设为NULLreturn poly;
}int main()
{long StartTime, EndTime;
// GeoTest.cpp : 定义控制台应用程序的入口点。
//#include <iostream>
#include <stdlib.h>#include "geos.h"using namespace std;cout << "GEOS库版本为:" << GEOS_VERSION << endl;LineString *ls = createGeosRing(10, 10, 5);cout << "线条点数:" << ls->getNumPoints() << " 线条长度:" << ls->getLength() << endl;Polygon *p1 = createGeosPolygon(0, 0, 5);Polygon *p2 = createGeosPolygon(10, 10, 5);cout << "多边形面积:" << p1->getArea() << endl;bool flag = p2->touches(p1);StartTime = clock();/*计时开始*/for(int i = 0; i < 1000; i++){flag = p2->touches(p1);}EndTime = clock();/*计时结束*/cout << (EndTime - StartTime) << endl;if(p2->disjoint(p1))cout<<"不相交"<<endl;else{if(p2->touches(p1))cout<<"接触"<<endl;else if(p2->overlaps(p1))cout<<"部分重叠"<<endl;else if(p2->covers(p1))cout<<"覆盖"<<endl;}return 0;
}

参考
Geos库学习

一、Geos库的安装和计算多边形是否相交相关推荐

  1. 用pip安装cartopy(windows平台),解决GEOS库的问题

    对象:cartopy Cartopy官方网站介绍: Cartopy is a Python package designed for geospatial data processing in ord ...

  2. Linux下MKL库的安装部署与使用,并利用cmake编译器调用MKL库去提升eigen库的计算速度

    文章目录 前言 一.MKL库的下载 二.MKL库的安装与配置 1.MKL库的安装与配置 2.代码测试 总结 前言 在用C/C++编写模型预测控制算法(MPC)的代码时候,由于预测步长和控制步长的设置较 ...

  3. 用进化算法来优化SVM的参数C和Gamma——利用SCOOP库进行分布式加速计算

    该案例展示了如何利用SCOOP库进行分布式加速计算Geatpy进化算法程序, 本案例和soea_demo6类似,同样是用进化算法来优化SVM的参数C和Gamma, 不同的是,本案例选用更庞大的数据集, ...

  4. 下面不属于python第三方库的安装方法的是-python第三方库的pip安装方法

    安装python第三方库的三种方法 方法1:使用pip命令 方法2:集成安装方法 方法3:文件安装方法 一.pip命令安装方法(需要联网): pip安装方法简单讲就是使用python自带的pip安装工 ...

  5. 下面不属于python第三方库的安装方法的是-Python第三方库安装和卸载

    系统:Windows 7 版本:Python 3.5 Python是一门简洁.优雅的语言,丰富的第三方库能让我们很多的编程任务变得更加简单.对于想要用Python进行数据分析,就需要强大的Python ...

  6. python3.7扩展库是什么_Python3.4以后的版本中,____________库用于安装管理Python扩展包,________________库用于发布Python包。_学小易找答案...

    [填空题]Python3.4以后的版本中,____________库用于安装管理Python扩展包,________________库用于发布Python包. [判断题]Directions: The ...

  7. 【已解决】nimfa 环境的详细搭建过程 + 各种依赖库的安装、下载、调试

    为方便交流学习,工具已整理上传至CSDN.作者:玉林师范学院计算机系 guomutian911. 开源包下载地址: 也可邮件获得,316190672@qq.com Numpy 下载地址:http:// ...

  8. python标准库math用来计算平方根的函数_《Python程序设计方案》题库

    < Python 程序设计>题库 一.填空题 第一章 基础知识 1 . Python 安装扩展库常用的是 _______ 工具.( pip ) 2 . Python 标准库 math 中用 ...

  9. 干货收藏!一文看懂8个常用Python库从安装到应用

    导读:Python本身的数据分析功能并不强,需要安装一些第三方扩展库来增强其相应的功能.本文将对NumPy.SciPy.Matplotlib.pandas.StatsModels.scikit-lea ...

  10. python标准库math用来计算平方根的函数_《Python程序设计》试试题题库

    WORD 格式可编辑 < Python 程序设计>题库 一.填空题 第一章 基础知识 1 . Python 安装扩展库常用的是 _______ 工具.( pip ) 2 . Python ...

最新文章

  1. php7.0 + mysql5.7.10 + nginx7.0 web开发环境搭建(CentOS7)
  2. 2016012086+杨岚青+散列函数应用及安全性
  3. 【DIY】废物利用,最简单粗暴便宜的DIY定时器方法,没有之一
  4. DL之LeNet-5:LeNet-5算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
  5. python 生成pdf收据_python如何与以太坊交互并将区块链信息写入SQLite
  6. epoll 边沿触发 非阻塞 IO 服务器
  7. QT的QStandardItemEditorCreator类的使用
  8. (计算机组成原理)第二章数据的表示和运算-第二节5:定点数乘法运算(原码/补码一位乘法)
  9. float,absolute脱离文档流的总结
  10. Linux获得命令帮助(学习笔记五)
  11. Python之路--协程/IO多路复用
  12. freemarker在线编辑
  13. 百度地图API自定义地图
  14. 【数据结构-栈】C语言实现顺序栈基本操作
  15. 【专升本计算机】计算机文化基础练习题(选择题300道附答案)
  16. 车载显示屏刷鸿蒙系统,华为智能品鉴会如期进行,鸿蒙系统车载显示屏首次亮相...
  17. Javapython实现网页内容自动识别与提取技术实现
  18. 南京湖南路学计算机哪家好,南京“最好吃餐厅排行榜”,去过8个,你就是超级美食达人......
  19. 心理测试软件沙盘游戏,如何学习沙盘游戏
  20. 万有引力不存在及爱因斯坦相对论中时空扭曲都是错误的

热门文章

  1. 2.语音增强短时谱估计算法——幅度谱减法
  2. QQ文件保险柜与Truecrypt之对比.
  3. JS 实现右击菜单功能
  4. python 横向拼接_python实现横向拼接图片
  5. js 打开选择本地文件对话框 及 获取选择文件中的内容
  6. k-anonimity、l-diversity 和 t-closeness
  7. 苹果ipadmini1 主板 电路图 超清
  8. java ip地址定位,Java根据IP地址定位位置
  9. word另存为pdf后,pdf文件中有空白页
  10. 桌面美化 | win10高仿mac桌面