Java调用GDAL实现postgresql数据生成shp和dxf

server/2024/9/24 8:51:18/

需求

由于shp数据存储到postgresql数据库中,前端调用数据库实现数据的渲染,最近有一个新的需求,前端圈选数据,实现数据的下载,数据可以是shp、dxf、excel格式,这里主要记录在后端通过调用gdal来实现这个需求

具体实现

实现数据查询

前端传递一个polygon,需要在后端计算这个polygon裁剪的数据,这样才能得出圈选的数据,polygon格式如下:

"geometry":"POLYGON ((110.97538610392178 21.560137351924578,110.97979054002349 21.560079864736938,110.97929410551264 21.55479668272995,110.97338335499501 21.554995383717067,110.97538610392178 21.560137351924578))"

主要查询:

java">List<Map<String, Object>> resultList = analysisMapper.queryContains(layer, wktPolygon);

sql语句

java"><select id="queryContains" resultType="java.util.Map">SELECT  ST_AsGeoJSON(geom) as geometry,*FROM"${tableName}"WHERE st_contains(st_setsrid('${polygon}'::geometry,4326),st_setsrid(geom,4326))or ST_Intersects(st_setsrid('${polygon}'::geometry,4326), st_setsrid(geom,4326))order by gid</select>

生成shp数据

生成数据需要使用GDAL库,在调用前,需要先注册一下:

java"> ogr.RegisterAll();

主要的思路如下:

  • 由于从数据库中查询的结果是一个表,要生成shp,需要先创建一个shp,在定义属性名及类型,然后写入普通属性,写入空间属性
  • 创建shp
java">   gdal.SetConfigOption("SHAPE_ENCODING", "");// 假设所有要素都有相同的几何类型,仅查看第一个要素String geomTypeStr = (String) resultList.get(0).get("the_geom");String upperWkt = geomTypeStr.toUpperCase();int geomType = ogr.wkbUnknown; // 默认值if (upperWkt.startsWith("POINT")) {geomType = ogr.wkbPoint;} else if (upperWkt.startsWith("LINESTRING")) {geomType = ogr.wkbLineString;} else if (upperWkt.startsWith("POLYGON")) {geomType = ogr.wkbPolygon;} else if (upperWkt.startsWith("MULTIPOINT")) {geomType = ogr.wkbPoint;} else if (upperWkt.startsWith("MULTILINESTRING")) {geomType = ogr.wkbLineString;} else if (upperWkt.startsWith("MULTIPOLYGON")) {geomType = ogr.wkbPolygon;} else if (upperWkt.startsWith("GEOMETRYCOLLECTION")) {geomType = ogr.wkbGeometryCollection;}Driver shpDriver = ogr.GetDriverByName("ESRI Shapefile");DataSource shpDS = shpDriver.CreateDataSource(filePath);Layer glayer = shpDS.CreateLayer(layer, null, geomType);
  • 添加字段
java"> // 添加字段,假设第一个元素包含所有字段Set<String> keys = resultList.get(0).keySet();for (String key : keys) {if (!"geom".equals(key)) {  // 排除几何字段Object value = resultList.get(0).get(key);if (value instanceof Integer) {glayer.CreateField(new FieldDefn(key, 0));} else if (value instanceof Double) {glayer.CreateField(new FieldDefn(key, 2));} else if (value instanceof String) {glayer.CreateField(new FieldDefn(key, 4));}}}
  • 天才数据
java">// 填充数据for (Map<String, Object> featureData : resultList) {Feature feature = new Feature(glayer.GetLayerDefn());for(Map.Entry<String,Object>entry:featureData.entrySet()){String key = entry.getKey();Object value = entry.getValue();if("the_geom".equals(key)){int[] pnSRID = new int[1];try{String  geoJsonString = (String)value;org.gdal.ogr.Geometry geom = org.gdal.ogr.Geometry.CreateFromWkt(geoJsonString);feature.SetGeometry(geom);geom.delete();}catch (Exception e){e.printStackTrace();}}else if(!"geom".equals(key)) {// 设置属性数据int fieldIndex = feature.GetFieldIndex(key);if (value instanceof Integer) {feature.SetField(fieldIndex, (Integer) value);} else if (value instanceof Double) {feature.SetField(fieldIndex, (Double) value);} else if (value instanceof String) {feature.SetField(fieldIndex, (String) value);}}}glayer.CreateFeature(feature);feature.delete();}shpDS.delete();

压缩数据

