public List<Triangle> parseFile(InputStream is) { try { VTDGen vg = new VTDGen(); vg.setDoc(IOUtils.toByteArray(is)); vg.parse(false); VTDNav vn = vg.getNav(); NODE_PATH.resetXPath(); NODE_PATH.bind(vn); // This is important state for the later method calls! NODE_REF_PATH.resetXPath(); NODE_REF_PATH.bind(vn); BUILDING_WAY_PATH.resetXPath(); BUILDING_WAY_PATH.bind(vn); BUILDING_MULTIPOLYGON_PATH.resetXPath(); BUILDING_MULTIPOLYGON_PATH.bind(vn); MEMBER_WAY_PATH.resetXPath(); MEMBER_WAY_PATH.bind(vn); WAY_PATH.resetXPath(); WAY_PATH.bind(vn); // A hash from node ids to actual node positions Hashtable<Long, Point2D> nodes = new Hashtable<>(); // A hash from way refs referenced from multipolygons to their actual list of node refs Hashtable<Long, List<Long>> multipolygonWays = new Hashtable<>(); // The following call initializes accessed node refs in nodes to a dummy value. List<List<Long>> buildingWays = extractWaysOfBuildings(vn, nodes); // The following call initializes accessed way refs from multipolygons to a dummy value. List<List<Tuple2<WayRole, Long>>> multipolygonWayRefs = extractWayRefsOfMultipolygons(vn, multipolygonWays); // This will extract all referenced multipolygon multipolygonWays, excluding the building // multipolygonWays // Also adds referenced nodes to nodes extractReferencedWays(vn, multipolygonWays, nodes); // This will extract all referenced nodes, but no more. extractReferencedNodes(vn, nodes); // Finally build the polygon list by following the node refs in wayRefs. // Triangulate each polygon and return the flattened list of triangles. // This way, poly2tri's types will not leak out of this class and we operate // on triangles anyway. return buildPolygons(nodes, buildingWays, multipolygonWays, multipolygonWayRefs) .flatMap( p -> { try { p.ComplexToSimplePolygon(); EarClipping ec = new EarClipping(p.SimplePolygon); return ec.Triangulation().stream(); } catch (RuntimeException ignored) { } return Stream.empty(); }) .toList(); } catch (XPathEvalException | NavException | IOException | ParseException e) { e.printStackTrace(); return new ArrayList<>(); } }
public static void main(String args[]) { try { VTDGen vg = new VTDGen(); if (vg.parseFile("po.xml", true)) { // recommended extension is .vxl vg.writeIndex("po.vxl"); } } catch (Exception e) { } }