zhongrj
2024-11-19 77031c2c9fca6c55f74e636c5ea765cc1dc1fd6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package org.springblade.common.utils;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSONObject;
import org.geotools.data.*;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.springblade.core.log.exception.ServiceException;
 
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;
 
public class ShapeFileUtil {
 
    /**
     * 导出shp文件
     *
     * @param dataPropertiesList 属性列表{属性名:属性值}
     * @param fileName           导出shp文件名
     * @param geomType           geometry类型
     * @param saveFolder         导出位置
     */
    public static void exportShp(List<Map<String, Object>> dataPropertiesList,
                                 String fileName,
                                 String geomType,
                                 String saveFolder) {
        String path = saveFolder + "/" + fileName;
        //创建保存shp文件夹
        File dir = new File(path);
        if (!dir.exists()) {
            FileUtil.mkdir(dir);
        }
        //shp文件路径
        String shpFileName = fileName + ".shp";
        String fileUrl = path+ "/" + shpFileName;
        File file = new File(fileUrl);
 
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
        ShapefileDataStore ds = null;
        try {
            Map<String, Serializable> params = new HashMap<>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
 
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            //设置坐标系
            CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true);
            tb.setCRS(crs84);
            //设置文件名
            tb.setName(fileName);
 
            //定义导出shp文件地块属性名称
            String geomProperty = "the_geom";
            String idProperty = "ID";
            String nameProperty = "name";
            String firmIdProperty = "firmId";
            String firmNameProperty = "firmName";
 
            //设置图形类型
            if ("Polygon".equals(geomType)) {
                tb.add(geomProperty, Polygon.class);
            } else if ("MultiPolygon".equals(geomType)) {
                tb.add(geomProperty, MultiPolygon.class);
            } else if ("Point".equals(geomType)) {
                tb.add(geomProperty, Point.class);
            } else if ("MultiPoint".equals(geomType)) {
                tb.add(geomProperty, MultiPoint.class);
            } else if ("LineString".equals(geomType)) {
                tb.add(geomProperty, LineString.class);
            } else if ("MultiLineString".equals(geomType)) {
                tb.add(geomProperty, MultiLineString.class);
            } else {
                throw new ServiceException("Geometry中没有该类型:" + geomType);
            }
            //设置对应属性类型
            tb.add(idProperty, String.class);
            tb.add(nameProperty, String.class);
            tb.add(firmIdProperty, String.class);
            tb.add(firmNameProperty, String.class);
 
            //设置默认geometry
            tb.setDefaultGeometry(geomProperty);
            //创建
            ds.createSchema(tb.buildFeatureType());
            ds.setCharset(StandardCharsets.UTF_8);
 
            //设置Writer
            writer = ds.getFeatureWriter(ds.getTypeNames()[0],
                Transaction.AUTO_COMMIT);
            SimpleFeature feature;
            for (Map<String, Object> map : dataPropertiesList) {
                feature = writer.next();
                //属性赋值  geometry要赋值wkt格式的
                if (map.containsKey("geometry")) {
                    feature.setAttribute(geomProperty, new WKTReader().read((MapUtil.getStr(map, "geometry"))));
                }
                feature.setAttribute(idProperty, MapUtil.getStr(map, idProperty));
                feature.setAttribute(nameProperty, MapUtil.getStr(map, nameProperty));
                feature.setAttribute(firmIdProperty, MapUtil.getStr(map, firmIdProperty));
                feature.setAttribute(firmNameProperty, MapUtil.getStr(map, firmNameProperty));
//                String description = MapUtil.getStr(map, "描述");
//                if (CharSequenceUtil.isNotBlank(description)) {
//                    feature.setAttribute(descriptionProperty, description);
//                }
            }
            writer.write();
        } catch (IOException | FactoryException | ParseException e) {
            e.printStackTrace();
        } finally {
            //关闭相关流
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (ds != null) {
                ds.dispose();
            }
        }
    }
 
    /**
     * 导出shp文件
     *
     * @param map 属性列表{属性名:属性值}
     * @param fileName           导出shp文件名
     * @param geomType           geometry类型
     * @param saveFolder         导出位置
     */
    public static void exportShp(Map<String, Object> map,
                                 String fileName,
                                 String geomType,
                                 String saveFolder) {
        String path = saveFolder + "/" + fileName;
        //创建保存shp文件夹
        File dir = new File(path);
        if (!dir.exists()) {
            FileUtil.mkdir(dir);
        }
        //shp文件路径
        String shpFileName = fileName + ".shp";
        String fileUrl = path+ "/" + shpFileName;
        File file = new File(fileUrl);
 
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = null;
        ShapefileDataStore ds = null;
        try {
            Map<String, Serializable> params = new HashMap<>();
            params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
            ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
 
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            //设置坐标系
            CoordinateReferenceSystem crs84 = CRS.decode("EPSG:4326", true);
            tb.setCRS(crs84);
            //设置文件名
            tb.setName(fileName);
 
            //定义导出shp文件地块属性名称
            String geomProperty = "the_geom";
            String idProperty = "ID";
            String nameProperty = "name";
 
            //设置图形类型
            if ("Polygon".equals(geomType)) {
                tb.add(geomProperty, Polygon.class);
            } else if ("MultiPolygon".equals(geomType)) {
                tb.add(geomProperty, MultiPolygon.class);
            } else if ("Point".equals(geomType)) {
                tb.add(geomProperty, Point.class);
            } else if ("MultiPoint".equals(geomType)) {
                tb.add(geomProperty, MultiPoint.class);
            } else if ("LineString".equals(geomType)) {
                tb.add(geomProperty, LineString.class);
            } else if ("MultiLineString".equals(geomType)) {
                tb.add(geomProperty, MultiLineString.class);
            } else {
                throw new ServiceException("Geometry中没有该类型:" + geomType);
            }
            //设置对应属性类型
            tb.add(idProperty, String.class);
            tb.add(nameProperty, String.class);
 
            //设置默认geometry
            tb.setDefaultGeometry(geomProperty);
            //创建
            ds.createSchema(tb.buildFeatureType());
            ds.setCharset(StandardCharsets.UTF_8);
 
            //设置Writer
            writer = ds.getFeatureWriter(ds.getTypeNames()[0],
                Transaction.AUTO_COMMIT);
            SimpleFeature feature;
            feature = writer.next();
            //属性赋值  geometry要赋值wkt格式的
            feature.setAttribute(geomProperty, new WKTReader().read((MapUtil.getStr(map, "geometry"))));
            feature.setAttribute(idProperty, MapUtil.getStr(map, idProperty));
            feature.setAttribute(nameProperty, MapUtil.getStr(map, nameProperty));
            writer.write();
        } catch (IOException | FactoryException | ParseException e) {
            e.printStackTrace();
        } finally {
            //关闭相关流
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (ds != null) {
                ds.dispose();
            }
        }
    }
 
    /**
     * 将文件解压
     * @param zipFile
     * @return
     * @throws IOException
     */
    public static FeatureCollection getFeatureCollectionByShpFile(File zipFile) throws IOException {
        try {
            String tempDir = FileUtil.getTmpDirPath();
            File shapeDir = new File(tempDir + File.separator + new Date().getTime());
            shapeDir.mkdir();
            List<String> files = ZipUtil.unZipFiles(zipFile, shapeDir.getPath() + File.separator);
            String shapFileName = "";
            for (String fileName : files) {
                String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                if ("shp".equals(suffix)) {
                    shapFileName = fileName;
                }
            }
            File shapeFile = new File(shapFileName);
            Map<String, Object> shapeFileParams = new HashMap();
            shapeFileParams.put("url", shapeFile.toURI().toURL());
            // 设置编码
            shapeFileParams.put("charset", "utf-8");
            DataStore dataStore = DataStoreFinder.getDataStore(shapeFileParams);
            if (dataStore == null) {
                throw new RuntimeException("couldn't load the damn data store: " + shapeFileParams);
            }
            String typeName = dataStore.getTypeNames()[0];
            FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
            Filter filter = Filter.INCLUDE;
            FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
            return collection;
        } catch (Exception e) {
            throw e;
        }
    }
 
    /**
     * 解析shp文件返回数据
     * @param zipFile:
     * @return JSONObject
     * @description 通过shp压缩文件,将其转换为GeoJson格式
     */
    //将解压后的文件转换成GeoJson格式
    public static List<Map<String,Object>> shpToGeoJson(File zipFile) throws IOException{
        List<Map<String,Object>> dtoList = new ArrayList<>();
        try {
            FeatureJSON featureJSON = new FeatureJSON();
            JSONObject geoJsonObject = new JSONObject();
            geoJsonObject.put("type", "FeatureCollection");
            // 解压zip文件,获取FeatureCollection
            FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
            // 获取迭代对象
            FeatureIterator iterator = collection.features();
            // 遍历feature转为json对象
            while (iterator.hasNext()) {
                Map<String,Object> shpDTO = new HashMap<>();
                SimpleFeature feature = (SimpleFeature) iterator.next();
                StringWriter writer = new StringWriter();
                featureJSON.writeFeature(feature, writer);
                String temp = writer.toString();
                JSONObject json = JSONObject.parseObject(temp);
                Object value = feature.getDefaultGeometryProperty().getValue();
                try {
                    // 空间坐标
                    shpDTO.put("geometry",value);
                    // 空间类型
                    if (json.getJSONObject("geometry").get("type") != null) {
                        shpDTO.put("type",json.getJSONObject("geometry").getString("type"));
                    }
                    // 名称
                    if (json.getJSONObject("properties").get("Name")!= null) {
                        shpDTO.put("name",json.getJSONObject("properties").getString("Name"));
                    }
                    if (json.getJSONObject("properties").get("TYPE")!= null) {
                        shpDTO.put("name",json.getJSONObject("properties").getString("TYPE"));
                    }
                    // 所属公司名称
                    if (json.getJSONObject("properties").get("name_1")!= null) {
                        shpDTO.put("firmName",json.getJSONObject("properties").getString("name_1"));
                    }
                    if (json.getJSONObject("properties").get("QYMC")!= null) {
                        shpDTO.put("firmName",json.getJSONObject("properties").getString("QYMC"));
                    }
                    dtoList.add(shpDTO);
                } catch (NumberFormatException e) {
                    System.err.println("处理单个特征时发生错误:" + e.getMessage());
                } catch (Exception e) {
                    System.err.println("处理单个特征时发生错误:" + e.getMessage());
                }
            }
            iterator.close();
        } catch (IOException e) {
            System.err.println("读取或解析文件时发生错误:" + e.getMessage());
            throw e;
        }
        return dtoList;
    }
 
 
    public static void main(String[] args) throws IOException, ParseException {
//        File file = new File("F:\\test4jdata\\shp\\import\\zrq.zip");
        File file = new File("F:\\test4jdata\\shp\\zip\\test.zip");
        List<Map<String, Object>> list = shpToGeoJson(file);
        System.out.println("list = " + list);
 
        // 导出shp 文件
//        List<Map<String,Object>> propertyList = new ArrayList<>();
//        for (int i = 0; i < 3; i++) {
//            Map<String,Object> map = new HashMap<>();
//            map.put("ID", i);
//            map.put("名称", "test" + i);
//            map.put("描述", "测试shp导出" + i);
//            map.put("geometry", "MULTILINESTRING ((114.0888763800001 22.549298400000055, 114.0897166200001 22.54931240800005, 114.09006708000004 22.549318250000056, 114.09104754000009 22.549328150000065))");
//            propertyList.add(map);
//        }
//        String exportPath = "F:\\test4jdata\\shp\\export\\";
//        exportShp(propertyList, "test", "MultiLineString",exportPath);
//        // 打包成 zip 包,然后导出
//        String zipPath = "F:\\test4jdata\\shp\\zip\\test.zip";
//        boolean folder = ZipUtil.zipFolder(exportPath, zipPath);
        // 打包成功后导出
 
    }
 
    public static double[] getLongitudeLatitude(Geometry wktPoint) throws ParseException {
        Geometry geometry = wktPoint;
        if (geometry instanceof Point) {
            Point point = (Point) geometry;
            double longitude = point.getX();
            double latitude = point.getY();
            return new double[]{longitude, latitude};
        } else {
            throw new IllegalArgumentException("The geometry is not a point.");
        }
    }
}