gpt4 book ai didi

c++ - GEOS OverlayOp 相交操作

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:40 26 4
gpt4 key购买 nike

我正在使用 GEOS 3.6.2 计算两个多边形之间的交点。我能够构建我的多边形,但是当我尝试计算交点时它不起作用。

在 Debug模式下编译我的程序,我收到错误信息:

The inferior stopped because it received a signal from the operating system.

Signal name : SIGSEG

Signal meaning : Segmentation fault

知道我哪里错了吗?

这是我的代码:

#include <geos/geom/Polygon.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/operation/overlay/OverlayOp.h>
#include <iostream>
#include <array>

////////////////////////////////////////////////////////////////////////////////
geos::geom::Polygon* MakePoly(std::vector<std::vector<int>> const& polyCoords)
{
geos::geom::GeometryFactory* factory = geos::geom::GeometryFactory::create().get();
geos::geom::CoordinateSequence* temp = factory->getCoordinateSequenceFactory()->create((std::size_t) 0, 0);

std::vector<std::vector<int>>::const_iterator it_x = polyCoords.begin();
int size = it_x->size();

for (int i=0; i<size; i++)
{
temp->add(geos::geom::Coordinate(polyCoords[0][i], polyCoords[1][i]));
}

geos::geom::LinearRing *shell=factory->createLinearRing(temp);


//NULL in this case could instead be a collection of one or more holes
//in the interior of the polygon
return factory->createPolygon(shell,NULL);
}


////////////////////////////////////////////////////////////////////////////////
int main()
{
// Create geometry.
std::vector<std::vector<int>> polyCoords1 = {
{1, 1, 2, 2, 1, 1, 4, 5, 4, 1},
{1, 2, 2, 4, 4, 5, 5, 3, 1, 1}
};

geos::geom::Polygon* poly1 = MakePoly(polyCoords1);

std::vector<std::vector<int>> polyCoords2 = {
{4, 4, 6, 6, 4},
{1, 5, 5, 1, 1}
};

geos::geom::Polygon* poly2 = MakePoly(polyCoords2);

// Actually perform the operation.
geos::operation::overlay::OverlayOp intersection(poly1, poly2);

// Extracting the geometry of the intersection (position of the error).
geos::geom::Geometry* intersectionGeo = intersection.getResultGeometry( geos::operation::overlay::OverlayOp::OpCode::opINTERSECTION );

std::cout<<intersectionGeo->getArea()<<std::endl;
}

最佳答案

您的代码中的问题是获取 GeometryFactory 指针。

geos::geom::GeometryFactory::create() 返回一个智能指针 (std::unique_ptr) 所以在这一行之后:

geos::geom::GeometryFactory* factory = geos::geom::GeometryFactory::create().get();

create 返回的 unique_ptr 被释放。

更改该行:

geos::geom::GeometryFactory::Ptr factory = geos::geom::GeometryFactory::create();

并且代码有效。

关于c++ - GEOS OverlayOp 相交操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49636462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com