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

org.openlr.binary.reader.LineLocationReferenceReader Maven / Gradle / Ivy

The newest version!
package org.openlr.binary.reader;

import org.locationtech.jts.geom.Coordinate;
import org.openlr.binary.format.*;
import org.openlr.locationreference.LineLocationReference;
import org.openlr.locationreference.LocationReferenceFactory;
import org.openlr.locationreference.LocationReferencePoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class LineLocationReferenceReader {
    private final LocationReferenceFactory locationReferenceFactory;
    private final StatusReader statusReader;
    private final CoordinateReader coordinateReader;
    private final AttributesReader attributesReader;
    private final OffsetReader offsetReader;

    LineLocationReferenceReader(LocationReferenceFactory locationReferenceFactory, StatusReader statusReader, CoordinateReader coordinateReader, AttributesReader attributesReader, OffsetReader offsetReader) {
        this.locationReferenceFactory = locationReferenceFactory;
        this.statusReader = statusReader;
        this.coordinateReader = coordinateReader;
        this.attributesReader = attributesReader;
        this.offsetReader = offsetReader;
    }

    LineLocationReference read(ByteArrayInputStream inputStream) throws IOException {
        Status status = statusReader.read(inputStream);

        if (status.getVersion() != 3) {
            throw new IllegalArgumentException();
        }

        if (!status.isAttributeFlag()) {
            throw new IllegalArgumentException();
        }

        if (status.getAreaFlag() != 0) {
            throw new IllegalArgumentException();
        }

        if (status.isPointFlag()) {
            throw new IllegalArgumentException();
        }

        List locationReferencePoints = new ArrayList<>();

        Coordinate firstCoordinate = coordinateReader.readAbsoluteCoordinate(inputStream);

        Attributes1 firstAttributes1 = attributesReader.readAttributes1(inputStream);
        Attributes2 firstAttributes2 = attributesReader.readAttributes2(inputStream);
        Attributes3 firstAttributes3 = attributesReader.readAttributes3(inputStream);

        LocationReferencePoint firstLocationReferencePoint = locationReferenceFactory.createLocationReferencePoint(
                firstCoordinate,
                firstAttributes2.getBearing(),
                firstAttributes1.getFunctionalRoadClass(),
                firstAttributes1.getFormOfWay(),
                firstAttributes3.getDistance(),
                firstAttributes2.getFunctionalRoadClass());

        locationReferencePoints.add(firstLocationReferencePoint);

        Coordinate referenceCoordinate = firstCoordinate;

        while (inputStream.available() > 8) {
            Coordinate intermediateCoordinate = coordinateReader.readRelativeCoordinate(referenceCoordinate, inputStream);

            Attributes1 intermediateAttributes1 = attributesReader.readAttributes1(inputStream);
            Attributes2 intermediateAttributes2 = attributesReader.readAttributes2(inputStream);
            Attributes3 intermediateAttributes3 = attributesReader.readAttributes3(inputStream);

            LocationReferencePoint intermediateLocationReferencePoint = locationReferenceFactory.createLocationReferencePoint(
                    intermediateCoordinate,
                    intermediateAttributes2.getBearing(),
                    intermediateAttributes1.getFunctionalRoadClass(),
                    intermediateAttributes1.getFormOfWay(),
                    intermediateAttributes3.getDistance(),
                    intermediateAttributes2.getFunctionalRoadClass());

            locationReferencePoints.add(intermediateLocationReferencePoint);

            referenceCoordinate = intermediateCoordinate;
        }

        Coordinate lastCoordinate = coordinateReader.readRelativeCoordinate(referenceCoordinate, inputStream);

        Attributes1 lastAttributes1 = attributesReader.readAttributes1(inputStream);
        Attributes4 lastAttributes4 = attributesReader.readAttributes4(inputStream);

        LocationReferencePoint lastLocationReferencePoint = locationReferenceFactory.createLocationReferencePoint(
                lastCoordinate,
                lastAttributes4.getBearing(),
                lastAttributes1.getFunctionalRoadClass(),
                lastAttributes1.getFormOfWay());

        locationReferencePoints.add(lastLocationReferencePoint);

        double relativePositiveOffset = lastAttributes4.isPositiveOffset() ? offsetReader.read(inputStream) : 0;
        double relativeNegativeOffset = lastAttributes4.isNegativeOffset() ? offsetReader.read(inputStream) : 0;

        return locationReferenceFactory.createLineLocationReference(
                locationReferencePoints,
                relativePositiveOffset,
                relativeNegativeOffset);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy