Skip to content

Commit

Permalink
Geo spatial: 2. Add geo functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Oct 8, 2021
1 parent 9462d35 commit 4d52a75
Show file tree
Hide file tree
Showing 37 changed files with 2,686 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ Checks: '-*,clang-diagnostic-*,clang-analyzer-*,-misc-unused-parameters,
modernize-use-emplace,
modernize-use-equals-default,
modernize-use-equals-delete,
modernize-use-nodiscard,
-modernize-use-nodiscard,
modernize-use-nullptr,
modernize-use-override,
modernize-use-trailing-return-type,
-modernize-use-trailing-return-type,
modernize-use-transparent-functors,
modernize-use-using,
Expand Down
15 changes: 13 additions & 2 deletions src/common/datatypes/Geography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ StatusOr<Geography> Geography::fromWKT(const std::string& wkt) {
auto geomRet = WKTReader().read(wkt);
NG_RETURN_IF_ERROR(geomRet);
auto geom = geomRet.value();
auto wkb = WKBWriter().write(geom);
return Geography(wkb);
if (!geom.isValid()) {
return Status::Error("It's not valid");
}
auto bytes = WKBWriter().write(geom);
return Geography(bytes);
}

StatusOr<Geography> Geography::fromGeometry(const Geometry& geom) {
if (!geom.isValid()) {
return Status::Error("It's not valid");
}
auto bytes = WKBWriter().write(geom);
return Geography(bytes);
}

GeoShape Geography::shape() const {
Expand Down
2 changes: 2 additions & 0 deletions src/common/datatypes/Geography.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ struct Geography {

Geography() = default;

// Factory method
static StatusOr<Geography> fromWKT(const std::string& wkt);
static StatusOr<Geography> fromGeometry(const Geometry& geom);

GeoShape shape() const;

Expand Down
20 changes: 14 additions & 6 deletions src/common/datatypes/test/ValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "common/datatypes/Set.h"
#include "common/datatypes/Value.h"
#include "common/datatypes/Vertex.h"
#include "common/geo/io/Geometry.h"

namespace nebula {

Expand Down Expand Up @@ -1156,12 +1157,19 @@ TEST(Value, Ctor) {
Value vMap(Map({{"a", 9}, {"b", 10}}));
EXPECT_TRUE(vMap.isMap());
// TODO(jie) Add more geography value test
Value vGeoPoint(Geography::fromWKT("POINT(0 1)").value());
EXPECT_TRUE(vGeoPoint.isGeography());
Value vGeoLine(Geography::fromWKT("LINESTRING(0 1,2 7)").value());
EXPECT_TRUE(vGeoLine.isGeography());
Value vGeoPolygon(Geography::fromWKT("POLYGON((0 1,2 3,4 5,6 7,0 1),(2 4,5 6,3 8,2 4))").value());
EXPECT_TRUE(vGeoPolygon.isGeography());
Geometry geomPoint(Point(Coordinate(3, 7)));
Value vGeogPoint{Geography::fromGeometry(geomPoint).value()};
EXPECT_TRUE(vGeogPoint.isGeography());
Geometry geomLine(LineString(std::vector<Coordinate>{Coordinate(0, 1), Coordinate(2, 7)}));
Value vGeogLine{Geography::fromGeometry(geomLine).value()};
EXPECT_TRUE(vGeogLine.isGeography());
Geometry geomPolygon(Polygon(std::vector<std::vector<Coordinate>>{
std::vector<Coordinate>{
Coordinate(0, 1), Coordinate(2, 3), Coordinate(4, 5), Coordinate(6, 7), Coordinate(0, 1)},
std::vector<Coordinate>{
Coordinate(2, 4), Coordinate(5, 6), Coordinate(3, 8), Coordinate(2, 4)}}));
Value vGeogPolygon{Geography::fromGeometry(geomPolygon).value()};
EXPECT_TRUE(vGeogPolygon.isGeography());
// Disabled
// Lead to compile error
// Value v(nullptr);
Expand Down
6 changes: 6 additions & 0 deletions src/common/function/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
nebula_add_library(
function_manager_obj OBJECT
FunctionManager.cpp
../geo/function/Covers.cpp
../geo/function/Distance.cpp
../geo/function/DWithin.cpp
../geo/function/Intersects.cpp
../geo/function/IsValid.cpp
../geo/function/S2Util.cpp
)

nebula_add_library(
Expand Down
Loading

0 comments on commit 4d52a75

Please sign in to comment.