Skip to content

Commit

Permalink
add lastContributionTimestamp to OSMEntitySnapshot objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrasd committed Mar 23, 2023
1 parent f4ef6eb commit ec1ec18
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public OSMEntitySnapshotImpl(
) {
this.data = new IterateByTimestampEntry(
other.getTimestamp(),
other.getLastContributionTimestamp(),
other.getEntity(),
other.getOSHEntity(),
reclippedGeometry,
Expand All @@ -50,6 +51,11 @@ public OSHDBTimestamp getTimestamp() {
return data.timestamp();
}

@Override
public OSHDBTimestamp getLastContributionTimestamp() {
return data.lastModificationTimestamp();
}

@Override
public Geometry getGeometry() {
return data.geometry().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ protected <R, S> S flatMapReduceCellsOSMEntitySnapshotGroupedById(

private static OSMEntitySnapshot snapshot(OSHNode node) {
var timestamp = timestamps.get().first();
var data = new IterateByTimestampEntry(timestamp, node.getVersions().iterator().next(), node,
var data = new IterateByTimestampEntry(timestamp, null,
node.getVersions().iterator().next(), node,
new LazyEvaluatedObject<>(point),
new LazyEvaluatedObject<>(point));
return new OSMEntitySnapshotImpl(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public CellIterator(
* Holds the result of a single item returned by {@link #iterateByTimestamps(GridOSHEntity)}.
*
* @param timestamp timestamp of the snapshot
* @param lastModificationTimestamp last modification timestamp before the snapshot's timestamp
* @param osmEntity the exact version of the OSM object
* @param oshEntity the whole version history of the OSM object
* @param geometry an object which holds the geometry of the OSM object, or a method to build it
Expand All @@ -220,6 +221,7 @@ public CellIterator(
*/
public record IterateByTimestampEntry(
OSHDBTimestamp timestamp,
OSHDBTimestamp lastModificationTimestamp,
@Nonnull OSMEntity osmEntity,
@Nonnull OSHEntity oshEntity,
LazyEvaluatedObject<Geometry> geometry,
Expand Down Expand Up @@ -281,22 +283,26 @@ public Stream<IterateByTimestampEntry> iterateByTimestamps(Stream<? extends OSHE
// optimize loop by requesting modification timestamps first, and skip geometry calculations
// where not needed
SortedMap<OSHDBTimestamp, List<OSHDBTimestamp>> queryTs = new TreeMap<>();
SortedMap<OSHDBTimestamp, OSHDBTimestamp> lastModificationTimestamps = new TreeMap<>();
if (!includeOldStyleMultipolygons) {
List<OSHDBTimestamp> modTs =
OSHEntityTimeUtils.getModificationTimestamps(oshEntity, osmEntityFilter);
int j = 0;
OSHDBTimestamp lastModificationTimestamp = null;
for (OSHDBTimestamp requestedT : timestamps) {
boolean needToRequest = false;
while (j < modTs.size()
&& modTs.get(j).getEpochSecond() <= requestedT.getEpochSecond()) {
needToRequest = true;
lastModificationTimestamp = modTs.get(j);
j++;
}
if (needToRequest) {
queryTs.put(requestedT, new LinkedList<>());
} else if (queryTs.size() > 0) {
queryTs.get(queryTs.lastKey()).add(requestedT);
}
lastModificationTimestamps.put(requestedT, lastModificationTimestamp);
}
} else {
// todo: make this work with old style multipolygons!!?!
Expand Down Expand Up @@ -381,16 +387,17 @@ public Stream<IterateByTimestampEntry> iterateByTimestamps(Stream<? extends OSHE
});
}

var lastModificationTimestamp = lastModificationTimestamps.get(timestamp);
if (fullyInside || !geom.get().isEmpty()) {
LazyEvaluatedObject<Geometry> fullGeom = fullyInside ? geom : new LazyEvaluatedObject<>(
() -> OSHDBGeometryBuilder.getGeometry(osmEntity, timestamp, tagInterpreter));
results.add(new IterateByTimestampEntry(
timestamp, osmEntity, oshEntity, geom, fullGeom)
timestamp, lastModificationTimestamp, osmEntity, oshEntity, geom, fullGeom)
);
// add skipped timestamps (where nothing has changed from the last timestamp) to result
for (OSHDBTimestamp additionalT : queryTs.get(timestamp)) {
results.add(new IterateByTimestampEntry(
additionalT, osmEntity, oshEntity, geom, fullGeom)
additionalT, lastModificationTimestamp, osmEntity, oshEntity, geom, fullGeom)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public interface OSMEntitySnapshot extends OSHDBMapReducible, Comparable<OSMEnti
*/
OSHDBTimestamp getTimestamp();

/**
* The timestamp when the entity of the snapshot was last modified before the snapshot timestamp.
*
* @return last modification timestamp as an OSHDBTimestamp object
*/
OSHDBTimestamp getLastContributionTimestamp();

/**
* The geometry of this entity at the snapshot's timestamp clipped to the requested area of
* interest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,35 @@ void testCoordinatesRelativeToPolygon() throws IOException {
assertEquals(13, result.get(1).osmEntity().getId());
assertEquals(14, result.get(2).osmEntity().getId());
}

@Test
void testLastContributionTimestamp() {
// node 3: creation and 4 visible changes, but no geometry and no tag changes

List<IterateByTimestampEntry> result = (new CellIterator(
new OSHDBTimestamps(
"2000-06-01T00:00:00Z",
"2018-06-01T00:00:00Z",
"P1Y"
).get(),
OSHDBBoundingBox.bboxWgs84Coordinates(-180.0, -90.0, 180.0, 90.0),
areaDecider,
oshEntity -> oshEntity.getId() == 3,
osmEntity -> true,
false
)).iterateByTimestamps(
oshdbDataGridCell
).toList();
assertEquals(5, result.size());
assertEquals("2007-06-01T00:00:00", result.get(0).timestamp().toString());
assertEquals("2007-01-01T00:00:00", result.get(0).lastModificationTimestamp().toString());
assertEquals("2014-06-01T00:00:00", result.get(1).timestamp().toString());
assertEquals("2014-01-01T00:00:00", result.get(1).lastModificationTimestamp().toString());
assertEquals("2016-06-01T00:00:00", result.get(2).timestamp().toString());
assertEquals("2016-01-01T00:00:00", result.get(2).lastModificationTimestamp().toString());
assertEquals("2017-06-01T00:00:00", result.get(3).timestamp().toString());
assertEquals("2016-01-01T00:00:00", result.get(3).lastModificationTimestamp().toString());
assertEquals("2018-06-01T00:00:00", result.get(4).timestamp().toString());
assertEquals("2016-01-01T00:00:00", result.get(4).lastModificationTimestamp().toString());
}
}

0 comments on commit ec1ec18

Please sign in to comment.