1.maven依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.4</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>net.postgis</groupId><artifactId>postgis-jdbc-jtsparser</artifactId><version>2.5.1</version></dependency><!-- JTS 几何库 --><dependency><groupId>org.locationtech.jts</groupId><artifactId>jts-core</artifactId><version>1.20.0</version></dependency></<dependencies>
2.转换类
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.geojson.GeoJsonReader;
import org.locationtech.jts.io.geojson.GeoJsonWriter;/*** GeoJSON 转换工具类* 用于 GeoJSON 与 JTS Geometry 之间的转换* */
public class GeoJsonUtil {private static final GeoJsonReader geoJsonReader = new GeoJsonReader();private static final GeoJsonWriter geoJsonWriter = new GeoJsonWriter();private static final ObjectMapper objectMapper = new ObjectMapper();/*** GeoJSON 字符串转 Geometry* @param geoJson GeoJSON 字符串,例如:{"type":"Point","coordinates":[113.93,22.58]}* @return JTS Geometry 对象*/public static Geometry geoJsonToGeometry(String geoJson) {if (geoJson == null || geoJson.trim().isEmpty()) {return null;}try {return geoJsonReader.read(geoJson);} catch (Exception e) {throw new RuntimeException("Failed to parse GeoJSON: " + geoJson, e);}}/*** Geometry 转 GeoJSON 字符串* @param geometry JTS Geometry 对象* @return GeoJSON 字符串*/public static String geometryToGeoJson(Geometry geometry) {if (geometry == null) {return null;}try {return geoJsonWriter.write(geometry);} catch (Exception e) {throw new RuntimeException("Failed to write GeoJSON", e);}}/*** 验证是否是有效的 GeoJSON* @param geoJson GeoJSON 字符串* @return true-有效,false-无效*/public static boolean isValidGeoJson(String geoJson) {if (geoJson == null || geoJson.trim().isEmpty()) {return false;}try {JsonNode node = objectMapper.readTree(geoJson);return node.has("type") && node.has("coordinates");} catch (Exception e) {return false;}}/*** 数据库 Geometry 字段转 GeoJSON* 支持 WKT 和 EWKB(十六进制字符串)两种格式* * @param dbGeometry 数据库返回的 geometry 字段值(WKT 或 EWKB 十六进制字符串)* @return GeoJSON 字符串,如果转换失败返回 null*/public static String dbGeometryToGeoJson(String dbGeometry) {if (dbGeometry == null || dbGeometry.trim().isEmpty()) {return null;}try {String geomStr = dbGeometry.trim();Geometry geometry;// 判断是 WKB(十六进制)还是 WKT 格式if (geomStr.matches("^[0-9A-Fa-f]+$")) {// EWKB 格式(十六进制字符串)- PostgreSQL PostGIS 默认返回格式WKBReader wkbReader = new WKBReader();byte[] wkbBytes = WKBReader.hexToBytes(geomStr);geometry = wkbReader.read(wkbBytes);} else {// WKT 格式(文本格式)WKTReader wktReader = new WKTReader();geometry = wktReader.read(geomStr);}return geometryToGeoJson(geometry);} catch (Exception e) {throw new RuntimeException("Failed to convert database geometry to GeoJSON: " + e.getMessage(), e);}}public static void main(String[] args) {// 标准 GeoJSON - 一个简单的矩形PolygonString geoJson = "{\"type\":\"Polygon\",\"coordinates\":[[[116.0,23.0],[116.1,23.0],[116.1,23.1],[116.0,23.1],[116.0,23.0]]]}";String dbGeometry = "0103000020E610000001000000050000000000000000005E40CDCCCCCCCC1C3F0000000000005E400000000000005E40CDCCCCCCCC1C3F00000000000062400000000000005E40CDCCCCCCCC1C3F00000000000062400000000000005E40CDCCCCCCCC1C3F0000000000005E40";System.out.println("【GeoJSON -> Geometry】" + geoJsonToGeometry(geoJson));System.out.println("【GeoJSON -> Geometry -> GeoJSON】" + geometryToGeoJson(geoJsonToGeometry(geoJson)));System.out.println("【数据库Geometry -> GeoJSON】" + dbGeometryToGeoJson(dbGeometry));}
}
3.效果图
![image]()