由于被圈选的图层不止一个,因此多个数据传输比较麻烦,最好将生成的数据打包成压缩包,最终传输给前端

java">public void zipShapefile(String shapefilePath ,String zipFilePath){byte[] buffer = new byte[1024];try{FileOutputStream fos = new FileOutputStream(zipFilePath);ZipOutputStream zos = new ZipOutputStream(fos);File dir = new File(shapefilePath);File[] files = dir.listFiles();for(File file:files){FileInputStream fis = new FileInputStream(file);zos.putNextEntry(new ZipEntry(file.getName()));int length;while ((length = fis.read(buffer)) > 0) {zos.write(buffer, 0, length);}zos.closeEntry();fis.close();}zos.close();}catch(IOException ioe){ioe.printStackTrace();}}

dxf_152">生成dxf

由于shp数据在生成的时候存放在本地的文件夹内,而gdal从shp生成dxf比较简单,因此这里生成dxf会直接先生成shp文件,再从shp文件转成dxf,最后,再将数据打包

java">public void ShapefileToDXF(String shpFilePath){File folder = new File(shpFilePath);if(folder.isDirectory()){File[] files = folder.listFiles();if(files !=null){for(File file:files){if(file.getName().endsWith(".shp")){// 调用转换函数convertShpToDxf(file.getAbsolutePath(), file.getAbsolutePath().replace(".shp", ".dxf"));}}}deleteFilesInFolder(folder,"shp");}}public static void convertShpToDxf(String shpPath, String dxfPath) {// 获取Shapefile驱动Driver shpDriver = ogr.GetDriverByName("ESRI Shapefile");if (shpDriver == null) {System.err.println("Shapefile driver not available.");return;}// 打开Shapefile数据源DataSource shpDataSource = shpDriver.Open(shpPath, 0); // 0 means read-onlyif (shpDataSource == null) {System.err.println("Failed to open shapefile.");return;}if (shpDataSource.GetLayerCount() == 0) {System.err.println("No layers found in shapefile.");shpDataSource.delete();return;}// 获取DXF驱动Driver dxfDriver = ogr.GetDriverByName("DXF");if (dxfDriver == null) {System.err.println("DXF driver not available.");return;}// 创建DXF文件DataSource dxfDataSource = dxfDriver.CreateDataSource(dxfPath, null);if (dxfDataSource == null) {System.err.println("Failed to create DXF file.");return;}// 从Shapefile复制图层到DXFLayer shpLayer = shpDataSource.GetLayerByIndex(0);if (shpLayer == null) {System.err.println("Failed to get layer from shapefile.");return;}// 创建DXF图层,只包含几何数据Layer dxfLayer = dxfDataSource.CreateLayer("dxf_layer", shpLayer.GetSpatialRef(), shpLayer.GetGeomType());if (dxfLayer == null) {System.err.println("Failed to create DXF layer.");System.exit(1);}// 复制几何对象到新图层Feature shpFeature;while ((shpFeature = shpLayer.GetNextFeature()) != null) {// 创建一个新特征,可能需要根据DXF的要求调整几何类型或属性Feature newFeature = new Feature(dxfLayer.GetLayerDefn());newFeature.SetGeometry(shpFeature.GetGeometryRef());// 可能需要调整属性设置代码if (dxfLayer.CreateFeature(newFeature) != 0) {System.err.println("Failed to add feature to DXF.");}newFeature.delete(); // 清理新创建的特征对象}
//        // 复制图层到DXF文件中
//        Layer newLayer = dxfDataSource.CopyLayer(shpLayer, "new_layer_name", null);
//        if (newLayer == null) {
//            System.err.println("Failed to copy layer to DXF.");
//        }// 清理资源shpDataSource.delete(); // 关闭ShapefiledxfDataSource.delete(); // 关闭DXF文件}

删除冗余文件

由于每次请求都会在本地生成文件,因此会造成数据的冗余,因此,再每开启下一次请求时,需要找到存储的文件夹,进行清空,使之一直保持只保存一次请求的文件;再者,再生成dxf时,会先生成shp,相当于shp是中间文件,再传输给前端时会对dxf进行压缩,此时中间文件就可能被压缩进去,因此此时也需要把shp文件删除

java"> public static void deleteFilesInFolder(final File folder,String delType) {if (!folder.exists()) {return;}File[] files = folder.listFiles();if (files != null) {  // 为空的文件夹路径可能导致 null 返回for (File f : files) {if(delType.equals("all")){if (f.isFile()) {f.delete(); // 删除每个文件}}else if(delType.equals("shp")){if(f.getName().endsWith(".dbf")||f.getName().endsWith(".shp")||f.getName().endsWith(".shx")){if (f.isFile()) {f.delete(); // 删除每个文件}}}}}}

