Skip to content

Commit

Permalink
support spatial data for protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
hanwen-sun committed Sep 18, 2023
1 parent c7d63d4 commit acde89d
Show file tree
Hide file tree
Showing 14 changed files with 482 additions and 347 deletions.
231 changes: 156 additions & 75 deletions include/lgraph/lgraph_spatial.h

Large diffs are not rendered by default.

80 changes: 40 additions & 40 deletions include/lgraph/lgraph_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ enum FieldType {
INT16 = 3, // 16-bit signed integer
INT32 = 4, // 32-bit signed integer
INT64 = 5, // 64-bit signed integer
FLOAT = 6, // 32-bit IEEE 754 floating point number
DOUBLE = 7, // 64-bit IEEE 754 floating point number
FLOAT = 6, // 32-bit IEEE 754 floating Point number
DOUBLE = 7, // 64-bit IEEE 754 floating Point number
DATE = 8, // Date ranging from 0/1/1 to 9999/12/31
DATETIME = 9, // DateTime ranging from 0000-01-01 00:00:00.000000 to
// 9999-12-31 23:59:59.999999
STRING = 10, // string type, in unicode encoding
BLOB = 11, // binary byte array
POINT = 12, // point type of spatial data
LINESTRING = 13, // linestring type of spatial data
POLYGON = 14, // polygon type of spatial data
POINT = 12, // Point type of spatial data
LINESTRING = 13, // LineString type of spatial data
POLYGON = 14, // Polygon type of spatial data
SPATIAL = 15 // spatial data, it's now unused but may be used in the future.
};

Expand Down Expand Up @@ -419,32 +419,32 @@ struct FieldData {
data.buf = new std::string(buf, buf + s);
}

