package com.appleyk.geotools;

import org.geotools.data.DataStore;
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.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @description:
 * @author: zzl
 * @date: Created in 2021/5/16 13:30
 * @version: 1
 * @modified By:
 */
public class GeoUtils {

    //测试代码
    public static void main(String[] args) throws IOException {

        JDBCDataStore connnection2mysql = GeoUtils.connnection2mysql("192.168.31.196", "nsgk_hc?serverTimezone=GMT%2B8", 3318, "gly", "glyadmin");
        // 会根据shp文件名称创建属性数据table
        SimpleFeatureSource featureSource = readSHP("D:\\NsgkSoft\\files\\uploadPath\\gis\\t_geo_hc_house.shp");
        JDBCDataStore ds = createTable(connnection2mysql, featureSource);
        writeShp2Mysql(ds, featureSource);

    }

    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("UTF-8");
            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 {
            MySQLDataStoreFactory factory1 = new MySQLDataStoreFactory();

            dataStore = factory1.createDataStore(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();
        System.out.println("表结构: " + schema);
        //开始写入数据
        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);


    }
}
