1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
//
// Test Suite for geos::algorithm::Area
// Ported from JTS junit/algorithm/AreaTest.java
#include <tut/tut.hpp>
// geos
#include <geos/algorithm/Area.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/Dimension.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LineString.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/io/WKTReader.h>
// std
#include <string>
#include <memory>
using namespace geos;
using namespace geos::geom;
using geos::algorithm::Area;
namespace tut {
//
// Test Group
//
// dummy data, not used
struct test_area_data {
geos::geom::Geometry* geom_;
geos::geom::PrecisionModel pm_;
geos::geom::GeometryFactory::Ptr factory_;
geos::io::WKTReader reader_;
test_area_data():
geom_(nullptr),
pm_(),
factory_(GeometryFactory::create(&pm_, 0)), reader_(factory_.get())
{
assert(nullptr == geom_);
}
~test_area_data()
{
factory_->destroyGeometry(geom_);
geom_ = nullptr;
}
void
checkAreaOfRing(std::string wkt, double expectedArea)
{
auto ringGeom = reader_.read<Curve>(wkt);
if (const LineString* line = dynamic_cast<const LineString*>(ringGeom.get())) {
const CoordinateSequence* ringSeq = line->getCoordinatesRO();
std::vector<Coordinate> ringCoords;
ringSeq->toVector(ringCoords);
double actual1 = algorithm::Area::ofRing(ringCoords);
double actual2 = algorithm::Area::ofRing(ringSeq);
ensure_equals(actual1, expectedArea);
ensure_equals(actual2, expectedArea);
}
double actual3 = algorithm::Area::ofClosedCurve(*ringGeom);
ensure_equals("Area::ofClosedCurve", actual3, expectedArea, 1e-6);
}
void
checkAreaOfRingSigned(std::string wkt, double expectedArea)
{
auto line = reader_.read<LineString>(wkt);
ensure(nullptr != line.get());
const CoordinateSequence* ringSeq = line->getCoordinatesRO();
std::vector<Coordinate> ringCoords;
ringSeq->toVector(ringCoords);
double actual1 = algorithm::Area::ofRingSigned(ringCoords);
double actual2 = algorithm::Area::ofRingSigned(ringSeq);
ensure_equals(actual1, expectedArea);
ensure_equals(actual2, expectedArea);
}
};
typedef test_group<test_area_data> group;
typedef group::object object;
group test_area_group("geos::algorithm::Area");
//
// Test Cases
//
template<>
template<>
void object::test<1>
()
{
checkAreaOfRing("LINESTRING (100 200, 200 200, 200 100, 100 100, 100 200)", 10000.0);
}
template<>
template<>
void object::test<2>
()
{
checkAreaOfRingSigned("LINESTRING (100 200, 200 200, 200 100, 100 100, 100 200)", 10000.0);
}
template<>
template<>
void object::test<3>
()
{
checkAreaOfRingSigned("LINESTRING (100 200, 100 100, 200 100, 200 200, 100 200)", -10000.0);
}
template<>
template<>
void object::test<4>
()
{
checkAreaOfRing("CIRCULARSTRING (0 0, 2 2, 4 0, 2 -2, 0 0)", 4*MATH_PI);
}
template<>
template<>
void object::test<5>
()
{
checkAreaOfRing("COMPOUNDCURVE (CIRCULARSTRING (0 0, 2 2, 4 0), (4 0, 0 0))", 2*MATH_PI);
}
template<>
template<>
void object::test<6>
()
{
// expected area from PostGIS after ST_CurveToLine(geom, 1e-13, 1)
checkAreaOfRing("CIRCULARSTRING (0 0, 2 2, 4 0, 2 1, 0 0)", 3.48759);
}
template<>
template<>
void object::test<7>
()
{
// expected area from PostGIS after ST_CurveToLine(geom, 1e-13, 1)
checkAreaOfRing("COMPOUNDCURVE (CIRCULARSTRING (0 0, 2 0, 2 1, 2 3, 4 3, 4 5, 1 4, 0.5 0.8, 0 0))", 11.243342);
checkAreaOfRing("COMPOUNDCURVE (CIRCULARSTRING (0 0, 2 0, 2 1, 2 3, 4 3), (4 3, 4 5, 1 4, 0 0))", 9.321903);
}
} // namespace tut
|