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

org.openlr.binary.reader.PolygonLocationReferenceReader 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.LocationReferenceFactory;
import org.openlr.locationreference.PolygonLocationReference;

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

class PolygonLocationReferenceReader {
    private final LocationReferenceFactory locationReferenceFactory;
    private final StatusReader statusReader;
    private final CoordinateReader coordinateReader;

    PolygonLocationReferenceReader(LocationReferenceFactory locationReferenceFactory, StatusReader statusReader, CoordinateReader coordinateReader) {
        this.locationReferenceFactory = locationReferenceFactory;
        this.statusReader = statusReader;
        this.coordinateReader = coordinateReader;
    }

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

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

        List coordinates = new ArrayList<>();

        Coordinate firstCoordinate = coordinateReader.readAbsoluteCoordinate(inputStream);
        coordinates.add(firstCoordinate);

        Coordinate reference = firstCoordinate;

        while (inputStream.available() > 0) {
            Coordinate coordinate = coordinateReader.readRelativeCoordinate(reference, inputStream);
            coordinates.add(coordinate);

            reference = coordinate;
        }

        return locationReferenceFactory.createPolygonLocationReference(coordinates);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy