/** * 根据经度、纬度获取区域编码 * * @param lon 经度 * @param lat 纬度 * @return * @throws Exception long */ public static long getAreaAnalyzer(double lon, double lat) throws Exception { long areacode = -1; Geometry geo = new GeometryFactory().createPoint(new Coordinate(lon, lat)); List<?> areas = areaTree.query(geo.getEnvelopeInternal()); for (Object obj : areas) { AreaNode anode = (AreaNode) obj; if (anode.getPolygon().contains(geo)) { areacode = anode.getAreaCode(); break; } } return areacode; }
/** * areaTree全国区域拓扑树初始化 * * @param fileName * @throws Exception void */ public static void buildTree(String fileName) throws Exception { FileInputStream fis = new FileInputStream(new File(fileName)); BufferedReader br = new BufferedReader(new InputStreamReader(fis, "utf-8")); String line = null; AreaNode node = null; while ((line = br.readLine()) != null) { // 读取区域文件 String[] lines = line.split(";"); Geometry polygon = new WKTReader().read(lines[lines.length - 1]); node = new AreaNode(); // 创建节点 node.setAreaCode(Long.parseLong(lines[0])); node.setAreaName(lines[1]); node.setPolygon(polygon); areaTree.insert(polygon.getEnvelopeInternal(), node); // 向树上添加节点 } fis.close(); br.close(); areaTree.build(); // 构建树 }