java数据库新增列null,将新列属性添加到shapefile并使用Geotools Java将其保存到数据库...

I am transforming a shapefile by adding a new column attributes. Since this task is performed using Java, the only option I know for now is using Geotools. I have 2 main concerns:

1. I am not able to figure out how do I actually add a new column variable. Is the feature.setAttribute("col","value") the answer?

//Upload the ShapeFile

File file = JFileDataStoreChooser.showOpenFile("shp", null);

Map params = new HashMap<>();

params.put("url", file.toURI().toURL());

DataStore store = DataStoreFinder.getDataStore(params);

SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);

String typeName = store.getTypeNames()[0];

FeatureSource source =

store.getFeatureSource(typeName);

Filter filter = Filter.INCLUDE;

FeatureCollection collection = source.getFeatures(filter);

try (FeatureIterator features = collection.features()) {

while (features.hasNext()) {

SimpleFeature feature = features.next();

//adding new columns

feature.setAttribute("ShapeID", "SHP1213");

feature.setAttribute("UserName", "John");

System.out.print(feature.getID());

System.out.print(":");

System.out.println(feature.getDefaultGeometryProperty().getValue());

}

}

/*

* Write the features to the shapefile

*/

Transaction transaction = new DefaultTransaction("create");

// featureSource.addFeatureListener(fl);

if (featureSource instanceof SimpleFeatureStore) {

SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

featureStore.setTransaction(transaction);

try {

featureStore.addFeatures(collection);

transaction.commit();

} catch (Exception problem) {

problem.printStackTrace();

transaction.rollback();

} finally {

transaction.close();

}

System.exit(0); // success!

} else {

System.out.println(typeName + " does not support read/write access");

System.exit(1);

}

Assuming that setattribute is the one which adds, I get the following error for the above code.

Exception in thread "main" org.geotools.feature.IllegalAttributeException:Unknown attribute ShapeID:null value:null

at org.geotools.feature.simple.SimpleFeatureImpl.setAttribute(SimpleFeatureImpl.java:238)

at org.geotools.Testing.WritetoDatabase.main(WritetoDatabase.java:73)

2. After modifying these changes I want to store it in the database(PostGIS). I figured out the below snippet does the task, but doesn't seems to work for me with just shape file insertion

Properties params = new Properties();

params.put("user", "postgres");

params.put("passwd", "postgres");

params.put("port", "5432");

params.put("host", "127.0.0.1");

params.put("database", "test");

params.put("dbtype", "postgis");

dataStore = DataStoreFinder.getDataStore(params);

The error is a NullPointerException in the above case.

解决方案

In GeoTools a (Simple)FeatureType is immutable (unchangeable) so you can't just add a new attribute to a shapefile. So first you must make a new FeatureType with your new attribute included.

FileDataStore ds = FileDataStoreFinder.getDataStore(new File("/home/ian/Data/states/states.shp"));

SimpleFeatureType schema = ds.getSchema();

// create new schema

SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();

builder.setName(schema.getName());

builder.setSuperType((SimpleFeatureType) schema.getSuper());

builder.addAll(schema.getAttributeDescriptors());

// add new attribute(s)

builder.add("shapeID", String.class);

// build new schema

SimpleFeatureType nSchema = builder.buildFeatureType();

Then you need to convert all your existing features to the new schema and add the new attribute.

// loop through features adding new attribute

List features = new ArrayList<>();

try (SimpleFeatureIterator itr = ds.getFeatureSource().getFeatures().features()) {

while (itr.hasNext()) {

SimpleFeature f = itr.next();

SimpleFeature f2 = DataUtilities.reType(nSchema, f);

f2.setAttribute("shapeID", "newAttrValue");

//System.out.println(f2);

features.add(f2);

}

}

Finally, open the Postgis datastore and write the new features to it.

Properties params = new Properties();

params.put("user", "postgres");

params.put("passwd", "postgres");

params.put("port", "5432");

params.put("host", "127.0.0.1");

params.put("database", "test");

params.put("dbtype", "postgis");

DataStore dataStore = DataStoreFinder.getDataStore(params);

SimpleFeatureSource source = dataStore.getFeatureSource("tablename");

if (source instanceof SimpleFeatureStore) {

SimpleFeatureStore store = (SimpleFeatureStore) source;

store.addFeatures(DataUtilities.collection(features));

} else {

System.err.println("Unable to write to database");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值