GEOS使用记录【几何运算】

GEOS使用记录【几何运算】

1.geos::geom::LineSegment相关计算

1.1 angle

计算的值为弧度,通过乘以180°除以Π得到角度,角度分正负,上半轴为正,下半轴为负;

        geos::geom::LineSegment l1 = geos::geom::LineSegment(0,0,1,0);
        geos::geom::LineSegment l2 = geos::geom::LineSegment(0,0,1,-1);
        geos::geom::LineSegment l3 = geos::geom::LineSegment(1,-1,0,0);
        qDebug()<<l1.angle()<<"       "<<l2.angle()*180/M_PI<<"     "<<l3.angle()*180/M_PI;

在这里插入图片描述

1.2 segmentFraction

给定一个点可以计算分位数,当点不在直线上时,通过计算垂足得到分位数;

        double s = l1.segmentFraction({0.4,0});

2.geos::geom::Geometry相关计算

2.1.getBoundary

当Geometry中有多个polygon的图形时,返回的是多个图形的MULTILINESTRING类型数据,当只有一个图形时,返回LINESTRING类型数据。

   geos::io::WKTReader reader;
   geos::io::WKTWriter writer;
   std::unique_ptr<geos::geom::Geometry> poly1 = reader.read("POLYGON((0 0,1 0,1 1,0 1,0 0))");
   std::unique_ptr<geos::geom::Geometry> poly4 = reader.read("POLYGON((0.5 0.5,1.5 0.5,1.5 1.5,0.5 1.5,0.5 0.5))");
   std::unique_ptr<geos::geom::Geometry> poly5 = reader.read("POLYGON((1 1,2 1,2 2,1 2,1 1))");
   std::unique_ptr<geos::geom::Geometry> x1 = poly1->Union(poly4.get());
   std::unique_ptr<geos::geom::Geometry> x2 = poly1->Union(poly5.get());
   std::unique_ptr<Geometry> f = x1->getBoundary();
   qDebug()<<QString::fromStdString(writer.write(f.get()));
   f = x2->getBoundary();
   qDebug()<<QString::fromStdString(writer.write(f.get()));

2.2.union

   geos::io::WKTReader reader;
   geos::io::WKTWriter writer;
   std::unique_ptr<geos::geom::Geometry> poly1 = reader.read("POLYGON((0 0,1 0,1 1,0 1,0 0))");
   std::unique_ptr<geos::geom::Geometry> poly2 = reader.read("POLYGON((0 2,2 0,3 1,1 2,0 2))");
   std::unique_ptr<geos::geom::Geometry> poly4 = reader.read("POLYGON((0.5 0.5,1.5 0.5,1.5 1.5,0.5 1.5,0.5 0.5))");
   std::unique_ptr<geos::geom::Geometry> x1 = poly1->Union(poly4.get());
   qDebug()<<x1->getNumGeometries();
   std::unique_ptr<geos::geom::Geometry> x2 = poly1->Union(poly2.get());
   qDebug()<<x2->getNumGeometries();

打印结果是1,2,结果说明如果两个图形相交了会合并成一个,如果两个图形不相交会有两个图形;如果连续合并,原来两个分开的图形会因为poly4的关系再次合并成一个。

   std::unique_ptr<geos::geom::Geometry> x3 = poly1->Union(poly2.get())->Union(poly4.get());
   qDebug()<<x3->getNumGeometries();

2.3.getEnvelope

该方法返回的是一个外接矩形,不管是多图形的情况还是单个图形仅返回一个矩形框,矩形框的范围囊括了所有图形。

   geos::io::WKTReader reader;
   geos::io::WKTWriter writer;
   std::unique_ptr<geos::geom::Geometry> poly1 = reader.read("POLYGON((0 0,1 0,1 1,0 1,0 0))");

  std::unique_ptr<geos::geom::Geometry> poly4 = reader.read("POLYGON((0.5 0.5,1.5 0.5,1.5 1.5,0.5 1.5,0.5 0.5))");
   std::unique_ptr<geos::geom::Geometry> poly5 = reader.read("POLYGON((1 1,2 1,2 2,1 2,1 1))");
   std::unique_ptr<geos::geom::Geometry> x1 = poly1->Union(poly4.get());
   std::unique_ptr<geos::geom::Geometry> x2 = poly1->Union(poly5.get());
   std::unique_ptr<Geometry> f = x1->getEnvelope();
   qDebug()<<QString::fromStdString(writer.write(f.get()));
   f = x2->getEnvelope();
   qDebug()<<QString::fromStdString(writer.write(f.get()));

打印结果:

"POLYGON ((0 0, 1.5 0, 1.5 1.5, 0 1.5, 0 0))"
"POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))"

2.4.convexHull

该方法返回的是一个外接图形,但不一定是矩形,有时跟getEnvelope的结果类似,比如上面的情况。

std::unique_ptr<geos::geom::Geometry> f = x1->convexHull();
qDebug()<<QString::fromStdString(writer.write(f.get()));
f = x2->convexHull();
qDebug()<<QString::fromStdString(writer.write(f.get()));

更多示例:

