首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GEOS OverlayOp交叉口操作

GEOS OverlayOp交叉口操作
EN

Stack Overflow用户
提问于 2018-04-03 18:01:33
回答 1查看 790关注 0票数 1

我使用GEOS 3.6.2计算两个多边形之间的交集。我能够构造我的多边形,但是当我试图计算交点时,它就不能工作了。

在调试模式下编译我的程序,我会得到错误消息:

因为接收到来自操作系统的信号,次方停止了操作。 信号名称: SIGSEG 信号意义:分割故障

知道我哪里错了吗?

这是我的代码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2018-08-17 08:55:24

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

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

代码语言:javascript
复制
geos::geom::GeometryFactory* factory = geos::geom::GeometryFactory::create().get();

将释放create返回的unique_ptr。

将这一行改为:

代码语言:javascript
复制
geos::geom::GeometryFactory::Ptr factory = geos::geom::GeometryFactory::create();

密码起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49636462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档