From dbd56c635c64d92dd23c7f4c9227f84f77bbd82b Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 16 十月 2024 18:00:27 +0800 Subject: [PATCH] 1 --- src/main/java/com/se/simu/helper/GdalHelper.java | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 202 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/se/simu/helper/GdalHelper.java b/src/main/java/com/se/simu/helper/GdalHelper.java new file mode 100644 index 0000000..1601ea1 --- /dev/null +++ b/src/main/java/com/se/simu/helper/GdalHelper.java @@ -0,0 +1,202 @@ +package com.se.simu.helper; + +import lombok.extern.slf4j.Slf4j; +import org.gdal.gdal.Band; +import org.gdal.gdal.Dataset; +import org.gdal.gdal.gdal; +import org.gdal.gdalconst.gdalconst; +import org.gdal.ogr.*; +import org.gdal.osr.SpatialReference; +import org.gdal.osr.osr; + +import java.io.File; + +/** + * GDAL甯姪绫� + * + * @author WWW + * @date 2024-09-12 + */ +@Slf4j +@SuppressWarnings("ALL") +public class GdalHelper { + public final static int I4326 = 4326; + + public final static int I4490 = 4490; + + public static SpatialReference SR4326; + + public static SpatialReference SR4490; + + public final static String CGCS2000 = "CGCS2000"; + + /** + * 鍒濆鍖� + */ + public static void init(String gdalPath) { + // 閰嶇疆鐜鍙橀噺 + if (!StringHelper.isEmpty(gdalPath)) { + if (WebHelper.isWin()) { + gdal.SetConfigOption("GDAL_DATA", gdalPath + "/gdal-data"); + gdal.SetConfigOption("PROJ_LIB", gdalPath + "/proj7/share"); + //System.setProperty("PROJ_LIB", gdalPath + "/proj7/share") + gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + "/gdalplugins"); + + String path = System.getenv("PATH"); + if (!path.contains(gdalPath)) { + System.setProperty("PATH", path + ";" + gdalPath); + } + } else { + //System.setProperty("java.library.path", gdalPath); + } + } + + // 鏀寔涓枃璺緞 + gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); + // 灞炴�ц〃鏀寔涓枃锛欳P936 + gdal.SetConfigOption("SHAPE_ENCODING", ""); + gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); + gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s"); + + // 娉ㄥ唽鎵�鏈夌殑椹卞姩 + gdal.AllRegister(); + ogr.RegisterAll(); + initSr(); + } + + /** + * 鍒濆鍖栧潗鏍囩郴 + * <p> + * https://blog.csdn.net/CallmeAdo/article/details/127558139 + */ + public static void initSr() { + try { + SR4326 = new SpatialReference(); + SR4326.ImportFromEPSG(I4326); + // 瀵逛簬lat/long椤哄簭鐨勫湴鐞咰RS锛屾暟鎹粛鐒舵槸long/lat椤哄簭鐨� + SR4326.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); + + SR4490 = new SpatialReference(); + SR4490.ImportFromEPSG(I4490); + SR4490.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + /** + * 鍒涘缓閲戝瓧濉� + */ + public static void createPyramid(String file) { + Dataset ds = null; + try { + File f = new File(file); + if (!f.exists() || f.isDirectory()) { + return; + } + + ds = gdal.Open(file, gdalconst.GA_ReadOnly); + if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) { + return; + } + + // 鍒涘缓閲戝瓧濉� + Band band = ds.GetRasterBand(1); + if (0 == band.GetOverviewCount()) { + ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } finally { + if (null != ds) { + ds.delete(); + } + } + } + + /** + * 閿�姣佽祫婧� + */ + public static void delete(Layer layer, DataSource dataSource, Driver driver) { + try { + if (null != layer) { + layer.delete(); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + try { + if (null != dataSource) { + dataSource.delete(); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + try { + if (null != driver) { + driver.delete(); + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + public static Geometry getMinPoint(Dataset ds) + { + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + + double xMin = transform[0]; + double yMin = transform[3] - ds.getRasterYSize() * transform[1]; + + Geometry point = new Geometry(ogr.wkbPoint); + point.AddPoint(xMin, yMin, 0); + + return Transform(ds, point); + } + + public static Geometry getMaxPoint(Dataset ds) + { + /* + * transform[0] 宸︿笂瑙抶鍧愭爣 + * transform[1] 涓滆タ鏂瑰悜鍒嗚鲸鐜� + * transform[2] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂" + * + * transform[3] 宸︿笂瑙抷鍧愭爣 + * transform[4] 鏃嬭浆瑙掑害, 0琛ㄧず鍥惧儚 "鍖楁柟鏈濅笂" + * transform[5] 鍗楀寳鏂瑰悜鍒嗚鲸鐜� + */ + double[] transform = new double[6]; + ds.GetGeoTransform(transform); + + double xMax = transform[0] + (ds.getRasterYSize() * transform[1]); + double yMax = transform[3]; + + Geometry point = new Geometry(ogr.wkbPoint); + point.AddPoint(xMax, yMax, 0); + + return Transform(ds, point); + } + + public static Geometry Transform(Dataset ds, Geometry point) + { + point.AssignSpatialReference(ds.GetSpatialRef()); + if (ds.GetSpatialRef().IsGeographic() > 0) + { + return point; + } + + String srsName = ds.GetSpatialRef().GetName(); + //if (srsName.Contains(CGCS2000)) + //{ + // point.TransformTo(sr4490); + //} + //else + //{ + point.TransformTo(SR4326); + //} + //point.SwapXY(); + + return point; + } +} -- Gitblit v1.9.3