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

org.geolatte.geom.codec.Wkb Maven / Gradle / Ivy

Go to download

This geoLatte-geom library offers a geometry model that conforms to the OGC Simple Features for SQL specification.

The newest version!
/*
 * 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 org.geolatte.geom.Position;

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 { /** * Implements SFA vs 1.1.0, OGC document 05_126 */ SFA_1_1_0, /** * Implements SFA vs 1.2.1, OGC document 06-103r4 */ SFA_1_2_1, /** * the PostGIS EWKB dialect (version < 2.2.1). * This encodes an empty {@code Point} as an empty {@code GeometryCollection} */ POSTGIS_EWKB_1, /** * the PostGIS EWKB dialect (version >= 2.2.2). * This encodes an empty {@code Point} as an {@code Point} with coordinates (NaN, NaN). */ POSTGIS_EWKB_2, MYSQL_WKB, HANA_EWKB, } private static final Dialect DEFAULT_DIALECT = Dialect.POSTGIS_EWKB_2; private static final Map> DECODERS = new HashMap<>(); private static final Map> ENCODERS = new HashMap<>(); static { DECODERS.put(Dialect.SFA_1_1_0, Sfa110WkbDecoder.class); DECODERS.put(Dialect.SFA_1_2_1, Sfa121WkbDecoder.class); DECODERS.put(Dialect.POSTGIS_EWKB_1, PostgisWkbDecoder.class); DECODERS.put(Dialect.POSTGIS_EWKB_2, PostgisWkbDecoder.class); DECODERS.put(Dialect.MYSQL_WKB, MySqlWkbDecoder.class); DECODERS.put(Dialect.HANA_EWKB, HANAWkbDecoder.class); ENCODERS.put(Dialect.SFA_1_1_0, Sfa110WkbEncoder.class); ENCODERS.put(Dialect.SFA_1_2_1, Sfa121WkbEncoder.class); ENCODERS.put(Dialect.POSTGIS_EWKB_1, PostgisWkbEncoder.class); ENCODERS.put(Dialect.POSTGIS_EWKB_2, PostgisWkbV2Encoder.class); ENCODERS.put(Dialect.MYSQL_WKB, MySqlWkbEncoder.class); ENCODERS.put(Dialect.HANA_EWKB, HANAWkbEncoder.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} * @param dialect the WKB dialect to use * @return A buffer of bytes that contains the WKB-encoded Geometry. */ public static ByteBuffer toWkb(Geometry geometry, ByteOrder byteOrder, Dialect dialect) { WkbEncoder encoder = newEncoder(dialect); return encoder.encode(geometry, byteOrder); } /** * 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. * @param dialect the WKB dialect to use * @return A buffer of bytes that contains the WKB-encoded Geometry. */ public static

ByteBuffer toWkb(Geometry

geometry, Dialect dialect) { return toWkb(geometry, ByteOrder.NDR, dialect); } /** * 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); } /** * 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. * @param dialect the WKB dialect to use * @return The Geometry that is encoded in the WKB. */ public static Geometry fromWkb(ByteBuffer byteBuffer, Dialect dialect) { WkbDecoder decoder = newDecoder(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 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 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 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 decoderClass = ENCODERS.get(DEFAULT_DIALECT); assert (decoderClass != null) : "A variant declared, but no encoder/decoder registered."; return createInstance(decoderClass); } private static T createInstance(Class codecClass) { if (codecClass == null) { throw new IllegalArgumentException("Null WKB codec class argument not allowed."); } try { return codecClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy