springboot中使用gdal将表中的空间数据转shapefile文件

springboot中使用gdal将表中的空间数据转shapefile文件

代码:

java 复制代码
// 样本导出-将样本表导出为shapefile,复制样本shp文件到临时目录下  sampleDir是文件夹path
    public void setYbShapeFile(Yb yb, File sampleDir) {
        // 创建 前时项 和 后时项 文件夹
        File ybDir = new File(sampleDir, "样本");
        ybDir.mkdirs();
        String tableName = yb.getLabelLayerName();
        boolean isYbLabelName = tableName != null && !tableName.equals("");
        String shapefileName = new File(ybDir, tableName + ".shp").getAbsolutePath();

        if (isYbLabelName) {
            // 查询数据
            SqlRowSet resultSet = jdbcTemplate.queryForRowSet("SELECT objectid, ST_AsText(shape) AS shape FROM " + tableName);

            // 使用GDAL库将查询到的数据转换为Shapefile
            ogr.RegisterAll();
            Driver driver = ogr.GetDriverByName("ESRI Shapefile");
            if (driver == null) {
                throw new RuntimeException("GDAL 驱动加载失败: ESRI Shapefile");
            }

            DataSource ds = null;
            Layer layer = null;
            try {
                ds = driver.CreateDataSource(shapefileName);
                if (ds == null) {
                    throw new RuntimeException("无法创建 Shapefile 数据源: " + shapefileName);
                }

                // 定义坐标参考系统(以 WGS 84 为例)
                SpatialReference srs = new SpatialReference();
                srs.ImportFromEPSG(4326);

                // 创建图层并指定参考系统
                layer = ds.CreateLayer(tableName, srs, ogr.wkbUnknown);
                if (layer == null) {
                    throw new RuntimeException("无法创建图层: " + tableName);
                }

                // 定义字段,根据实际数据库字段调整
                layer.CreateField(new FieldDefn("objectid", ogr.OFTInteger));

                // 遍历结果集,添加要素到图层
                while (resultSet.next()) {
                    Feature feature = new Feature(layer.GetLayerDefn());
                    feature.SetField("objectid", resultSet.getInt("objectid"));

                    // 获取几何数据并设置
                    String wktGeometry = resultSet.getString("shape");
                    if (wktGeometry != null && !wktGeometry.isEmpty()) {
                        Geometry geom = ogr.CreateGeometryFromWkt(wktGeometry);
                        if (geom == null) {
                            throw new RuntimeException("无效的WKT几何: " + wktGeometry);
                        }
                        feature.SetGeometry(geom);
                    }

                    // 添加Feature到图层
                    if (layer.CreateFeature(feature) != 0) {
                        throw new RuntimeException("无法创建要素");
                    }

                    feature.delete(); // 释放资源
                }

                // 同步数据到磁盘
                layer.SyncToDisk();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("导出Shapefile时发生错误: " + e.getMessage(), e);
            } finally {
                if (ds != null) {
                    ds.delete(); // 确保数据源关闭
                }

                if (layer != null) {
                    layer.delete(); // 显式释放图层资源
                }
            }
        }
    }

示例表:

转为shp文件后:

相关推荐
CoderYanger2 分钟前
A.每日一题——2141.同时运行N台电脑的最长时间
java·算法·leetcode·职场和发展·1024程序员节
旺仔Sec2 分钟前
2025年广东省职业院校技能大赛应用软件系统开发赛项(高职组)赛题(一)
java·应用软件系统开发
小周在成长7 分钟前
Java 匿名内部类简明指南(重点)
后端
雨中飘荡的记忆8 分钟前
Spring AI + Redis 向量库实战
java·redis·spring
CC.GG11 分钟前
【C++】面向对象三大特性之一——继承
java·数据库·c++
零匠学堂202513 分钟前
woapi-server为Office Online Server文档在线预览提供文档加载地址
java·运维·服务器·oos·wopi
Hui Baby14 分钟前
maven自动构建到镜像仓库
java·maven
郑州光合科技余经理14 分钟前
技术架构:跑腿配送系统海外版源码全解析
java·开发语言·前端·数据库·架构·uni-app·php
czlczl2002092520 分钟前
SpringBoot手动配置:WebMvcConfigurer接口实现类的生效原理
java·spring boot·后端
野生技术架构师21 分钟前
SpringBoot项目使用Redis对用户IP进行接口限流
spring boot·redis·bootstrap