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

org.openlr.binary.reader.ClosedLineLocationReferenceReader 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.ClosedLineLocationReference;
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 ClosedLineLocationReferenceReader {
    private final LocationReferenceFactory locationReferenceFactory;
    private final StatusReader statusReader;
    private final CoordinateReader coordinateReader;
    private final AttributesReader attributesReader;

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

    ClosedLineLocationReference 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() != 3) {
            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() > 2) {
            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;
        }

        Attributes1 lastAttributes1 = attributesReader.readAttributes1(inputStream);
        Attributes7 lastAttributes7 = attributesReader.readAttributes7(inputStream);

        LocationReferencePoint lastLocationReferencePoint = locationReferenceFactory.createLocationReferencePoint(
                firstCoordinate,
                lastAttributes7.getBearing(),
                lastAttributes1.getFunctionalRoadClass(),
                lastAttributes1.getFormOfWay());

        locationReferencePoints.add(lastLocationReferencePoint);

        return locationReferenceFactory.createClosedLineLocationReference(locationReferencePoints);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy