protected Geometry decodeLineString()

in baremaps-core/src/main/java/org/apache/baremaps/vectortile/VectorTileDecoder.java [252:302]


  protected Geometry decodeLineString(List<Integer> encoding) {
    List<LineString> lineStrings = new ArrayList<>();
    List<Coordinate> coordinates = new ArrayList<>();

    // Iterate over the commands and parameters
    int i = 0;
    while (i < encoding.size()) {
      int value = encoding.get(i);
      int command = command(value);
      int count = count(value);

      // Increment the index to the first parameter
      i++;

      // Iterate over the parameters
      int length = count * 2;
      for (int j = 0; j < length; j += 2) {
        // Decode the parameters and move the cursor
        cx += parameter(encoding.get(i + j));
        cy += parameter(encoding.get(i + j + 1));

        // Start a new linestring
        if (command == MOVE_TO) {
          coordinates.clear();
          coordinates.add(new Coordinate(cx, cy));
        }

        // Add the coordinate to the current linestring
        else if (command == LINE_TO) {
          coordinates.add(new Coordinate(cx, cy));
        }
      }

      // Add the linestring to the list of linestrings
      if (coordinates.size() > 1) {
        lineStrings.add(GEOMETRY_FACTORY.createLineString(coordinates.toArray(new Coordinate[0])));
      }

      // Increment the index to the next command
      i += length;
    }

    // Build the final geometry
    if (lineStrings.size() == 1) {
      return lineStrings.get(0);
    } else if (lineStrings.size() > 1) {
      return GEOMETRY_FACTORY.createMultiLineString(lineStrings.toArray(new LineString[0]));
    } else {
      throw new IllegalStateException("No linestrings found.");
    }
  }