@Override public void serialize(DataOutput dataOutput, GeobufFeature geobufFeature) throws IOException { GeobufEncoder enc = getEncoder(); // This could be more efficient, we're wrapping a single feature in a feature collection. But // that way the key/value // serialization all works. Geobuf.Data feat = enc.makeFeatureCollection(Arrays.asList(geobufFeature)); byte[] data = feat.toByteArray(); dataOutput.writeInt(data.length); dataOutput.write(data); }
@Override public GeobufFeature deserialize(DataInput dataInput, int i) throws IOException { int len = dataInput.readInt(); byte[] feat = new byte[len]; dataInput.readFully(feat); Geobuf.Data data = Geobuf.Data.parseFrom(feat); return new GeobufFeature( data.getFeatureCollection().getFeatures(0), data.getKeysList(), Math.pow(10, data.getPrecision())); }
public Geobuf.Data makeFeatureCollection(Collection<GeobufFeature> featureCollection) { Geobuf.Data.Builder data = Geobuf.Data.newBuilder() .setPrecision(this.precision) // TODO don't hardwire .setDimensions(2); Geobuf.Data.FeatureCollection.Builder fc = Geobuf.Data.FeatureCollection.newBuilder(); // deduplicate keys List<String> keys = new ArrayList<>(); featureCollection.stream().map(f -> this.makeFeature(f, keys)).forEach(fc::addFeatures); fc.addAllValues(Collections.emptyList()); fc.addAllCustomProperties(Collections.emptyList()); data.setFeatureCollection(fc); data.addAllKeys(keys); return data.build(); }