Skip to content

Commit

Permalink
support spatial function (#378)
Browse files Browse the repository at this point in the history
* fix datetime&audit_log bug

* start spatial

* test for data_type done

* add comment for spatial

* update ci

* support schema for spatial

* test alter field done

* test field_extractor done

* test field_data_helper

* fix ci images bug

* support spatial data convert

* begin spatial support

* fix cpplint

* fix cppplugin

* add test for spatial

* fix cmakelists

* improve spatial comments

* support big endian and comments

* fix big endian bug for spatial data

* support spatial data for protobuf

* add function for spatial type

* spatial bug

* add test for spatial

* fix error for spatial test

* fix loadfromzip cppplugin

* modify for merge

* reset files

* add pytest for spatial data

* modify pyunit_test

* fix spatial test

* enhance spatial data tests

* add more test for spatial data

* enhance embedded_api_unittest

* support spatial data creation

* support built-in function & procedure for spatial data

* support spatial function

* modify head file

* reslove confilts between boost and cpprest

* move boost/geometry.hpp from header file to cpp file

* fix it test

* restore sortstr.zip

* add comments

* fix cpplint

* Update cypher_types.h

* modify code style

* fix python intergation bug

* fix intergation

* fix ci images

* begin docs of spatial data

* fix throw bug

* fix cpplint

* fix procedure

---------

Co-authored-by: Shipeng Qi <[email protected]>
Co-authored-by: Shipeng Qi <[email protected]>
Co-authored-by: lipanpan03 <[email protected]>
  • Loading branch information
4 people authored Apr 24, 2024
1 parent fb48f80 commit f710d6c
Show file tree
Hide file tree
Showing 17 changed files with 802 additions and 28 deletions.
3 changes: 3 additions & 0 deletions docs/en-US/source/2.introduction/4.schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ TuGraph Supports a variety of data types that can be used as attributes, the spe
| DOUBLE | | | 64-bit float |
| STRING | | | A string of variable length |
| BLOB | | | Binary data |
| POINT | | | EWKB format data of point |
| LINESTRING | | | EWKB format data of linestring |
| POLYGON | | | EWKB format data of polygon |

_BLOB data is BASE64 encoded in input and output_

Expand Down
3 changes: 3 additions & 0 deletions docs/zh-CN/source/2.introduction/4.schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ TuGraph支持多种可用于属性的数据类型。具体支持的数据类型
| DOUBLE | | | 64位浮点数 |
| STRING | | | 不定长度的字符串 |
| BLOB | | | 二进制数据(在输入输出时使用Base64编码) |
| POINT | | | EWKB格式数据,表示点 |
| LINESTRING | | | EWKB格式数据,表示线 |
| POLYGON | | | EWKB格式数据,表示面(多边形) |

### 1.3. 索引

Expand Down
100 changes: 87 additions & 13 deletions include/lgraph/lgraph_spatial.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

/**
* @file lgraph_spatial.h
* @file lgraph_spatial.h
* @brief Implemnets the Spatial, SpatialBase and SpatialDerive classes.
*
* TODO(shw):
Expand All @@ -30,7 +30,6 @@
#include <boost/geometry/io/wkt/read.hpp>
#include <boost/geometry/extensions/gis/io/wkb/write_wkb.hpp>
#include <boost/geometry/extensions/gis/io/wkb/utility.hpp>

#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>

Expand Down Expand Up @@ -328,9 +327,14 @@ class Spatial {
return type_;
}

bool operator==(const Spatial<SRID_Type>& other);
/**
* @brief return the distance between two spatial data;
*
* @param other the other data of spatial type;
*/
double Distance(Spatial<SRID_Type>& other);

// double Distance(geography<T>& other);
bool operator==(const Spatial<SRID_Type>& other);
};

/**
Expand All @@ -347,14 +351,10 @@ class SpatialBase {
*/
SpatialBase(SRID srid, SpatialType type) : srid_(srid), type_(type) {}

// virtual ~SpatialBase();

virtual std::string AsEWKB() const = 0;

virtual std::string AsEWKT() const = 0;

// virtual std::string ToString() const = 0;

SpatialType GetType() const {
return type_;
}
Expand All @@ -365,7 +365,7 @@ class SpatialBase {
};

