All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openlr.binary.writer.ClosedLineLocationReferenceWriter 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.ClosedLineLocationReference;
import org.openlr.locationreference.LocationReferencePoint;
import org.openlr.locationreference.PathAttributes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

class ClosedLineLocationReferenceWriter {
    private final StatusWriter statusWriter;
    private final CoordinateWriter coordinateWriter;
    private final AttributesWriter attributesWriter;

    ClosedLineLocationReferenceWriter(StatusWriter statusWriter, CoordinateWriter coordinateWriter, AttributesWriter attributesWriter) {
        this.statusWriter = statusWriter;
        this.coordinateWriter = coordinateWriter;
        this.attributesWriter = attributesWriter;
    }

    void write(ClosedLineLocationReference closedLineLocationReference, ByteArrayOutputStream outputStream) throws IOException {
        Status status = new Status(3, true, 3, false);
        statusWriter.write(status, outputStream);

        List locationReferencePoints = closedLineLocationReference.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);

        Attributes1 lastAttributes1 = new Attributes1(lastLocationReferencePoint.getFunctionalRoadClass(), lastLocationReferencePoint.getFormOfWay());
        attributesWriter.writeAttributes1(lastAttributes1, outputStream);

        Attributes7 lastAttributes7 = new Attributes7(lastLocationReferencePoint.getBearing());
        attributesWriter.writeAttributes7(lastAttributes7, outputStream);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy