code: private static Geometry force3DStartEnd(LineString lineString, final double startZ,
              final double endZ, final boolean interpolate) {
          final double bigD = lineString.getLength();
          final double z = endZ - startZ;
          final Coordinate coordEnd = lineString.getCoordinates()[lineString.getCoordinates().length - 1];
          lineString.apply(new CoordinateSequenceFilter() {
              boolean done = false;
  
              @Override
              public boolean isGeometryChanged() {
                  return true;
              }
  
              @Override
              public boolean isDone() {
                  return done;
              }
  
              @Override
              public void filter(CoordinateSequence seq, int i) {
                  double x = seq.getX(i);
                  double y = seq.getY(i);
                  if (i == 0) {
                      seq.setOrdinate(i, 0, x);
                      seq.setOrdinate(i, 1, y);
                      seq.setOrdinate(i, 2, startZ);
                  } else if (i == seq.size() - 1) {
                      seq.setOrdinate(i, 0, x);
                      seq.setOrdinate(i, 1, y);
                      seq.setOrdinate(i, 2, endZ);
                  } else {
                      if (interpolate) {
                          double smallD = seq.getCoordinate(i).distance(coordEnd);
                          double factor = smallD / bigD;
                          seq.setOrdinate(i, 0, x);
                          seq.setOrdinate(i, 1, y);
                          seq.setOrdinate(i, 2, startZ + (factor * z));
                      }
                  }
                  if (i == seq.size()) {
                      done = true;
                  }
              }
          });
          return lineString;
      }
docs: Updates all z values by a new value using the specified first and the
  last coordinates.
  
  @param geom
  @param startZ
  @param endZ
  @param interpolate is true the z value of the vertices are interpolate
  according the length of the line.
  @return