From ca8d3861b4e9a28beead77e81ecfa23d15d5eb5f Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 29 九月 2024 09:36:42 +0800 Subject: [PATCH] 获取CPU核心数 --- src/main/java/com/se/simu/helper/GdalHelper.java | 131 +++++++++++++++++++++++++++++++++++++++---- 1 files changed, 117 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/se/simu/helper/GdalHelper.java b/src/main/java/com/se/simu/helper/GdalHelper.java index 5de0452..32ca4ff 100644 --- a/src/main/java/com/se/simu/helper/GdalHelper.java +++ b/src/main/java/com/se/simu/helper/GdalHelper.java @@ -1,8 +1,16 @@ 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.DataSource; +import org.gdal.ogr.Driver; +import org.gdal.ogr.Layer; import org.gdal.ogr.ogr; +import org.gdal.osr.SpatialReference; +import org.gdal.osr.osr; import java.io.File; @@ -10,12 +18,42 @@ * GDAL甯姪绫� * * @author WWW - * @date 2024-07-16 + * @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 @@ -23,21 +61,86 @@ 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"); - // 閰嶇疆鐜鍙橀噺 - if (!StringHelper.isEmpty(gdalPath)) { - gdal.SetConfigOption("GDAL_DATA", gdalPath + File.separator + "gdal-data"); - gdal.SetConfigOption("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share"); - //System.setProperty("PROJ_LIB", gdalPath + File.separator + "proj7" + File.separator + "share") - gdal.SetConfigOption("GDAL_DRIVER_PATH", gdalPath + File.separator + "gdalplugins"); - - String path = System.getenv("PATH"); - if (!path.contains(gdalPath)) { - System.setProperty("PATH", path + ";" + gdalPath); - } - } - // 娉ㄥ唽鎵�鏈夌殑椹卞姩 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); + } } } -- Gitblit v1.9.3