Skip to content

Commit

Permalink
let Geography store variant
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Oct 10, 2021
1 parent edb5fac commit 1adae70
Show file tree
Hide file tree
Showing 24 changed files with 942 additions and 473 deletions.
8 changes: 8 additions & 0 deletions src/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct Map;
struct Set;
struct List;
struct DataSet;
struct Coordinate;
struct Point;
struct LineString;
struct Polygon;
struct Geography;
} // namespace nebula

Expand All @@ -44,6 +48,10 @@ SPECIALIZE_CPP2OPS(nebula::Map);
SPECIALIZE_CPP2OPS(nebula::Set);
SPECIALIZE_CPP2OPS(nebula::List);
SPECIALIZE_CPP2OPS(nebula::DataSet);
SPECIALIZE_CPP2OPS(nebula::Coordinate);
SPECIALIZE_CPP2OPS(nebula::Point);
SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);

} // namespace apache::thrift
Expand Down
4 changes: 4 additions & 0 deletions src/common/datatypes/DataSetOps-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ _readField_rows : {
}

if (proto->kUsesFieldNames()) {
LOG(INFO) << "Dataset: proto->kUsesFieldNames()";
detail::TccStructTraits<nebula::DataSet>::translateFieldName(
readState.fieldName(), readState.fieldId, readState.fieldType);
} else {
LOG(INFO) << "Dataset:: !proto->kUsesFieldNames()";
}
LOG(INFO) << "jie, dataset, readState.fieldId: " << readState.fieldId;

switch (readState.fieldId) {
case 1: {
Expand Down
134 changes: 69 additions & 65 deletions src/common/datatypes/Geography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,33 @@

namespace nebula {

Geography::Geography(const Geometry& geom) { wkb = WKBWriter().write(geom); }

StatusOr<Geography> Geography::fromWKT(const std::string& wkt) {
auto geomRet = WKTReader().read(wkt);
NG_RETURN_IF_ERROR(geomRet);
auto geom = geomRet.value();
auto bytes = WKBWriter().write(geom);
return Geography(bytes);
}
StatusOr<Geography> Geography::fromWKT(const std::string& wkt) { return WKTReader().read(wkt); }

GeoShape Geography::shape() const {
ByteOrderDataInStream dis(wkb);
auto byteOrderValRet = dis.readUint8();
if (!byteOrderValRet.ok()) {
return GeoShape::UNKNOWN;
}
ByteOrder byteOrder = static_cast<ByteOrder>(byteOrderValRet.value());
dis.setByteOrder(byteOrder);
auto shapeTypeValRet = dis.readUint32();
if (!shapeTypeValRet.ok()) {
return GeoShape::UNKNOWN;
switch (geoVariant.index()) {
case 0:
return GeoShape::POINT;
case 1:
return GeoShape::LINESTRING;
case 2:
return GeoShape::POLYGON;
default:
return GeoShape::UNKNOWN;
}
return static_cast<GeoShape>(shapeTypeValRet.value());
}

bool Geography::isValid() const {
auto geom = this->asGeometry();
if (!geom) {
return false;
}

if (!geom->isValid()) {
return false;
}

switch (shape()) {
case GeoShape::POINT: {
const auto& point = geom->point();
const auto& point = this->point();
return std::abs(point.coord.x) <= 180.0 && std::abs(point.coord.y) <= 90.0;
}
case GeoShape::LINESTRING: {
auto s2Region = GeoUtils::s2RegionFromGeomtry(*geom);
auto s2Region = GeoUtils::s2RegionFromGeography(*this);
return static_cast<S2Polyline*>(s2Region.get())->IsValid();
}
case GeoShape::POLYGON: {
auto s2Region = GeoUtils::s2RegionFromGeomtry(*geom);
auto s2Region = GeoUtils::s2RegionFromGeography(*this);
return static_cast<S2Polygon*>(s2Region.get())->IsValid();
}
case GeoShape::UNKNOWN:
Expand All @@ -78,54 +59,77 @@ bool Geography::isValid() const {
return false;
}

std::unique_ptr<std::string> Geography::asWKT() const {
auto geomRet = WKBReader().read(wkb);
if (!geomRet.ok()) {
LOG(ERROR) << geomRet.status();
return nullptr;
}
auto geom = geomRet.value();
return std::make_unique<std::string>(WKTWriter().write(geom));
std::string Geography::asWKT() const { return WKTWriter().write(*this); }

std::string Geography::asWKB() const { return WKBWriter().write(*this); }

std::string Geography::asWKBHex() const { return folly::hexlify(WKBWriter().write(*this)); }

std::unique_ptr<S2Region> Geography::asS2() const {
// geoVariant.normalize();
return GeoUtils::s2RegionFromGeography(*this);
}

std::unique_ptr<std::string> Geography::asWKBHex() const {
auto geomRet = WKBReader().read(wkb);
if (!geomRet.ok()) {
LOG(ERROR) << geomRet.status();
return nullptr;
bool Geography::operator==(const Geography& rhs) const {
auto lhsShape = this->shape();
auto rhsShape = rhs.shape();
if (lhsShape != rhsShape) {
return false;
}
auto geom = geomRet.value();
return std::make_unique<std::string>(folly::hexlify(WKBWriter().write(geom)));
}

std::unique_ptr<Geometry> Geography::asGeometry() const {
auto geomRet = WKBReader().read(wkb);
if (!geomRet.ok()) {
LOG(ERROR) << geomRet.status();
return nullptr;
switch (lhsShape) {
case GeoShape::POINT: {
return this->point() == rhs.point();
}
case GeoShape::LINESTRING: {
return this->lineString() == rhs.lineString();
}
case GeoShape::POLYGON: {
return this->polygon() == rhs.polygon();
}
case GeoShape::UNKNOWN:
default: {
LOG(ERROR)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return false;
}
}
auto geom = geomRet.value();
return std::make_unique<Geometry>(std::move(geom));
}
bool Geography::operator<(const Geography& rhs) const {
auto lhsShape = this->shape();
auto rhsShape = rhs.shape();
if (lhsShape != rhsShape) {
return lhsShape < rhsShape;
}

std::unique_ptr<S2Region> Geography::asS2() const {
auto geomRet = WKBReader().read(wkb);
if (!geomRet.ok()) {
LOG(ERROR) << geomRet.status();
return nullptr;
switch (lhsShape) {
case GeoShape::POINT: {
return this->point() < rhs.point();
}
case GeoShape::LINESTRING: {
return this->lineString() < rhs.lineString();
}
case GeoShape::POLYGON: {
return this->polygon() < rhs.polygon();
}
case GeoShape::UNKNOWN:
default: {
LOG(ERROR)
<< "Geography shapes other than Point/LineString/Polygon are not currently supported";
return false;
}
}
auto geom = geomRet.value();
geom.normalize();
return GeoUtils::s2RegionFromGeomtry(geom);
return false;
}

} // namespace nebula

namespace std {

// Inject a customized hash function
std::size_t hash<nebula::Geography>::operator()(const nebula::Geography& h) const noexcept {
return hash<std::string>{}(h.wkb);
std::size_t hash<nebula::Geography>::operator()(const nebula::Geography& v) const noexcept {
std::string wkb = v.asWKB();
return hash<std::string>{}(wkb);
}

} // namespace std
Loading

0 comments on commit 1adae70

Please sign in to comment.