explicit FieldData(const point<Cartesian>& p) {
explicit FieldData(const Point<Cartesian>& p) {
type = FieldType::POINT;
data.buf = new std::string(p.AsEWKB());
}

explicit FieldData(const point<Wsg84>& p) {
explicit FieldData(const Point<Wsg84>& p) {
type = FieldType::POINT;
data.buf = new std::string(p.AsEWKB());
}

explicit FieldData(const linestring<Cartesian>& l) {
explicit FieldData(const LineString<Cartesian>& l) {
type = FieldType::LINESTRING;
data.buf = new std::string(l.AsEWKB());
}

explicit FieldData(const linestring<Wsg84>& l) {
explicit FieldData(const LineString<Wsg84>& l) {
type = FieldType::LINESTRING;
data.buf = new std::string(l.AsEWKB());
}

explicit FieldData(const polygon<Cartesian>& p) {
explicit FieldData(const Polygon<Cartesian>& p) {
type = FieldType::POLYGON;
data.buf = new std::string(p.AsEWKB());
}

explicit FieldData(const polygon<Wsg84>& p) {
explicit FieldData(const Polygon<Wsg84>& p) {
type = FieldType::POLYGON;
data.buf = new std::string(p.AsEWKB());
}
Expand Down Expand Up @@ -526,50 +526,50 @@ struct FieldData {
static inline FieldData String(const char* str) { return FieldData(str); }
static inline FieldData String(const char* p, size_t s) { return FieldData(p, s); }

static inline FieldData Point(const ::lgraph_api::point<Cartesian>& p) {
static inline FieldData Point(const ::lgraph_api::Point<Cartesian>& p) {
return FieldData(p); }
static inline FieldData Point(const ::lgraph_api::point<Wsg84>& p) {return FieldData(p); }
static inline FieldData Point(const ::lgraph_api::Point<Wsg84>& p) {return FieldData(p); }
static inline FieldData Point(const std::string& str) {
switch (::lgraph_api::ExtractSRID(str)) {
case ::lgraph_api::SRID::NUL:
throw InputError("Unsupported SRID!");
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(point<Cartesian>(str));
return FieldData(::lgraph_api::Point<Cartesian>(str));
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::point<Wsg84>(str));
return FieldData(::lgraph_api::Point<Wsg84>(str));
default:
throw InputError("Unsupported SRID!");
}
}

static inline FieldData LineString(const ::lgraph_api::linestring<Cartesian>& l) {
static inline FieldData LineString(const ::lgraph_api::LineString<Cartesian>& l) {
return FieldData(l); }
static inline FieldData LineString(const ::lgraph_api::linestring<Wsg84>& l) {
static inline FieldData LineString(const ::lgraph_api::LineString<Wsg84>& l) {
return FieldData(l); }
static inline FieldData LineString(const std::string& str) {
switch (::lgraph_api::ExtractSRID(str)) {
case ::lgraph_api::SRID::NUL:
throw InputError("Unsupported SRID!");
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(linestring<Cartesian>(str));
return FieldData(::lgraph_api::LineString<Cartesian>(str));
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::linestring<Wsg84>(str));
return FieldData(::lgraph_api::LineString<Wsg84>(str));
default:
throw InputError("Unsupported SRID!");
}
}

static inline FieldData Polygon(const ::lgraph_api::polygon<Cartesian>& p) {
static inline FieldData Polygon(const ::lgraph_api::Polygon<Cartesian>& p) {
return FieldData(p); }
static inline FieldData Polygon(const ::lgraph_api::polygon<Wsg84>& p) {return FieldData(p); }
static inline FieldData Polygon(const ::lgraph_api::Polygon<Wsg84>& p) {return FieldData(p); }
static inline FieldData Polygon(const std::string& str) {
switch (::lgraph_api::ExtractSRID(str)) {
case ::lgraph_api::SRID::NUL:
throw InputError("Unsupported SRID!");
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(polygon<Cartesian>(str));
return FieldData(::lgraph_api::Polygon<Cartesian>(str));
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::polygon<Wsg84>(str));
return FieldData(::lgraph_api::Polygon<Wsg84>(str));
default:
throw InputError("Unsupported SRID!");
}
Expand Down Expand Up @@ -779,40 +779,40 @@ struct FieldData {
throw std::bad_cast();
}

inline ::lgraph_api::point<::lgraph_api::Wsg84> AsWsgPoint() const {
if (type == FieldType::POINT) return ::lgraph_api::point
inline ::lgraph_api::Point<::lgraph_api::Wsg84> AsWsgPoint() const {
if (type == FieldType::POINT) return ::lgraph_api::Point
<::lgraph_api::Wsg84>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::point<::lgraph_api::Cartesian> AsCartesianPoint() const {
if (type == FieldType::POINT) return ::lgraph_api::point
inline ::lgraph_api::Point<::lgraph_api::Cartesian> AsCartesianPoint() const {
if (type == FieldType::POINT) return ::lgraph_api::Point
<::lgraph_api::Cartesian>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::linestring<::lgraph_api::Wsg84> AsWsgLineString()
inline ::lgraph_api::LineString<::lgraph_api::Wsg84> AsWsgLineString()
const {
if (type == FieldType::LINESTRING) return ::lgraph_api::linestring
if (type == FieldType::LINESTRING) return ::lgraph_api::LineString
<::lgraph_api::Wsg84>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::linestring<::lgraph_api::Cartesian> AsCartesianLineString()
inline ::lgraph_api::LineString<::lgraph_api::Cartesian> AsCartesianLineString()
const {
if (type == FieldType::LINESTRING) return ::lgraph_api::linestring
if (type == FieldType::LINESTRING) return ::lgraph_api::LineString
<::lgraph_api::Cartesian>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::polygon<::lgraph_api::Wsg84> AsWsgPolygon() const {
if (type == FieldType::POLYGON) return ::lgraph_api::polygon
inline ::lgraph_api::Polygon<::lgraph_api::Wsg84> AsWsgPolygon() const {
if (type == FieldType::POLYGON) return ::lgraph_api::Polygon
<::lgraph_api::Wsg84>(*data.buf);
throw std::bad_cast();
}

inline ::lgraph_api::polygon<::lgraph_api::Cartesian> AsCartesianPolygon() const {
if (type == FieldType::POLYGON) return ::lgraph_api::polygon
inline ::lgraph_api::Polygon<::lgraph_api::Cartesian> AsCartesianPolygon() const {
if (type == FieldType::POLYGON) return ::lgraph_api::Polygon
<::lgraph_api::Cartesian>(*data.buf);
throw std::bad_cast();
}
Expand Down Expand Up @@ -874,7 +874,7 @@ struct FieldData {
* 2. they are both numeric types
* In case 1, the underlying values are compared directly. In case 2, integer
* values are converted to int64_t if compared with another integer, or to double
* if compared with a floating-point number.
* if compared with a floating-Point number.
*/

inline bool operator==(const FieldData& rhs) const {
Expand Down Expand Up @@ -1095,13 +1095,13 @@ struct FieldData {
/** @brief Query if this object is date time */
bool IsDateTime() const { return type == FieldType::DATETIME; }

/** @brief Query if this object is point */
/** @brief Query if this object is Point */
bool IsPoint() const { return type == FieldType::POINT; }

/** @brief Query if this object is linestring */
/** @brief Query if this object is LineString */
bool IsLineString() const { return type == FieldType::LINESTRING; }

/** @brief Query if this object is polygon*/
/** @brief Query if this object is Polygon*/
bool IsPolygon() const { return type == FieldType::POLYGON; }

/** @brief Query if this object is spatial*/
Expand All @@ -1123,7 +1123,7 @@ struct FieldData {

static_assert(sizeof(decltype(data.int64)) == sizeof(decltype(data.buf)) &&
sizeof(decltype(data.int64)) >= sizeof(decltype(data.dp)),
"sizeof int64_t is supposed to be equal to pointer types");
"sizeof int64_t is supposed to be equal to Pointer types");
};

/** @brief Specification for a field. */
Expand Down
12 changes: 6 additions & 6 deletions src/core/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ typedef lgraph_api::FieldSpec FieldSpec;
typedef lgraph_api::EdgeUid EdgeUid;
typedef lgraph_api::Date Date;
typedef lgraph_api::DateTime DateTime;
typedef lgraph_api::point<lgraph_api::Wsg84> PointWsg84;
typedef lgraph_api::point<lgraph_api::Cartesian> PointCartesian;
typedef lgraph_api::linestring<lgraph_api::Wsg84> LineStringWsg84;
typedef lgraph_api::linestring<lgraph_api::Cartesian> LineStringCartesian;
typedef lgraph_api::polygon<lgraph_api::Wsg84> PolygonWsg84;
typedef lgraph_api::polygon<lgraph_api::Cartesian> PolygonCartesian;
typedef lgraph_api::Point<lgraph_api::Wsg84> PointWsg84;
typedef lgraph_api::Point<lgraph_api::Cartesian> PointCartesian;
typedef lgraph_api::LineString<lgraph_api::Wsg84> LineStringWsg84;
typedef lgraph_api::LineString<lgraph_api::Cartesian> LineStringCartesian;
typedef lgraph_api::Polygon<lgraph_api::Wsg84> PolygonWsg84;
typedef lgraph_api::Polygon<lgraph_api::Cartesian> PolygonCartesian;
typedef lgraph_api::LGraphType ElementType;
typedef lgraph_api::Result Result;
typedef lgraph_api::EdgeConstraints EdgeConstraints;
Expand Down
56 changes: 28 additions & 28 deletions src/core/field_data_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ typedef lgraph_api::FieldData FieldData;
typedef lgraph_api::FieldType FieldType;
typedef lgraph_api::Date Date;
typedef lgraph_api::DateTime DateTime;
typedef lgraph_api::point<lgraph_api::Wsg84> PointWsg84;
typedef lgraph_api::point<lgraph_api::Cartesian> PointCartesian;
typedef lgraph_api::linestring<lgraph_api::Wsg84> LineStringWsg84;
typedef lgraph_api::linestring<lgraph_api::Cartesian> LineStringCartesian;
typedef lgraph_api::polygon<lgraph_api::Wsg84> PolygonWsg84;
typedef lgraph_api::polygon<lgraph_api::Cartesian> PolygonCartesian;
typedef lgraph_api::Point<lgraph_api::Wsg84> PointWsg84;
typedef lgraph_api::Point<lgraph_api::Cartesian> PointCartesian;
typedef lgraph_api::LineString<lgraph_api::Wsg84> LineStringWsg84;
typedef lgraph_api::LineString<lgraph_api::Cartesian> LineStringCartesian;
typedef lgraph_api::Polygon<lgraph_api::Wsg84> PolygonWsg84;
typedef lgraph_api::Polygon<lgraph_api::Cartesian> PolygonCartesian;


//===============================
Expand Down Expand Up @@ -92,9 +92,9 @@ static constexpr size_t FieldTypeSizes[] = {
8, // datetime
0, // string
0, // blob
50, // point
0, // linestring
0 // polygon
50, // Point
0, // LineString
0 // Polygon
};

static constexpr bool IsFixedLengthType[] = {
Expand All @@ -110,9 +110,9 @@ static constexpr bool IsFixedLengthType[] = {
true, // datetime
false, // string
false, // blob
true, // point
false, // linestring
false // polygon
true, // Point
false, // LineString
false // Polygon
};

template <class T, size_t N>
Expand Down Expand Up @@ -753,13 +753,13 @@ struct FieldDataTypeConvert {
// nothing can be converted from bin
break;
case FieldType::POINT:
// nothing can be converted from point
// nothing can be converted from Point
break;
case FieldType::LINESTRING:
// nothing can be converted from linestring
// nothing can be converted from LineString
break;
case FieldType::POLYGON:
// nothing can be converted from polygon;
// nothing can be converted from Polygon;
break;
case FieldType::SPATIAL:
// nothing can be converted from spatial
Expand Down Expand Up @@ -886,8 +886,8 @@ inline bool TryFieldDataToValueOfFieldType(const FieldData& fd, FieldType ft, Va
}
case FieldType::POINT:
{
// can only convert string to point
// point类型为fixed_length, string类型为variable, 应该不可以直接copy!;
// can only convert string to Point
// Point类型为fixed_length, string类型为variable, 应该不可以直接copy!;
// 怎么处理?
if (fd.type != FieldType::STRING) return false;
const std::string EWKB = *fd.data.buf;
Expand All @@ -898,7 +898,7 @@ inline bool TryFieldDataToValueOfFieldType(const FieldData& fd, FieldType ft, Va
}
case FieldType::LINESTRING:
{
// can only convert string to linestring
// can only convert string to LineString
if (fd.type != FieldType::STRING) return false;
const std::string EWKB = *fd.data.buf;
if (!::lgraph_api::TryDecodeEWKB(EWKB, ::lgraph_api::SpatialType::LINESTRING))
Expand All @@ -908,7 +908,7 @@ inline bool TryFieldDataToValueOfFieldType(const FieldData& fd, FieldType ft, Va
}
case FieldType::POLYGON:
{
// can only convert string to polygon
// can only convert string to Polygon
if (fd.type != FieldType::STRING) return false;
const std::string EWKB = *fd.data.buf;
if (!::lgraph_api::TryDecodeEWKB(EWKB, ::lgraph_api::SpatialType::POLYGON))
Expand Down Expand Up @@ -1043,11 +1043,11 @@ inline FieldData ValueToFieldData(const Value& v, FieldType ft) {
::lgraph_api::SRID s = ::lgraph_api::ExtractSRID(ewkb);
switch (s) {
case ::lgraph_api::SRID::NUL:
throw std::runtime_error("cannot convert to point data!");
throw std::runtime_error("cannot convert to Point data!");
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::point<::lgraph_api::Wsg84>(ewkb));
return FieldData(::lgraph_api::Point<::lgraph_api::Wsg84>(ewkb));
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(::lgraph_api::point<::lgraph_api::Cartesian>(ewkb));
return FieldData(::lgraph_api::Point<::lgraph_api::Cartesian>(ewkb));
}
}
case FieldType::LINESTRING:
Expand All @@ -1056,11 +1056,11 @@ inline FieldData ValueToFieldData(const Value& v, FieldType ft) {
::lgraph_api::SRID s = ::lgraph_api::ExtractSRID(ewkb);
switch (s) {
case ::lgraph_api::SRID::NUL:
throw std::runtime_error("cannot convert to point data!");
throw std::runtime_error("cannot convert to Point data!");
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::linestring<::lgraph_api::Wsg84>(ewkb));
return FieldData(::lgraph_api::LineString<::lgraph_api::Wsg84>(ewkb));
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(::lgraph_api::linestring<::lgraph_api::Cartesian>(ewkb));
return FieldData(::lgraph_api::LineString<::lgraph_api::Cartesian>(ewkb));
}
}
case FieldType::POLYGON:
Expand All @@ -1069,11 +1069,11 @@ inline FieldData ValueToFieldData(const Value& v, FieldType ft) {
::lgraph_api::SRID s = ::lgraph_api::ExtractSRID(ewkb);
switch (s) {
case ::lgraph_api::SRID::NUL:
throw std::runtime_error("cannot convert to point data!");
throw std::runtime_error("cannot convert to Point data!");
case ::lgraph_api::SRID::WSG84:
return FieldData(::lgraph_api::polygon<::lgraph_api::Wsg84>(ewkb));
return FieldData(::lgraph_api::Polygon<::lgraph_api::Wsg84>(ewkb));
case ::lgraph_api::SRID::CARTESIAN:
return FieldData(::lgraph_api::polygon<::lgraph_api::Cartesian>(ewkb));
return FieldData(::lgraph_api::Polygon<::lgraph_api::Cartesian>(ewkb));
}
}
case FieldType::SPATIAL:
Expand Down
Loading

0 comments on commit acde89d

Please sign in to comment.