目录
具体如下:
1、shp文件
SHP文件(Shapefile)是由**ESRI(Environmental Systems Research Institute)开发的一种常用的地理空间数据格式,用于存储矢量类型的地理信息数据。SHP文件通常用于GIS(地理信息系统)软件中,用来表示地理对象,如点、线、面等。
Shapefile 格式通常由至少三部分组成:
- .shp 文件:存储几何数据(如点、线、面)。
- .shx 文件:存储几何数据的索引。
- .dbf 文件:存储与几何数据相关的属性数据,采用表格格式。
其他可能的辅助文件包括:
- .prj:存储空间参考信息(坐标系等),投帧式,用于保存地理坐标系统与投影信息,是一个存储well-known text投影描述符的文本文件。
- .cpg:存储字符编码信息。
其他可选的文件:
- .sbnand.sbx— 几何体的空间索引
- .fbnand.fbx— 只读的Shapefiles的几何体的空间索引
- .ainand.aih— 列表中活动字段的属性索引。
- .ixs— 可读写Shapefile文件的地理编码索引
- .mxs— 可读写Shapefile文件的地理编码索引(ODB格式)
- .atx—.dbf文件的属性索引,其文件名格式为shapefile.columnname.atx(ArcGIS 8及之后的版本)
- .shp.xml— 以XML格式保存元数据。
SHP文件结构概述
- 几何数据:通常包括点(Point)、线(Line)、多边形(Polygon)等矢量数据。
- 属性数据:通过
.dbf
文件与每个几何对象相关联,描述其属性(如名字、ID、面积等)。 - 索引数据:通过
.shx
文件,便于快速定位几何数据的位置。
2、具体实现
实现方式有多种,选择2种做简要的说明
①使用GDAL实现
GDAL 提供了对矢量数据(如 Shapefile)的强大支持,包含读取、写入、转换等多种功能。使用GDAL实现的时候需要安装GDAL相关的软件包和JDK,也可以使用带GDAL的JDK,下面是:Java GIS 开发基础环境,带有GDAL的JDK1.8
https://download.youkuaiyun.com/download/qq_43544074/90203311https://download.youkuaiyun.com/download/qq_43544074/90203311上面是整理过的,可以直接用,也可以在网上其他可下载的地方下载使用。
Download — GDAL documentationhttps://gdal.org/en/stable/download.html有了基础环境,就可以具体实现解析了,如下:
添加依赖库
<!--gdal-->
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.5.0</version> <!--3.9.0、 3.2.0-->
</dependency>
<!--1-sqlite相关-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
<!--2-sqlite 方言-->
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<!-- 对数据库操作的统计 -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
实现代码类
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
/**
* gdalshapefile 读取器
*
* @author xjs
* @date 2024/12/30
*/
public class GDALShapefileReader {
public static void main(String[] args) {
// 设置GDAL配置以支持UTF-8路径(包含中文等非ASCII字符)
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
// 调用读取Shapefile的封装方法
// String shapefilePath = "D:\\app\\point\\point.shp"; // 修改为实际文件路径
// String shapefilePath = "D:\\app\\line\\line.shp"; // 修改为实际文件路径
String shapefilePath = "D:\\app\\polygon\\polygon.shp"; // 修改为实际文件路径
try {
readShp(shapefilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取Shapefile的方法
*
* @param shapefilePath Shapefile文件路径
* @throws Exception 异常
*/
public static void readShp(String shapefilePath) throws Exception {
// 初始化GDAL和OGR
ogr.RegisterAll();
// 获取驱动,ESRI Shapefile 驱动
String strDriverName = "ESRI Shapefile";
org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
if (oDriver == null) {
throw new Exception(strDriverName + " 驱动不可用!");
}
// 打开SHP文件(只读模式)
DataSource dataSource = ogr.Open(shapefilePath, 0); // 0表示只读模式
if (dataSource == null) {
throw new Exception("无法打开SHP文件!");
}
// 获取SHP文件中的第一个图层
Layer layer = dataSource.GetLayer(0); // 获取第一个图层
int featureCount = (int) layer.GetFeatureCount();
System.out.println("总共有 " + featureCount + " 个要素。");
// 遍历每个Feature
for (int i = 0; i < featureCount; i++) {
// 获取单个Feature
Feature feature = layer.GetFeature(i);
// 打印Feature的ID
System.out.println("\nFeature ID: " + feature.GetFID());
// 获取几何数据(点、线、多边形等)
Geometry geometry = feature.GetGeometryRef();
if (geometry != null) {
// 获取几何类型
int geometryType = geometry.GetGeometryType();
System.out.println("几何类型: " + geometryType);
// 根据不同几何类型分别处理
handleGeometryByType(geometry, geometryType);
}
// 遍历每个字段属性
printFeatureFields(feature, layer);
}
// 清理资源
dataSource.delete();
}
/**
* 根据几何类型处理几何数据
*
* @param geometry 几何对象
* @param geometryType 几何类型
*/
private static void handleGeometryByType(Geometry geometry, int geometryType) {
switch (geometryType) {
case ogr.wkbPoint: // 点类型
System.out.println("这是一个点: " + geometry.ExportToWkt());
break;
case ogr.wkbLineString: // 线类型
System.out.println("这是一个线: " + geometry.ExportToWkt());
break;
case ogr.wkbPolygon: // 面类型
System.out.println("这是一个面: " + geometry.ExportToWkt());
break;
case ogr.wkbMultiPoint: // 多点类型
System.out.println("这是一个多点: " + geometry.ExportToWkt());
break;
case ogr.wkbMultiLineString: // 多线类型
System.out.println("这是一个多线: " + geometry.ExportToWkt());
break;
case ogr.wkbMultiPolygon: // 多面类型
System.out.println("这是一个多面: " + geometry.ExportToWkt());
break;
case ogr.wkbGeometryCollection: // 几何集合类型
System.out.println("这是一个几何集合: " + geometry.ExportToWkt());
break;
default:
System.out.println("无法识别的几何类型: " + geometry.ExportToWkt());
}
}
/**
* 打印Feature的字段信息
*
* @param feature Feature对象
* @param layer Layer对象
*/
private static void printFeatureFields(Feature feature, Layer layer) {
// 遍历每个字段属性
int fieldCount = feature.GetFieldCount();
for (int j = 0; j < fieldCount; j++) {
FieldDefn fieldDefn = layer.GetLayerDefn().GetFieldDefn(j);
String fieldName = fieldDefn.GetNameRef();
String fieldValue = feature.GetFieldAsString(j);
System.out.println("字段: " + fieldName + " = " + fieldValue);
}
}
}
考虑保存到数据库中
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* gdalshapefile 读取器
*
* @author xjs
* @date 2024/12/30
*/
public class GDALShapefileReaderToDb {
public static void main(String[] args) {
// 设置GDAL配置以支持UTF-8路径(包含中文等非ASCII字符)
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
// Shapefile路径和SQLite数据库路径
// String shapefilePath = "D:\\app\\point\\point.shp"; // 修改为实际文件路径
// String shapefilePath = "D:\\app\\line\\line.shp"; // 修改为实际文件路径
String shapefilePath = "D:\\app\\polygon\\polygon.shp"; // 修改为实际文件路径
String sqliteDbPath = "D:\\app\\shapefile_data.db"; // SQLite数据库路径
try {
// 创建SQLite数据库并插入Shapefile数据
readShpAndInsertToSQLite(shapefilePath, sqliteDbPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取Shapefile并将数据插入SQLite数据库
*
* @param shapefilePath Shapefile文件路径
* @param sqliteDbPath SQLite数据库文件路径
* @throws Exception 异常
*/
public static void readShpAndInsertToSQLite(String shapefilePath, String sqliteDbPath) throws Exception {
// 初始化GDAL和OGR
ogr.RegisterAll();
// 获取驱动,ESRI Shapefile 驱动
String strDriverName = "ESRI Shapefile";
org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
if (oDriver == null) {
throw new Exception(strDriverName + " 驱动不可用!");
}
// 打开SHP文件(只读模式)
DataSource dataSource = ogr.Open(shapefilePath, 0); // 0表示只读模式
if (dataSource == null) {
throw new Exception("无法打开SHP文件!");
}
// 获取SHP文件中的第一个图层
Layer layer = dataSource.GetLayer(0); // 获取第一个图层
int featureCount = (int) layer.GetFeatureCount();
System.out.println("总共有 " + featureCount + " 个要素。");
// 创建SQLite数据库连接
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteDbPath);
Statement stmt = conn.createStatement();
// 为不同的几何类型创建对应的表
createTablesForGeometryTypes(stmt);
// 遍历每个Feature并插入数据
for (int i = 0; i < featureCount; i++) {
Feature feature = layer.GetFeature(i);
insertFeatureToSQLite(conn, stmt, feature, layer);
}
// 关闭数据库连接
conn.close();
// 清理资源
dataSource.delete();
}
/**
* 为不同几何类型创建SQLite表(Point, LineString, Polygon)
*
* @param stmt Statement对象
* @throws SQLException 异常
*/
private static void createTablesForGeometryTypes(Statement stmt) throws SQLException {
// 创建Point表
String createPointTableSQL = "CREATE TABLE IF NOT EXISTS point_data (id INTEGER PRIMARY KEY, geometry TEXT, attributes TEXT);";
stmt.execute(createPointTableSQL);
// 创建LineString表
String createLineTableSQL = "CREATE TABLE IF NOT EXISTS line_data (id INTEGER PRIMARY KEY, geometry TEXT, attributes TEXT);";
stmt.execute(createLineTableSQL);
// 创建Polygon表
String createPolygonTableSQL = "CREATE TABLE IF NOT EXISTS polygon_data (id INTEGER PRIMARY KEY, geometry TEXT, attributes TEXT);";
stmt.execute(createPolygonTableSQL);
}
/**
* 将Feature插入到SQLite数据库
*
* @param conn 数据库连接
* @param stmt Statement对象
* @param feature Feature对象
* @param layer 图层对象
* @throws SQLException 异常
*/
private static void insertFeatureToSQLite(Connection conn, Statement stmt, Feature feature, Layer layer) throws SQLException {
// 获取字段数目和字段值
int fieldCount = feature.GetFieldCount();
StringBuilder insertSQL = new StringBuilder();
// 获取几何类型并决定插入哪个表
Geometry geometry = feature.GetGeometryRef();
if (geometry == null) return; // 如果没有几何数据,跳过
int geometryType = geometry.GetGeometryType();
String geometryWKT = geometry.ExportToWkt(); // 将几何数据转为WKT格式
// 基于几何类型选择插入表
switch (geometryType) {
case ogr.wkbPoint:
insertSQL.append("INSERT INTO point_data (geometry, attributes) VALUES ('")
.append(geometryWKT).append("', '").append(getAttributes(feature, layer))
.append("');");
break;
case ogr.wkbLineString:
insertSQL.append("INSERT INTO line_data (geometry, attributes) VALUES ('")
.append(geometryWKT).append("', '").append(getAttributes(feature, layer))
.append("');");
break;
case ogr.wkbPolygon:
insertSQL.append("INSERT INTO polygon_data (geometry, attributes) VALUES ('")
.append(geometryWKT).append("', '").append(getAttributes(feature, layer))
.append("');");
break;
default:
// 如果是其他类型,可以选择忽略或者添加更多表
System.out.println("不支持的几何类型,跳过该Feature。");
return;
}
// 执行插入操作
stmt.executeUpdate(insertSQL.toString());
}
/**
* 获取Feature的属性字符串
*
* @param feature Feature对象
* @param layer 图层对象
* @return 属性的字符串表示
*/
private static String getAttributes(Feature feature, Layer layer) {
StringBuilder attributes = new StringBuilder();
int fieldCount = feature.GetFieldCount();
for (int i = 0; i < fieldCount; i++) {
FieldDefn fieldDefn = layer.GetLayerDefn().GetFieldDefn(i);
String fieldValue = feature.GetFieldAsString(i);
attributes.append(fieldDefn.GetNameRef()).append(": ").append(fieldValue).append(", ");
}
// 删除最后一个逗号和空格
if (attributes.length() > 2) {
attributes.setLength(attributes.length() - 2);
}
return attributes.toString();
}
}
添加一个其他的相关方法:
/**
* 获取图层信息
* // 获取Layer信息 调用
* getLayerInfo(layer);
*
* @throws Exception 例外
*/
private static void getLayerInfo(Layer layer) throws Exception {
// 图层名称
String layerName = layer.GetName();
System.out.println("图层名称:" + layerName);
// 获取图层的空间参考
SpatialReference spatialReference = layer.GetSpatialRef();
System.out.println("空间参考坐标系:" + spatialReference.GetAttrValue("AUTHORITY", 0) + spatialReference.GetAttrValue("AUTHORITY", 1));
// 图层范围
double[] layerExtent = layer.GetExtent();
System.out.println("layerExtent = " + Arrays.toString(layerExtent));
System.out.println("图层范围:minx:" + layerExtent[0] + ",maxx:" + layerExtent[1] + ",miny:" + layerExtent[2] + ",maxy:" + layerExtent[3]);
}
/**
* 坐标转换
* // 转换坐标 调用
* Geometry geometry = feature.GetGeometryRef();
* coordinateTransform(geometry);
*/
private static void coordinateTransform(Geometry geometry) throws Exception {
// 创建目标空间参考 (EPSG:4326)
SpatialReference targetSpatialRef = new SpatialReference();
targetSpatialRef.ImportFromEPSG(4326); // EPSG:4326 是 WGS84
// 获取图层的空间参考
SpatialReference spatialReference = geometry.GetSpatialReference();
System.out.println("空间参考坐标系:" + spatialReference.GetAttrValue("AUTHORITY", 0) + spatialReference.GetAttrValue("AUTHORITY", 1));
// 创建坐标转换对象
CoordinateTransformation coordTransform = CoordinateTransformation.CreateCoordinateTransformation(spatialReference, targetSpatialRef);
double x = geometry.GetX();
double y = geometry.GetY();
// 创建包含 3 个元素的数组,Z 坐标可以设为 0
double[] coords = new double[]{x, y, 0.0}; // Z 坐标默认值为 0
coordTransform.TransformPoint(coords);
System.out.println("转换后的坐标:" + Arrays.toString(coords));
}
②使用geotools实现
官网:GeoTools The Open Source Java GIS Toolkit — GeoToolshttps://geotools.org/添加对应的依赖
<!-- 添加GeoTools依赖 <gt.version>20.0</gt.version> -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-jdbc</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-render</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-coverage</artifactId>
<version>${gt.version}</version>
</dependency>
<!-- 如果使用的 GeoTools 功能需要额外的依赖,如坐标转换,可能还需要添加这个 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
<!-- <!– wkt查询 –>-->
<!-- <dependency>-->
<!-- <groupId>com.vividsolutions</groupId>-->
<!-- <artifactId>jts-core</artifactId>-->
<!-- <version>1.14.0</version>-->
<!-- </dependency>-->
<!-- 1 栅格相关依赖 -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-image</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-process</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-process-raster</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
<version>${gt.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-swing -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${gt.version}</version>
</dependency>
<!--<dependency>
<groupId>org.jdal</groupId>
<artifactId>jdal-core</artifactId>
<version>2.0.0</version>
</dependency>
-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-imaging</artifactId>
<version>1.0-alpha3</version>
</dependency>
<!-- <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-netcdf</artifactId>
<version>20.0</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/edu.ucar/netcdf -->
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.22</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geometry</artifactId>
<version>20.0</version>
</dependency>
<!-- TwelveMonkeys ImageIO TIFF -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.8.0</version>
</dependency>
<!-- Maven example for JAI TIFF plugin -->
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai-core</artifactId>
<version>1.1.3</version>
</dependency>
<!--gdal-->
<dependency>
<groupId>org.gdal</groupId>
<artifactId>gdal</artifactId>
<version>3.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.postgis/postgis-jdbc -->
<dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2024.1.0</version>
</dependency>
<!--1-sqlite相关-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
<!--2-sqlite 方言-->
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<!-- 对数据库操作的统计 -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
代码实现:
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class ShapefileReader {
private static final Logger logger = LoggerFactory.getLogger(ShapefileReader.class);
public static void main(String[] args) {
String shapefilePath = "D:\\app\\point\\point.shp"; // Shapefile 文件路径
// String shapefilePath = "D:\\app\\line\\line.shp"; // 修改为实际文件路径
// String shapefilePath = "D:\\app\\polygon\\polygon.shp"; // 修改为实际文件路径
// Shapefile 文件路径
File shapefile = new File(shapefilePath);
File shapefile = new File(shapefilePath);
if (!shapefile.exists()) {
logger.error("Shapefile 文件不存在: {}", shapefilePath);
return;
}
DataStore dataStore = null;
SimpleFeatureIterator featureIterator = null;
try {
// 创建数据存储对象
dataStore = createDataStore(shapefile);
if (dataStore == null) {
logger.error("无法加载数据存储。");
return;
}
// 获取 Shapefile 的第一个要素类型
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
// 获取要素集合并遍历
featureIterator = featureSource.getFeatures().features();
while (featureIterator.hasNext()) {
SimpleFeature feature = featureIterator.next();
processFeature(feature);
}
} catch (Exception e) {
logger.error("处理 Shapefile 时发生错误", e);
} finally {
// 清理资源
closeResources(featureIterator, dataStore);
}
}
/**
* 创建 Shapefile 数据存储
*
* @param shapefile Shapefile 文件
* @return DataStore 对象
*/
private static DataStore createDataStore(File shapefile) {
try {
Map<String, Object> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, shapefile.toURI().toURL());
return DataStoreFinder.getDataStore(params);
} catch (Exception e) {
logger.error("无法创建 DataStore", e);
}
return null;
}
/**
* 处理每个 SimpleFeature 的几何类型
*
* @param feature SimpleFeature 对象
*/
private static void processFeature(SimpleFeature feature) {
Geometry geometry = (Geometry) feature.getDefaultGeometry();
if (geometry != null) {
if (geometry instanceof Point) {
handlePoint((Point) geometry);
} else if (geometry instanceof LineString) {
handleLineString((LineString) geometry);
} else if (geometry instanceof Polygon) {
handlePolygon((Polygon) geometry);
} else {
logger.warn("未知几何类型: {}", geometry.getClass().getName());
}
}
}
/**
* 处理点类型的几何对象
*
* @param point 点类型几何
*/
private static void handlePoint(Point point) {
logger.info("处理点类型: {}", point.getCoordinate());
}
/**
* 处理线类型的几何对象
*
* @param line 线类型几何
*/
private static void handleLineString(LineString line) {
logger.info("处理线类型: {}", line.getCoordinates());
}
/**
* 处理面类型的几何对象
*
* @param polygon 面类型几何
*/
private static void handlePolygon(Polygon polygon) {
logger.info("处理面类型: {}", polygon.getCoordinates());
}
/**
* 清理资源
*
* @param featureIterator SimpleFeatureIterator
* @param dataStore DataStore
*/
private static void closeResources(SimpleFeatureIterator featureIterator, DataStore dataStore) {
try {
if (featureIterator != null) {
featureIterator.close();
}
if (dataStore != null) {
dataStore.dispose();
}
} catch (Exception e) {
logger.error("清理资源时发生错误", e);
}
}
}
测试资源:
java gis 开发中,点线面shp样例文件https://download.youkuaiyun.com/download/qq_43544074/90203599
JAVA GIS开发测试使用的样例文件https://download.youkuaiyun.com/download/qq_43544074/90203961

geojson文件内容:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
108.42059285538897,
34.43175777680618
],
"type": "Point"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
111.15959608531722,
34.74576956006696
],
"type": "Point"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
106.11540367928148,
33.91109288219454
],
[
106.11614709728264,
32.20222171484548
],
[
108.56687126625343,
32.21199643897913
],
[
113.29200530656033,
32.26044556624461
],
[
115.71682073269943,
33.36702730103086
],
[
112.0523114542068,
34.09439185179981
],
[
110.95158929605105,
33.26505021211912
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
109.73894408656122,
30.501331719180385
],
[
109.85604450474239,
28.348034378250603
],
[
113.43162339245538,
28.682911024734366
],
[
114.76736388663636,
30.528086094570284
],
[
111.4658127739807,
31.040201068612618
],
[
109.73894408656122,
30.501331719180385
]
]
],
"type": "Polygon"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
108.81149696296643,
26.805083738957336
],
[
108.81149696296643,
24.938673483978008
],
[
111.79210342158211,
24.938673483978008
],
[
111.79210342158211,
26.805083738957336
],
[
108.81149696296643,
26.805083738957336
]
]
],
"type": "Polygon"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
114.41327342080456,
28.390688695200648
],
[
114.2598987512277,
28.383975751952153
],
[
114.1080578394897,
28.363904106245688
],
[
113.95926732020769,
28.330674555650916
],
[
113.81500985283358,
28.284619259957868
],
[
113.67671780403441,
28.22619802969625
],
[
113.54575771142683,
28.155993214149294
],
[
113.4234157527747,
28.074703269700738
],
[
113.31088441614443,
27.98313510802512
],
[
113.20925053367442,
27.88219533934431
],
[
113.11948480616361,
27.772880538427156
],
[
113.04243290924884,
27.65626667007572
],
[
112.97880823607287,
27.53349781655032
],
[
112.92918629741655,
27.405774351912804
],
[
112.89400076940365,
27.274340707906045
],
[
112.87354115192251,
27.1404728731205
],
[
112.8679519783689,
27.005465762260297
],
[
112.87723349942844,
26.870620585770503
],
[
112.90124375033986,
26.73723234237224
],
[
112.93970190213702,
26.60657754858957
],
[
112.99219279231522,
26.479902310505853
],
[
113.05817252863386,
26.358410834057835
],
[
113.1369750607291,
26.243254461394585
],
[
113.22781961721812,
26.135521312356662
],
[
113.32981891041645,
26.036226602066268
],
[
113.44198801610243,
25.946303698003263
],
[
113.56325384146727,
25.866595972770742
],
[
113.6924651000978,
25.79784950199156
],
[
113.82840271825316,
25.74070665036505
],
[
113.96979060160737,
25.695700582783626
],
[
114.11530669590213,
25.663250731492724
],
[
114.26359427851854,
25.643659244510534
],
[
114.41327342080456,
25.637108434854714
],
[
114.56295256309056,
25.643659244510534
],
[
114.711240145707,
25.66325073149272
],
[
114.85675624000173,
25.695700582783626
],
[
114.99814412335596,
25.74070665036505
],
[
115.13408174151132,
25.79784950199156
],
[
115.26329300014183,
25.866595972770742
],
[
115.38455882550666,
25.946303698003263
],
[
115.49672793119268,
26.036226602066264
],
[
115.598727224391,
26.135521312356662
],
[
115.68957178088,
26.24325446139458
],
[
115.76837431297527,
26.358410834057835
],
[
115.8343540492939,
26.479902310505853
],
[
115.88684493947208,
26.60657754858957
],
[
115.92530309126927,
26.737232342372238
],
[
115.94931334218064,
26.870620585770503
],
[
115.95859486324022,
27.005465762260297
],
[
115.9530056896866,
27.1404728731205
],
[
115.93254607220548,
27.274340707906045
],
[
115.89736054419258,
27.405774351912804
],
[
115.84773860553625,
27.53349781655032
],
[
115.7841139323603,
27.65626667007572
],
[
115.70706203544552,
27.772880538427156
],
[
115.6172963079347,
27.88219533934431
],
[
115.51566242546467,
27.98313510802512
],
[
115.40313108883441,
28.074703269700738
],
[
115.28078913018228,
28.155993214149294
],
[
115.1498290375747,
28.22619802969625
],
[
115.01153698877555,
28.284619259957868
],
[
114.86727952140144,
28.330674555650916
],
[
114.71848900211943,
28.363904106245688
],
[
114.56664809038142,
28.383975751952153
],
[
114.41327342080456,
28.390688695200648
]
]
]
}
}
]
}
至此,就可以实现对shp文件的读取和解析了