|
|
@@ -193,6 +193,47 @@ public class ParsingShpFileUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public static String shape2Geojson(String shpPath, String jsonPath) {
|
|
|
+ FeatureJSON fjson = new FeatureJSON();
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ try {
|
|
|
+ sb.append("{\"type\": \"FeatureCollection\",\"features\": ");
|
|
|
+ //读取shp
|
|
|
+ SimpleFeatureCollection colls = readShp(shpPath);
|
|
|
+ //拿到所有features
|
|
|
+ SimpleFeatureIterator itertor = colls.features();
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ while (itertor.hasNext()) {
|
|
|
+ GeoJSONUtil e = new GeoJSONUtil();
|
|
|
+ SimpleFeature feature = itertor.next();
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
+ fjson.writeFeature(feature, writer);
|
|
|
+ JSONObject json = JSONObject.parseObject(writer.toString());
|
|
|
+ array.add(json);
|
|
|
+ }
|
|
|
+ itertor.close();
|
|
|
+ sb.append(array.toString());
|
|
|
+ sb.append("}");
|
|
|
+ if (!StringUtils.isEmpty(jsonPath)) {
|
|
|
+ //写入文件
|
|
|
+ FileOutputStream fos = new FileOutputStream(jsonPath, false);
|
|
|
+ //true表示在文件末尾追加
|
|
|
+ fos.write(sb.toString().getBytes());
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * shp转换为Geojson
|
|
|
+ *
|
|
|
+ * @param shpPath shp文件地址
|
|
|
+ * @param jsonPath 要写入的json文件地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String shape2Geojson2(String shpPath, String jsonPath) {
|
|
|
FeatureJSON fjson = new FeatureJSON(new GeometryJSON(7));
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
try {
|