geotools学习之将shp文件导入postgresql数据库

777 阅读1分钟

添加依赖

      <dependency>
        <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <!--geotools-->
        <dependency>
            <groupId>org.geotools.jdbc</groupId>
            <artifactId>gt-jdbc-postgis</artifactId>
            <version>${geotools.version}</version>
        </dependency>

添加仓库

       <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>boundless</id>
            <name>Boundless Maven Repository</name>
            <url>http://repo.boundlessgeo.com/main</url>
        </repository>
    </repositories>

代码实现

   @Test
    public void contextLoads() {
        JDBCDataStore jdbcDataStore=connectPostgis();
        SimpleFeatureSource simpleFeatureSource=readSHP("E:/青海省草原类型图_xa80/青海省草原类型图_xa80.shp");
        JDBCDataStore ds=createTable(jdbcDataStore,simpleFeatureSource);
        writeShp2Postgis(ds,simpleFeatureSource);
    }
    /**
     * 读取shp文件
     * @param shpPath
     * @return
     */
    public static SimpleFeatureSource readSHP(String shpPath){
        //数据源
        SimpleFeatureSource simpleFeatureSource=null;
        try{
            File file=new File(shpPath);
            //定义数据存储
            ShapefileDataStore shapefileDataStore=null;
            shapefileDataStore=new ShapefileDataStore(file.toURL());
            //设置编码
            Charset charset=Charset.forName("GBK");
            shapefileDataStore.setCharset(charset);
            String tableName=shapefileDataStore.getTypeNames()[0];
            //获取数据源
            simpleFeatureSource=shapefileDataStore.getFeatureSource(tableName);


        }catch (Exception e){
            e.printStackTrace();
        }
        return  simpleFeatureSource;
    }


    /**
     * 链接到postgis
     * @return
     */
    public static JDBCDataStore connectPostgis() {
        JDBCDataStore jdbcDataStore=null;
        DataStore dataStore;
        Map<String,Object> params=new HashMap<String,Object>();
        params.put("dbtype", "postgis");
        params.put("host", "localhost");
        params.put("port", 5432);
        params.put("schema", "public");
        params.put("database", "test");
        params.put("user", "postgres");
        params.put("passwd", "123456");

        try{
            dataStore = DataStoreFinder.getDataStore(params);
            if(dataStore!=null){
                jdbcDataStore=(JDBCDataStore) dataStore;
                System.out.println("链接数据库成功");
            }else{
                System.out.println("链接数据库失败");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return jdbcDataStore;
    }

    /**
     * 创建表
     */
    public static JDBCDataStore createTable(JDBCDataStore jdbcDataStore,SimpleFeatureSource simpleFeatureSource){
        SimpleFeatureType simpleFeatureType=simpleFeatureSource.getSchema();
        try{
            jdbcDataStore.createSchema(simpleFeatureType);
        }catch (IOException e){
            e.printStackTrace();
        }
        return jdbcDataStore;
    }

    /**
     * 把shp写入postgis
     * @param jdbcDataStore
     * @param simpleFeatureSource
     */
    public static void writeShp2Postgis(JDBCDataStore jdbcDataStore,SimpleFeatureSource simpleFeatureSource){
        //获取模式
        SimpleFeatureType simpleFeatureType=simpleFeatureSource.getSchema();
        //开始写入数据
        try{
            //获取要素写入
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = jdbcDataStore .getFeatureWriter(simpleFeatureType.getTypeName().toLowerCase(),null);
            //获取数据源
            SimpleFeatureCollection  simpleFeatureCollection=simpleFeatureSource.getFeatures();
            //获取要素遍历器
            SimpleFeatureIterator iterator=simpleFeatureCollection.features();
            while (iterator.hasNext()){
                //写入下一个
                writer.hasNext();
                //要写入的空要素
                SimpleFeature writeNext=writer.next();
                //遍历获得的要素
                SimpleFeature simpleFeature=iterator.next();
                //赋值属性
                for(int i=0;i<simpleFeature.getAttributeCount();i++){
                    writeNext.setAttribute(i,simpleFeature.getAttribute(i));
                }
                //写入
                writer.write();
            }
            writer.close();
            jdbcDataStore.dispose();
            System.out.println("导入成功");
        }catch (IOException e){
            e.printStackTrace();
        }
    }