基于geotools,java 存储geojson、shapefile、postgis的导入导出和转换功能

这篇博客介绍了如何利用geotools库在Java环境下将shp文件导入到postgis数据库,将geojson数据存储到postgis,以及从postgis导出矢量表为shp和geojson文件。提供了GitHub资源链接及简单的调用方法说明。
摘要由CSDN通过智能技术生成

2021年1月23日
自己简单总结了以下,在github上。链接地址 :https://github.com/yieryi/gts4vect
如果改功能对您有用,欢迎star,代码只写了第一版,有人反馈问题的话我会改进的。
该博客为原创,转载请注明链接;https://blog.csdn.net/imlang/article/details/81434652;
下面的代码介绍参考性不大,大约是18年写的。没有再检查过。建议直接github上查看代码,封装了几个非常简单的静态方法;
以下不删,仅供参考。不再回复答复。
下面的geojson导入postgis,shp导入postgis,postgis导出shp,postgis导出geojson已经封装好在GitHub上。maven里导入对应的jar(复制pom相应代码),复制PostgisUtility.java和PostgisDataStore.java文件,就可以按照app.java里的方式调用这两个类文件里封装完的四个方法。这四个方法代码如下:
validateshp,validategeojson方法为验证路径有效性,可不调用或者自己判断,否则java可能会报文件路径异常
转载请说明来源。

shp文件导入到postgis数据库中

       public static boolean importShp(String shppath, String tablename) throws IOException {
   
        if (!validateShp(shppath, true)) return false;
        DataStore pgDatastore = postgisDataStore.getInstance();
        ShapefileDataStore shapefileDataStore = null;
        shapefileDataStore = new ShapefileDataStore(new File(shppath).toURI().toURL());
        shapefileDataStore.setCharset(Charset.forName("utf-8"));
        FeatureSource featureSource = shapefileDataStore.getFeatureSource();
        FeatureCollection featureCollection = featureSource.getFeatures();
        SimpleFeatureType shpfeaturetype = shapefileDataStore.getSchema();
        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.init(shpfeaturetype);
        typeBuilder.setName(tablename);
        SimpleFeatureType newtype = typeBuilder.buildFeatureType();
        pgDatastore.createSchema(newtype);
        logger.info("\npostgis创建数据表成功");

        FeatureIterator iterator = featureCollection.features();
        FeatureWriter<SimpleFeatureType, SimpleFeature> featureWriter = pgDatastore.getFeatureWriterAppend(tablename, Transaction.AUTO_COMMIT);

        while (iterator.hasNext()) {
   
            Feature feature = iterator.next();
            SimpleFeature simpleFeature = featureWriter.next();
            Collection<Property> properties =<
  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值