| | |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.helper.WebHelper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.gdal.Dataset; |
| | |
| | | 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; |
| | |
| | | private SpatialReference sr4326; |
| | | |
| | | private SpatialReference sr4490; |
| | | |
| | | @Value("${sys.gdal_path}") |
| | | private String gdalPath; |
| | | |
| | | private final static Log log = LogFactory.getLog(RasterService.class); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 查询坐标转换 |
| | | * 使用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(); |
| | |
| | | |
| | | 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); |
| | | } |
| | | } |