/**
* @brief implements a Point spatial class which spatial data is Point;
* @brief implements a Point spatial class which spatial type is Point;
*/
template<typename SRID_Type>
class Point : public SpatialBase {
Expand Down Expand Up @@ -399,6 +399,17 @@ class Point : public SpatialBase {
*/
explicit Point(const std::string& ewkb);

/**
* @brief construct Point from Coordinate pairs and srid;
*
* @param arg1 the first coordinate datum;
*
* @param arg2 the second coordinate datum;
*
* @param srid the srid of the Point;
*/
Point(double arg1, double arg2, SRID& srid);

std::string AsEWKB() const override {
return ewkb_;
}
Expand All @@ -416,6 +427,27 @@ class Point : public SpatialBase {
return point_;
}

/**
* @brief caculate the distance between data of point type;
*
* @param other the other data of point type;
*/
double Distance(Point<SRID_Type>& other);

/**
* @brief caculate the distance between point and linestring;
*
* @param other the other data of linestring type;
*/
double Distance(LineString<SRID_Type>& other);

/**
* @brief caculate the distance between point and polygon;
*
* @param other the other data of polygon type;
*/
double Distance(Polygon<SRID_Type>& other);

bool operator==(const Point<SRID_Type>& other);
};

Expand All @@ -425,8 +457,8 @@ class Point : public SpatialBase {
template<typename SRID_Type>
class LineString : public SpatialBase {
std::string ewkb_;
typedef bg::model::point<double, 2, SRID_Type> Point;
bg::model::linestring<Point> line_;
typedef bg::model::point<double, 2, SRID_Type> Point_;
bg::model::linestring<Point_> line_;

public:
LineString(SRID srid, SpatialType type, int construct_type, std::string& content);
Expand All @@ -448,6 +480,27 @@ class LineString : public SpatialBase {
return line_;
}

/**
* @brief caculate the distance between linestring and point;
*
* @param other the other data of distance type;
*/
double Distance(Point<SRID_Type>& other);

/**
* @brief caculate the distance between linestring and linestring;
*
* @param other the other data of linestring type;
*/
double Distance(LineString<SRID_Type>& other);

/**
* @brief caculate the distance between linestring and polygon;
*
* @param other the other data of polygon type;
*/
double Distance(Polygon<SRID_Type>& other);

bool operator==(const LineString<SRID_Type>& other);
};

Expand All @@ -457,8 +510,8 @@ class LineString : public SpatialBase {
template<typename SRID_Type>
class Polygon : public SpatialBase {
std::string ewkb_;
typedef bg::model::point<double, 2, SRID_Type> Point;
bg::model::polygon<Point> polygon_;
typedef bg::model::point<double, 2, SRID_Type> Point_;
bg::model::polygon<Point_> polygon_;

public:
Polygon(SRID srid, SpatialType type, int construct_type, std::string& content);
Expand All @@ -480,6 +533,27 @@ class Polygon : public SpatialBase {
return polygon_;
}

/**
* @brief caculate the distance between linestring and point;
*
* @param other the other data of point type;
*/
double Distance(Point<SRID_Type>& other);

/**
* @brief caculate the distance between linestring and linestring;
*
* @param other the other data of linestring type;
*/
double Distance(LineString<SRID_Type>& other);

/**
* @brief caculate the distance between linestring and polygon;
*
* @param other the other data of polygon type;
*/
double Distance(Polygon<SRID_Type>& other);

bool operator==(const Polygon<SRID_Type>& other);
};
} // namespace lgraph_api
7 changes: 4 additions & 3 deletions include/lgraph/lgraph_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,14 @@ struct FieldData {

inline ::lgraph_api::Spatial<::lgraph_api::Wgs84> AsWgsSpatial()
const {
if (type == FieldType::SPATIAL) return ::lgraph_api::Spatial
if (IsSpatial()) return ::lgraph_api::Spatial
<::lgraph_api::Wgs84>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::Spatial<::lgraph_api::Cartesian> AsCartesianSpatial()
const {
if (type == FieldType::SPATIAL) return ::lgraph_api::Spatial
if (IsSpatial()) return ::lgraph_api::Spatial
<::lgraph_api::Cartesian>(*data.buf);
throw std::bad_cast();
}
Expand Down Expand Up @@ -1140,7 +1140,8 @@ struct FieldData {
bool IsPolygon() const { return type == FieldType::POLYGON; }

/** @brief Query if this object is spatial*/
bool IsSpatial() const { return type == FieldType::SPATIAL; }
bool IsSpatial() const { return type == FieldType::SPATIAL || IsPoint() || IsLineString()
|| IsPolygon(); }

private:
/** @brief Query if 't' is BLOB or STRING */
Expand Down
Loading

0 comments on commit f710d6c

Please sign in to comment.