baremaps-core/src/main/java/org/apache/baremaps/database/type/LonLatDataType.java [24:63]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class LonLatDataType extends MemoryAlignedDataType<Coordinate> {

  private static final double BITS = Math.pow(2, 31);
  private static final long SHIFT = 32;
  private static final long MASK = (1L << 32) - 1L;

  /** Constructs a {@link LonLatDataType}. */
  public LonLatDataType() {
    super(Long.BYTES);
  }

  public static long encodeLonLat(final double lon, final double lat) {
    long x = (long) (((lon + 180) / 360) * BITS);
    long y = (long) (((lat + 90) / 180) * BITS);
    long l = (x << SHIFT);
    long r = (y & MASK);
    return l | r;
  }

  public static double decodeLon(final long value) {
    double l = (value >>> 32);
    return (l / BITS) * 360 - 180;
  }

  public static double decodeLat(final long value) {
    long r = (value & MASK);
    return (r / BITS) * 180 - 90;
  }

  /** {@inheritDoc} */
  @Override
  public void write(final ByteBuffer buffer, final int position, final Coordinate value) {
    buffer.putLong(position, encodeLonLat(value.x, value.y));
  }

  /** {@inheritDoc} */
  @Override
  public Coordinate read(final ByteBuffer buffer, final int position) {
    var value = buffer.getLong(position);
    return new Coordinate(decodeLon(value), decodeLat(value));
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



baremaps-core/src/main/java/org/apache/baremaps/database/type/geometry/LonLatDataType.java [26:65]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class LonLatDataType extends MemoryAlignedDataType<Coordinate> {

  private static final double BITS = Math.pow(2, 31);
  private static final long SHIFT = 32;
  private static final long MASK = (1L << 32) - 1L;

  /** Constructs a {@link LonLatDataType}. */
  public LonLatDataType() {
    super(Long.BYTES);
  }

  public static long encodeLonLat(final double lon, final double lat) {
    long x = (long) (((lon + 180) / 360) * BITS);
    long y = (long) (((lat + 90) / 180) * BITS);
    long l = (x << SHIFT);
    long r = (y & MASK);
    return l | r;
  }

  public static double decodeLon(final long value) {
    double l = (value >>> 32);
    return (l / BITS) * 360 - 180;
  }

  public static double decodeLat(final long value) {
    long r = (value & MASK);
    return (r / BITS) * 180 - 90;
  }

  /** {@inheritDoc} */
  @Override
  public void write(final ByteBuffer buffer, final int position, final Coordinate value) {
    buffer.putLong(position, encodeLonLat(value.x, value.y));
  }

  /** {@inheritDoc} */
  @Override
  public Coordinate read(final ByteBuffer buffer, final int position) {
    var value = buffer.getLong(position);
    return new Coordinate(decodeLon(value), decodeLat(value));
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



