shp文件生成

功能

从数据库查询数据,导出为 Shapefile 文件


关键技术

1. 数据库查询 - MyBatis-Plus LambdaQuery

依赖com.baomidou.mybatisplus.core.toolkit.Wrappers

java 复制代码
// Lambda 查询
List<KsJkpcqk> ksJkpcqks = ksJkpcqkMapper.selectList(
    Wrappers.lambdaQuery(KsJkpcqk.class)
        .gt(KsJkpcqk::getId, 2457)                      // 大于条件
);

2. 创建 Shapefile 数据存储 - GeoTools

依赖org.geotools.data.shapefile.ShapefileDataStoreFactory

java 复制代码
// 配置参数
Map<String, Serializable> params = new HashMap<>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);

// 创建 ShapefileDataStore
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(params);

// 设置字符集
dataStore.setCharset(Charset.forName("UTF-8"));

说明

  • ShapefileDataStoreFactory - Shapefile 数据存储工厂
  • createNewDataStore(params) - 创建新的数据存储
  • setCharset() - 设置编码(解决中文乱码)

3. 定义要素类型 - SimpleFeatureTypeBuilder

依赖org.geotools.feature.simple.SimpleFeatureTypeBuilder

java 复制代码
// 创建要素类型构建器
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("fqkdpc");                               // 类型名称

// 设置坐标系(EPSG:4490 CGCS2000)
CoordinateReferenceSystem crs = CRS.decode("EPSG:4490");
builder.setCRS(crs);

// 添加几何字段
builder.add("the_geom", Point.class);                    // 点几何

// 添加属性字段
builder.add("ksmc", String.class);
builder.add("sxzqdm", Double.class);
builder.add("sxzqmc", String.class);

// 构建要素类型
SimpleFeatureType featureType = builder.buildFeatureType();

说明

  • builder.setCRS() - 设置坐标系
  • builder.add(name, type) - 添加字段(几何/属性)
  • buildFeatureType() - 构建要素类型

4. 创建要素 - SimpleFeatureBuilder + GeometryFactory

依赖org.geotools.feature.simple.SimpleFeatureBuilder, org.locationtech.jts.geom.GeometryFactory

java 复制代码
// 创建要素构建器和几何工厂
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
GeometryFactory geometryFactory = new GeometryFactory();

// 创建点几何
Coordinate coord = new Coordinate(lon, lat);
Point point = geometryFactory.createPoint(coord);

// 添加几何和属性
featureBuilder.add(point);                               // 几何字段
featureBuilder.add("矿山名称");                           // 属性字段

// 构建要素
SimpleFeature feature = featureBuilder.buildFeature("point" + id);

说明

  • GeometryFactory.createPoint() - 创建点几何
  • featureBuilder.add() - 添加字段值(按定义顺序)
  • buildFeature(id) - 构建要素并指定 ID

5. 写入 Shapefile - Transaction

依赖org.geotools.data.DefaultTransaction

java 复制代码
// 获取要素存储
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);

// 创建事务
Transaction transaction = new DefaultTransaction("create");
featureStore.setTransaction(transaction);

try {
    // 添加要素
    featureStore.addFeatures(collection);
    transaction.commit();                                // 提交事务
} catch (Exception e) {
    transaction.rollback();                              // 回滚事务
    throw e;
} finally {
    transaction.close();                                 // 关闭事务
}

// 释放资源
dataStore.dispose();

说明

  • Transaction - 事务管理,确保数据完整性
  • addFeatures() - 添加要素集合
  • commit() / rollback() - 提交/回滚
  • dispose() - 释放资源
相关推荐
野生技术架构师3 小时前
2026 Java 面试全套总结,八股 + 场景 + AI 相关面试考点
java·人工智能·面试
香吧香3 小时前
java服务异常日志只打印异常类型,没有堆栈定位分析
java·jvm·异常
qq_2518364574 小时前
AI 疾病自查功能SpringbootAI项目实战 · 技术实现文档
java·ai·ai编程
逆境不可逃4 小时前
Pi Agent 学习笔记:对话太长以后,如何压缩上下文
java
MetaLite4 小时前
JDK 8~26 核心特性一览:从 Stream 到 Scoped Values
java
wno7045 小时前
Spring Boot WebFlux增删改查
java·spring boot·后端
君顾15 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
SL_staff6 小时前
别急着买商业规则引擎:90%风控场景根本不需要全量编码能力
java·程序员·groovy
ManageEngineITSM7 小时前
DevOps和ITIL是什么关系?是替代还是互补一文讲清
java·服务器·资产管理·变更管理
SL_staff7 小时前
ERP排程总在纸上谈兵?JVS-APS如何用真实产能约束打通计划与执行闭环
java·人工智能·开源