Subversion Repositories hibernate-spatial

Rev

Blame | Last modification | View Log | RSS feed

package org.hibernatespatial.wkb;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import org.hibernatespatial.mgeom.MGeometryFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Karel Maesen, Geovise BVBA
 *         creation-date: Oct 29, 2010
 */
public class WKB {

    public static final int Z_FLAG = 0x80000000;
    public static final int M_FLAG = 0x40000000;
    public static final int SRID_FLAG = 0x20000000;


    public static class Encoders {

        private static final Map ENCODERS = new HashMap();

        static {
            register(Point.class, new WKBPointEncoder());
            register(LineString.class, new WKBLineStringEncoder());

        }
        public static <G extends Geometry> void register(Class<G> geometryClass, WKBEncoder<G> encoderClass){
            ENCODERS.put(geometryClass, encoderClass);
        }

        public static <G extends Geometry> WKBEncoder<G> getFor(G geometry){
            return (WKBEncoder<G>)ENCODERS.get(geometry.getClass());
        }

    }

    public static class Decoders {

        private static final Map<WKBGeometryType, WKBElementDecoder> DECODERS = new HashMap<WKBGeometryType, WKBElementDecoder>();
        private static final WKBDecoder DECODER;

        static {
            GeometryFactory factory = new MGeometryFactory();
            DECODER = new WKBDecoder();
            DECODERS.put(WKBGeometryType.POINT, new WKBPointDecoder(factory));
            DECODERS.put(WKBGeometryType.LINESTRING, new WKBLineStringDecoder(factory));
        }

        public static WKBElementDecoder getFor(WKBGeometryType type){
            return DECODERS.get(type);
        }

        public static WKBDecoder getWKBDecoder(){
            return DECODER;
        }

    }




    public static <G extends Geometry> Bytes toWKB(G geometry) throws UnsupportedConversionException {
        return toWKB(geometry, null);
    }

    public static <G extends Geometry> Bytes toWKB(G geometry, WKBByteOrder byteOrder) throws UnsupportedConversionException {
        WKBEncoder<G> encoder = Encoders.getFor(geometry);
        return encoder.encode(geometry, byteOrder);
    }

    public static Geometry fromWKB(Bytes bytes) {
        return Decoders.getWKBDecoder().decode(bytes);
    }

}