目录
一、介绍
为了能够更加方便的在java中操作几何对象,提高开发效果,编写了几何对象的操作工具类。
二、工具类实现
工具类具体如下。
public class GeometryToolUtil { private static WKTReader reader = new WKTReader(); public static void main(String[] args) throws IOException, ParseException { } /** * wkt转Geometry * * @param wktStr * @return * @throws IOException * @throws ParseException */ public static Geometry wktToGeom(String wktStr) throws IOException, ParseException { Geometry geometry = null; if (oConvertUtils.isNotEmpty(wktStr)) { geometry = reader.read(wktStr); } return geometry; } /** * 从wkt格式的坐标系定义中获取坐标系名称 * * @param wkt wkt格式的坐标系定义 * @return */ public static String getCoordinateSystemName(String wkt) { try { CoordinateReferenceSystem coordinateReferenceSystem = CRS.parseWKT(wkt); return coordinateReferenceSystem.getName().getCode(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 创建空MultiPolygon * * @param srid * @return */ public static MultiPolygon creatMultiPolygon_empty(int srid) { MultiPolygon multiPolygon = null; try { WKTReader reader = new WKTReader(); multiPolygon = (MultiPolygon) reader.read("MULTIPOLYGON EMPTY"); multiPolygon.setSRID(srid); } catch (Exception e) { e.printStackTrace(); } return multiPolygon; } /** * 创建空point * * @param srid * @return */ public static Point creatPoint_empty(int srid) { Point point = null; try { WKTReader reader = new WKTReader(); point = (Point) reader.read("POINT EMPTY"); point.setSRID(srid); } catch (Exception e) { e.printStackTrace(); } return point; } /** * 判定几何类型是否为面/多面 * * @param geometryType 几何类型 * @return */ public static boolean geomTypeIsPolygon(String geometryType) { if (geometryType.toLowerCase().equals("multipolygon") || geometryType.toLowerCase().equals("polygon")) { return true; } return false; } /** * 检查wkt是否为多面 * * @param wkt * @return */ public static boolean wktIsMultiPolygon(String wkt) { if (wkt.toLowerCase().startsWith("multipolygon")) { return true; } else { return false; } } /** * 检查wkt是否为面 * * @param wkt * @return */ public static boolean wktIsPolygon(String wkt) { if (wkt.toLowerCase().startsWith("polygon")) { return true; } else { return false; } } }