org.geolatte.geom.codec.Wkb Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geolatte-geom Show documentation
Show all versions of geolatte-geom Show documentation
This geoLatte-geom library offers a geometry model that conforms to the OGC Simple Features for SQL
specification.
/*
* This file is part of the GeoLatte project.
*
* GeoLatte is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoLatte is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GeoLatte. If not, see .
*
* Copyright (C) 2010 - 2011 and Ownership of code is shared by:
* Qmino bvba - Romeinsestraat 18 - 3001 Heverlee (http://www.qmino.com)
* Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
*/
package org.geolatte.geom.codec;
import org.geolatte.geom.ByteBuffer;
import org.geolatte.geom.ByteOrder;
import org.geolatte.geom.Geometry;
import java.util.HashMap;
import java.util.Map;
/**
* Creates encoders/decoders for WKB geometry representations.
*
* Note that the WkbEncoder /WkbDecoder
instances returned by the factory
* methods are not thread-safe.
*
* @author Karel Maesen, Geovise BVBA
* creation-date: Oct 29, 2010
*/
public class Wkb {
public enum Dialect {
//the PostGIS EWKB dialect (versions 1.0 to 1.6).
POSTGIS_EWKB_1,
MYSQL_WKB
}
private static final Dialect DEFAULT_DIALECT = Dialect.POSTGIS_EWKB_1;
private static final Map> DECODERS = new HashMap>();
private static final Map> ENCODERS = new HashMap>();
static {
DECODERS.put(Dialect.POSTGIS_EWKB_1, PostgisWkbDecoder.class);
DECODERS.put(Dialect.MYSQL_WKB, MySqlWkbDecoder.class);
ENCODERS.put(Dialect.POSTGIS_EWKB_1, PostgisWkbEncoder.class);
ENCODERS.put(Dialect.MYSQL_WKB, MySqlWkbEncoder.class);
}
/**
* Encodes a Geometry
into a WKB representation using the NDR (little-endian) byte-order.
*
* This methods uses the default WKB dialect (Postgis v1.5 EWKB ).
*
* @param geometry The Geometry
to be encoded as WKB.
* @return A buffer of bytes that contains the WKB-encoded Geometry
.
*/
public static ByteBuffer toWkb(Geometry geometry) {
return toWkb(geometry, ByteOrder.NDR);
}
/**
* Encodes a Geometry
into a WKB representation using the specified byte-order.
* This methods uses the default WKB dialect (Postgis v1.5 EWKB ).
*
* @param geometry The Geometry
to be encoded as WKB.
* @param byteOrder The WKB byte order, either {@link ByteOrder#XDR XDR} or {@link ByteOrder#NDR NDR}
* @return A buffer of bytes that contains the WKB-encoded Geometry
.
*/
public static ByteBuffer toWkb(Geometry> geometry, ByteOrder byteOrder) {
WkbEncoder encoder = newEncoder(DEFAULT_DIALECT);
return encoder.encode(geometry, byteOrder);
}
/**
* Decodes a WKB representation in a ByteBuffer
to a Geometry
.
* This methods uses the default WKB dialect (Postgis v1.5 EWKB ).
*
* @param byteBuffer A buffer of bytes that contains a WKB-encoded Geometry
.
* @return The Geometry
that is encoded in the WKB.
*/
public static Geometry> fromWkb(ByteBuffer byteBuffer) {
WkbDecoder decoder = newDecoder(DEFAULT_DIALECT);
return decoder.decode(byteBuffer);
}
/**
* Creates a WkbDecoder
for the specified WKB Dialect
.
*
* @param dialect the WKB dialect
* @return an WkbDecoder
that supports the specified dialect
*/
public static WkbDecoder newDecoder(Dialect dialect) {
Class extends WkbDecoder> decoderClass = DECODERS.get(dialect);
assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered.";
return createInstance(decoderClass);
}
/**
* Creates a WkbDecoder
for the default WKB Dialect
.
*
* @return an WkbDecoder
that supports the specified dialect
*/
public static WkbDecoder newDecoder() {
Class extends WkbDecoder> decoderClass = DECODERS.get(DEFAULT_DIALECT);
assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered.";
return createInstance(decoderClass);
}
/**
* Creates a WkbEncoder
for the specified WKB Dialect
.
*
* @param dialect the WKB dialect
* @return an WkbEncoder
that supports the specified dialect
*/
public static WkbEncoder newEncoder(Dialect dialect) {
Class extends WkbEncoder> decoderClass = ENCODERS.get(dialect);
assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered.";
return createInstance(decoderClass);
}
/**
* Creates a WkbEncoder
for the default WKB Dialect
.
*
* @return an WkbEncoder
that supports the specified dialect
*/
public static WkbEncoder newEncoder() {
Class extends WkbEncoder> decoderClass = ENCODERS.get(DEFAULT_DIALECT);
assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered.";
return createInstance(decoderClass);
}
private static T createInstance(Class extends T> codecClass) {
if (codecClass == null) {
throw new IllegalArgumentException("Null WKB codec class argument not allowed.");
}
try {
return codecClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy