geotools实现将shp导入mysql

一、需要的依赖(mysql)

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

二、需要导入的jar包

 1   package com.hpu.jdbc;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.nio.charset.Charset;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 import org.geotools.data.DataStore;
10 import org.geotools.data.DataStoreFinder;
11 import org.geotools.data.FeatureWriter; 
12 import org.geotools.data.Transaction; 
13 import org.geotools.data.mysql.MySQLDataStoreFactory;
14 import org.geotools.data.shapefile.ShapefileDataStore;
15 import org.geotools.data.simple.SimpleFeatureCollection;
16 import org.geotools.data.simple.SimpleFeatureIterator;
17 import org.geotools.data.simple.SimpleFeatureSource;
18 import org.geotools.data.store.ContentEntry; 
19 import org.geotools.feature.NameImpl; 
20 import org.geotools.feature.simple.SimpleFeatureBuilder; 
21 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; 
22 import org.geotools.geometry.jts.JTSFactoryFinder; 
23 import org.geotools.jdbc.JDBCDataStore;
24 
25 import org.opengis.feature.simple.SimpleFeature; 
26 import org.opengis.feature.simple.SimpleFeatureType;
27 import org.opengis.feature.type.AttributeDescriptor;

三、读取shp文件

 1 public static SimpleFeatureSource readSHP( String shpfile){
 2         SimpleFeatureSource featureSource =null;
 3         try {
 4             File file = new File(shpfile);
 5             ShapefileDataStore shpDataStore = null;
 6 
 7             shpDataStore = new ShapefileDataStore(file.toURL());
 8             //设置编码
 9             Charset charset = Charset.forName("GBK");
10             shpDataStore.setCharset(charset);
11             String tableName = shpDataStore.getTypeNames()[0];
12              featureSource =  shpDataStore.getFeatureSource (tableName);
13         }catch (Exception e){
14             e.printStackTrace();
15         }
16         return featureSource;
17     }

四、连接数据库

 1     public static JDBCDataStore  connnection2mysql(String host,String dataBase,int port,String userName,String pwd ){
 2         JDBCDataStore ds=null;
 3         DataStore dataStore=null;
 4         //连接数据库参数
 5         java.util.Map params = new java.util.HashMap();
 6         params.put(MySQLDataStoreFactory.DBTYPE.key, "mysql");
 7         params.put(MySQLDataStoreFactory.HOST.key, host);
 8         params.put(MySQLDataStoreFactory.PORT.key, port);
 9         params.put(MySQLDataStoreFactory.DATABASE.key, dataBase);
10         params.put(MySQLDataStoreFactory.USER.key, userName);
11         params.put(MySQLDataStoreFactory.PASSWD.key, pwd);        
12         try {
13             dataStore=DataStoreFinder.getDataStore(params);
14             if (dataStore!=null) {
15                 ds=(JDBCDataStore)dataStore;
16                 System.out.println(dataBase+"连接成功");
17             }else{
18                 
19                 System.out.println(dataBase+"连接失败");        
20             }
21 
22         } catch (IOException e) {
23             // TODO Auto-generated catch block
24             
25             e.printStackTrace();
26             
27         }
28         
29       return ds;
30     }

五、创建表格

 1     public static JDBCDataStore createTable(JDBCDataStore ds, SimpleFeatureSource featureSource){
 2         SimpleFeatureType schema = featureSource.getSchema();
 3         try {
 4                  //创建数据表
 5                 ds.createSchema(schema);
 6                         
 7         } catch (IOException e) {
 8             // TODO Auto-generated catch block
 9             e.printStackTrace();
10         }
11         return ds;
12     }

六、写入数据

 1     public static void writeShp2Mysql(JDBCDataStore ds, SimpleFeatureSource featureSource ){
 2         SimpleFeatureType schema = featureSource.getSchema();
 3         //开始写入数据 
 4         try {
 5             FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds .getFeatureWriter(schema.getTypeName().toLowerCase(), Transaction.AUTO_COMMIT);
 6             SimpleFeatureCollection featureCollection = featureSource.getFeatures();
 7             SimpleFeatureIterator features = featureCollection.features();
 8             while (features.hasNext()) {
 9                 writer.hasNext();
10                 SimpleFeature next = writer.next();
11                 SimpleFeature feature = features.next();                
12                 for (int i = 0; i < feature.getAttributeCount(); i++) {
13                     next.setAttribute(i,feature.getAttribute(i) );
14                 }                
15                 writer.write();
16             }            
17             writer.close();
18             ds.dispose();
19             System.out.println("导入成功");
20         } catch (IOException e) {
21             // TODO Auto-generated catch block
22             e.printStackTrace();
23         } // SimpleFeatureIterator itertor = featureSource.getFeatures() // .features(); //create the builder SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
24        
25         
26         
27     }

七、测试代码

1     //测试代码
2     public static void main(String[] args) {
3         JDBCDataStore connnection2mysql = shp2mysql.connnection2mysql("localhost", "testjdbc", 3306, "root", "mysql");
4         SimpleFeatureSource featureSource = readSHP("C:/Users/lenovo/Desktop/beijing/beijing.shp");
5         JDBCDataStore ds = createTable(connnection2mysql, featureSource);
6         writeShp2Mysql(ds, featureSource);
7     }

八、数据中的数据

 

posted @ 2019-04-22 10:45  HPUGIS  阅读(2317)  评论(2编辑  收藏  举报