ArcSDE Java SDK获得要素类的要素数量FeatureCount

很早就有用户反映,ArcSDE的SDK没有直接获得要素类要素数量的函数,用户只能通过基本查询循环来获得要素的总数,因为SDE的开发基本没有看过,也没有太在意,今天看了一下相关的帮助,其实还是有不少办法来获得这个FeatureCount的。

不过我是以如果是版本数据和非版本数据来进行函数的编写,而且版本数据的获得肯定是通用的。

相关环境:

ArcSDE SDK 10

Java SDK

注意:我的SDE里面一个要素类名称“conn”

如果是非版本数据的查询,我们可以直接使用SQL语句来获得,非常方便的

public static void queryLayerFeatureCount() {try {SeConnection conn = getConn();SeQuery query = new SeQuery(conn);//直接使用SQL语句,该情况只适用于非arcsde版本String sql="select count(objectid) from conn";query.prepareSql(sql);query.execute();SeRow row = query.fetch();while (row != null){System.out.println("------featureclass feature count -----------------");System.out.println(row.getObject(0).toString());System.out.println("--------------------------------------------------");row = query.fetch();}} catch (Exception ex) {ex.printStackTrace();}}

但是如果是用户使用版本编辑的话,上面的函数肯定不行,但是我发现有一个表的统计可以获得RowCount

public static void queryLayerVersionedFeatureCount() {try {SeConnection conn = getConn();SeTable table = new SeTable(conn, "conn");SeColumnDefinition[] tableDef = table.describe();String[] cols = new String[tableDef.length];for (int j = 0; j < cols.length; j++){cols[j] = tableDef[j].getName();}SeSqlConstruct sqlCons = new SeSqlConstruct("conn");SeQuery query = new SeQuery(conn, cols, sqlCons);SeQueryInfo queryInfo = new SeQueryInfo();queryInfo.setQueryType(SeQueryInfo.SE_QUERYTYPE_ATTRIBUTE_FIRST);queryInfo.setColumns(cols);queryInfo.setConstruct(sqlCons);//query.prepareQueryInfo(queryInfo);//query.execute();SeTable.SeTableStats stats=query.calculateTableStatistics("*", SeTable.SeTableStats.SE_COUNT_STATS, queryInfo, 0);int count= stats.getCount();System.out.println("------featureclass feature count -----------------");System.out.println(count);System.out.println("--------------------------------------------------");} catch (Exception ex) {ex.printStackTrace();}}

大家是否看到注释的两行代码,如果加上会报如下错误:

 ArcSDE Error Number        : -52 Error Description          : SHAPE/FID STREAM NOT FINISHED, CAN'T EXECUTE. Extended Error Description : 

浪费了我不少时间。

好了,有了上面的例子,大家就不用再整个对要素类进行循环累计计数来获得要素类的要素总数了。

以下代码都运行没有问题,用户只需要修改相关的参数即可,把全部的例子送上

package lish.test;import com.esri.sde.sdk.client.*;public class test {private static SeConnection conn = null;private static String server = "192.168.205.142";private static String instance = "5353";private static String database = "";private static String username = "sde";private static String password = "sde";private static SeConnection getConn() {if (conn == null) {try {conn = new SeConnection(server, instance, database, username,password);} catch (SeException ex) {ex.printStackTrace();}}    return conn;}/** * @param args */public static void main(String[] args) {queryLayerVersionedFeatureCount(); }public static void queryLayerVersionedFeatureCount() {try {SeConnection conn = getConn();SeTable table = new SeTable(conn, "conn");SeColumnDefinition[] tableDef = table.describe();String[] cols = new String[tableDef.length];for (int j = 0; j < cols.length; j++){cols[j] = tableDef[j].getName();}SeSqlConstruct sqlCons = new SeSqlConstruct("conn");SeQuery query = new SeQuery(conn, cols, sqlCons);SeQueryInfo queryInfo = new SeQueryInfo();queryInfo.setQueryType(SeQueryInfo.SE_QUERYTYPE_ATTRIBUTE_FIRST);queryInfo.setColumns(cols);queryInfo.setConstruct(sqlCons);//query.prepareQueryInfo(queryInfo);//query.execute();SeTable.SeTableStats stats=query.calculateTableStatistics("*", SeTable.SeTableStats.SE_COUNT_STATS, queryInfo, 0);int count= stats.getCount();System.out.println("------featureclass feature count -----------------");System.out.println(count);System.out.println("--------------------------------------------------");} catch (Exception ex) {ex.printStackTrace();}}public static void queryLayerFeatureCount() {try {SeConnection conn = getConn();SeQuery query = new SeQuery(conn);//直接使用SQL语句,该情况只适用于非arcsde版本String sql="select count(objectid) from con";query.prepareSql(sql);query.execute();SeRow row = query.fetch();while (row != null){System.out.println("------featureclass feature count -----------------");System.out.println(row.getObject(0).toString());System.out.println("--------------------------------------------------");row = query.fetch();}} catch (Exception ex) {ex.printStackTrace();}}}

————————————————————————————————————————————————————————————————————-

没有朋友的人生是孤独的,不完整的,可是,因为生活的忙碌,

ArcSDE Java SDK获得要素类的要素数量FeatureCount

相关文章:

你感兴趣的文章:

标签云: