Skip to content

Commit

Permalink
Use factory::toGeometry to convert envelope to geometry to evaluate
Browse files Browse the repository at this point in the history
Signed-off-by: Kunlin Yu <[email protected]>
  • Loading branch information
kunlinyu committed Dec 27, 2024
1 parent 7a49ca8 commit 7ada101
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
37 changes: 31 additions & 6 deletions include/cql2cpp/node_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <variant>

#include "evaluator.h"
#include "geos/geom/Envelope.h"
#include "geos/geom/Geometry.h"
#include "geos/geom/GeometryFactory.h"

namespace cql2cpp {

Expand All @@ -26,7 +28,7 @@ bool CheckValueNumberType(const std::string& op, size_t num,
return false;
}
for (size_t i = 0; i < num; i++)
if (not std::holds_alternative<ValueType>(vs.at(0))) {
if (not std::holds_alternative<ValueType>(vs.at(i))) {
*errmsg = "value " + std::to_string(i) + "of " + op + " is incorrect";
return false;
}
Expand Down Expand Up @@ -62,12 +64,35 @@ const std::map<NodeType, std::map<Operator, NodeEval>> node_evals = {
{SpatialPred,
{{S_Intersects,
[](auto n, auto vs, auto fs, auto value, auto errmsg) -> bool {
if (not CheckValueNumberType<const geos::geom::Geometry*>(
"S_INTERSECTS", 2, vs, errmsg))
if (vs.size() != 2) {
*errmsg = "S_INTERSECTS needs two values but we have " +
std::to_string(vs.size());
return false;
}
geos::geom::GeometryFactory::Ptr factory =
geos::geom::GeometryFactory::create();
const geos::geom::Geometry* lhs = nullptr;
std::shared_ptr<geos::geom::Geometry> lhs_shared;
if (std::holds_alternative<const geos::geom::Geometry*>(vs.at(0)))
lhs = std::get<const geos::geom::Geometry*>(vs.at(0));
else if (std::holds_alternative<const geos::geom::Envelope*>(vs.at(0))) {
lhs_shared = factory->toGeometry(std::get<const geos::geom::Envelope*>(vs.at(0)));
lhs = lhs_shared.get();
} else {
*errmsg = "left hand side value type of S_INTERSECTS should be geometry or bbox";
return false;
const geos::geom::Geometry* lhs =
std::get<const geos::geom::Geometry*>(vs.at(0));
auto rhs = std::get<const geos::geom::Geometry*>(vs.at(1));
}
const geos::geom::Geometry* rhs = nullptr;
std::shared_ptr<geos::geom::Geometry> rhs_shared;
if (std::holds_alternative<const geos::geom::Geometry*>(vs.at(1)))
rhs = std::get<const geos::geom::Geometry*>(vs.at(1));
else if (std::holds_alternative<const geos::geom::Envelope*>(vs.at(1))) {
rhs_shared = factory->toGeometry(std::get<const geos::geom::Envelope*>(vs.at(1)));
rhs = rhs_shared.get();
} else {
*errmsg = "right hand side value type of S_INTERSECTS should be geometry or bbox";
return false;
}
*value = lhs->intersects(rhs);
return true;
}}}},
Expand Down
5 changes: 1 addition & 4 deletions test/geojson/1.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
"id": 0,
"type": "Feature",
"geometry": {
"coordinates": [
-70,
5
],
"coordinates": [ 0.5, 0.5 ],
"type": "Point"
},
"properties": {
Expand Down

0 comments on commit 7ada101

Please sign in to comment.