2012-10-24 31 views
0

我嘗試使用GDAL/OGR將數據從.mif/.tab文件寫入數據庫。我做的:如何在Java中使用集合?

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(dbFeatureType); 
SimpleFeatureCollection collection = FeatureCollections.newCollection(); 
while (mifReader.hasNext()){  
    in = (SimpleFeatureImpl) mifReader.next(); 
    SimpleFeature dbFeature = featureBuilder.buildFeature(null); 
    for(AttributeDescriptor ad : adList){ 
    String name = ad.getLocalName(); 
    Object attrValue; 
    attrValue = in.getAttribute(name); 
    if(attrValue instanceof String){ 
     String string3 = new String((attrValue.toString()).getBytes("ISO-8859-1"), "cp1251"); 
     if(name.trim().equalsIgnoreCase(kadname.trim())){ 
     dbFeature.setAttribute("kadnum", string3); 
     } 
    } 
    if (attrValue instanceof Geometry){ 
     idn++; 
     com.vividsolutions.jts.geom.Geometry geom = (Geometry) in.getAttribute(name); 
     dbFeature.setAttribute("id", idn); 
     System.out.println("after insrt="+idn); 
     dbFeature.setAttribute("deleted", deleted); 
     dbFeature.setAttribute("the_geom", geom); 
     dbFeature.setAttribute("status_id", status_id); 

    } 
    collection.add(dbFeature); 
    } 
} 

其從.MID文件簡單的數據:

__id_|___kadnum_____ 

    3,"66:2:00:88 951" 
    4,"66:2:00:88 952" 
    5,"66:2:00:88 954" 

,用dB我得到這個:

enter image description here

我的數據是2-4行。你看到我得到增量變量idn,並把它放在屬性id。從.mid中取出kadnum,並將其置於屬性kadnum中。
很好,但在BD我得到錯誤的順序行。我的意思是屬性爲kadnum=66:2:00:88 951的元素會在db的第二行,但在第四行。
它的意思是我會改變集合中物品的順序到相反的。它的權利解決?以及如何做到這一點?或GDAL可以自己做到這一點?

UPDATE

我:

SimplePropertyImpl in =(SimpleFeatureImpl) mifReader.next(); 

我嘗試:

in.getProperty("id").getName() ; 

,我不得到一個屬性名,我得到的字符串。我做錯了什麼?

+4

數據庫通常不保證行按特定順序存儲。如果您想按特定順序從數據庫中獲取行,請在SQL查詢中使用「ORDER BY」子句。 – Jesper

回答

1

您是否想根據id對數據庫行進行排序?

select * from <yourtable> order by id asc; 

如果要排序的FeatureCollection在代碼中,你可能想看看SimpleFeatureCollection.sortSortBy。我會期待像

collection.sort(new SortBy() 
       { 
        @Override PropertyName getPropertyName() { return YourPropertyNameImpl("id"); } 
        @Override SortOrder getSortOrder() { return SortOrder.ASCENDING; } 
       } 

會讓你開始在正確的方向。

乾杯,

+0

嗨,我想排序收集。但我無法找到如何使用sortBy。 –

+0

在這一行'@Override getPropertyName(){return「id」; }'錯誤'類型不匹配:無法從字符串轉換爲PropertyName' –

+0

我確實寫過「...讓你開始......」,你應該自己做一些工作:-) –