如何在GEOS中制作矩形?

时间:2014-12-25 00:01:32

标签: c++ geos

如何使用GEOS库的C ++ API制作矩形?

1 个答案:

答案 0 :(得分:6)

以下实施可在GEOS中完成工作。

//Compile with: g++ code.cpp -lgeos
//Updated: 2019-03-31
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <iostream>
#include <memory>

geos::geom::Polygon* MakeBox(double xmin, double ymin, double xmax, double ymax){
  std::unique_ptr<geos::geom::PrecisionModel> pm(new geos::geom::PrecisionModel());

  geos::geom::GeometryFactory::unique_ptr factory = geos::geom::GeometryFactory::create(pm.get(), -1);
  geos::geom::CoordinateSequence *temp = factory->getCoordinateSequenceFactory()->create((std::size_t) 0, 0);

  temp->add(geos::geom::Coordinate(xmin, ymin));
  temp->add(geos::geom::Coordinate(xmin, ymax));
  temp->add(geos::geom::Coordinate(xmax, ymax));
  temp->add(geos::geom::Coordinate(xmax, ymin));
  //Must close the linear ring or we will get an error:
  //"Points of LinearRing do not form a closed linestring"
  temp->add(geos::geom::Coordinate(xmin, ymin));

  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(){
  geos::geom::Polygon* box = MakeBox(0,0,10,10);
  std::cout<<box->getArea()<<std::endl;
  delete box; //Important to avoid memory leaks
}

作为参考,您也可以使用boost::polygon库完成此操作,如下所示。

//Compile with: g++ code.cpp
#include <boost/polygon/polygon.hpp>
#include <iostream>
namespace gtl = boost::polygon;

typedef gtl::polygon_data<float> Polygon;

Polygon MakeBox(float xmin, float ymin, float xmax, float ymax){
  typedef gtl::polygon_traits<Polygon>::point_type Point;
  Point pts[] = {
    gtl::construct<Point>(xmin, ymin),
    gtl::construct<Point>(xmin, ymax),
    gtl::construct<Point>(xmax, ymax),
    gtl::construct<Point>(xmax, ymin)
  };
  Polygon poly;
  gtl::set_points(poly, pts, pts+4);

  return poly;
}

int main(){
  Polygon box = MakeBox(0,0,10,10);
  std::cout<<gtl::area(box)<<std::endl;
}

还有Clipper。请注意,Clipper无法使用浮点坐标。

#include "clipper_cpp/clipper.hpp"
#include <iostream>

ClipperLib::Paths MakeBox(int xmin, int ymin, int xmax, int ymax){
  ClipperLib::Paths box(1);
  //Note that we run the path in the reverse direction to previous examples in
  //order to maintain positive area
  box[0] << ClipperLib::IntPoint(xmin,ymin) << ClipperLib::IntPoint(xmax,ymin)
         << ClipperLib::IntPoint(xmax,ymax) << ClipperLib::IntPoint(xmin,ymax);
  return box;
}

int main(){
  ClipperLib::Paths box = MakeBox(0,0,10,10);
  std::cout<<ClipperLib::Area(box[0])<<std::endl;
}