ParsingShpFileUtils.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package com.onemap.file.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.onemap.common.core.utils.StringUtils;
  4. import com.vividsolutions.jts.geom.Coordinate;
  5. import com.vividsolutions.jts.geom.Geometry;
  6. import com.vividsolutions.jts.geom.Point;
  7. import org.geotools.data.FileDataStore;
  8. import org.geotools.data.FileDataStoreFinder;
  9. import org.geotools.data.shapefile.ShapefileDataStore;
  10. import org.geotools.data.simple.SimpleFeatureCollection;
  11. import org.geotools.data.simple.SimpleFeatureIterator;
  12. import org.geotools.data.simple.SimpleFeatureSource;
  13. import org.geotools.geojson.GeoJSONUtil;
  14. import org.geotools.geojson.feature.FeatureJSON;
  15. import org.geotools.geometry.jts.ReferencedEnvelope;
  16. import org.json.simple.JSONArray;
  17. import org.opengis.feature.Property;
  18. import org.opengis.feature.simple.SimpleFeature;
  19. import org.opengis.feature.simple.SimpleFeatureType;
  20. import org.opengis.feature.type.Name;
  21. import org.opengis.filter.Filter;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.StringWriter;
  26. import java.nio.charset.Charset;
  27. import java.util.*;
  28. /**
  29. * @Author wanger
  30. * @Date 2022/8/22 上午8:8
  31. * @Version 1.0
  32. */
  33. public class ParsingShpFileUtils {
  34. /**
  35. * 解析shp文件
  36. *
  37. * @param filePath
  38. * @return
  39. * @throws Exception
  40. */
  41. public static Map ParsingShpFile(String filePath) throws Exception {
  42. File file = new File(filePath);
  43. if (!file.exists()) {
  44. throw new Exception("文件不存在!");
  45. }
  46. if (!filePath.endsWith("shp")) {
  47. throw new Exception("只能指定后缀为shp的文件");
  48. }
  49. Map map = new HashMap();
  50. List<Map> list = new ArrayList();
  51. //读取shp
  52. SimpleFeatureCollection colls1 = readShp(filePath);
  53. SimpleFeatureType schema = colls1.getSchema();
  54. Name name = schema.getGeometryDescriptor().getType().getName();
  55. ReferencedEnvelope bounds = colls1.getBounds();
  56. //拿到所有features
  57. SimpleFeatureIterator iters = colls1.features();
  58. String s = name.toString();
  59. if ("Point".equals(s)) {
  60. list = parsingPoint(iters);
  61. } else if ("MultiLineString".equals(s) || "MultiPolygon".equals(s)) {
  62. list = parsingLineOrPoly(iters);
  63. }
  64. map.put("data", list);
  65. map.put("maxX", bounds.getMaxX());
  66. map.put("minX", bounds.getMinX());
  67. map.put("maxY", bounds.getMaxY());
  68. map.put("minY", bounds.getMinY());
  69. map.put("shapeFile", name.toString());
  70. return map;
  71. }
  72. /**
  73. * 解析点数据
  74. *
  75. * @param iters
  76. * @return
  77. */
  78. public static List<Map> parsingPoint(SimpleFeatureIterator iters) {
  79. List<Map> list = new ArrayList();
  80. while (iters.hasNext()) {
  81. SimpleFeature sf = iters.next();
  82. Map map = new HashMap();
  83. Iterator<? extends Property> iterator = sf.getValue().iterator();
  84. while (iterator.hasNext()) {
  85. Property property = iterator.next();
  86. if (property.getValue() instanceof Point) {
  87. map.put("PointX", ((Point) (property.getValue())).getX());
  88. map.put("PointY", ((Point) (property.getValue())).getY());
  89. } else {
  90. Name name = property.getName();//属性名称
  91. Object value = property.getValue();//属性值
  92. map.put(name, value);
  93. }
  94. }
  95. list.add(map);
  96. }
  97. iters.close();
  98. return list;
  99. }
  100. /**
  101. * 解析线和面
  102. *
  103. * @param iters
  104. * @return
  105. */
  106. public static List<Map> parsingLineOrPoly(SimpleFeatureIterator iters) {
  107. List<Map> list = new ArrayList();
  108. while (iters.hasNext()) {
  109. SimpleFeature sf = iters.next();
  110. Map map = new HashMap();
  111. Iterator<? extends Property> iterator = sf.getValue().iterator();
  112. while (iterator.hasNext()) {
  113. Property property = iterator.next();
  114. if (property.getValue() instanceof Geometry) {
  115. Geometry geometry = (Geometry) property.getValue();
  116. Coordinate[] coordinates = geometry.getCoordinates();
  117. List<Map> paths = new ArrayList<Map>();
  118. for (Coordinate coordinate : coordinates) {
  119. Map path = new HashMap();
  120. path.put("x", coordinate.x);
  121. path.put("y", coordinate.y);
  122. path.put("z", coordinate.z);
  123. paths.add(path);
  124. }
  125. map.put("path", paths);
  126. } else {
  127. Name name = property.getName();//属性名称
  128. Object value = property.getValue();//属性值
  129. map.put(name, value);
  130. }
  131. }
  132. list.add(map);
  133. }
  134. iters.close();
  135. return list;
  136. }
  137. public static Integer getShpWkt(String path) {
  138. File file = new File(path);
  139. FileDataStore store;
  140. try {
  141. store = FileDataStoreFinder.getDataStore(file);
  142. ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));
  143. store.getFeatureSource().getBounds().getCoordinateReferenceSystem().getName().getCode();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. return 4326;
  148. }
  149. public static SimpleFeatureCollection readShp(String path) {
  150. return readShp(path, null);
  151. }
  152. public static SimpleFeatureCollection readShp(String path, Filter filter) {
  153. SimpleFeatureSource featureSource = readStoreByShp(path);
  154. if (featureSource == null) {
  155. return null;
  156. }
  157. ;
  158. try {
  159. return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures();
  160. } catch (IOException e) {
  161. e.printStackTrace();
  162. }
  163. return null;
  164. }
  165. public static SimpleFeatureSource readStoreByShp(String path) {
  166. File file = new File(path);
  167. FileDataStore store;
  168. SimpleFeatureSource featureSource = null;
  169. try {
  170. store = FileDataStoreFinder.getDataStore(file);
  171. ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));
  172. featureSource = store.getFeatureSource();
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. }
  176. return featureSource;
  177. }
  178. /**
  179. * shp转换为Geojson
  180. *
  181. * @param shpPath shp文件地址
  182. * @param jsonPath 要写入的json文件地址
  183. * @return
  184. */
  185. public static String shape2Geojson(String shpPath, String jsonPath) {
  186. FeatureJSON fjson = new FeatureJSON();
  187. StringBuffer sb = new StringBuffer();
  188. try {
  189. sb.append("{\"type\": \"FeatureCollection\",\"features\": ");
  190. //读取shp
  191. SimpleFeatureCollection colls = readShp(shpPath);
  192. //拿到所有features
  193. SimpleFeatureIterator itertor = colls.features();
  194. JSONArray array = new JSONArray();
  195. while (itertor.hasNext()) {
  196. GeoJSONUtil e = new GeoJSONUtil();
  197. SimpleFeature feature = itertor.next();
  198. StringWriter writer = new StringWriter();
  199. fjson.writeFeature(feature, writer);
  200. JSONObject json = JSONObject.parseObject(writer.toString());
  201. array.add(json);
  202. }
  203. itertor.close();
  204. sb.append(array.toString());
  205. sb.append("}");
  206. if (!StringUtils.isEmpty(jsonPath)) {
  207. //写入文件
  208. FileOutputStream fos = new FileOutputStream(jsonPath, false);
  209. //true表示在文件末尾追加
  210. fos.write(sb.toString().getBytes());
  211. fos.close();
  212. }
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. }
  216. return sb.toString();
  217. }
  218. }