package com.webadmin.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
public class SHPFileReader {
/**
* 获取shape文件feature集合
* @param shapePath shape文件路径
* @param charSet 读取shape文件编码
* @return SimpleFeatureCollection
*/
public static SimpleFeatureCollection getFeatures(String shapePath, String charSet){
SimpleFeatureCollection sfc = null;
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
ShapefileDataStore sds = null;
try {
sds = (ShapefileDataStore)dataStoreFactory.createDataStore(new File(shapePath).toURI().toURL());
sds.setCharset(Charset.forName(charSet));
SimpleFeatureSource featureSource = sds.getFeatureSource();
sfc = featureSource.getFeatures();
} catch (Exception e) {
e.printStackTrace();
}finally{
sds.dispose();
}
return sfc;
}
/**
* 读取ShapeFile中的空间数据
* @param shapeFilePath
* @return List<Geometry>
*/
public static List<Geometry> getGeometries(String shapeFilePath){
List<Geometry> result = new ArrayList<Geometry>();
try {
ShpFiles file = new ShpFiles(shapeFilePath);
ShapefileReader reader = new ShapefileReader(file, false, false, new GeometryFactory());
while(reader.hasNext()){
result.add((Geometry)reader.nextRecord().shape());
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
// 读取Feature测试
// SimpleFeatureCollection sfc = getFeatures("F:/1.科技管矿/1.内网/科技管矿模板/shp测试/Export_Output_2.shp", "GBK");
readDBF("F:/1.科技管矿/1.内网/科技管矿模板/shp测试/Export_Output_2");
}
public static List getShpCoor(String path) {
Map convex = new HashMap();//凸
List list2 = new ArrayList();
List list3 = new ArrayList();
SimpleFeatureCollection sfc = getFeatures(path+".shp", "GBK");
SimpleFeatureIterator iterator = sfc.features();
while(iterator.hasNext()) {
List list = new ArrayList();
SimpleFeature feature = iterator.next();
System.out.println(feature.getAttribute("NAME"));//获取属性名称
Geometry g = (Geometry) feature.getDefaultGeometry();//获取空间数据
Coordinate[] ll =g.getCoordinates();
for (Coordinate coordinate : ll) {
List coorList = new ArrayList();
coorList.add(coordinate.x);
coorList.add(coordinate.y);
list.add(coorList);
}
list3.add(list);
}
convex.put("convex", list3);
convex.put("coorsystem", readPrj(path));
convex.put("table", readDBF(path));
list2.add(convex);
iterator.close();
return list2 ;
}
public static Map readDBF(String path) {
Map map = new HashMap();
try
{
InputStream inputStream = new FileInputStream(path+".dbf");
DBFReader reader = new DBFReader(inputStream,"gbk");
reader.setCharactersetName("gbk");
List headerlist = new ArrayList();
List valuelist = new ArrayList();
int numberOfFields = reader.getFieldCount();
for (int i = 0; i < numberOfFields; i++)
{
DBFField field = reader.getField(i);
System.out.print(field.getName());
headerlist.add(field.getName());
}
Object[] rowObjects;
while ((rowObjects = reader.nextRecord()) != null)
{ List value = new ArrayList();
for (int i = 0; i < rowObjects.length; i++) {
System.out.println(rowObjects[i] + "\t");
value.add(rowObjects[i]);
}
valuelist.add(value);
}
inputStream.close();
map.put("header", headerlist);
map.put("value", valuelist);
}
catch (DBFException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return map;
}
public static String readPrj(String filePath) {
String string = "" ;
try {
File file = new File(filePath+".prj");
if(file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
StringBuffer sBuffer = new StringBuffer();
while ((lineTxt = br.readLine()) != null) {
System.out.println(lineTxt);
sBuffer.append(lineTxt);
}
String[] coor= sBuffer.toString().split("\"");
br.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件读取错误!");
}
return string;
}
}