利用SDE的C API对SQL数据库进行操作

 摘要:本文以一个C/C++实例分步讲述了利用ArcSDE C API向空间数据库中添加实体图形的方法。

利用ArcSDE8.1 C API向空间数据库中添加实体图形大致可分为六步。步骤如下: 第一步:与空间数据库建立连接。
SE_CONNECTION handle; //空间数据库连接对象
         SE_ERROR Connect_error; //错误状态
         LONG rc; //执行结果状态
         CHAR server[30] = "LCR"; //空间数据库服务器名
         CHAR instance[30] = "esri_sde"; //空间数据库实例名
         CHAR database[30] = ""; //数据库名
         CHAR user[30] = "sa"; //用户名
         CHAR passwd[30] = "sapassword"; //密码

         /* 创建连接 */
         rc = SE_connection_create(
                   server, instance, database, user, passwd, &Connect_error, &handle);
         /*错误检查 */
         check_error (handle, NULL, rc, "SE_connection_create");

第二步:建立用户定义属性表。
         LONG num_cols; //列数
         SE_SHAPE shape; //形
         SE_ENVELOPE rect; //矩形范围
         LONG part_offsets[2], int_val;
         CHAR **attrs; //属性列
         CHAR column[32], table[32]; //列与表
         SE_COORDREF coordref; //坐标系引用
         SE_POINT pt[10]; //坐标点
         SE_LAYERINFO layer; //层信息
         SE_COLUMN_DEF *column_defs; //列定义对象
         SE_STREAM stream; //数据流

         /* 创建数据流连接 */
         SE_stream_create (handle, &stream);
         check_error (handle, NULL, rc, "SE_stream_create");
        
         /* 创建用户表 */
         strcpy(table, "MYTABLE");
         num_cols = 1;
         column_defs = (SE_COLUMN_DEF *) calloc (num_cols, sizeof(SE_COLUMN_DEF));
         strcpy (column_defs[0].column_name, "ATTR_COL");
         column_defs[0].sde_type = SE_INTEGER_TYPE; //为整形列
         column_defs[0].nulls_allowed = TRUE; //允许为空
         rc = SE_table_create(handle, table, num_cols, column_defs,"DEFAULTS");
         check_error (handle, NULL, rc, "SE_table_create");
第三步:创建并设置图层信息。
         /* 创建层的坐标系参考 */
         rc = SE_coordref_create (&coordref);
         check_error (handle, NULL, rc, "SE_coordref_create");
         //设置假定坐标源点与比例尺
         rc = SE_coordref_set_xy (coordref,0,0,10000);
         check_error (handle, NULL, rc, "SE_coordref_set_xy");
         //分配并初始化层信息结构layer
         rc = SE_layerinfo_create (coordref, &layer);
         check_error (handle, NULL, rc, "SE_layerinfo_create");

         //设置层的格网尺寸、允许的形类型、创建关键字和空间列
         rc = SE_layerinfo_set_grid_sizes (layer,1000,0,0);
         check_error (handle, NULL, rc, "SE_layerinfo_set_grid_sizes");
         rc = SE_layerinfo_set_shape_types(layer, SE_NIL_TYPE_MASK |
         SE_POINT_TYPE_MASK |
         SE_LINE_TYPE_MASK |
         SE_SIMPLE_LINE_TYPE_MASK |
         SE_AREA_TYPE_MASK |
         SE_MULTIPART_TYPE_MASK );
         check_error(handle, NULL, rc, "SE_layerinfo_set_shape_types");
         rc = SE_layerinfo_set_creation_keyword (layer, "DEFAULTS");
         check_error (handle, NULL, rc, "SE_layerinfo_set_creation_keyword");
         strcpy(column, "SPATIAL_COL");
         rc = SE_layerinfo_set_spatial_column (layer, table, column);
         check_error (handle, NULL, rc, "SE_layerinfo_set_spatial_column");

         /* 创建层 */
         rc = SE_layer_create (handle, layer, 0,0);
         check_error (handle, NULL, rc, "SE_layer_create");

第四步:创建图形实体对象,并设置属性与空间列。
         /* 创建形 */
         rc = SE_shape_create (coordref, &shape);
         check_error (handle, NULL, rc, "SE_shape_create");

         /* 将表插入数据流 */
         attrs = (CHAR **) malloc (sizeof(CHAR *) * 2);
         attrs[0] = "ATTR_COL";
         attrs[1] = column;
         rc = SE_stream_insert_table (stream, table, 2, (const CHAR**) attrs);
         check_error (handle, NULL, rc, "SE_stream_insert_table");

         free (attrs);


         /* 设置矩形的最值并产生矩形*/
         rect.minx = 5000.0;
         rect.miny = 1000.0;
         rect.maxx = 8000.0;
         rect.maxy = 4000.0;
        
         //生成矩形
         rc = SE_shape_generate_rectangle (&rect, shape);
         check_error (handle, NULL, rc, "SE_shape_generate_rectangle");

         /* 设定形和属性列 */
         int_val = 2;
         rc = SE_stream_set_integer (stream, 1, &int_val);
         check_error (NULL, stream, rc, "SE_stream_set_integer");
         rc = SE_stream_set_shape (stream, 2, shape);
         check_error (NULL, stream, rc, "SE_stream_set_shape");
         /*插入行 */
         rc = SE_stream_execute (stream);
         check_error (NULL, stream, rc, "SE_stream_execute");

         /* 对面域设定坐标数组 – 一个中间有洞的矩形,并生成多边形 */
         part_offsets[0] = 0;
         pt[0].x = 1000.; pt[0].y = 1000.;
         pt[1].x = 4000.; pt[1].y = 1000.;
         pt[2].x = 4000.; pt[2].y = 4000.;
         pt[3].x = 1000.; pt[3].y = 4000.;
         pt[4].x = 1000.; pt[4].y = 1000.;
         pt[5].x = 2000.; pt[5].y = 2000.;
         pt[6].x = 2000.; pt[6].y = 3000.;
         pt[7].x = 3000.; pt[7].y = 3000.;
         pt[8].x = 3000.; pt[8].y = 2000.;
         pt[9].x = 2000.; pt[9].y = 2000.;
        
         //生成多边形
         rc = SE_shape_generate_polygon (10,1,part_offsets,pt,NULL,NULL,shape);
         check_error (handle, NULL, rc, "SE_shape_generate_polygon");

         /* 设定形及属性列 */
         int_val = 3;
         rc=SE_stream_set_integer (stream, 1, &int_val);
         check_error (NULL, stream, rc, "SE_stream_set_integer");
         rc = SE_stream_set_shape (stream, 2, shape);
         check_error (NULL, stream, rc, "SE_stream_set_shape");

         /* 插入行 */
         rc = SE_stream_execute (stream);
         check_error (NULL, stream, rc, "SE_stream_execute");

第五步:释放资源,例如,形对象、坐标参考对象、数据流对象等。
         SE_shape_free (shape);
         SE_coordref_free (coordref);
         SE_stream_free (stream);
        
第六步:关闭与空间数据库的连接。
         SE_connection_free (handle);

posted @ 2008-12-04 18:20  googlegis  阅读(166)  评论(0编辑  收藏  举报

坐标合肥,非典型GIS开发人员 GitHub