org.openlr.binary.writer.AttributesWriter Maven / Gradle / Ivy
The newest version!
package org.openlr.binary.writer;
import org.openlr.binary.format.*;
import org.openlr.map.FormOfWay;
import org.openlr.map.FunctionalRoadClass;
import org.openlr.map.Orientation;
import org.openlr.map.SideOfRoad;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
class AttributesWriter {
void writeAttributes1(Attributes1 attributes1, ByteArrayOutputStream outputStream) throws IOException {
int functionalRoadClassBits = fromFunctionalRoadClass(attributes1.getFunctionalRoadClass()) << 3;
int formOfWayBits = fromFormOfWay(attributes1.getFormOfWay());
byte b = (byte) (functionalRoadClassBits | formOfWayBits);
outputStream.write(new byte[] { b });
}
void writeAttributes2(Attributes2 attributes2, ByteArrayOutputStream outputStream) throws IOException {
int functionalRoadClassBits = fromFunctionalRoadClass(attributes2.getFunctionalRoadClass()) << 5;
int bearingBits = fromBearing(attributes2.getBearing());
byte b = (byte) (functionalRoadClassBits | bearingBits);
outputStream.write(new byte[] { b });
}
void writeAttributes3(Attributes3 attributes3, ByteArrayOutputStream outputStream) throws IOException {
int distanceBits = fromDistance(attributes3.getDistance());
byte b = (byte) (distanceBits);
outputStream.write(new byte[] { b });
}
void writeAttributes4(Attributes4 attributes4, ByteArrayOutputStream outputStream) throws IOException {
int positiveOffsetBit = (attributes4.isPositiveOffset() ? 1 : 0) << 6;
int negativeOffsetBit = (attributes4.isNegativeOffset() ? 1 : 0) << 5;
int bearingBits = fromBearing(attributes4.getBearing());
byte b = (byte) (positiveOffsetBit | negativeOffsetBit | bearingBits);
outputStream.write(new byte[] { b });
}
void writeAttributes5(Attributes5 attributes5, ByteArrayOutputStream outputStream) throws IOException {
int orientationBits = fromOrientation(attributes5.getOrientation()) << 6;
int functionalRoadClassBits = fromFunctionalRoadClass(attributes5.getFunctionalRoadClass()) << 3;
int formOfWaysBits = fromFormOfWay(attributes5.getFormOfWay());
byte b = (byte) (orientationBits | functionalRoadClassBits | formOfWaysBits);
outputStream.write(new byte[] { b });
}
void writeAttributes6(Attributes6 attributes6, ByteArrayOutputStream outputStream) throws IOException {
int sideOfRoadBits = fromSideOfRoad(attributes6.getSideOfRoad()) << 6;
int functionalRoadClassBits = fromFunctionalRoadClass(attributes6.getFunctionalRoadClass()) << 3;
int formOfWaysBits = fromFormOfWay(attributes6.getFormOfWay());
byte b = (byte) (sideOfRoadBits | functionalRoadClassBits | formOfWaysBits);
outputStream.write(new byte[] { b });
}
void writeAttributes7(Attributes7 attributes7, ByteArrayOutputStream outputStream) throws IOException {
int bearingBits = fromBearing(attributes7.getBearing());
byte b = (byte) (bearingBits);
outputStream.write(new byte[] { b });
}
private int fromFunctionalRoadClass(FunctionalRoadClass functionalRoadClass) {
return functionalRoadClass.ordinal();
}
private int fromFormOfWay(FormOfWay formOfWay) {
return formOfWay.ordinal();
}
private int fromBearing(double bearing) {
return (int) Math.floor(bearing / (360.0 / 32));
}
private int fromDistance(double distance) {
return (int) Math.floor(distance / (15000.0 / 256));
}
private int fromOrientation(Orientation orientation) {
return orientation.ordinal();
}
private int fromSideOfRoad(SideOfRoad sideOfRoad) {
return sideOfRoad.ordinal();
}
}