从零搭建GIS工具链:用Maven+GeoTools实现自动化SHP转GeoJSON工作流
地理信息系统(GIS)开发中,数据格式转换是每个工程师都会遇到的挑战。当我们需要将传统的Shapefile(SHP)批量转换为更现代的GeoJSON格式时,一个稳定、高效的自动化工作流可以节省大量时间。本文将带你从零开始,构建一个基于Maven和GeoTools的完整GIS工具链,实现SHP到GeoJSON的自动化转换。
1. 环境准备与项目初始化
在开始编码前,我们需要搭建一个可靠的Java开发环境。不同于简单的示例项目,生产级GIS工具链需要考虑依赖管理、构建工具和开发环境的稳定性。
首先确保你的系统已安装:
- JDK 8或更高版本(推荐JDK 11 LTS)
- Apache Maven 3.6+
- IDE(IntelliJ IDEA或Eclipse)
创建Maven项目时,建议使用以下目录结构:
gis-converter/
├── src/
│ ├── main/
│ │ ├── java/com/yourcompany/gis/
│ │ └── resources/
│ └── test/
├── input/ # 存放待转换的SHP文件
├── output/ # 转换后的GeoJSON输出目录
└── pom.xml
关键Maven配置需要注意GeoTools仓库的设置。许多国内开发者会遇到依赖下载失败的问题,这是因为GeoTools不在Maven中央仓库中。正确的pom.xml配置应包含OSGeo仓库:
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
</repository>
</repositories>
同时,为避免国内网络问题,可以配置阿里云镜像仅代理中央仓库:
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Aliyun Maven Mirror</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
</mirrors>
2. 核心依赖与GeoTools模块选择
GeoTools是一个模块化的GIS库,我们需要根据转换任务选择特定模块。对于SHP转GeoJSON,以下依赖必不可少:
<dependencies>
<!-- Shapefile支持 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- GeoJSON支持 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- 坐标系支持 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
版本选择上,建议使用最新的稳定版(目前为28.x系列)。同时添加JTS拓扑套件用于几何操作:
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.18.2</version>
</dependency>
3. 基础转换功能实现
让我们从最基本的单个文件转换开始。SHP转GeoJSON的核心流程包括:
- 读取Shapefile数据源
- 获取要素集合(FeatureCollection)
- 设置正确的字符编码
- 写入GeoJSON文件
以下是基础实现代码:
public class ShpToGeoJsonConverter {
private static final Logger LOGGER = LoggerFactory.getLogger(ShpToGeoJsonConverter.class);
public boolean convert(String shpPath, String geoJsonPath) {
File shpFile = new File(shpPath);
try {
// 1. 创建Shapefile数据存储
ShapefileDataStore dataStore = (ShapefileDataStore) FileDataStoreFinder.getDataStore(shpFile);
dataStore.setCharset(StandardCharsets.UTF_8); // 解决中文乱码
// 2. 获取要素集合
SimpleFeatureSource featureSource = dataStore.getFeatureSource();
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
// 3. 创建GeoJSON写入器
FeatureJSON fjson = new FeatureJSON();
try (FileWriter writer = new FileWriter(geoJsonPath)) {
// 写入CRS信息
writer.write("{\"type\":\"FeatureCollection\",\"crs\":");
fjson.writeCRS(featureSource.getSchema().getCoordinateReferenceSystem(), writer);
writer.write(",\"features\":[");
// 4. 逐个写入要素
try (SimpleFeatureIterator iterator = featureCollection.features()) {
boolean first = true;
while (iterator.hasNext()) {
if (!first) writer.write(",");
first = false;
fjson.writeFeature(iterator.next(), writer);
}
}
writer.write("]}");
}
return true;
} catch (IOException e) {
LOGGER.error("转换失败", e);
return false;
}
}
}
这段代码已经可以处理基本的转换需求,但在生产环境中还需要考虑以下问题:
- 内存管理:大文件可能导致内存溢出
- 坐标系处理:确保CRS信息正确转换
- 属性字段类型:保持字段类型一致性
4. 批量处理与性能优化
实际项目中,我们往往需要处理成百上千个SHP文件。直接串行处理效率低下,需要引入批量处理和并行化技术。
4.1 文件批量处理
首先实现一个目录扫描工具,自动发现SHP文件:
public List<File> findShpFiles(String directory) {
return Files.walk(Paths.get(directory))
.filter(path -> path.toString().toLowerCase().endsWith(".shp"))
.map(Path::
347




被折叠的 条评论
为什么被折叠?



