I try make a project to write data from .mif/.tab files to Data Base using GDAL/OGR. I do:
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);
}
}
Its simple data from .mid file:
__id_|___kadnum_____
3,"66:2:00:88 951"
4,"66:2:00:88 952"
5,"66:2:00:88 954"
And in db i get this:
My data is 2-4 rows. You see that i get increment variable idn
and put it in attribute id
. Take a kadnum
from .mid and put it in attribute kadnum
.
Its fine but in bd i get rows in the wrong order. I mean that element with attribute kadnum=66:2:00:88 951
gonna be in second row in db but it in 4th row.
Its mean that i gonna change the order of items in the collection to the opposite. Its right solvetion? And how to do this? Or GDAL can do it by self?
UPDATE
I have:
SimplePropertyImpl in =(SimpleFeatureImpl) mifReader.next();
I try:
in.getProperty("id").getName() ;
And i dont get a PropertyName i get a String. What i doing wrong?
Do you want to sort the database rows according to id
?
select * from <yourtable> order by id asc;
If you want to sort your FeatureCollection
in code, you may want to look into SimpleFeatureCollection.sort and SortBy. I would expect something like
collection.sort( new SortBy()
{
@Override PropertyName getPropertyName() { return YourPropertyNameImpl( "id" ); }
@Override SortOrder getSortOrder() { return SortOrder.ASCENDING; }
}
would get you started in the right direction.
Cheers,