From b6b0cb226fcf184525ee7b36af3a09471e9c0057 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 25 三月 2024 11:29:33 +0800 Subject: [PATCH] 修改数据统计的查询条件 --- src/main/java/com/lf/server/helper/GdalHelper.java | 101 +++++++++++++++----------------------------------- 1 files changed, 30 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/lf/server/helper/GdalHelper.java b/src/main/java/com/lf/server/helper/GdalHelper.java index 1d0f0df..c03235f 100644 --- a/src/main/java/com/lf/server/helper/GdalHelper.java +++ b/src/main/java/com/lf/server/helper/GdalHelper.java @@ -1,6 +1,5 @@ package com.lf.server.helper; -import com.lf.server.entity.ctrl.ShpRecordEntity; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.gdal.gdal.Band; @@ -11,6 +10,7 @@ import org.gdal.ogr.*; import org.gdal.osr.SpatialReference; +import java.io.File; import java.sql.Time; import java.sql.Timestamp; import java.time.LocalDate; @@ -29,52 +29,30 @@ public class GdalHelper { private final static Log log = LogFactory.getLog(GdalHelper.class); - static { + public static void init(String 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"); + + // 閰嶇疆鐜鍙橀噺 + 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(); - - // 涓轰簡鏀寔涓枃璺緞锛岃娣诲姞涓嬮潰杩欏彞浠g爜 - gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); - - // 閰嶇疆GDAL_DATA璺緞锛坓dal鏍圭洰褰曚笅鐨刡in\gdal-data锛� - // gdal.SetConfigOption("GDAL_DATA", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\gdal-data"); - // gdal.SetConfigOption("PROJ_LIB", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\proj7\\share"); - - // 涓轰簡浣垮睘鎬ц〃瀛楁鏀寔涓枃锛岃娣诲姞涓嬮潰杩欏彞锛欳P936 - gdal.SetConfigOption("SHAPE_ENCODING", ""); - } - - /** - * 璇诲彇Shp绗竴鏉¤褰曠殑WKT - */ - public static ShpRecordEntity readShpFirstRecord(String filePath) { - try { - org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); - if (driver == null) { - return null; - } - - DataSource dataSource = driver.Open(filePath); - Layer layer = dataSource.GetLayer(0); - if (layer.GetFeatureCount() < 1) { - return null; - } - - SpatialReference spatialReference = layer.GetSpatialRef(); - String csid = spatialReference.GetAttrValue("AUTHORITY", 1); - - Feature feature = layer.GetFeature(0); - String wkt = feature.GetGeometryRef().ExportToWkt(); - - layer.delete(); - dataSource.delete(); - driver.delete(); - - return new ShpRecordEntity(wkt, csid); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } + ogr.RegisterAll(); } /** @@ -111,29 +89,9 @@ gdal.GDALDestroyDriverManager(); } - private static void writeShp(String filePath) { - try { - org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); - if (driver == null) { - System.out.println(" ESRI shapefile椹卞姩涓嶅彲鐢紒\n"); - System.out.println("fail"); - } - - // Open()鐨勭浜屼釜鍙傛暟榛樿涓�0锛屾槸浠ュ彧璇绘柟寮忔墦寮�鏂囦欢锛�1鏄鍐欐柟寮忔墦寮� - DataSource dSource = driver.Open(filePath, 0); - - Layer layer = dSource.GetLayerByIndex(0); - FieldDefn fieldDefn = new FieldDefn("name", ogr.OFTString); - fieldDefn.SetWidth(50); - layer.CreateField(fieldDefn, 1); - layer.DeleteField(layer.FindFieldIndex("name", 1)); - } catch (Exception ex) { - ex.printStackTrace(); - } - } - /** * 璇诲彇shp鏂囦欢 + * * @param filePath */ public static void readShp(String filePath) { @@ -183,7 +141,7 @@ break; } } catch (Exception ex) { - ex.printStackTrace(); + log.error(ex.getMessage(), ex); } } @@ -246,8 +204,8 @@ //layerDto.setCount(count); //layerDto.setMap(mapList); } - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); } finally { if (dataSource != null) { dataSource.delete(); @@ -257,6 +215,7 @@ /** * 璇诲彇gdb鏂囦欢 + * * @param filePath */ public static void readGdb(String filePath) { @@ -291,11 +250,11 @@ } while (true); } } catch (Exception ex) { - ex.printStackTrace(); + log.error(ex.getMessage(), ex); } } - private static Object getProperty(Feature feature, int index) { + public static Object getProperty(Feature feature, int index) { int type = feature.GetFieldType(index); PropertyGetter propertyGetter; -- Gitblit v1.9.3