geotools实现将shp导入mysql
首先该程序是不改变文件的任何信息,直接读取存入到mysql数据库,每次读取一个shp,需要批量读取shp到mysql数据库需要你自己在扩展一下,导入到mysql的shp要保证不能与mysql具有相同的表明。
需要的依赖(mysql)
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-mysql</artifactId>
<version>${geotools.version}</version>
</dependency>
需要导入的jar包
package com.hpu.jdbc;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.mysql.MySQLDataStoreFactory;
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.data.store.ContentEntry;
import org.geotools.feature.NameImpl;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
一、读取shp文件
public static SimpleFeatureSource readSHP( String shpfile){
SimpleFeatureSource featureSource =null;
try {
File file = new File(shpfile);
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(file.toURL());
//设置编码
Charset charset = Charset.forName("GBK");
shpDataStore.setCharset(charset);
String tableName = shpDataStore.getTypeNames()[0];
featureSource = shpDataStore.getFeatureSource (tableName);
}catch (Exception e){
e.printStackTrace();
}
return featureSource;
}
二、连接数据库
public static JDBCDataStore connnection2mysql(String host,String dataBase,int port,String userName,String pwd ){
JDBCDataStore ds=null;
DataStore dataStore=null;
//连接数据库参数
java.util.Map params = new java.util.HashMap();
params.put(MySQLDataStoreFactory.DBTYPE.key, "mysql");
params.put(MySQLDataStoreFactory.HOST.key, host);
params.put(MySQLDataStoreFactory.PORT.key, port);
params.put(MySQLDataStoreFactory.DATABASE.key, dataBase);
params.put(MySQLDataStoreFactory.USER.key, userName);
params.put(MySQLDataStoreFactory.PASSWD.key, pwd);
try {
dataStore=DataStoreFinder.getDataStore(params);
if (dataStore!=null) {
ds=(JDBCDataStore)dataStore;
System.out.println(dataBase+"连接成功");
}else{
System.out.println(dataBase+"连接失败");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ds;
}
三、创建表格
public static JDBCDataStore createTable(JDBCDataStore ds, SimpleFeatureSource featureSource){
SimpleFeatureType schema = featureSource.getSchema();
try {
//创建数据表
ds.createSchema(schema);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ds;
}
四、写入数据
public static void writeShp2Mysql(JDBCDataStore ds, SimpleFeatureSource featureSource ){
SimpleFeatureType schema = featureSource.getSchema();
//开始写入数据
try {
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds .getFeatureWriter(schema.getTypeName().toLowerCase(), Transaction.AUTO_COMMIT);
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator features = featureCollection.features();
while (features.hasNext()) {
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();
}
writer.close();
ds.dispose();
System.out.println("导入成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // SimpleFeatureIterator itertor = featureSource.getFeatures() // .features(); //create the builder SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
}
五、测试代码
//测试代码
public static void main(String[] args) {
JDBCDataStore connnection2mysql = shp2mysql.connnection2mysql("localhost", "testjdbc", 3306, "root", "mysql");
SimpleFeatureSource featureSource = readSHP("C:/Users/lenovo/Desktop/beijing/beijing.shp");
JDBCDataStore ds = createTable(connnection2mysql, featureSource);
writeShp2Mysql(ds, featureSource);
}
mysql导入的数据
转载自:https://blog.csdn.net/weixin_40184249/article/details/88644932