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

相关推荐
SimonKing4 分钟前
颠覆传统IO:零拷贝技术如何重塑Java高性能编程?
java·后端·程序员
sniper_fandc15 分钟前
SpringBoot系列—MyBatis(xml使用)
java·spring boot·mybatis
mCell28 分钟前
为什么我们需要 `.proto` 文件
后端·微服务·架构
胚芽鞘68143 分钟前
查询依赖冲突工具maven Helper
java·数据库·maven
Charlie__ZS1 小时前
若依框架去掉Redis
java·redis·mybatis
mit6.8241 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
陈随易1 小时前
VSCode v1.102发布,AI体验大幅提升
前端·后端·程序员
生无谓1 小时前
什么是跨域,如何处理跨域
后端
咖啡啡不加糖1 小时前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
Smilejudy1 小时前
极具特色的位置运算
后端