/** * // // // Ex.: MoveTo(3, 6), LineTo(8, 12), LineTo(20, 34), ClosePath // * Encoded as: [ 9 3 6 18 5 6 12 22 15 ] // == command type 7 (ClosePath), * length 1 // ===== relative LineTo(+12, +22) == LineTo(20, 34) // === * relative LineTo(+5, +6) == LineTo(8, 12) // == [00010 010] = command type * 2 (LineTo), length 2 // === relative MoveTo(+3, +6) // == [00001 001] = * command type 1 (MoveTo), length 1 // Commands are encoded as uint32 * varints, vertex parameters are // encoded as sint32 varints (zigzag). * Vertex parameters are // also encoded as deltas to the previous position. * The original // position is (0,0) * * @param cs * @return */ List<Integer> commands(Coordinate[] cs, boolean closePathAtEnd) { return commands(cs, closePathAtEnd, false); }
/** * // // // Ex.: MoveTo(3, 6), LineTo(8, 12), LineTo(20, 34), ClosePath // * Encoded as: [ 9 3 6 18 5 6 12 22 15 ] // == command type 7 (ClosePath), * length 1 // ===== relative LineTo(+12, +22) == LineTo(20, 34) // === * relative LineTo(+5, +6) == LineTo(8, 12) // == [00010 010] = command type * 2 (LineTo), length 2 // === relative MoveTo(+3, +6) // == [00001 001] = * command type 1 (MoveTo), length 1 // Commands are encoded as uint32 * varints, vertex parameters are // encoded as sint32 varints (zigzag). * Vertex parameters are // also encoded as deltas to the previous position. * The original // position is (0,0) * * @param cs * @return */ List<Integer> commands(Coordinate[] cs, boolean closePathAtEnd) { return commands(cs, closePathAtEnd, false); }
List<Integer> commands(Geometry geometry) { if (geometry instanceof MultiLineString) { return commands((MultiLineString) geometry); } if (geometry instanceof Polygon) { return commands((Polygon) geometry); } if (geometry instanceof MultiPolygon) { return commands((MultiPolygon) geometry); } return commands(geometry.getCoordinates(), shouldClosePath(geometry), geometry instanceof MultiPoint); }
List<Integer> commands(MultiPolygon mp) { List<Integer> commands = new ArrayList<Integer>(); for (int i = 0; i < mp.getNumGeometries(); i++) { Polygon polygon = (Polygon) mp.getGeometryN(i); commands.addAll(commands(polygon)); } return commands; }
List<Integer> commands(MultiLineString mls) { List<Integer> commands = new ArrayList<Integer>(); for (int i = 0; i < mls.getNumGeometries(); i++) { commands.addAll(commands(mls.getGeometryN(i).getCoordinates(), false)); } return commands; }
featureBuilder.geometry = toIntArray(commands(geometry));
List<Integer> commands(Polygon polygon) { List<Integer> commands = new ArrayList<Integer>(); // According to the vector tile specification, the exterior ring of a polygon // must be in clockwise order, while the interior ring in counter-clockwise order. // In the tile coordinate system, Y axis is positive down. // // However, in geographic coordinate system, Y axis is positive up. // Therefore, we must reverse the coordinates. // So, the code below will make sure that exterior ring is in counter-clockwise order // and interior ring in clockwise order. LineString exteriorRing = polygon.getExteriorRing(); if (!Orientation.isCCW(exteriorRing.getCoordinates())) { exteriorRing = (LineString) exteriorRing.reverse(); } commands.addAll(commands(exteriorRing.getCoordinates(), true)); for (int i = 0; i < polygon.getNumInteriorRing(); i++) { LineString interiorRing = polygon.getInteriorRingN(i); if (Orientation.isCCW(interiorRing.getCoordinates())) { interiorRing = (LineString) interiorRing.reverse(); } commands.addAll(commands(interiorRing.getCoordinates(), true)); } return commands; }
exteriorRing = (LineString) exteriorRing.reverse(); commands.addAll(commands(exteriorRing.getCoordinates(), true)); interiorRing = (LineString) interiorRing.reverse(); commands.addAll(commands(interiorRing.getCoordinates(), true)); GeometryCollection gc = (GeometryCollection) geometry; for (int i = 0; i < gc.getNumGeometries(); i++) { commands.addAll(commands(gc.getGeometryN(i).getCoordinates(), false)); return commands(geometry.getCoordinates(), shouldClosePath(geometry), geometry instanceof MultiPoint);
featureBuilder.addAllGeometry(commands(geometry));