Skip to content

Commit

Permalink
Add tests for curved geometry types
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Mar 3, 2024
1 parent 9ae407d commit c85565c
Show file tree
Hide file tree
Showing 29 changed files with 758 additions and 120 deletions.
6 changes: 5 additions & 1 deletion include/geos/algorithm/ConvexHull.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <geos/util/UniqueCoordinateArrayFilter.h>
#include <geos/util/CoordinateArrayFilter.h>

#include "geos/util.h"

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
Expand Down Expand Up @@ -178,7 +180,9 @@ class GEOS_DLL ConvexHull {
ConvexHull(const geom::Geometry* newGeometry)
: inputGeom(newGeometry)
, geomFactory(newGeometry->getFactory())
{};
{
util::ensureNotCurvedType(inputGeom);
};

~ConvexHull() {};

Expand Down
5 changes: 5 additions & 0 deletions include/geos/geom/CircularString.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <geos/geom/SimpleCurve.h>
#include <geos/util/UnsupportedOperationException.h>

namespace geos {
namespace geom {
Expand Down Expand Up @@ -47,6 +48,10 @@ class GEOS_DLL CircularString : public SimpleCurve {
return SORTINDEX_LINESTRING;
};

double getLength() const override {
throw util::UnsupportedOperationException("Cannot calculate length of CircularString");
}

};


Expand Down
20 changes: 7 additions & 13 deletions include/geos/geom/CompoundCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <geos/geom/SimpleCurve.h>
#include <geos/util.h>
#include <vector>

namespace geos {
Expand Down Expand Up @@ -48,10 +49,6 @@ class GEOS_DLL CompoundCurve : public Curve {

const SimpleCurve* getCurveN(std::size_t) const;

std::size_t getNumGeometries() const override;

const Geometry* getGeometryN(std::size_t) const override;

std::size_t getNumPoints() const override;

std::unique_ptr<Geometry> getBoundary() const override;
Expand All @@ -78,17 +75,14 @@ class GEOS_DLL CompoundCurve : public Curve {

CompoundCurve* reverseImpl() const override;

void apply_rw(const CoordinateFilter* filter) override;

void apply_ro(CoordinateFilter* filter) const override;

void apply_rw(GeometryFilter* filter) override;
double getLength() const override;

void apply_ro(GeometryFilter* filter) const override;
using Curve::apply_ro;
using Curve::apply_rw;

void apply_rw(GeometryComponentFilter* filter) override;
void apply_rw(const CoordinateFilter* filter) override;

void apply_ro(GeometryComponentFilter* filter) const override;
void apply_ro(CoordinateFilter* filter) const override;

void apply_rw(CoordinateSequenceFilter& filter) override;

Expand All @@ -113,7 +107,7 @@ class GEOS_DLL CompoundCurve : public Curve {
envelope = computeEnvelopeInternal();
}

Envelope computeEnvelopeInternal();
Envelope computeEnvelopeInternal() const;

private:
std::vector<std::unique_ptr<SimpleCurve>> curves;
Expand Down
11 changes: 11 additions & 0 deletions include/geos/geom/Curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class GEOS_DLL Curve : public Geometry {

virtual bool isClosed() const = 0;

using Geometry::apply_ro;
using Geometry::apply_rw;

void apply_rw(GeometryFilter* filter) override;

void apply_ro(GeometryFilter* filter) const override;

void apply_rw(GeometryComponentFilter* filter) override;

void apply_ro(GeometryComponentFilter* filter) const override;

protected:

Curve(const GeometryFactory& factory) : Geometry(&factory) {}
Expand Down
5 changes: 5 additions & 0 deletions include/geos/geom/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ class GEOS_DLL Geometry {
/// Return a string representation of this Geometry type
virtual std::string getGeometryType() const = 0; //Abstract

/// Returns whether the Geometry type _may_ contain curved elements
virtual bool isCurvedType() const;

static bool isCurvedType(GeometryTypeId);

/// Return an integer representation of this Geometry type
virtual GeometryTypeId getGeometryTypeId() const = 0; //Abstract

Expand Down
13 changes: 4 additions & 9 deletions include/geos/geom/SimpleCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GEOS_DLL SimpleCurve : public Curve {

bool isRing() const;

virtual bool isCoordinate(Coordinate& pt) const;
virtual bool isCoordinate(CoordinateXY& pt) const;

bool equalsExact(const Geometry* other, double tolerance = 0)
const override;
Expand All @@ -99,18 +99,13 @@ class GEOS_DLL SimpleCurve : public Curve {
return &envelope;
}

using Curve::apply_ro;
using Curve::apply_rw;

void apply_rw(const CoordinateFilter* filter) override;

void apply_ro(CoordinateFilter* filter) const override;

void apply_rw(GeometryFilter* filter) override;

void apply_ro(GeometryFilter* filter) const override;

void apply_rw(GeometryComponentFilter* filter) override;

void apply_ro(GeometryComponentFilter* filter) const override;

void apply_rw(CoordinateSequenceFilter& filter) override;

void apply_ro(CoordinateSequenceFilter& filter) const override;
Expand Down
4 changes: 4 additions & 0 deletions include/geos/operation/union/UnaryUnionOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <geos/geom/util/GeometryExtracter.h>
#include <geos/operation/union/CascadedPolygonUnion.h>

#include <geos/util.h>

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
Expand Down Expand Up @@ -169,6 +171,8 @@ class GEOS_DLL UnaryUnionOp {
void
extract(const geom::Geometry& geom)
{
util::ensureNotCurvedType(geom);

using namespace geom::util;

if(! geomFact) {
Expand Down
23 changes: 23 additions & 0 deletions include/geos/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cassert>
#include <memory>
#include <type_traits>
#include <geos/util/UnsupportedOperationException.h>

//
// Private macros definition
Expand Down Expand Up @@ -59,6 +60,28 @@ template<typename To, typename From> inline To down_cast(From* f)
}

} // namespace detail

namespace util {

template<typename T>
void ensureNotCurvedType(const T* geom)
{
if (geom->isCurvedType()) {
throw UnsupportedOperationException("Curved geometry types are not supported.");
}
}

template<typename T>
void ensureNotCurvedType(const T& geom)
{
if (geom.isCurvedType()) {
throw UnsupportedOperationException("Curved geometry types are not supported.");
}
}

}


} // namespace geos

#endif // GEOS_UTIL_H
4 changes: 4 additions & 0 deletions src/algorithm/Centroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <cmath> // for std::abs

#include "geos/util.h"

using namespace geos::geom;

namespace geos {
Expand Down Expand Up @@ -68,6 +70,8 @@ Centroid::getCentroid(CoordinateXY& cent) const
void
Centroid::add(const Geometry& geom)
{
util::ensureNotCurvedType(geom);

if(geom.isEmpty()) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/geom/CircularString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <geos/geom/CircularString.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/util/UnsupportedOperationException.h>

namespace geos {
namespace geom {
Expand Down
81 changes: 33 additions & 48 deletions src/geom/CompoundCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
**********************************************************************/

#include <geos/geom/CoordinateFilter.h>
#include <geos/geom/CompoundCurve.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/operation/BoundaryOp.h>
Expand All @@ -23,7 +24,8 @@ namespace geom {
CompoundCurve::CompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&& p_curves,
const GeometryFactory& gf)
: Curve(gf),
curves(std::move(p_curves)) {}
curves(std::move(p_curves)),
envelope(computeEnvelopeInternal()) {}

CompoundCurve::CompoundCurve(const CompoundCurve& other)
: Curve(other),
Expand Down Expand Up @@ -130,24 +132,12 @@ CompoundCurve::getNumCurves() const
return curves.size();
}

std::size_t
CompoundCurve::getNumGeometries() const
{
return curves.size();
}

const SimpleCurve*
CompoundCurve::getCurveN(std::size_t i) const
{
return curves[i].get();
}

const Geometry*
CompoundCurve::getGeometryN(std::size_t i) const
{
return curves[i].get();
}

std::unique_ptr<Geometry>
CompoundCurve::getBoundary() const
{
Expand Down Expand Up @@ -232,16 +222,23 @@ CompoundCurve*
CompoundCurve::reverseImpl() const
{
std::vector<std::unique_ptr<SimpleCurve>> reversed(curves.size());
std::transform(curves.rbegin(), curves.rend(), reversed.end(), [](const auto& curve) {
std::transform(curves.rbegin(), curves.rend(), reversed.begin(), [](const auto& curve) {
return std::unique_ptr<SimpleCurve>(static_cast<SimpleCurve*>(curve->reverse().release()));
});

return getFactory()->createCompoundCurve(std::move(reversed)).release();
}

double CompoundCurve::getLength() const {
double sum = 0;
for (const auto& curve : curves) {
sum += curve->getLength();
}
return sum;
}

Envelope
CompoundCurve::computeEnvelopeInternal()
{
CompoundCurve::computeEnvelopeInternal() const {
Envelope e;
for (const auto& curve : curves) {
e.expandToInclude(curve->getEnvelopeInternal());
Expand All @@ -259,55 +256,43 @@ CompoundCurve::compareToSameClass(const Geometry* g) const
void
CompoundCurve::normalize()
{
throw std::runtime_error("Not implemented.");
}

void
CompoundCurve::apply_ro(GeometryFilter*) const
{
throw std::runtime_error("Not implemented.");
throw util::UnsupportedOperationException();
}

void
CompoundCurve::apply_ro(GeometryComponentFilter*) const
CompoundCurve::apply_ro(CoordinateFilter* cf) const
{
throw std::runtime_error("Not implemented.");
}

void
CompoundCurve::apply_ro(CoordinateFilter*) const
{
throw std::runtime_error("Not implemented.");
}

void
CompoundCurve::apply_ro(CoordinateSequenceFilter&) const
{
throw std::runtime_error("Not implemented.");
}

void
CompoundCurve::apply_rw(GeometryFilter*)
{
throw std::runtime_error("Not implemented.");
for (const auto& curve : curves) {
curve->apply_ro(cf);
}
}

void
CompoundCurve::apply_rw(GeometryComponentFilter*)
CompoundCurve::apply_ro(CoordinateSequenceFilter& csf) const
{
throw std::runtime_error("Not implemented.");
for (const auto& curve : curves) {
const auto& seq = *curve->getCoordinatesRO();
for (std::size_t i = 0; i < seq.size(); i++) {
if (csf.isDone()) {
return;
}
csf.filter_ro(seq, i);
}
}
}

void
CompoundCurve::apply_rw(const CoordinateFilter*)
CompoundCurve::apply_rw(const CoordinateFilter* cf)
{
throw std::runtime_error("Not implemented.");
for (auto& curve : curves) {
curve->apply_rw(cf);
}
}

void
CompoundCurve::apply_rw(CoordinateSequenceFilter&)
{
throw std::runtime_error("Not implemented.");
throw util::UnsupportedOperationException();
}


Expand Down
Loading

0 comments on commit c85565c

Please sign in to comment.