|
@@ -4,7 +4,7 @@ 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.MultiLineString;
|
|
|
+import org.locationtech.jts.geom.Geometry;
|
|
|
import org.opengis.feature.simple.SimpleFeature;
|
|
|
|
|
|
import java.io.File;
|
|
@@ -19,49 +19,47 @@ import java.util.*;
|
|
|
**/
|
|
|
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 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;
|
|
|
+// }
|
|
|
|
|
|
- private ArrayList<Map> getShapeFile(File file) throws Exception {
|
|
|
+ public static List<String> getShapeFile(File file) throws Exception {
|
|
|
|
|
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
- map.put("url", file.toURI().toURL());
|
|
|
- ArrayList<Map> models = new ArrayList<>();
|
|
|
- ShapefileDataStore shpDataStore = null;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
|
|
+ ShapefileDataStore shpDataStore = null;
|
|
|
shpDataStore = new ShapefileDataStore(file.toURL());
|
|
|
//设置编码
|
|
|
Charset charset = Charset.forName("GBK");
|
|
@@ -73,22 +71,11 @@ public class ShapeReader {
|
|
|
FeatureIterator<SimpleFeature> itertor = result.features();
|
|
|
while (itertor.hasNext()) {
|
|
|
SimpleFeature feature = itertor.next();
|
|
|
- Map<String, String> model = new HashMap<>();
|
|
|
- MultiLineString multiLineString = (MultiLineString) feature.getDefaultGeometry();
|
|
|
- System.out.println(multiLineString.toText());
|
|
|
- models.add(model);
|
|
|
+ list.add(GeoCastUtil.geomToWkt((Geometry)feature.getDefaultGeometry()));
|
|
|
}
|
|
|
- return models;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- //此处传值既可以是一个目录也可以是一个以.shp结尾的文件,如果是目录的话,会遍历读取所有的以.shp结尾的文件
|
|
|
- String filePath = "E:\\zcjy\\一级河流hyd1_4m\\hyd1_4l.shp";
|
|
|
- ShapeReader shapeReader = new ShapeReader();
|
|
|
- List<Map> shapeModels = shapeReader.readShapeFile(filePath);
|
|
|
- for(Map shapeModel : shapeModels){
|
|
|
- System.out.println(shapeModel.toString());
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
}
|