rain
2024-08-20 74d1c21ebb6b3b916904d95d13d289df23dcdedd
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
package com.dji.sample.patches.utils;
 
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.dji.sample.patches.model.dto.ShpDTO;
import com.dji.sample.territory.utils.CoordinateSystemUtil;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geojson.feature.FeatureJSON;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.ParseException;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
 
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
 
import static com.dji.sample.patches.utils.TimerUtil.getNowDay;
import static com.dji.sample.patches.utils.TimerUtil.getNowTimeName;
 
public class ShapeFileUtil {
 
    //将文件解压
    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);
            List<SimpleFeature> list = new ArrayList<>();
            Map<String, Object> shapeFileParams = new HashMap();
            shapeFileParams.put("url", shapeFile.toURI().toURL());
            // 设置编码
            shapeFileParams.put("charset", "GBK");
            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;
        }
    }
 
    /**
     * @param zipFile:
     * @return JSONObject
     * @description 通过shp压缩文件,将其转换为GeoJson格式
     */
    //将解压后的文件转换成GeoJson格式
    public static List<ShpDTO> shpToGeoJson(File zipFile) throws IOException, ParseException {
        List<ShpDTO> dtoList = new ArrayList<>();
        FeatureJSON fjson = new FeatureJSON();
        JSONObject geoJsonObject = new JSONObject();
        geoJsonObject.put("type", "FeatureCollection");
        // 获取FeatureCollection
        FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
 
        FeatureIterator iterator = collection.features();
        //遍历feature转为json对象
        while (iterator.hasNext()) {
            ShpDTO shpDTO = new ShpDTO();
            SimpleFeature feature = (SimpleFeature) iterator.next();
            StringWriter writer = new StringWriter();
            fjson.writeFeature(feature, writer);
            String temp = writer.toString();
            byte[] b = temp.getBytes("iso8859-1");
            temp = new String(b, "gbk");
            JSONObject json = JSONObject.parseObject(temp);
            shpDTO.setDKFW(json.getJSONObject("geometry").getString("coordinates"));
 
            if ((json.getJSONObject("geometry").get("type")) != null) {
                shpDTO.setGEO(json.getJSONObject("geometry").getString("type"));
            }
 
            if ((json.getJSONObject("properties").get("XZQDM")) != null) {
                shpDTO.setXZQDM(json.getJSONObject("properties").getString("XZQDM"));
            }
            if ((json.getJSONObject("properties").getDouble("XZB")) != null &&
                    json.getJSONObject("properties").getDouble("YZB") != null) {
                double[] xy = getLongitudeLatitude(CoordinateSystemUtil.pointCGCStoWGS(
                        json.getJSONObject("properties").getDouble("XZB"),
                        json.getJSONObject("properties").getDouble("YZB")));
                shpDTO.setXZB(xy[0]);
                shpDTO.setYZB(xy[1]);
            }
            if ((json.getJSONObject("properties").getString("JCBH") != null)) {
                shpDTO.setDKBH(json.getJSONObject("properties").getString("JCBH"));
            }else {
                shpDTO.setDKBH(getNowDay());
            }
            if (json.getJSONObject("properties").getDouble("JCMC") != null) {
                shpDTO.setJCMJ(json.getJSONObject("properties").getDouble("JCMC"));
            }
            if (json.getJSONObject("properties").getString("TBLX") != null) {
                shpDTO.setTBLX(json.getJSONObject("properties").getString("TBLX"));
            }
            if (json.getJSONObject("properties").getString("DDTC") != null) {
                shpDTO.setDDTC(json.getJSONObject("properties").getString("DDTC"));
            }
            if (json.getJSONObject("properties").getString("HSX") != null) {
                shpDTO.setHSX(json.getJSONObject("properties").getString("HSX"));
            }
            if (json.getJSONObject("properties").getString("JCLX") != null) {
                shpDTO.setJCLX(json.getJSONObject("properties").getString("JCLX"));
            }
            dtoList.add(shpDTO);
        }
        iterator.close();
        return dtoList;
    }
 
    public static void main(String[] args) throws IOException, ParseException {
        File file = new File("D:\\ceshi\\ceshi.zip");
        shpToGeoJson(file);
    }
 
    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.");
        }
    }
}