Annotations filter by viewport

Implementation for #540 and related minor improvements.