package com.sysu.admin.utils.shape;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.store.ContentFeatureCollection;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.feature.FeatureIterator;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @program: commons
 * @description: 读取Shp文件
 * @author: Mr.superbeyone
 * @create: 2018-09-19 15:26
 **/
public class ShapeReader {

//    public ArrayList<Map> readShapeFile(String filePath) {
//        ArrayList<Map> modelList = new ArrayList<>();
//        File folder = new File(filePath);
//        if (!folder.isDirectory()) {
//            if (folder.toString().endsWith(".shp")) {
//                try {
//                    modelList = getShapeFile(folder);
//                    return modelList;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            } else {
//                System.out.println("选择的文件后缀名不是.shp");
//            }
//        }else{
//	        File[] files = folder.listFiles();
//	        if (!(files.length > 0)) {
//	            System.out.println("目录文件为空");
//	            return modelList;
//	        }
//
//	        for (File file : files) {
//	            if (!file.toString().endsWith(".shp")) {
//	                continue;
//	            }
//	            try {
//	                ArrayList<Map> models = getShapeFile(file);
//	                modelList.addAll(models);
//	            } catch (Exception e) {
//	                e.printStackTrace();
//	            }
//	        }
//        }
//        return modelList;
//    }

    public static List<String> getShapeFile(File file) throws Exception {

        List<String> list = new ArrayList<>();

        ShapefileDataStore shpDataStore = null;
        shpDataStore = new ShapefileDataStore(file.toURL());
        //设置编码
        Charset charset = Charset.forName("GBK");

        shpDataStore.setCharset(charset);
        String typeName = shpDataStore.getTypeNames()[0];
        ContentFeatureSource featureSource = null;
        featureSource =  shpDataStore.getFeatureSource (typeName);


        ContentFeatureCollection result = featureSource.getFeatures();
        FeatureIterator<SimpleFeature> itertor = result.features();
        while (itertor.hasNext()) {
            SimpleFeature feature = itertor.next();
            list.add(GeoCastUtil.geomToWkt((Geometry)feature.getDefaultGeometry()));
        }
        return list;
    }

    public static List<Map<String, Object>> getShapeFileLeizhou(File file) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        ShapefileDataStore shpDataStore = null;
        shpDataStore = new ShapefileDataStore(file.toURL());
        //设置编码
        Charset charset = Charset.forName("UTF-8");
        shpDataStore.setCharset(charset);
        String typeName = shpDataStore.getTypeNames()[0];
        ContentFeatureSource featureSource = null;
        featureSource =  shpDataStore.getFeatureSource (typeName);
        ContentFeatureCollection result = featureSource.getFeatures();
        List<AttributeType> attributeTypes = result.getSchema().getTypes();
        FeatureIterator<SimpleFeature> itertor = result.features();
        for(AttributeType attributeType : attributeTypes){
            System.out.print(attributeType.getName());
            System.out.print(",");
        }
        while (itertor.hasNext()) {
            SimpleFeature feature = itertor.next();

            Map<String, Object> map = new HashMap<>();
            addValue(map, feature, "Area");
            addValue(map, feature, "SimPgnFlag");
            addValue(map, feature, "TOC");
            addValue(map, feature, "quxian");
            addValue(map, feature, "shengdao");
            addValue(map, feature, "hupo");
            addValue(map, feature, "gdp_pop");
            addValue(map, feature, "slope");
            addValue(map, feature, "aspect");
            addValue(map, feature, "name");
            addValue(map, feature, "id_card");
            addValue(map, feature, "crop_type");
            addValue(map, feature, "area_ha");
            addValue(map, feature, "value");
            addValue(map, feature, "intensity");
            addValue(map, feature, "ParcelType");
            map.put("district_code","440882");
            map.put("town_id",file.getName().indexOf("nanxing") > -1 ? "143" : "151");
            Geometry geometry = (Geometry)feature.getDefaultGeometry();
            for(Coordinate coordinate : geometry.getCoordinates()){
                Double[] res = GeoCastUtil.mactorToLonlat(new Double[]{coordinate.getX(), coordinate.getY()});
                coordinate.setX(res[0]);
                coordinate.setY(res[1]);
            }
            map.put("geom",GeoCastUtil.geomToWkt(geometry));
            list.add(map);
        }
        return list;
    }

    public static void addValue(Map<String,Object> map, SimpleFeature feature, String key){
        Object val = feature.getAttribute(key);
        map.put(key.toLowerCase(), val);
    }



}
