| | |
| | | package org.sxkj.common.utils; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | import org.geotools.geometry.jts.JTSFactoryFinder; |
| | | import org.locationtech.jts.geom.Geometry; |
| | | import org.locationtech.jts.geom.GeometryFactory; |
| | | import org.locationtech.jts.io.ParseException; |
| | | import org.locationtech.jts.io.WKTReader; |
| | | |
| | | public class GeomUtils { |
| | | |
| | | private static final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); |
| | | private static final WKTReader wktReader = new WKTReader(geometryFactory); |
| | | |
| | | public static boolean isGeomInvalid(String geom) { |
| | | if (StringUtils.isBlank(geom)) { |
| | |
| | | String upper = geom.trim().toUpperCase(); |
| | | return !upper.contains("POLYGON"); |
| | | } |
| | | |
| | | /** |
| | | * 验证并转换几何数据,确保其有效 |
| | | * @param geom WKT格式的几何数据 |
| | | * @return 有效的几何数据,或null如果转换失败 |
| | | */ |
| | | public static String validateAndFormatGeom(String geom) { |
| | | if (StringUtils.isBlank(geom)) { |
| | | return null; |
| | | } |
| | | |
| | | try { |
| | | // 解析WKT字符串 |
| | | Geometry geometry = wktReader.read(geom); |
| | | |
| | | // 如果几何数据无效,尝试修复 |
| | | if (!geometry.isValid()) { |
| | | geometry = geometry.buffer(0); |
| | | } |
| | | |
| | | // 确保几何数据有效 |
| | | if (geometry.isValid()) { |
| | | return geometry.toText(); |
| | | } else { |
| | | return null; |
| | | } |
| | | } catch (ParseException e) { |
| | | // 解析失败 |
| | | return null; |
| | | } |
| | | } |
| | | } |