C++;查询oracle spatial(sdo_Geometry) 我正在编写一个简单的测试应用程序,用C++中的Oracle OCI查询数据库中的SDOOG几何。由于sdo_Geometry是一种复杂类型,因此我在寻找如何准确实现的好例子时遇到了一些问题。有一些关于使用集合SQL类型的说明,但没有具体说明

C++;查询oracle spatial(sdo_Geometry) 我正在编写一个简单的测试应用程序,用C++中的Oracle OCI查询数据库中的SDOOG几何。由于sdo_Geometry是一种复杂类型,因此我在寻找如何准确实现的好例子时遇到了一些问题。有一些关于使用集合SQL类型的说明,但没有具体说明,c++,oracle11g,spatial,oracle-call-interface,C++,Oracle11g,Spatial,Oracle Call Interface,我的代码基于adpoci项目,可在此处找到: 但是,我需要在该示例上展开以查询sdo_几何体 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; 任何帮助都将不胜感激 是的,这很复杂。

我的代码基于adpoci项目,可在此处找到:

但是,我需要在该示例上展开以查询sdo_几何体

   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),通常使用对象类型转换器(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;
}
请注意,您需要一个普通的绑定/定义对象,然后是一个绑定/定义对象。同样,请参阅关于对象关系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 Sudio C++中的OCI空间也需要很长的时间才能得到正确的答案,并且有大量的文档阅读和实验。 希望以上内容能对您有所帮助。祝你好运

/*!
 * \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);
    }
};