public GeometryAttributeDiff(@Nullable Geometry oldGeom, @Nullable Geometry newGeom) { Preconditions.checkArgument(oldGeom != null || newGeom != null); oldGeometry = oldGeom; newGeometry = newGeom; if (newGeom == null) { type = TYPE.REMOVED; } else if (oldGeom == null) { type = TYPE.ADDED; } else if (oldGeom.equalsExact(newGeom)) { // Note the use of Geometry.equalsExact() instead of equals() (which defers to // equalsTopo() and may throw a TopologyException, while equalsExact() just compares // coordinate by coordinate, which is what we intend to do here. Not doing topology // validation which is up to the application. type = TYPE.NO_CHANGE; diff = new LCSGeometryDiffImpl(oldGeom, newGeom); } else { type = TYPE.MODIFIED; diff = new LCSGeometryDiffImpl(oldGeom, newGeom); } }
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); }
public GeometryAttributeDiff(@Nullable Geometry oldGeom, @Nullable Geometry newGeom) { Preconditions.checkArgument(oldGeom != null || newGeom != null); oldGeometry = oldGeom; newGeometry = newGeom; if (newGeom == null) { type = TYPE.REMOVED; } else if (oldGeom == null) { type = TYPE.ADDED; } else if (oldGeom.equalsExact(newGeom)) { // Note the use of Geometry.equalsExact() instead of equals() (which defers to // equalsTopo() and may throw a TopologyException, while equalsExact() just compares // coordinate by coordinate, which is what we intend to do here. Not doing topology // validation which is up to the application. type = TYPE.NO_CHANGE; diff = new LCSGeometryDiffImpl(oldGeom, newGeom); } else { type = TYPE.MODIFIED; diff = new LCSGeometryDiffImpl(oldGeom, newGeom); } }
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); }
public GeometryAttributeDiff(String s) { String[] tokens = s.split("\t"); if (tokens[0].equals("M")) { type = TYPE.MODIFIED; diff = new LCSGeometryDiffImpl(s.substring(s.indexOf("\t") + 1)); } else if (tokens[0].equals("A")) { Preconditions.checkArgument(tokens.length == 3); type = TYPE.ADDED; String wkt = tokens[1]; newGeometry = (Geometry) TextValueSerializer .fromString(FieldType.forBinding(Geometry.class), wkt); } else if (tokens[0].equals("R")) { Preconditions.checkArgument(tokens.length == 3); type = TYPE.REMOVED; String wkt = tokens[1]; oldGeometry = (Geometry) TextValueSerializer .fromString(FieldType.forBinding(Geometry.class), wkt); } else { throw new IllegalArgumentException("Wrong difference definition:" + s); } }
public GeometryAttributeDiff(String s) { String[] tokens = s.split("\t"); if (tokens[0].equals("M")) { type = TYPE.MODIFIED; diff = new LCSGeometryDiffImpl(s.substring(s.indexOf("\t") + 1)); } else if (tokens[0].equals("A")) { Preconditions.checkArgument(tokens.length == 3); type = TYPE.ADDED; String wkt = tokens[1]; newGeometry = (Geometry) TextValueSerializer .fromString(FieldType.forBinding(Geometry.class), wkt); } else if (tokens[0].equals("R")) { Preconditions.checkArgument(tokens.length == 3); type = TYPE.REMOVED; String wkt = tokens[1]; oldGeometry = (Geometry) TextValueSerializer .fromString(FieldType.forBinding(Geometry.class), wkt); } else { throw new IllegalArgumentException("Wrong difference definition:" + s); } }
@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 testModifiedMultiPolygon() throws Exception { int NUM_COORDS = 10; Random rand = new Random(); List<Coordinate> list = Lists.newArrayList(); for (int i = 0; i < NUM_COORDS; i++) { list.add(new Coordinate(rand.nextInt(), rand.nextInt())); } Geometry oldGeom = new WKTReader().read( "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 10, 30 5, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20)))"); Geometry newGeom = new WKTReader().read( "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35)))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("4 point(s) deleted, 1 new point(s) added, 1 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, 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 testModifiedMultiPolygon() throws Exception { int NUM_COORDS = 10; Random rand = new Random(); List<Coordinate> list = Lists.newArrayList(); for (int i = 0; i < NUM_COORDS; i++) { list.add(new Coordinate(rand.nextInt(), rand.nextInt())); } Geometry oldGeom = new WKTReader().read( "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 10, 30 5, 10 30, 20 35),(30 20, 20 25, 20 15, 30 20)))"); Geometry newGeom = new WKTReader().read( "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 45 20, 30 5, 10 10, 10 30, 20 35)))"); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("4 point(s) deleted, 1 new point(s) added, 1 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, resultingGeom); }
@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 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 testModifiedMultiLineString() 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 deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 4 new point(s) added, 3 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, resultingGeom); }
@Test public void testModifiedMultiLineString() 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 deserializedDiff = new LCSGeometryDiffImpl(diff.asText()); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 4 new point(s) added, 3 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, resultingGeom); }
@Test public void testSameMultiPolygon() throws Exception { String wkt = "MULTIPOLYGON (((-75.0740073 38.6938098, -75.0739683 38.6935296, " + "-75.0745695 38.6934786, -75.0745824 38.6935715, -75.0741091 38.6936117, " + "-75.0741352 38.6937989, -75.0740073 38.6938098)))"; Geometry oldGeom = new WKTReader().read(wkt); Geometry newGeom = new WKTReader().read(wkt); assertTrue(oldGeom.equalsExact(newGeom)); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); String asText = diff.asText(); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(asText); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, resultingGeom); }
@Test public void testSameMultiPolygon() throws Exception { String wkt = "MULTIPOLYGON (((-75.0740073 38.6938098, -75.0739683 38.6935296, " + "-75.0745695 38.6934786, -75.0745824 38.6935715, -75.0741091 38.6936117, " + "-75.0741352 38.6937989, -75.0740073 38.6938098)))"; Geometry oldGeom = new WKTReader().read(wkt); Geometry newGeom = new WKTReader().read(wkt); assertTrue(oldGeom.equalsExact(newGeom)); LCSGeometryDiffImpl diff = new LCSGeometryDiffImpl(oldGeom, newGeom); String asText = diff.asText(); LCSGeometryDiffImpl deserializedDiff = new LCSGeometryDiffImpl(asText); assertEquals(diff, deserializedDiff); assertEquals("0 point(s) deleted, 0 new point(s) added, 0 point(s) moved", diff.toString()); Geometry resultingGeom = diff.applyOn(oldGeom); assertEquals(newGeom, resultingGeom); }
@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 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)); }
@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)); }