java 操作shape文件进行坐标转换

1、引入gt-api,gt-shapefile,gt-opengis...相关jar包。

 /**
     * shape文件坐标转换
     * @param srcfilepath 原目录
     * @param destfilepath 目标目录
     */
    public static void transShape(String srcfilepath, String destfilepath,String param,String type,double L1,double L2) throws IOException,ArrayIndexOutOfBoundsException{
        try {
            //源shape文件
            ShapefileDataStore shapeDS = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(new File(srcfilepath).toURI().toURL());
            //创建目标shape文件对象
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
            params.put(ShapefileDataStoreFactory.URLP.key, new File(destfilepath).toURI().toURL());
            ShapefileDataStore ds = (ShapefileDataStore) factory.createNewDataStore(params);
            // 获取原shape文件的坐标系
            CoordinateReferenceSystem coordinateReferenceSystem = shapeDS.getSchema().getCoordinateReferenceSystem();
            // 设置属性
            SimpleFeatureSource fs = shapeDS.getFeatureSource(shapeDS.getTypeNames()[0]);
            //根据源shape文件的simpleFeatureType可以不用retype,而直接用fs.getSchema设置,设置坐标系 DefaultGeographicCRS.WGS84
            ds.createSchema(SimpleFeatureTypeBuilder.retype(fs.getSchema(), coordinateReferenceSystem));

            //设置writer
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);

            //写记录
            SimpleFeatureIterator it = fs.getFeatures().features();
            ICoordTransAppMore ICoordTransAppMore = new CoordTransAppMoreImpl();
            try {
                while (it.hasNext()) {
                    SimpleFeature f = it.next();
                    SimpleFeature fNew = writer.next();
                    List<Object> attributes = f.getAttributes();
                    //System.out.println(attributes.toString());
                    Object the_geom = f.getAttribute("the_geom");
                    Geometry the_geom1 = (Geometry) f.getAttribute("the_geom");
                    // 获取原文件的坐标值

                    String oldWkt = the_geom.toString();
                    String newWkt = "";
                    if ("4param".equals(type)) {
                        ParameterFour parameter = new ParameterFour(param.split(","));
                        newWkt = ICoordTransAppMore.CS_1ToCS_2(oldWkt, parameter);
                    } else {
                        ParameterSeven parameter = new ParameterSeven(param.split(","));
                        newWkt = ICoordTransAppMore.CS_1ToCS_2(oldWkt, parameter, L1, L2, type);
                    }
                    fNew.setAttributes(f.getAttributes());
                    // 设置转后之后的坐标值
                    fNew.setAttribute("the_geom", newWkt);
                    writer.write();
                }
            } finally {
                it.close();
                writer.close();
                ds.dispose();
                shapeDS.dispose();

            }

        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } catch (ArrayIndexOutOfBoundsException ae){
            ae.printStackTrace();
            throw ae;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值