栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

geotools将shp数据存入postgres

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

geotools将shp数据存入postgres

一、引入geotools的依赖

引入geotools的方法


            org.geotools
            gt-shapefile
            27-SNAPSHOT
        
        
            org.geotools.jdbc
            gt-jdbc-postgis
            27-SNAPSHOT
        
二、代码编写

1、代码如下;

package com.example.forestry.util.geotools;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class Shp2Pgsql {


    
    public JDBCDataStore getDataStore(){

        Map params = new HashMap<>();
        // 必须是字符串 postgis
        params.put("dbtype", "postgis");
        // ip
        params.put("host", "192.168.xx.xx");
        // 端口
        params.put("port", 5432);
        // 数据库模式
        params.put("schema", "public");
        // 数据库名称
        params.put("database", "cim");
        params.put("user", "postgres");
        params.put("passwd", "postgres");

        JDBCDataStore dataStore = null;

        try {
            DataStore ds = DataStoreFinder.getDataStore(params);

            if(ds==null){
                System.out.println("连接postgres失败");
            }else {
                dataStore = (JDBCDataStore) ds;
                System.out.println("连接postgres成功");
            }

        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接postres出错");
        }

        return dataStore;
    }


    
    public SimpleFeatureSource readShp(File file){
        SimpleFeatureSource featureSource = null;

        ShapefileDataStore shpDataStore = null;
        try {
            shpDataStore = new ShapefileDataStore(file.toURL());
            
            //设置编码
            Charset charset = Charset.forName("UTF8");
            shpDataStore.setCharset(charset);
            String tableName = shpDataStore.getTypeNames()[0];
            featureSource = shpDataStore.getFeatureSource(tableName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            shpDataStore.dispose();
        }
        return featureSource;

    }


    
    public JDBCDataStore createTable(JDBCDataStore ds , SimpleFeatureSource source){
        SimpleFeatureType schema = source.getSchema();

        try {
            ds.createSchema(schema);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("创建postgres数据表失败");
        }

        return ds;
    }


    
    public void shp2Table(JDBCDataStore ds , SimpleFeatureSource source){
        SimpleFeatureIterator features  = null;

        //表名为shp文件的名字
        String tableName = source.getSchema().getTypeName();

        try {

            FeatureWriter writer = ds.getFeatureWriter(tableName, Transaction.AUTO_COMMIT);
            SimpleFeatureCollection featureCollection = source.getFeatures();

            //创建图层数据迭代器
            features = featureCollection.features();

            //进行逐行写入
            while (features.hasNext()) {
                try {
                    writer.hasNext();
                    SimpleFeature next = writer.next();
                    SimpleFeature feature = features.next();
                    for (int i = 0; i < feature.getAttributeCount(); i++) {
                        next.setAttribute(i, feature.getAttribute(i));
                    }
                    writer.write();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("添加数据的方法错误:" + e.toString());
                    continue;
                }
            }
            writer.close();

            System.out.println("导入成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("创建写入条件失败:"+e.toString());
        }finally {
            ds.dispose();
            features.close();
        }

    }


    //进行测试
    public static void main(String[] args) {
        Shp2Pgsql shp2Pgsql = new Shp2Pgsql();

        File file = new File("D:\data\shapefile\tree\tree.shp");

        Long startTime = System.currentTimeMillis();

        //获取postgres连接对象
        JDBCDataStore dataStore = shp2Pgsql.getDataStore();

        //读取shapefile文件
        SimpleFeatureSource simpleFeatureSource = shp2Pgsql.readShp(file);

        //创建数据表
        JDBCDataStore ds = shp2Pgsql.createTable(dataStore, simpleFeatureSource);

        //进行数据写入
        shp2Pgsql.shp2Table(ds , simpleFeatureSource);

        Long endTime = System.currentTimeMillis();

        Long time = (endTime - startTime)/1000;

        System.out.println("时间为: "+time+" 秒");

    }


}

2、插入速度;我电脑i5-10th的cpu,8G内存,跑49万条数据用了255秒;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号