[CP] [GIS] gis ewkt output srid=0

This commit is contained in:
helloamateur 2024-10-31 15:48:09 +00:00 committed by ob-robot
parent ce588b54c8
commit fd9226d311
8 changed files with 33 additions and 12 deletions

View File

@ -124,7 +124,7 @@ int ObGeometry3D::read_nums_value(ObGeoWkbByteOrder bo, uint32_t &nums)
return ret;
}
int ObGeometry3D::to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid/* = 0*/, int64_t maxdecimaldigits/* = -1*/)
int ObGeometry3D::to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid/* = 0*/, int64_t maxdecimaldigits/* = -1*/, bool output_srid0/* = false*/)
{
int ret = OB_SUCCESS;
ObStringBuffer *buf = NULL;
@ -133,7 +133,7 @@ int ObGeometry3D::to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid/*
if (OB_ISNULL(buf = OB_NEWx(ObStringBuffer, &allocator, (&allocator)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to allocate buffer", K(ret));
} else if (srid != 0) {
} else if (srid != 0 || output_srid0) {
ObFastFormatInt ffi(srid);
uint64_t reserve_len = strlen("srid") + 1 + ffi.length() + 1;
// [srid][=][1][2][3][4][;]
@ -141,7 +141,9 @@ int ObGeometry3D::to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid/*
LOG_WARN("fail to reserve memory for buffer_", K(ret), K(reserve_len));
} else if (OB_FAIL(buf->append("SRID="))) {
LOG_WARN("fail to append buffer", K(ret));
} else if (OB_FAIL(buf->append(ffi.ptr(), ffi.length()))) {
} else if (srid == UINT32_MAX && OB_FAIL(buf->append("NULL"))) {
LOG_WARN("fail to append buffer", K(ret));
} else if (srid != UINT32_MAX && OB_FAIL(buf->append(ffi.ptr(), ffi.length()))) {
LOG_WARN("fail to append buffer", K(ret), K(ffi.length()));
} else if (OB_FAIL(buf->append(";"))) {
LOG_WARN("fail to append buffer", K(ret));

View File

@ -56,7 +56,7 @@ public:
ObGeoType type(uint64_t pos) const;
void set_crs(ObGeoCRS crs) { crs_ = crs; }
int to_2d_geo(ObIAllocator &allocator, ObGeometry *&res, uint32_t srid = 0);
int to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid = 0, int64_t maxdecimaldigits = -1);
int to_wkt(ObIAllocator &allocator, ObString &wkt, uint32_t srid = 0, int64_t maxdecimaldigits = -1, bool output_srid0 = false);
int reverse_coordinate();
int check_wkb_valid();
int check_3d_coordinate_range(const ObSrsItem *srs, const bool is_normalized, ObGeoCoordRangeResult &result);

View File

@ -631,10 +631,10 @@ void ObGeoToWktVisitor::get_wkt(ObString &wkt)
wkt.assign(buffer_.ptr(), static_cast<int32_t>(buffer_.length()));
}
int ObGeoToWktVisitor::init(uint32_t srid, int64_t maxdecimaldigits)
int ObGeoToWktVisitor::init(uint32_t srid, int64_t maxdecimaldigits, bool output_srid0)
{
INIT_SUCC(ret);
if (srid != 0) {
if (srid != 0 || output_srid0) {
ObFastFormatInt ffi(srid);
uint64_t reserve_len = strlen("srid") + 1 + ffi.length() + 1;
// [srid][=][1][2][3][4][;]

View File

@ -74,7 +74,7 @@ public:
virtual int finish(ObIWkbGeomCollection *geo) override;
void get_wkt(ObString &wkt);
int init(uint32_t srid, int64_t maxdecimaldigits);
int init(uint32_t srid, int64_t maxdecimaldigits, bool output_srid0 = false);
private:
template<typename T_IBIN>

View File

@ -1616,7 +1616,8 @@ int ObGeoTypeUtil::tree_to_bin(ObIAllocator &allocator,
int ObGeoTypeUtil::geo_to_ewkt(const ObString &swkb,
ObString &ewkt,
ObIAllocator &allocator,
int64_t max_decimal_digits)
int64_t max_decimal_digits,
bool output_srid0)
{
int ret = OB_SUCCESS;
ObGeometry *geo = NULL;
@ -1644,11 +1645,11 @@ int ObGeoTypeUtil::geo_to_ewkt(const ObString &swkb,
// do nothing
} else if (is_3d_geo_type(geo->type())) {
ObGeometry3D *geo_3d = static_cast<ObGeometry3D *>(geo);
if (OB_FAIL(geo_3d->to_wkt(allocator, ewkt, header.srid_))) {
if (OB_FAIL(geo_3d->to_wkt(allocator, ewkt, header.srid_, -1, output_srid0))) {
LOG_WARN("fail to transform ewkt from 3d-wkb", K(ret));
}
} else {
if (OB_FAIL(wkt_visitor.init(header.srid_, max_decimal_digits))) {
if (OB_FAIL(wkt_visitor.init(header.srid_, max_decimal_digits, output_srid0))) {
LOG_WARN("failed to init wkt_visitor with srid_", K(ret), K(header.srid_));
} else if (OB_FAIL(geo->do_visit(wkt_visitor))) {
LOG_WARN("failed to transform geo to wkt", K(ret));

View File

@ -207,7 +207,8 @@ public:
static int geo_to_ewkt(const ObString &geo_wkb,
ObString &ewkt,
ObIAllocator &allocator,
int64_t max_decimal_digits);
int64_t max_decimal_digits,
bool output_srid0 = false);
static int geo_close_ring(ObGeometry &geo, ObIAllocator &allocator);
static int get_mbr_polygon(ObIAllocator &allocator,
const ObSrsBoundsItem *bounds,

View File

@ -458,7 +458,7 @@ int ObObj2strHelper::convert_ob_geometry_to_ewkt_(const common::ObObj &obj,
common::ObIAllocator &allocator) const
{
const ObString &wkb = obj.get_string();
return ObGeoTypeUtil::geo_to_ewkt(wkb, str, allocator, 0);
return ObGeoTypeUtil::geo_to_ewkt(wkb, str, allocator, -1 /*use default prec*/, true /*output_srid0*/);
}
int ObObj2strHelper::convert_xmltype_to_text_(

View File

@ -930,6 +930,23 @@ TEST_F(TestGeoTree, ewkt_with_null)
ASSERT_EQ(wkt_cal == wkt_res, true);
}
TEST_F(TestGeoTree, ewkt_with_0)
{
ObArenaAllocator allocator(ObModIds::TEST);
ObGeometry *geo_tree = nullptr;
wkt_to_tree_geo("POINT(1 1)", allocator, geo_tree);
geo_tree->set_srid(0);
ObGeometry *geo_bin = NULL;
ASSERT_EQ(ObGeoTypeUtil::tree_to_bin(allocator, geo_tree, geo_bin, nullptr), OB_SUCCESS);
ObWkbBuffer buffer(allocator);
ASSERT_EQ(buffer.append(static_cast<uint32_t>(0)), OB_SUCCESS);
ASSERT_EQ(buffer.append(geo_bin->val(), geo_bin->length()), OB_SUCCESS);
ObString wkt_cal;
ASSERT_EQ(ObGeoTypeUtil::geo_to_ewkt(buffer.string(), wkt_cal, allocator, 14, true), OB_SUCCESS);
ObString wkt_res = "SRID=0;POINT(1 1)";
ASSERT_EQ(wkt_cal == wkt_res, true);
}
} // namespace common
} // namespace oceanbase