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文件后:

相关推荐
guojl1 小时前
营销画像客群架构
后端
guojl1 小时前
Java多任务编排技术
java
为神敬酒者1 小时前
从银行转账实践理解互斥和同步
后端
丶意冷1 小时前
mybatisPlus分页方言设置错误问题 mybatisPlus对于Oceanbase的Oracle租户分页识别错误
java·数据库·oracle·oceanbase
要开心吖ZSH1 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring
桦说编程2 小时前
深入解析CompletableFuture源码实现
java·性能优化·源码
考虑考虑2 小时前
Springboot3.4.x中的@Bean使用
spring boot·后端·spring
努力的小雨2 小时前
AI编程实战:云开发疯狂助攻,React + Vite 做出 FPS 网页游戏不是梦
后端
蓝澈11212 小时前
迪杰斯特拉算法之解决单源最短路径问题
java·数据结构
Kali_072 小时前
使用 Mathematical_Expression 从零开始实现数学题目的作答小游戏【可复制代码】
java·人工智能·免费