Skip to content

Shapefile Support ​

shapefile 模块提供对 ESRI Shapefile 格式的全面支持,这是最广泛使用的地理空间矢量数据格式之一。本文档解释了 shapefile 模块在 GeoTools 中的工作原理,包括其组件、使用模式和高级功能。

Shapefile 格式概述 ​

shapefile 实际上是一组文件的集合,它们共同存储几何图形和属性数据。每个 shapefile 至少由三个强制性文件组成,这些文件具有相同的基本名称但扩展名不同:

扩展名描述必需
.shp包含几何数据是
.shx包含几何位置信息的索引文件是
.dbf包含属性数据的 dBASE 文件是
.prj以 WKT 格式的投影定义文件否(推荐)
.qix用于更快查询的可选空间索引否
.fix可选的要素 ID 索引否
.cpg可选的字符编码信息否
SVG
100%

核心架构 ​

shapefile 模块围绕几个关键类构建,这些类协同工作以提供对 shapefile 数据的访问:

SVG
100%

主要组件 ​

  1. ShapefileDataStore: 提供对 shapefile 数据访问的中心类。它是专门为 shapefile 实现的 GeoTools DataStore 接口。

  2. ShapefileDataStoreFactory: 从参数创建 ShapefileDataStore 实例。

  3. ShpFiles: 管理对构成 shapefile 的各种文件的访问。

  4. ShapefileSetManager: 处理打开各种文件读取器/写入器。

  5. IndexManager: 管理空间和要素 ID 索引。

  6. ShapefileFeatureSource: 提供对 shapefile 中要素的读取访问。

  7. ShapefileFeatureStore: 提供对 shapefile 中要素的读写访问。

  8. ShapefileFeatureReader: 从 shapefile 读取要素。

  9. ShapefileFeatureWriter: 向 shapefile 写入要素。

读取 Shapefiles ​

从 shapefiles 读取涉及几个组件协同工作以提取几何图形和属性数据:

SVG
100%

读取过程协调几种文件类型:

  • .shp 文件提供几何数据
  • .shx 文件索引几何记录
  • .dbf 文件提供属性数据
  • .prj 文件(如果存在)提供坐标参考系统

示例代码 ​

// 创建 ShapefileDataStore
File file = new File("states.shp");
Map<String, Object> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
ShapefileDataStore dataStore = (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(params);

// 获取要素源和要素
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
SimpleFeatureCollection features = source.getFeatures();

// 遍历要素
try (SimpleFeatureIterator iterator = features.features()) {
    while (iterator.hasNext()) {
        SimpleFeature feature = iterator.next();
        // 处理要素...
    }
}

写入 Shapefiles ​

创建和写入 shapefiles 涉及一组类似的组件:

SVG
100%

写入过程创建或更新几个文件:

  • .shp 文件存储几何数据
  • .shx 文件存储几何记录的索引
  • .dbf 文件存储属性数据
  • .prj 文件存储坐标参考系统

示例代码 ​

// 创建新的 shapefile
File file = new File("output.shp");
ShapefileDataStore store = new ShapefileDataStore(file.toURI().toURL());

// 定义要素类型
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add("the_geom", Point.class);
builder.add("name", String.class);
builder.add("count", Integer.class);
SimpleFeatureType featureType = builder.buildFeatureType();

// 创建模式(创建空 shapefile)
store.createSchema(featureType);

// 获取要素写入器
FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
    store.getFeatureWriterAppend(store.getTypeNames()[0], Transaction.AUTO_COMMIT);

// 写入要素
SimpleFeature feature = writer.next();
feature.setAttribute("the_geom", point);
feature.setAttribute("name", "Test Point");
feature.setAttribute("count", 1);
writer.write();
writer.close();

Shapefile 和 DBF 文件限制 ​

shapefiles 具有模块解决的固有限制:

  1. 文件大小限制:

    • SHP 文件:限制为 2GB(由于 32 位偏移量)
    • DBF 文件:限制为 4GB(尽管许多系统只能处理 2GB)
  2. 字段名限制:

    • DBF 中限制为 10 个字符
    • 重复名称通过附加数字处理
  3. 属性类型限制:

    • 仅支持基本类型:String、Number、Date、Boolean
    • 字符串字段限制为 254 个字符
  4. 几何类型限制:

    • shapefile 只能包含一种类型的几何图形
    • 多几何图形支持因类型而异

ShapefileDumper ​

ShapefileDumper 工具通过将数据分割成多个文件来帮助克服 shapefile 限制:

SVG
100%

主要功能:

  • 通过创建单独的 shapefiles 来处理不同的几何类型
  • 将大型数据集分割成具有顺序命名的多个文件
  • 通过配置控制 shapefile 大小限制
  • 为 DBF 文件设置字符编码

配置选项 ​

模块提供几个配置选项:

编码支持 ​

DBF 文件的字符编码:

// 创建数据存储时设置字符集
params.put(ShapefileDataStoreFactory.DBFCHARSET.key, Charset.forName("UTF-8"));

// 或创建后设置
shapefileDataStore.setCharset(Charset.forName("UTF-8"));

// 从 CPG 文件启用自动检测
shapefileDataStore.setTryCPGFile(true);

索引 ​

控制空间和要素 ID 索引:

// 创建数据存储时
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.TRUE);

// 创建后
shapefileDataStore.setIndexed(true);          // 使用现有索引
shapefileDataStore.setIndexCreationEnabled(true);  // 如果缺少则创建
shapefileDataStore.setFidIndexed(true);       // 使用 FID 索引

内存映射 ​

通过内存映射增强性能:

// 创建数据存储时
params.put(ShapefileDataStoreFactory.MEMORY_MAPPED.key, Boolean.TRUE);
params.put(ShapefileDataStoreFactory.CACHE_MEMORY_MAPS.key, Boolean.TRUE);

// 创建后
shapefileDataStore.setMemoryMapped(true);
shapefileDataStore.setBufferCachingEnabled(true);

读取和写入过程流程 ​

shapefile 模块协调跨多个文件的读取和写入:

SVG
100%

技术细节 ​

几何类型映射 ​

Shapefile 和 JTS 几何类型映射:

Shapefile 类型JTS 几何类型
POINTPoint
MULTIPOINTMultiPoint
ARC (POLYLINE)LineString, MultiLineString
POLYGONPolygon, MultiPolygon

DBF 属性类型 ​

支持的 DBF 属性类型及其映射:

DBF 类型描述Java 类型
C字符String
N数字Integer、Long 或 Double
F浮点Double
L逻辑Boolean
D日期Date
@时间戳Timestamp

与 GeoTools 集成 ​

shapefile 模块与更广泛的 GeoTools 生态系统集成:

  1. 实现 DataStore API 以一致地访问不同的数据源
  2. 使用 JTS(Java Topology Suite)进行几何表示
  3. 支持 GeoTools 的 CRS(坐标参考系统)框架
  4. 使用 GeoTools Filter API 进行筛选和查询

性能考虑 ​

为了使用 shapefiles 获得最佳性能:

  1. 使用空间索引: 启用 QIX 索引以加快空间查询
  2. 使用 FID 索引: 启用 FIX 索引以加快要素 ID 查找
  3. 内存映射: 当有足够内存可用时,对大文件启用
  4. 缓冲区缓存: 缓存缓冲区以减少 I/O 操作
  5. 字符集处理: 仅在需要时指定非默认字符集