org.openlr.binary.writer.LineLocationReferenceWriter Maven / Gradle / Ivy
The newest version!
package org.openlr.binary.writer;
import org.locationtech.jts.geom.Coordinate;
import org.openlr.binary.format.*;
import org.openlr.locationreference.LineLocationReference;
import org.openlr.locationreference.LocationReferencePoint;
import org.openlr.locationreference.PathAttributes;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
class LineLocationReferenceWriter {
private final StatusWriter statusWriter;
private final CoordinateWriter coordinateWriter;
private final AttributesWriter attributesWriter;
private final OffsetWriter offsetWriter;
LineLocationReferenceWriter(StatusWriter statusWriter, CoordinateWriter coordinateWriter, AttributesWriter attributesWriter, OffsetWriter offsetWriter) {
this.statusWriter = statusWriter;
this.coordinateWriter = coordinateWriter;
this.attributesWriter = attributesWriter;
this.offsetWriter = offsetWriter;
}
void write(LineLocationReference lineLocationReference, ByteArrayOutputStream outputStream) throws IOException {
Status status = new Status(3, true, 0, false);
statusWriter.write(status, outputStream);
List locationReferencePoints = lineLocationReference.getLocationReferencePoints();
LocationReferencePoint firstLocationReferencePoint = locationReferencePoints.get(0);
Coordinate firstCoordinate = firstLocationReferencePoint.getCoordinate();
coordinateWriter.writeAbsoluteCoordinate(firstCoordinate, outputStream);
Attributes1 firstAttributes1 = new Attributes1(firstLocationReferencePoint.getFunctionalRoadClass(), firstLocationReferencePoint.getFormOfWay());
attributesWriter.writeAttributes1(firstAttributes1, outputStream);
PathAttributes firstPathAttributes = firstLocationReferencePoint.getPathAttributes().orElseThrow(() -> new IllegalArgumentException());
Attributes2 firstAttributes2 = new Attributes2(firstPathAttributes.getLowestFunctionalRoadClass(), firstLocationReferencePoint.getBearing());
attributesWriter.writeAttributes2(firstAttributes2, outputStream);
Attributes3 firstAttributes3 = new Attributes3(firstPathAttributes.getDistance());
attributesWriter.writeAttributes3(firstAttributes3, outputStream);
Coordinate reference = firstCoordinate;
for (int i = 1; i < locationReferencePoints.size() - 1; i++) {
LocationReferencePoint intermediateLocationReferencePoint = locationReferencePoints.get(i);
Coordinate intermediateCoordinate = intermediateLocationReferencePoint.getCoordinate();
coordinateWriter.writeRelativeCoordinate(intermediateCoordinate, reference, outputStream);
Attributes1 intermediateAttributes1 = new Attributes1(intermediateLocationReferencePoint.getFunctionalRoadClass(), intermediateLocationReferencePoint.getFormOfWay());
attributesWriter.writeAttributes1(intermediateAttributes1, outputStream);
PathAttributes intermediatePathAttributes = intermediateLocationReferencePoint.getPathAttributes().orElseThrow(() -> new IllegalArgumentException());
Attributes2 intermediateAttributes2 = new Attributes2(intermediatePathAttributes.getLowestFunctionalRoadClass(), intermediateLocationReferencePoint.getBearing());
attributesWriter.writeAttributes2(intermediateAttributes2, outputStream);
Attributes3 intermediateAttributes3 = new Attributes3(intermediatePathAttributes.getDistance());
attributesWriter.writeAttributes3(intermediateAttributes3, outputStream);
reference = intermediateCoordinate;
}
LocationReferencePoint lastLocationReferencePoint = locationReferencePoints.get(locationReferencePoints.size() - 1);
Coordinate lastCoordinate = lastLocationReferencePoint.getCoordinate();
coordinateWriter.writeRelativeCoordinate(lastCoordinate, reference, outputStream);
Attributes1 lastAttributes1 = new Attributes1(lastLocationReferencePoint.getFunctionalRoadClass(), lastLocationReferencePoint.getFormOfWay());
attributesWriter.writeAttributes1(lastAttributes1, outputStream);
boolean positiveOffset = lineLocationReference.getRelativePositiveOffset() > 0;
boolean negativeOffset = lineLocationReference.getRelativeNegativeOffset() > 0;
Attributes4 lastAttributes4 = new Attributes4(positiveOffset, negativeOffset, lastLocationReferencePoint.getBearing());
attributesWriter.writeAttributes4(lastAttributes4, outputStream);
if (positiveOffset) {
offsetWriter.write(lineLocationReference.getRelativePositiveOffset(), outputStream);
}
if (negativeOffset) {
offsetWriter.write(lineLocationReference.getRelativeNegativeOffset(), outputStream);
}
}
}