std::unique_ptr<geos::geom::Geometry> poly1 = reader.read("POLYGON((1 0,2 0,2 0.2,1 0.2,1 0))");
std::unique_ptr<geos::geom::Geometry> poly2 = reader.read("POLYGON((1 0,1 0.2,0.2 1,0 1,1 0))");
std::unique_ptr<geos::geom::Geometry> poly3 = reader.read("POLYGON((0 1,0.2 1,0.2 2,0 2,0 1))");
std::unique_ptr<geos::geom::Geometry> x1 = poly1->Union(poly2.get());
std::unique_ptr<geos::geom::Geometry> x2 = x1->Union(poly3.get());

std::unique_ptr<geos::geom::Geometry> f = x2->convexHull();
qDebug()<<QString::fromStdString(writer.write(f.get()));
f = x2->getBoundary();
qDebug()<<QString::fromStdString(writer.write(f.get()));
f = x2->getEnvelope();
qDebug()<<QString::fromStdString(writer.write(f.get()));

打印结果:

"POLYGON ((1 0, 0 1, 0 2, 0.2 2, 2 0.2, 2 0, 1 0))"
"LINESTRING (2 0, 1 0, 0 1, 0 2, 0.2 2, 0.2 1, 1 0.2, 2 0.2, 2 0)"
"POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))"

2.5.project

该方法在原线上找到与参考线最近的两个点,按照参考线的顺序返回segment类型。

geos::geom::LineSegment l1 = geos::geom::LineSegment(0,0,10,0);
geos::geom::LineSegment l2 = geos::geom::LineSegment(-1,1,9,1);
geos::geom::LineSegment ret;
l1.project(l2, ret);//在原线上找到与参考线最近的两个点,按照参考线的顺序
qDebug()<<"ret result"<<ret.p0.x<<ret.p0.y<<ret.p1.x<<ret.p1.y;

打印结果:

ret result 0 0 9 0

3.geos::geom::MultiPoint

创建一个多点的方法

std::unique_ptr<geos::geom::Geometry> poly1 = reader.read("POLYGON((1 0,2 0,2 0.2,1 0.2,1 0))");
//    std::unique_ptr<geos::geom::Geometry> poly2 = reader.read("POLYGON((1 0,1 0.2,0.2 1,0 1,1 0))");
std::unique_ptr<geos::geom::Geometry> poly3 = reader.read("POLYGON((0 1,0.2 1,0.2 2,0 2,0 1))");
//    std::unique_ptr<geos::geom::Geometry> x1 = poly1->Union(poly2.get());
std::unique_ptr<geos::geom::Geometry> x2 = poly1->Union(poly3.get());
std::vector<geos::geom::Coordinate> seq;
for (int var = 0; var < x2->getNumGeometries(); ++var) {
   for (int j = 0; j < x2->getGeometryN(var)->getNumPoints(); ++j) {
       geos::geom::Coordinate c = x2->getGeometryN(var)->getCoordinates()->getAt(j);
       seq.push_back(c);
   }
}
std::unique_ptr<geos::geom::MultiPoint> points = factory->createMultiPoint(seq);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Linux上编译GDAL和GEOS,并使用Java调用它们,您可以按照以下步骤进行操作: 1. 首先,按照前面提到的步骤在Linux上编译和安装GEOS。 2. 接下来,下载GDAL源代码并解压缩。您可以从GDAL的官方网站(https://gdal.org/)上找到最新版本的下载链接。 3. 进入解压后的GDAL源代码目录: ``` cd gdal-x.x.x ``` 4. 运行以下命令来配置GDAL的编译选项,并指定GEOS的路径: ``` ./configure --with-geos=/usr/local ``` 确保将`/usr/local`替换为您实际安装GEOS的路径。 5. 编译和安装GDAL: ``` make sudo make install ``` 6. 完成上述步骤后,GDAL将成功编译和安装在您的Linux系统中。 7. 使用Java调用GDAL和GEOS需要使用Java绑定库。安装Java绑定库可以通过以下步骤完成: - 下载Java绑定库(JavaGDAL)的源代码。您可以从GDAL官方网站(https://gdal.org/java/)上找到相应的下载链接。 - 解压缩下载的源代码文件,并进入解压后的目录。 - 编译和安装Java绑定库: ``` make sudo make install ``` 8. 在使用Java调用GDAL和GEOS之前,确保您的Java项目中已经正确设置了相关的类路径和依赖项。 例如,您可以使用以下命令设置类路径: ``` export CLASSPATH=/usr/local/share/java/gdal.jar:$CLASSPATH ``` 确保将`/usr/local/share/java/gdal.jar`替换为实际的gdal.jar文件路径。 9. 在您的Java代码中,您可以使用GDAL和GEOS的API来执行各种地理空间操作和分析。 例如,您可以使用以下示例代码加载一个矢量文件并执行缓冲区分析: ```java import org.gdal.ogr.DataSource; import org.gdal.ogr.Layer; public class GDALExample { public static void main(String[] args) { // 打开矢量文件 String filePath = "path/to/your/vector/file.shp"; DataSource dataSource = ogr.Open(filePath); // 获取第一个图层 Layer layer = dataSource.GetLayer(0); // 执行缓冲区分析等其他地理空间操作... } } ``` 请注意,以上步骤和示例代码仅提供了一个基本的指导,具体的设置和操作可能因您的环境和需求而有所不同。确保您在使用之前阅读并遵循相关的官方文档和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值