rain
2024-07-11 c719838525c874e1527b6ee137a163f044ba4a80
src/main/java/com/dji/sample/patches/utils/ShapeFileUtil.java
@@ -3,15 +3,20 @@
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;
@@ -59,21 +64,18 @@
     * @description 通过shp压缩文件,将其转换为GeoJson格式
     */
    //将解压后的文件转换成GeoJson格式
    public static List<ShpDTO> shpToGeoJson(File zipFile) throws IOException {
        ShpDTO shpDTO=new ShpDTO();
        List<ShpDTO> dtoList= new ArrayList<>();
    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");
        List<String> strings = new ArrayList<>();
        try {
            // 获取FeatureCollection
            FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
            FeatureIterator iterator = collection.features();
            List<JSONObject> array = new ArrayList<JSONObject>();
            //遍历feature转为json对象
            while (iterator.hasNext()) {
                ShpDTO shpDTO = new ShpDTO();
                SimpleFeature feature = (SimpleFeature) iterator.next();
                StringWriter writer = new StringWriter();
                fjson.writeFeature(feature, writer);
@@ -84,29 +86,38 @@
                shpDTO.setDKFW(json.getJSONObject("geometry").get("coordinates").toString());
                shpDTO.setGEO(json.getJSONObject("geometry").get("type").toString());
                shpDTO.setXZQDM(json.getJSONObject("properties").get("XZQDM").toString());
                shpDTO.setXZB( json.getJSONObject("properties").getDouble("XZB"));
                shpDTO.setYZB( json.getJSONObject("properties").getDouble("YZB"));
                shpDTO.setDKBH(json.getJSONObject("properties").get("JCBH").toString());
                double[] xy = getLongitudeLatitude(CoordinateSystemUtil.pointCGCStoWGS(
                                json.getJSONObject("properties").getDouble("XZB"),
                                json.getJSONObject("properties").getDouble("YZB")));
                shpDTO.setXZB(xy[0]);
                shpDTO.setYZB(xy[1]);
                shpDTO.setDKBH(json.getJSONObject("properties").getString("JCBH"));
                shpDTO.setJCMJ(json.getJSONObject("properties").getDouble("JCMC"));
                shpDTO.setTBLX(json.getJSONObject("properties").getString("TBLX"));
                shpDTO.setDDTC(json.getJSONObject("properties").getString("DDTC"));
                shpDTO.setHSX(json.getJSONObject("properties").getString("HSX"));
                shpDTO.setJCLX(json.getJSONObject("properties").getString("JCLX"));
                dtoList.add(shpDTO);
                String str2 = json.getJSONObject("geometry").get("coordinates").toString();
                strings.add(str2);
                array.add(json);
            }
            iterator.close();
            //添加到geojsonObject
            geoJsonObject.put("features", array);
            iterator.close();
        } catch (Exception e) {
            throw e;
        }
        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.");
        }
    }
}