public LCSGeometryDiffImpl reversed() { LinkedList<Patch> reversedPatches = diffMatchPatch.patch_deepCopy(patches); for (Patch patch : reversedPatches) { LinkedList<Diff> diffs = patch.diffs; for (Diff diff : diffs) { if (diff.operation == Operation.DELETE) { diff.operation = Operation.INSERT; } else if (diff.operation == Operation.INSERT) { diff.operation = Operation.DELETE; } } } return new LCSGeometryDiffImpl(reversedPatches); }
@Override public Geometry applyOn(@Nullable Object value) { Preconditions.checkState(canBeAppliedOn(value)); switch (type) { case ADDED: return newGeometry; case REMOVED: return null; case MODIFIED: default: return diff.applyOn((Geometry) value); } }
@Override public boolean canBeAppliedOn(@Nullable Object value) { switch (this.type) { case ADDED: return value == null; case REMOVED: checkNotNull(oldGeometry); checkNotNull(value); Geometry geom = (Geometry) value; return geom.equalsExact(oldGeometry); case MODIFIED: default: return diff.canBeAppliedOn((Geometry) value); } }
public LCSGeometryDiffImpl(@Nullable Geometry oldGeom, @Nullable Geometry newGeom) { String oldText = oldGeom == null ? "" : oldGeom.toText(); String newText = newGeom == null ? "" : newGeom.toText(); diffMatchPatch = new DiffMatchPatch(); LinkedList<Diff> diffs = diffMatchPatch.diff_main(oldText, newText); patches = diffMatchPatch.patch_make(diffs); // to calculate number of edits in the geometry, we do a diffing based on a string // representation of the coordinates of the geometry, instead of the WKT. // This is more more practical for counting added/removed/edited points and generating a // human-readable and easy-to-parse string representation of the diff. // NOTE! This is limited to geometries with less than 65535 different points, and might // yield wrong results for geometries over that limit. // This is a temporary hack, until a better solution is developed. oldText = geomToStringOfCoordinates(oldGeom); newText = geomToStringOfCoordinates(newGeom); LinesToCharsResult chars = coordsToChars(oldText, newText); diffs = diffMatchPatch.diff_main(chars.chars1, chars.chars2); charsToCoords(diffs, chars.lineArray); processDiffs(diffs); }
@Test public void testNoNewGeometry() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, (Geometry) null); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("9 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertNull(resultingGeom); }
@Test public void testNoOldGeometry() throws Exception { Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl((Geometry) null, newGeom); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 13 new point(s) added, 0 point(s) moved", diff.toString()); }
@Test public void testDoubleReverseEquality() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); LCSGeometryDiffImpl diff2 = diff.reversed().reversed(); assertEquals(diff, diff2); }
@Test public void testCanApply() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); Geometry oldGeomModified = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 41),(20 35, 45 10, 30 5, 10 30, 20 35))"); assertTrue(diff.canBeAppliedOn(oldGeomModified)); Geometry oldGeomModified2 = new WKTReader().read("MULTILINESTRING ((40 40, 10 10))"); assertFalse(diff.canBeAppliedOn(oldGeomModified2)); }
ansi.a(pd.getName()).a(": "); ansi.reset(); String text = gd.getDiff().getDiffCoordsString(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '(') {
@Override public String asText() { switch (type) { case ADDED: return type.name().toCharArray()[0] + "\t" + TextValueSerializer.asString(newGeometry); case REMOVED: return type.name().toCharArray()[0] + "\t" + TextValueSerializer.asString(oldGeometry); case MODIFIED: default: return type.name().toCharArray()[0] + "\t" + diff.asText(); } }
@Override public AttributeDiff reversed() { if (type == TYPE.MODIFIED) { return new GeometryAttributeDiff(this.diff.reversed()); } else { return new GeometryAttributeDiff(oldGeometry, newGeometry); } }
public String toString() { switch (type) { case ADDED: return "[MISSING] -> " + TextValueSerializer.asString(newGeometry); case REMOVED: return TextValueSerializer.asString(oldGeometry) + " -> [MISSING]"; case MODIFIED: default: return diff.toString(); } }
@Override public boolean equals(Object o) { if (!(o instanceof GeometryAttributeDiff)) { return false; } GeometryAttributeDiff d = (GeometryAttributeDiff) o; if (oldGeometry == null && newGeometry == null) { return d.oldGeometry == null && d.newGeometry == null && Objects.equal(type, d.type); } else { return diff.equals(d.diff); } }
@Test public void testNoNewGeometry() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, (Geometry) null); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("9 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertNull(resultingGeom); }
@Test public void testNoOldGeometry() throws Exception { Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl((Geometry) null, newGeom); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 13 new point(s) added, 0 point(s) moved", diff.toString()); }
@Test public void testDoubleReverseEquality() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); LCSGeometryDiffImpl diff2 = diff.reversed().reversed(); assertEquals(diff, diff2); }
@Test public void testCanApply() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 35, 45 30, 40 40),(20 35, 45 20, 30 15, 10 10, 10 30, 20 35),(10 10, 20 20, 35 30))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); Geometry oldGeomModified = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 41),(20 35, 45 10, 30 5, 10 30, 20 35))"); assertTrue(diff.canBeAppliedOn(oldGeomModified)); Geometry oldGeomModified2 = new WKTReader().read("MULTILINESTRING ((40 40, 10 10))"); assertFalse(diff.canBeAppliedOn(oldGeomModified2)); }
public LCSGeometryDiffImpl(@Nullable Geometry oldGeom, @Nullable Geometry newGeom) { String oldText = oldGeom == null ? "" : oldGeom.toText(); String newText = newGeom == null ? "" : newGeom.toText(); diffMatchPatch = new DiffMatchPatch(); LinkedList<Diff> diffs = diffMatchPatch.diff_main(oldText, newText); patches = diffMatchPatch.patch_make(diffs); // to calculate number of edits in the geometry, we do a diffing based on a string // representation of the coordinates of the geometry, instead of the WKT. // This is more more practical for counting added/removed/edited points and generating a // human-readable and easy-to-parse string representation of the diff. // NOTE! This is limited to geometries with less than 65535 different points, and might // yield wrong results for geometries over that limit. // This is a temporary hack, until a better solution is developed. oldText = geomToStringOfCoordinates(oldGeom); newText = geomToStringOfCoordinates(newGeom); LinesToCharsResult chars = coordsToChars(oldText, newText); diffs = diffMatchPatch.diff_main(chars.chars1, chars.chars2); charsToCoords(diffs, chars.lineArray); processDiffs(diffs); }
ansi.a(pd.getName()).a(": "); ansi.reset(); String text = gd.getDiff().getDiffCoordsString(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '(') {
@Override public String asText() { switch (type) { case ADDED: return type.name().toCharArray()[0] + "\t" + TextValueSerializer.asString(newGeometry); case REMOVED: return type.name().toCharArray()[0] + "\t" + TextValueSerializer.asString(oldGeometry); case MODIFIED: default: return type.name().toCharArray()[0] + "\t" + diff.asText(); } }