org.geolatte.geom.codec.PostgisWkbV1Dialect Maven / Gradle / Ivy
Show all versions of geolatte-geom Show documentation
package org.geolatte.geom.codec;
import org.geolatte.geom.*;
import org.geolatte.geom.crs.CoordinateReferenceSystem;
class PostgisWkbV1Dialect extends WkbDialect {
final public static WkbDialect INSTANCE = new PostgisWkbV1Dialect();
@Override
boolean emptyPointAsNaN() {
return false;
}
@Override
protected Long geometryTypeCode(Geometry
geometry) {
if (geometry.isEmpty() && geometry.getGeometryType() == GeometryType.POINT) {
return (long) WKB_GEOMETRYCOLLECTION;
}
return super.geometryTypeCode(geometry);
}
GeometryType parseType(long tpe) {
return super.parseType( tpe & 0xFFFF );
}
@Override
protected
int extraHeaderSize(Geometry
geom) {
return geom.getSRID() > 0 ? 4 : 0; //4 bytes for SRID
}
@Override
BaseWkbVisitor
mkVisitor(Geometry
geom, ByteOrder bo) {
return new PostgisWkbVisitor
(mkByteBuffer(geom, bo), this);
}
static class PostgisWkbVisitor
extends BaseWkbVisitor
{
private boolean hasWrittenSrid = false;
PostgisWkbVisitor(ByteBuffer byteBuffer, WkbDialect dialect) {
super(byteBuffer, dialect);
}
protected void writeTypeCodeAndSrid(Geometry
geometry, ByteBuffer output) {
long typeCode = dialect().geometryTypeCode(geometry);
boolean hasSrid = (geometry.getSRID() > 0);
CoordinateReferenceSystem
crs = geometry.getCoordinateReferenceSystem();
if (hasSrid && !hasWrittenSrid) {
typeCode |= PostgisWkbTypeMasks.SRID_FLAG;
}
if (geometry.hasM()) {
typeCode |= PostgisWkbTypeMasks.M_FLAG;
}
if (geometry.hasZ()) {
typeCode |= PostgisWkbTypeMasks.Z_FLAG;
}
output.putUInt(typeCode);
if (hasSrid && !hasWrittenSrid) {
output.putInt(geometry.getSRID());
hasWrittenSrid = true;
}
}
}
}