一、引入geotools的依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>27-SNAPSHOT</version>
</dependency>
二、代码编写
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 {
/**
* 获取数据库连接对象
*
* @return
* @throws Exception
*/
public JDBCDataStore getDataStore(){
Map<String, Object> 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;
}
/**
* 读取shapefile文件
*
* @param file
* @return
*/
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;
}
/**
* 创建表
*
* @param ds
* @param source
* @return
*/
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;
}
/**
* 将shapefile的数据存入数据表中
*
* @param ds
* @param source
*/
public void shp2Table(JDBCDataStore ds , SimpleFeatureSource source){
SimpleFeatureIterator features = null;
//表名为shp文件的名字
String tableName = source.getSchema().getTypeName();
try {
FeatureWriter<SimpleFeatureType, SimpleFeature> 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秒;