目录
一.GeoTools介绍
GeoTools是一套操作和显示地图的开源Java 代码库,提供了一系列符合规范的操作地理空间数据的方法,GeoTools被许多项目使用,在地理信息领域有着广泛的应用。
二.背景
本文介绍的是如何使用GeoTools来获取几何对象的四至坐标。
三.实现代码
这里编写了一个getEnvelopeMap()方法每次使用只需要调用getEnvelopeMap()方法传入Geometry对象,即可获取几何对象的四至坐标,结果返回Map,通过key:xMax、xMin、yMax、yMin来获取具体的x或y坐标。
/** * 获取几何的的四至xy * * @param geom 几何图形 * @return key:xMax、xMin、yMax、yMin */ public static Map<String, Double> getEnvelopeMap(Geometry geom) { Map<String, Double> map = new HashMap<>(); if (geom == null || geom.isEmpty()) { return map; } Coordinate[] coordinates = geom.getCoordinates(); List<Double> xList = new ArrayList<>(); List<Double> yList = new ArrayList<>(); for (int i = 0; i < coordinates.length; i++) { Coordinate coordinate = coordinates[i]; xList.add(coordinate.x); yList.add(coordinate.y); } if (xList.isEmpty() || yList.isEmpty()) { return map; } xList.sort(Double::compareTo); yList.sort(Double::compareTo); map.put("xMax", xList.get(xList.size() - 1)); map.put("xMin", xList.get(0)); map.put("yMax", yList.get(yList.size() - 1)); map.put("yMin", yList.get(0)); return map; }