生成excel

这个步骤比较简单,代码如下:

java"> Workbook workbook = new HSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");Row headerRow = sheet.createRow(0);//创建表头单元格,并设置单元格的值Set<String> keys = resultList.get(0).keySet();Map<String,Integer> excelKey = new HashMap<>();int colIndex = 0;for (String key : keys) {Cell headerCell1 = headerRow.createCell(colIndex);excelKey.put(key,colIndex);headerCell1.setCellValue(key);colIndex++;}//填充数据int rowIndex = 1;for (Map<String, Object> featureData : resultList){Row row = sheet.createRow(rowIndex++);for(Map.Entry<String,Object>entry:featureData.entrySet()){String key = entry.getKey();Object value = entry.getValue();colIndex = excelKey.get(key);Cell cell = row.createCell(colIndex);cell.setCellValue(value.toString());}}//保存excel文件try(FileOutputStream outputStream = new FileOutputStream(filePath+"\\"+layer+".xls")){workbook.write(outputStream);}

http://www.ppmy.cn/server/23194.html

相关文章

【threejs教程8】threejs添加3D场景键盘控制

【完整效果代码位于文章末】 目录 准备工作 目标 步骤1&#xff1a;初始化按键状态对象 步骤2&#xff1a;监听键盘事件 步骤3&#xff1a;编写事件处理函数 步骤4&#xff1a;更新相机移动方向 总结 完整代码如下 在3D应用开发中&#xff0c;用户交互是一个关键…

Tensorflow AutoGraph 的作用和功能

&#x1f349; CSDN 叶庭云&#xff1a;https://yetingyun.blog.csdn.net/ TensorFlow AutoGraph 是 TensorFlow 中的一个重要特性&#xff0c;它允许开发者使用普通的 Python 语法编写高效的 TensorFlow 图&#xff08;graph&#xff09;。这意味着开发者可以利用 Python 的易…

PhaGCN2:病毒聚类

https://github.com/KennthShang/PhaGCN2.0 安装 mamba create -n phagcn2 python3.9 numpy pytorch networkx2.5 pandas mcl14.137 diamond0.9.14 biopython1.78 scipy1.5.2 conda activate phagcn2 git clone https://github.com/KennthShang/PhaGCN2.0cd database tar -zx…

iOS实现一个高性能的跑马灯

该跑马灯完全通过CATextLayer 实现&#xff0c;轻量级&#xff0c;并且通过 系统的位移动画实现滚动效果&#xff0c;避免了使用displaylink造成的性能瓶颈&#xff0c;使用系统动画&#xff0c;系统自动做了很多性能优化&#xff0c;实现更好的性能&#xff0c;并使用遮罩实现…

NTP授时服务器(GPS授时器)在DCS系统应用

NTP授时服务器&#xff08;GPS授时器&#xff09;在DCS系统应用 前言 随着计算机和网络通信技术的飞速发展&#xff0c;各行业自动化系统数字化、网络化的时代已经到来。这一方面为各控制和信息系统之间的数据交换、分析和应用提供了更好的平台、另一方面对各种实时和历史数据…

SG-9101CG,2520有源晶振,扩展频率晶振

扩展频率晶振中的SG-9101CG&#xff0c;是一款小尺寸2520有源晶振。随着市场小型化、多功能、高信赖度的电子产品需求量大增&#xff0c;产品开发周期时间要求越来越短&#xff0c;传统的石英品振性能优异&#xff0c;但设计制造周期相对较长。SG-9101CG内置一个高稳定的有源晶…

python程序设计语言超详细知识总结

Python 首先 python 并不是简单&#xff0c;什么语言都有基础和高级之分&#xff0c;要想掌握一门语言&#xff0c;必须把高级部分掌握才行。 HelloWorld helloWorld.py print(hello, world)数据类型与变量 变量的数据类型数据类型描述变量的定义方式整数型 (int)整数&…

Django框架之Django安装与使用

一、Django框架下载 首先我们需要先确定好自己电脑上的python解释器环境&#xff0c;否则会导致后面项目所需要的库安装不了以及项目无法运行的问题。 要下载Django并开始使用它&#xff0c;你可以按照以下步骤进行&#xff1a; 1、安装Python 首先&#xff0c;确保你的计算…