package com.leador.gsp.module.parse.test;
import com.leador.gsp.module.parse.service.model.constant.DxtLdType;
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class shpAddAttribute {
public static void main(String[] args) throws IOException {
Map<String, Object> map = new HashMap();
File file = new File("C:\\Users\\82305\\Desktop\\493-0086177280257519255552090\\dxt_element_l_polygon.shp");
map.put("url", file.toURI().toURL());// 必须是URL类型
DataStore dataStore = DataStoreFinder.getDataStore(map);
String typeName = dataStore.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
// 定义要添加的新属性
SimpleFeatureType schema = source.getSchema();
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.init(schema);
builder.add("codePixel", String.class);
SimpleFeatureType newSchema = builder.buildFeatureType();
// 创建新的 shapefile 数据源
File newFile = new File("C:\\Users\\82305\\Desktop\\1\\L_shp\\dxt_element_l_polygon.shp");
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> newParams = new HashMap<>();
newParams.put("url", newFile.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(newParams);
newDataStore.createSchema(newSchema);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(newSchema);
Transaction transaction = new DefaultTransaction("create");
try {
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = newDataStore.getFeatureWriter(newSchema.getTypeName(), transaction);
try (FeatureIterator<SimpleFeature> iterator = collection.features()) {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
featureBuilder.addAll(feature.getAttributes());
int code = Integer.parseInt(feature.getAttribute("code").toString());
featureBuilder.set("codePixel",DxtLdType.ofType(code).codePixel);
SimpleFeature newFeature = featureBuilder.buildFeature(null);
System.out.println(feature.getAttribute("code"));
System.out.println(newFeature.getAttribute("codePixel"));
SimpleFeature newFeatureWithId = writer.next();
for (int i = 0; i < newFeatureWithId.getAttributeCount(); i++) {
newFeatureWithId.setAttribute(i, newFeature.getAttribute(i));
}
writer.write();
}
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
writer.close();
}
transaction.commit();
} finally {
transaction.close();
newDataStore.dispose();
}
System.out.println("New attribute added successfully.");
}
}
shp数据新增属性
最新推荐文章于 2024-06-13 13:14:53 发布