查询oracle空间(sdo_geomtery)

C++ query oracle spatial (sdo_geomtery)

本文关键字:geomtery sdo oracle 空间 查询      更新时间:2023-10-16

我目前正在编写一个简单的测试应用程序,该应用程序将使用c++中的oracle oci从数据库中查询sdo_geometry。由于sdo_geomegeometry是一个复杂的类型,所以我在寻找一个如何准确完成的好例子时遇到了一些问题。这里有一些关于使用集合SQL类型的注意事项,但没有具体说明。

我的代码是基于adpoci项目,可以在这里找到:https://github.com/ReneNyffenegger/development_misc/tree/master/c%2B%2B/oci/adpoci

但是,我需要在该示例的基础上扩展以查询sdo_geometry。

   select mdsys.sdo_geometry (2003,
        NULL,
        NULL,
        mdsys.sdo_elem_info_array (1, 1003, 1),
        mdsys.sdo_ordinate_array (1,1,2,1,2,2,1,2,1,1))
    from dual;

任何帮助都将非常感激。

是的,这很复杂。我很久以前就这样做了,但不幸的是,我没有以一种可以轻松共享的方式拥有它(它都被封装在OCI之上的专有c++层中)。对于任何用户定义的Oracle类型(udt),您通常使用Object Type Translator (OTT)来生成您绑定和定义的C结构(用于值和指示符)。我在下面列出了我当时使用的(11gR1),我希望它们仍然有效,但理想情况下,您应该阅读OCI的对象关系方面并自己生成它们。

struct sdo_point_type {
    OCINumber x, y, z;
};
struct sdo_point_type_ind {
    OCIInd _atomic;
    OCIInd x, y, z;
};
struct sdo_geometry {
    OCINumber sdo_gtype;
    OCINumber sdo_srid;
    sdo_point_type sdo_point;
    OCIArray* sdo_elem_info;
    OCIArray* sdo_ordinates;
};
struct sdo_geometry_ind {
    OCIInd _atomic;
    OCIInd sdo_gtype;
    OCIInd sdo_srid;
    sdo_point_type_ind sdo_point;
    OCIInd sdo_elem_info;
    OCIInd sdo_ordinates;
};
要绑定/定义,您需要为udt使用TDO。下面是我使用的代码:
/*!
 * brief Looks up raw OCI type descriptor for a given SQL type name.
 *
 * param type_name the possibly qualified type name. Note that
 *        types are implicitly capitalized by Oracle Server when
 *        defined, and thus type_name should usually be uppercase only.
 *        a type_name can be qualified, as in "MDSYS.SDO_GEOMETRY", or
 *        unqualified as in "MY_TYPE", in which case it is looked up in
 *        the current user's schema.
 * return the object type, either obtained from the server when
 *        the type is looked up for the first time, or from a cache
 *        this connection keeps. Note that the return type is valid
 *        only for this connection, and only as long as this connection
 *        is valid itself.
 * throw std::runtime_error if the type cannot be found.
 */
OCIType* Connection::get_object_type(const char* type_name) {
    typedef std::map<std::string, OCIType*> TdoMap;
    TdoMap::iterator tdo_iter = tdo_map_.find(type_name);
    if (tdo_iter != tdo_map_.end()) {
        // return type already in the cache
        return tdo_iter->second;
    }
    // lookup the type for the first time
    OCIType* tdo = 0;
    OraTextString tdo_name(type_name);
    const sword rc = OCITypeByName(
        env_->envhp(), errhp(), svchp(),
        0, 0, // schema name (default schema when 0)
        tdo_name.text, tdo_name.length,
        0, 0, // version name (ignored)
        OCI_DURATION_SESSION,
        OCI_TYPEGET_ALL,
        &tdo
    );
    checkerr(rc, errhp());
    // add it to the cache before returning it
    tdo_map_.insert(TdoMap::value_type(type_name, tdo));
    return tdo;
}

然后使用SQLT_NTY类型绑定/定义,OCI调用对象。例如:

OCIType* tdo = conn.get_object_type(tdo_name);
// WARNING: Addresses passed to OCIBindObject *MUST* still be valid
// *after* OCIStmtExecute, so cannot be addresses to local(stack) vars.
checkerr(
    OCIBindObject(
        bind_hndl, errhp(), tdo,
        (void**)&obj_ref.p_value_,     0, // value/indicator sizes
        (void**)&obj_ref.p_indicator_, 0  // not used apparently...
    ),
    errhp()
);

