shp数据新增属性

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.");
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值