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

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

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

import org.locationtech.jts.geom.Coordinate;
import org.openlr.binary.format.Status;
import org.openlr.locationreference.GridLocationReference;
import org.openlr.locationreference.LocationReferenceFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;

class GridLocationReferenceReader {
    private final LocationReferenceFactory locationReferenceFactory;
    private final StatusReader statusReader;
    private final CoordinateReader coordinateReader;
    private final IntegerReader integerReader;

    GridLocationReferenceReader(LocationReferenceFactory locationReferenceFactory, StatusReader statusReader, CoordinateReader coordinateReader, IntegerReader integerReader) {
        this.locationReferenceFactory = locationReferenceFactory;
        this.statusReader = statusReader;
        this.coordinateReader = coordinateReader;
        this.integerReader = integerReader;
    }

    GridLocationReference 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() != 2) {
            throw new IllegalArgumentException();
        }

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

        Coordinate lowerLeft = coordinateReader.readAbsoluteCoordinate(inputStream);

        Coordinate upperRight = inputStream.available() > 8 ?
                coordinateReader.readAbsoluteCoordinate(inputStream) :
                coordinateReader.readRelativeCoordinate(lowerLeft, inputStream);

        int numberOfColumns = integerReader.readUnsignedInteger(2, inputStream);
        int numberOfRows = integerReader.readUnsignedInteger(2, inputStream);

        return locationReferenceFactory.createGridLocationReference(lowerLeft, upperRight, numberOfColumns, numberOfRows);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy