添加依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<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);
}
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;
}
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;
}
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();
}
}