Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed-KartaView-Filtering-by-date-removes-sequence-line #10718

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions modules/services/kartaview.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,52 @@ export default {
var sequenceKeys = {};

// all sequences for images in viewport
_oscCache.images.rtree.search(bbox)
.forEach(function(d) { sequenceKeys[d.data.sequence_id] = true; });
var imagesInViewport = _oscCache.images.rtree.search(bbox);

// Collect unique sequence keys
imagesInViewport.forEach(function(d) {
if (d.data && d.data.sequence_id) {
sequenceKeys[d.data.sequence_id] = true;
} else {
console.warn('Image without sequence_id:', d);
}
});

// make linestrings from those sequences
var lineStrings = [];
Object.keys(sequenceKeys)
.forEach(function(sequenceKey) {
var seq = _oscCache.sequences[sequenceKey];
var images = seq && seq.images;

if (images) {
lineStrings.push({
type: 'LineString',
coordinates: images.map(function (d) { return d.loc; }).filter(Boolean),
properties: {
captured_at: images[0] ? images[0].captured_at: null,
captured_by: images[0] ? images[0].captured_by: null,
key: sequenceKey
}
});

// Validate sequence
if (!seq || !seq.images || seq.images.length === 0) {
console.warn('Invalid or empty sequence:', sequenceKey);
return;
}

// Filter out images without locations
var validImages = seq.images.filter(function(image) {
return image && image.loc && image.loc.length === 2;
});

if (validImages.length === 0) {
console.warn('Sequence with no valid images:', sequenceKey);
return;
}

var sequenceLine = {
type: 'LineString',
coordinates: validImages.map(function (d) { return d.loc; }),
properties: {
captured_at: validImages[0].captured_at || null,
captured_by: validImages[0].captured_by || null,
key: sequenceKey
}
};

lineStrings.push(sequenceLine);
});

return lineStrings;
},

Expand Down
58 changes: 39 additions & 19 deletions modules/svg/kartaview_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,45 @@ export function svgKartaviewImages(projection, context, dispatch) {
var toDate = context.photos().toDate();
var usernames = context.photos().usernames();

if (fromDate) {
var fromTimestamp = new Date(fromDate).getTime();
sequences = sequences.filter(function(image) {
return new Date(image.properties.captured_at).getTime() >= fromTimestamp;
});
}
if (toDate) {
var toTimestamp = new Date(toDate).getTime();
sequences = sequences.filter(function(image) {
return new Date(image.properties.captured_at).getTime() <= toTimestamp;
});
}
if (usernames) {
sequences = sequences.filter(function(image) {
return usernames.indexOf(image.properties.captured_by) !== -1;
});
}
// Defensive filtering to handle potential undefined or malformed sequences
sequences = sequences.filter(function(sequence) {
// Ensure sequence and its properties exist
if (!sequence || !sequence.properties) {
return false;
}

// If no date filters, keep the sequence
if (!fromDate && !toDate && !usernames) {
return true;
}

// Check date filtering
if (fromDate) {
var fromTimestamp = new Date(fromDate).getTime();
if (!sequence.properties.captured_at ||
new Date(sequence.properties.captured_at).getTime() < fromTimestamp) {
return false;
}
}

if (toDate) {
var toTimestamp = new Date(toDate).getTime();
if (!sequence.properties.captured_at ||
new Date(sequence.properties.captured_at).getTime() > toTimestamp) {
return false;
}
}

// Check username filtering
if (usernames && usernames.length > 0) {
if (!sequence.properties.captured_by ||
usernames.indexOf(sequence.properties.captured_by) === -1) {
return false;
}
}

return true;
});

return sequences;
}
Expand Down Expand Up @@ -190,7 +212,6 @@ export function svgKartaviewImages(projection, context, dispatch) {
.merge(traces)
.attr('d', svgPath(projection).geojson);


var groups = layer.selectAll('.markers').selectAll('.viewfield-group')
.data(images, function(d) { return d.key; });

Expand Down Expand Up @@ -221,7 +242,6 @@ export function svgKartaviewImages(projection, context, dispatch) {
.attr('transform', transform)
.select('.viewfield-scale');


markers.selectAll('circle')
.data([0])
.enter()
Expand Down
4 changes: 2 additions & 2 deletions test/spec/services/kartaview.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ describe('iD.serviceKartaview', function() {
type: 'LineString',
coordinates: [[10,0], [10,0], [10,1]],
properties: {
captured_at: undefined,
captured_by: undefined,
captured_at: null,
captured_by: null,
key: '100'
}
}]);
Expand Down
Loading