1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*******************************************************************************
* Copyright (c) 2015 MITRE
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0 which
* accompanies this distribution and is available at
* http://www.apache.org/licenses/LICENSE-2.0.txt
******************************************************************************/
package com.spatial4j.core.io.jts;
import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.context.jts.JtsSpatialContextFactory;
import com.spatial4j.core.exception.InvalidShapeException;
import com.spatial4j.core.io.BinaryCodec;
import com.spatial4j.core.shape.Shape;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.InStream;
import org.locationtech.jts.io.OutStream;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBConstants;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* Writes shapes in WKB, if it isn't otherwise supported by the superclass.
*/
public class JtsBinaryCodec extends BinaryCodec {
protected final boolean useFloat;//instead of double
public JtsBinaryCodec(JtsSpatialContext ctx, JtsSpatialContextFactory factory) {
super(ctx, factory);
//note: ctx.geometryFactory hasn't been set yet
useFloat = (factory.precisionModel.getType() == PrecisionModel.FLOATING_SINGLE);
}
@Override
protected double readDim(DataInput dataInput) throws IOException {
if (useFloat)
return dataInput.readFloat();
return super.readDim(dataInput);
}
@Override
protected void writeDim(DataOutput dataOutput, double v) throws IOException {
if (useFloat)
dataOutput.writeFloat((float) v);
else
super.writeDim(dataOutput, v);
}
@Override
protected byte typeForShape(Shape s) {
byte type = super.typeForShape(s);
if (type == 0) {
type = TYPE_GEOM;//handles everything
}
return type;
}
@Override
protected Shape readShapeByTypeIfSupported(final DataInput dataInput, byte type) throws IOException {
if (type != TYPE_GEOM)
return super.readShapeByTypeIfSupported(dataInput, type);
return readJtsGeom(dataInput);
}
@Override
protected boolean writeShapeByTypeIfSupported(DataOutput dataOutput, Shape s, byte type) throws IOException {
if (type != TYPE_GEOM)
return super.writeShapeByTypeIfSupported(dataOutput, s, type);
writeJtsGeom(dataOutput, s);
return true;
}
public Shape readJtsGeom(final DataInput dataInput) throws IOException {
JtsSpatialContext ctx = (JtsSpatialContext)super.ctx;
WKBReader reader = new WKBReader(ctx.getGeometryFactory());
try {
InStream inStream = new InStream() {//a strange JTS abstraction
boolean first = true;
@Override
public void read(byte[] buf) throws IOException {
if (first) {//we don't write JTS's leading BOM so synthesize reading it
if (buf.length != 1)
throw new IllegalStateException("Expected initial read of one byte, not: " + buf.length);
buf[0] = WKBConstants.wkbXDR;//0
first = false;
} else {
//TODO for performance, specialize for common array lengths: 1, 4, 8
dataInput.readFully(buf);
}
}
};
Geometry geom = reader.read(inStream);
//false: don't check for dateline-180 cross or multi-polygon overlaps; this won't happen
// once it gets written, and we're reading it now
return ctx.makeShape(geom, false, false);
} catch (ParseException ex) {
throw new InvalidShapeException("error reading WKT", ex);
}
}
public void writeJtsGeom(final DataOutput dataOutput, Shape s) throws IOException {
JtsSpatialContext ctx = (JtsSpatialContext)super.ctx;
Geometry geom = ctx.getGeometryFrom(s);//might even translate it
new WKBWriter().write(geom, new OutStream() {//a strange JTS abstraction
boolean first = true;
@Override
public void write(byte[] buf, int len) throws IOException {
if (first) {
first = false;
//skip byte order mark
if (len != 1 || buf[0] != WKBConstants.wkbXDR)//the default
throw new IllegalStateException("Unexpected WKB byte order mark");
return;
}
dataOutput.write(buf, 0, len);
}
});
}
}
|