注意,你需要一个普通的bind/define和一个bind/define-object。同样,请参阅关于对象-关系OCI的文档。

然后需要深入研究OCIArray*实例。SDO(空间)udt实际上也不简单……以下是我的几何包装器中的定义:

/*!
 * brief All possible geometries dimensions.
 */
enum Dimension {
    TWO_D = 2, //! a 2D geomertry
    THREE_D = 3, //! a 3D geomertry
    FOUR_D = 4 //! a 4D geomertry
};
/*!
 * brief All possible types of geometries.
 */
enum Type {
    CUSTOM_GEOMETRY = 0,
    POINT = 1,
    LINE = 2,
    POLY = 3,
    COLLECTION = 4, // heterogeous group of all other geometry types
    MULTI_POINT = COLLECTION + POINT,
    MULTI_LINE  = COLLECTION + LINE,
    MULTI_POLY  = COLLECTION + POLY,
    SOLID = 8,
    MULTI_SOLID = 9
};
/*!
 * brief All possible type of geometry elements.
 */
enum ElementType {
    CUSTOM_ELEMENT = 0,
    POINT_ELEMENT = 1,
    LINE_ELEMENT = 2,
    EXTERIOR_POLY = 1003,
    INTERIOR_POLY = 2003,
    COMPOUND_LINE = 4,
    EXTERIOR_COMPOUND_POLY = 1005,
    INTERIOR_COMPOUND_POLY = 2005,
    SURFACE = 1006,
    SURFACE_HOLE = 2006,
    SOLID_ELEMENT = 1007
};
/*!
 * Meaning of the interpretation field for ElementType LINE.
 */
enum LineType {
    SEG_LINE = 1,
    ARC_LINE = 2
};
/*!
 * Meaning of the interpretation field for ElementType
 * EXTERIOR_POLY or INTERIOR_POLY.
 */
enum PolygonType {
    SEG_POLY = 1,
    ARC_POLY = 2,
    RECTANGLE = 3,
    CIRCLE = 4
};
/*!
 * brief A triplet describing a element (piece) of this geometry.
 *
 * A geometry is composed of elements which are described by one or
 * more info elements, each refering to zero or more ordinates.
 */
struct PDGM_OCI_API ElemInfo {
    /*!
     * brief the element's type.
     *
     * Valid values are: 0, 1, 2, 1003, 2003, 4, 1005, 2005, 1006, 2006, 1007.
     */
    ub2 type;
    /*!
     * brief The offset in the ordinate array
     *        at which this element's coordinates starts.
     *
     * The end offset is determined either via the element-specific
     * interpretation field, or the offset for the next ElemInfo.
     *
     * The maximum size of the ordinate array is 1,048,576 (1024^2).
     *
     * Note that persistent offsets are 1-based in Oracle, but this
     * offset is transparently adjusted to be 0-based.
     */
    ub4 offset;
    /*!
     * brief The number of ordinates corresponding to this element.
     *
     * For example, a 2D rectangle has only 4 ordinate, for the x,y
     * of its lower-left and upper-right corners.
     *
     * Note that some elements have no ordinates, for example a compound
     * line string has an initial element info which introduces and precedes
     * the info elements each each line string, but itself has no ordinates
     * per se, only its composed line-strings refer to ordinates.
     */
    ub4 ordinate_count;
    /*!
     * brief element-specific em detail about this element.
     *
     * I'm assuming this is an integer, but could be any number in fact...
     *
     * For an ElemInfo of type 1 (point or vector or multi-point),
     * interpretation can be 0 (vector, and thus ordinates must contain
     * two points, the second normalized), 1 (single point), or N > 1
     * (multi-point) where N is the number of points.
     */
    ub4 interpretation;
    ElementType get_type() const {
        return ElementType(type);
    }
};

正如你所看到的,它绝对是不明显的。Oracle Spatial在c++中的OCI也是我花了很长时间才得到正确的,还有大量的文档阅读和实验。

希望以上内容对您有所帮助。祝你好运-DD

相关文章:
  • 没有找到相关文章