How Do I Make A Rectangle in GEOS? [closed]

北战南征 提交于 2019-12-11 02:39:32

问题


How do you make a rectangle using the GEOS library's C++ API?


回答1:


The following implementation gets the job done in 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
}

For reference, you can also accomplish this using the boost::polygon library, as follows.

//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;
}

And also with Clipper. Note that Clipper cannot use floating-point coordinates.

#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;
}


来源:https://stackoverflow.com/questions/27643210/how-do-i-make-a-rectangle-in-geos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!