JAVA实现shp文件转wkb文件

时间:2019年11月16日 23:39:38

Java实现shp文件转wkb文件

这是作者在工作中遇到的问题,希望能给大家一点参考价值
文章代码所需包和地址如下,其中 geotools 的地址必须是下面这个,不然会出现问题

仓库与依赖

repositories {
    maven { url 'http://download.osgeo.org/webdav/geotools/' }
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.geotools', name: 'gt-shapefile', version: '21.1'
}

主代码

其中将shp文件转为wkb格式并保存为本地文件的代码如下



import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.opengis.feature.simple.SimpleFeature;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Shp2Wkb {

    private File shpFile = null;
    
    public Shp2Wkb(File shpFile) {
        this.shpFile = shpFile;
    }

   // 将Shapefile中的空间几何对象保存到WKB文件
    public void save(File wkbFile){
        try {
            if (!wkbFile.exists()) {
                wkbFile.createNewFile();
            }
            byte[] WKBByteArray = getGeometryBytes();
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(wkbFile);
                out.write(WKBByteArray);
            } finally {
                out.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //将ShapeFile文件中所有Geometry存入GeometryCollection,并转换为byte[]
    public byte[] getGeometryBytes(){
        ArrayList<Geometry> geometryList = readShpFile();
        Geometry[] geometries = geometryList.toArray(new Geometry[geometryList.size()]);
        GeometryCollection geometryCollection = Utils.getGeometryCollection(geometries);
        byte[] WKBByteArray = Utils.getBytesFromGeometry(geometryCollection);
        return WKBByteArray;
    }
    
   //获取Shapefile文件中的所有空间几何对象
    public ArrayList<Geometry> getGeometry(){
        return readShpFile();
    }

   //读取Shapefile,将其中所有的空间几何对象保存在GeometryCollection中
    private ArrayList<Geometry> readShpFile()  {
        ArrayList<Geometry> geometryArrayList = new ArrayList<>();
        try {
            FileDataStore store = FileDataStoreFinder.getDataStore(shpFile);
            SimpleFeatureSource featureSource = store.getFeatureSource();
            SimpleFeatureCollection featureCollection = featureSource.getFeatures();
            SimpleFeatureIterator featureIterator = featureCollection.features();
            while (featureIterator.hasNext()) {
                SimpleFeature feature = featureIterator.next();
                Object geomObj = feature.getDefaultGeometry();
                geometryArrayList.add((Geometry) geomObj);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return geometryArrayList;
    }
}

utils涉及代码


    public static GeometryCollection getGeometryCollection(Geometry[] geomList) {
        GeometryFactory geometryFactory = new GeometryFactory();
        return new GeometryCollection(geomList, geometryFactory);
    }

    public static byte[] getBytesFromGeometry(Geometry geometry) {
        WKBWriter writer = new WKBWriter();
        byte[] bytes = writer.write(geometry);
        return bytes;
    }

测试代码

测试代码如下

import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import java.io.File;
import java.util.ArrayList;

public class Shp2WkbTest {

    private String shpURL = this.getClass().getResource("/D/D.shp").getFile();
    private File shpFile = new File(shpURL);
    private Shp2Wkb shp2WKB = new Shp2Wkb(shpFile);

    @Test
    public void testShp2Wkb() {
        ArrayList<Geometry> geometryArrayList = shp2WKB.getGeometry();
        System.out.println(geometryArrayList);
    }

    @Test
    public void testSaveWKB(){
        String path = "E:\\SuperMapData\\test\\test.wkb";
        shp2WKB.save(new File(path));
    }
}

这是本人在工作中的一点小总结,希望能给大家带来一点帮助,谢谢观看!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值