13693261870
6 天以前 73e913fb24bf163ab9c5332ab960b1eb56a6402b
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
package com.terra.system.service.data;
 
import com.terra.common.entity.all.StaticData;
import com.terra.system.helper.StringHelper;
import com.terra.system.helper.WebHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.Geometry;
import org.gdal.ogr.ogr;
import org.gdal.osr.SpatialReference;
//import org.osgeo.proj4j.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.File;
 
/**
 * 栅格服务
 * @author WWW
 * @date 2023-08-27
 */
@Service
public class RasterService {
    private SpatialReference sr4326;
 
    private SpatialReference sr4490;
 
    @Value("${sys.gdal_path}")
    private String gdalPath;
 
    private final static Log log = LogFactory.getLog(RasterService.class);
 
    /**
     * 初始化空间参考
     */
    public void initSr() {
        if (null == sr4326) {
            sr4326 = new SpatialReference();
            sr4326.ImportFromEPSG(StaticData.I4326);
        }
        if (null == sr4490) {
            sr4490 = new SpatialReference();
            sr4490.ImportFromEPSG(StaticData.I4490);
        }
    }
 
    /**
     * 获取栅格数据的EPSG编码
     */
    public Integer getRaterEpsg(String file) {
        Dataset ds = null;
        try {
            File f = new File(file);
            if (!f.exists() || f.isDirectory()) {
                return null;
            }
 
            ds = gdal.Open(file, gdalconst.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) {
                return null;
            }
 
            if (null == ds.GetSpatialRef()) {
                return null;
            }
 
            // PROJCS、 GEOGCS、GEOGCS 或 NULL
            String code = ds.GetSpatialRef().GetAuthorityCode(null);
            if (StringHelper.isEmpty(code)) {
                return null;
            }
 
            return Integer.parseInt(code);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        } finally {
            if (null != ds) {
                ds.delete();
            }
        }
    }
 
    /**
     * 使用GDAL获取EPSG编码
     */
    public Integer getEpsgByGdal(String file) {
        try {
            File f = new File(file);
            if (!f.exists() || f.isDirectory()) {
                return null;
            }
 
            String cmd = String.format("%s\\gdalsrsinfo.exe \"%s\" -o epsg", gdalPath, file);
            String rs = WebHelper.exec2(cmd);
            if (StringHelper.isEmpty(rs)) {
                return null;
            }
 
            String[] strs = rs.split("\n");
            for (String str : strs) {
                if (str.contains("EPSG:")) {
                    String epsg = str.replace("EPSG:", "");
 
                    return Integer.parseInt(epsg);
                }
            }
 
            return 0;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 坐标转换
     */
    public Object csTransform(double x, double y, int epsg) {
        this.initSr();
 
        SpatialReference srs = new SpatialReference();
        srs.ImportFromEPSG(epsg);
 
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(x, y, 0);
        point.AssignSpatialReference(srs);
 
        if (srs.GetName().contains(StaticData.CGCS2000)) {
            point.TransformTo(sr4490);
        } else {
            point.TransformTo(sr4326);
        }
        point.SwapXY();
 
        return new double[]{point.GetX(), point.GetY()};
    }
 
    /**
     * 坐标转换-使用Proj4j库
     */
    public String transformByProj4j(double x, double y, int epsg) {
        /*CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem fromCrs = crsFactory.createFromName("EPSG:" + epsg);
        CoordinateReferenceSystem toCrs = crsFactory.createFromName("EPSG:4326");
 
        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
        CoordinateTransform transform = ctf.createTransform(fromCrs, toCrs);
 
        ProjCoordinate fromCoord = new ProjCoordinate(x, y);
        ProjCoordinate toCoord = new ProjCoordinate();
        transform.transform(fromCoord, toCoord);
 
        return String.format("%f,%f", toCoord.x, toCoord.y);*/
 
        return String.format("%f,%f", 0.0, 0.0);
    }
}