ArcSDE Java SDK獲得要素類的要素數量FeatureCount – JAVA編程語言程序開發技術文章

很早就有用戶反映,ArcSDE的SDK沒有直接獲得要素類要素數量的函數,用戶隻能通過基本查詢循環來獲得要素的總數,因為SDE的開發基本沒有看過,也沒有太在意,今天看瞭一下相關的幫助,其實還是有不少辦法來獲得這個FeatureCount的。
不過我是以如果是版本數據和非版本數據來進行函數的編寫,而且版本數據的獲得肯定是通用的。
相關環境:
ArcSDE SDK 10
Java SDK
註意:我的SDE裡面一個要素類名稱“conn”
如果是非版本數據的查詢,我們可以直接使用SQL語句來獲得,非常方便的
[python]
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
[python] 
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(); 
    } 

大傢是否看到註釋的兩行代碼,如果加上會報如下錯誤:
[python] 
ArcSDE Error Number        : -52 
Error Description          : SHAPE/FID STREAM NOT FINISHED, CAN'T EXECUTE. 
Extended Error Description :  
浪費瞭我不少時間。
好瞭,有瞭上面的例子,大傢就不用再整個對要素類進行循環累計計數來獲得要素類的要素總數瞭。

以下代碼都運行沒有問題,用戶隻需要修改相關的參數即可,把全部的例子送上
[python]
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(); 
        } 
    } 

發佈留言