/**  
 * All rights Reserved, Designed By www.summit.com.cn
 */
package com.sysu.admin.utils.shape;

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

import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**   
 * @ClassName:  QuickStart   
 * @Description: geotools 入门引导-- 读取shapefile文件
 * @author: zhangcj
 * @date:   2019年9月29日 下午5:11:51   
 */
public class QuickStart {

//	public static void showShape() throws IOException {
//		File file = JFileDataStoreChooser.showOpenFile("shp", null);
//		if(file == null) {
//			return ;
//		}
//		
//		Map<String,Object> params = new HashMap<>(4);
//		params.put("url", file.toURI().toURL());
//		params.put("create spatial index", false);
//        params.put("memory mapped buffer", false);
//        params.put("charset", "UTF-8");
//        DataStore store = DataStoreFinder.getDataStore(params);
//        SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
//        // 创建地图
//		MapContent map = new MapContent();
//		map.setTitle("QuickStart");
//		Style style = SLD.createSimpleStyle(featureSource.getSchema());
//		Layer layer = new FeatureLayer(featureSource, style);
//		map.addLayer(layer);
//		// 展示地图
//		JMapFrame.showMap(map);
//
//		
//	}
	
	
	
	public static void showShape() {
		File file = JFileDataStoreChooser.showOpenFile("shp", null);
		if(file == null) {
			return ;
		}
		
		try {
//			FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
			ShapefileDataStore dataStore = (ShapefileDataStore) FileDataStoreFinder.getDataStore(file);
//			设置读取shapefile文件的编码格式
			dataStore.setCharset(Charset.forName("UTF-8"));
			ContentFeatureSource featureSource = dataStore.getFeatureSource();


			// 创建地图
			MapContent map = new MapContent();
			map.setTitle("QuickStart");
//			Style style = SLD.createSimpleStyle(featureSource.getSchema());
			Style style = SLD.createLineStyle(new Color(0x1890ff), 2);
			Layer layer = new FeatureLayer(featureSource, style);



			map.addLayer(layer);
			// 展示地图
			JMapFrame.showMap(map);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		showShape();
	}
}

