Skip to content

Commit

Permalink
[c++] WKB geometry utilities (#3149)
Browse files Browse the repository at this point in the history
* Add serialization/deserialization support for geometry entities

* Add default constructors

* Extract geometry utilities into separate namespace
Move template function implementation outside of header files

* lint format

* Bump C++ version

* Replace lambdas with struct overloading operator()

* Revert C++ version bump

* Split write operator declaration

* Add named constants

* Moved geometry out of source directory
Address PR comments
  • Loading branch information
XanthosXanthopoulos authored Oct 14, 2024
1 parent 176bbf8 commit 7381508
Show file tree
Hide file tree
Showing 21 changed files with 1,126 additions and 0 deletions.
46 changes: 46 additions & 0 deletions libtiledbsoma/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,55 @@ target_include_directories(TILEDB_SOMA_OBJECTS
${pybind11_INCLUDE_DIRS}
)

# ###########################################################
# tiledbsoma geometry library target
# ###########################################################

add_library(TILEDB_SOMA_GEOMETRY_OBJECTS OBJECT
${CMAKE_CURRENT_SOURCE_DIR}/geometry/point.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/linestring.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/polygon.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/multipoint.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/multilinestring.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/multipolygon.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/operators/io/read.cc
${CMAKE_CURRENT_SOURCE_DIR}/geometry/operators/io/write.cc
)

target_compile_definitions(TILEDB_SOMA_GEOMETRY_OBJECTS
PRIVATE
-DTILEDB_NO_API_DEPRECATION_WARNINGS
)

target_compile_options(TILEDB_SOMA_GEOMETRY_OBJECTS
PRIVATE
${TILEDBSOMA_COMPILE_OPTIONS}
${TILEDBSOMA_WERROR_OPTION}
${TILEDBSOMA_SANITIZER_OPTIONS}
)

set_property(TARGET TILEDB_SOMA_GEOMETRY_OBJECTS PROPERTY POSITION_INDEPENDENT_CODE ON)
target_include_directories(TILEDB_SOMA_GEOMETRY_OBJECTS
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/vendor
${CMAKE_CURRENT_SOURCE_DIR}/soma
${CMAKE_CURRENT_SOURCE_DIR}/external/khash
${CMAKE_CURRENT_SOURCE_DIR}/external/include
${CMAKE_CURRENT_SOURCE_DIR}/external/include/nanoarrow
$<TARGET_PROPERTY:TileDB::tiledb_shared,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:spdlog::spdlog,INTERFACE_INCLUDE_DIRECTORIES>
${pybind11_INCLUDE_DIRS}
)

# ###########################################################
# tiledbsoma library target
# ###########################################################
set(TILEDBSOMA_INSTALL_TARGETS "")
if(TILEDBSOMA_BUILD_STATIC)
add_library(tiledbsoma_static STATIC
$<TARGET_OBJECTS:TILEDB_SOMA_OBJECTS>
$<TARGET_OBJECTS:TILEDB_SOMA_GEOMETRY_OBJECTS>
$<TARGET_OBJECTS:TILEDBSOMA_NANOARROW_OBJECT>
)
list(APPEND TILEDBSOMA_INSTALL_TARGETS tiledbsoma_static)
Expand All @@ -108,6 +150,7 @@ if(TILEDBSOMA_BUILD_STATIC)
else()
add_library(tiledbsoma SHARED
$<TARGET_OBJECTS:TILEDB_SOMA_OBJECTS>
$<TARGET_OBJECTS:TILEDB_SOMA_GEOMETRY_OBJECTS>
$<TARGET_OBJECTS:TILEDBSOMA_NANOARROW_OBJECT>
)
list(APPEND TILEDBSOMA_INSTALL_TARGETS tiledbsoma)
Expand Down Expand Up @@ -222,6 +265,9 @@ set(TILEDB_SOMA_EXPORT_HEADER "${TILEDB_SOMA_EXPORT_HEADER}" PARENT_SCOPE)
target_compile_definitions(TILEDB_SOMA_OBJECTS PRIVATE -DTILEDB_SOMA_OBJECTS_EXPORTS)
target_include_directories(TILEDB_SOMA_OBJECTS PRIVATE ${TILEDB_SOMA_EXPORT_HEADER_DIR})

target_compile_definitions(TILEDB_SOMA_GEOMETRY_OBJECTS PRIVATE -DTILEDB_SOMA_OBJECTS_EXPORTS)
target_include_directories(TILEDB_SOMA_GEOMETRY_OBJECTS PRIVATE ${TILEDB_SOMA_EXPORT_HEADER_DIR})

# Add the generated header to the public headers list
list(APPEND TILEDB_SOMA_PUBLIC_HEADERS
"${TILEDB_SOMA_EXPORT_HEADER}"
Expand Down
30 changes: 30 additions & 0 deletions libtiledbsoma/src/geometry/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TILEDBSOMA_GEOMETRY_BASE_H
#define TILEDBSOMA_GEOMETRY_BASE_H

#include <math.h>
#include <memory>
#include <optional>
#include <vector>

namespace tiledbsoma::geometry {

struct BasePoint {
BasePoint(
double_t x,
double_t y,
std::optional<double_t> z = std::nullopt,
std::optional<double_t> m = std::nullopt)
: x(x)
, y(y)
, z(z)
, m(m) {
}

double_t x;
double_t y;
std::optional<double_t> z;
std::optional<double_t> m;
};
} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_GEOMETRY_BASE_H
42 changes: 42 additions & 0 deletions libtiledbsoma/src/geometry/geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef TILEDBSOMA_GEOMETRY_H
#define TILEDBSOMA_GEOMETRY_H

#include <variant>

#include "linestring.h"
#include "multilinestring.h"
#include "multipoint.h"
#include "multipolygon.h"
#include "point.h"
#include "polygon.h"

namespace tiledbsoma::geometry {

enum GeometryType : uint32_t {
POINT = 1,
LINESTRING = 2,
POLYGON = 3,
MULTIPOINT = 4,
MULTILINESTRING = 5,
MULTIPOLYGON = 6,
GEOMETRYCOLLECTION = 7
};

using BinaryBuffer = std::vector<uint8_t>;

struct GeometryCollection;
using GenericGeometry = std::variant<
Point,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
GeometryCollection>;

struct GeometryCollection : public std::vector<GenericGeometry> {
using std::vector<GenericGeometry>::vector;
};
} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_GEOMETRY_H
10 changes: 10 additions & 0 deletions libtiledbsoma/src/geometry/linestring.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "linestring.h"

namespace tiledbsoma::geometry {
LineString::LineString(std::vector<BasePoint>&& points)
: points(points) {
}

LineString::~LineString() {
}
} // namespace tiledbsoma::geometry
19 changes: 19 additions & 0 deletions libtiledbsoma/src/geometry/linestring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef TILEDBSOMA_LINESTRING_H
#define TILEDBSOMA_LINESTRING_H

#include <vector>

#include "base.h"

namespace tiledbsoma::geometry {

class LineString {
public:
LineString(std::vector<BasePoint>&& points = std::vector<BasePoint>());
~LineString();

std::vector<BasePoint> points;
};
} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_LINESTRING_H
10 changes: 10 additions & 0 deletions libtiledbsoma/src/geometry/multilinestring.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "multilinestring.h"

namespace tiledbsoma::geometry {
MultiLineString::MultiLineString(std::vector<LineString>&& linestrings)
: linestrings(linestrings) {
}

MultiLineString::~MultiLineString() {
}
} // namespace tiledbsoma::geometry
19 changes: 19 additions & 0 deletions libtiledbsoma/src/geometry/multilinestring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef TILEDBSOMA_MULTILINESTRING_H
#define TILEDBSOMA_MULTILINESTRING_H

#include <vector>

#include "linestring.h"

namespace tiledbsoma::geometry {
class MultiLineString {
public:
MultiLineString(
std::vector<LineString>&& linestring = std::vector<LineString>());
~MultiLineString();

std::vector<LineString> linestrings;
};
} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_MULTILINESTRING_H
10 changes: 10 additions & 0 deletions libtiledbsoma/src/geometry/multipoint.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "multipoint.h"

namespace tiledbsoma::geometry {
MultiPoint::MultiPoint(std::vector<Point>&& points)
: points(points) {
}

MultiPoint::~MultiPoint() {
}
} // namespace tiledbsoma::geometry
19 changes: 19 additions & 0 deletions libtiledbsoma/src/geometry/multipoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef TILEDBSOMA_MULTIPOINT_H
#define TILEDBSOMA_MULTIPOINT_H

#include <vector>

#include "point.h"

namespace tiledbsoma::geometry {
class MultiPoint {
public:
MultiPoint(std::vector<Point>&& points = std::vector<Point>());
~MultiPoint();

std::vector<Point> points;
};

} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_MULTIPOINT_H
10 changes: 10 additions & 0 deletions libtiledbsoma/src/geometry/multipolygon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "multipolygon.h"

namespace tiledbsoma::geometry {
MultiPolygon::MultiPolygon(std::vector<Polygon>&& polygons)
: polygons(polygons) {
}

MultiPolygon::~MultiPolygon() {
}
} // namespace tiledbsoma::geometry
18 changes: 18 additions & 0 deletions libtiledbsoma/src/geometry/multipolygon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef TILEDBSOMA_MULTIPOLYGON_H
#define TILEDBSOMA_MULTIPOLYGON_H

#include <vector>

#include "polygon.h"

namespace tiledbsoma::geometry {
class MultiPolygon {
public:
MultiPolygon(std::vector<Polygon>&& polygons = std::vector<Polygon>());
~MultiPolygon();

std::vector<Polygon> polygons;
};
} // namespace tiledbsoma::geometry

#endif // TILEDBSOMA_MULTIPOLYGON_H
Loading

0 comments on commit 7381508

Please sign in to comment.