fairdm.core.measurement.managers#

QuerySet for measurements, with helpers that load related records in a bounded number of queries.

Module Contents#

Classes#

MeasurementQuerySet

QuerySet for measurements, whose methods chain with the standard queryset operations.

API#

class MeasurementQuerySet(*args: Any, **kwargs: Any)#

Bases: fairdm.core.managers.RecordVisibilityMixin, polymorphic.managers.PolymorphicQuerySet

QuerySet for measurements, whose methods chain with the standard queryset operations.

with_related() prefetches direct relationships without deep nesting, which views add themselves when needed. See docs/developers/how-to/add-a-measurement-type.md.

inside(), within_distance() and nearest() find measurements by their effective location: their own, else their sample’s for a viewer who may open the sample.

Initialization

Load each measurement’s sample and dataset, and prefetch its contributors and their roles.

Deeper relationships, such as the sample’s dataset, are left to callers to chain.

Returns: The queryset with the related records loaded.

Example: python     measurements = Measurement.objects.with_related().filter(dataset=my_dataset)    

with_effective_location(user=None)#

Annotate effective_location: the measurement’s own location, else its sample’s.

The sample’s location is used only where the viewer may open the sample, so a measurement in a published dataset never gives away where an unpublished sample was taken.

Args: user: The viewer, or None for a visitor who is not signed in.

Returns: The queryset, each measurement carrying effective_location as a WGS 84 point or None.

Example: python     for measurement in Measurement.objects.with_effective_location(         request.user     ):         print(measurement.effective_location)    

inside(area, user=None)#

Return the measurements whose effective location lies in an area, the edge included.

The answer is the union of two subqueries, each on one table so each uses its own spatial index: measurements whose own location is covered, and measurements with none whose sample, visible to the viewer, is covered. An OR across the join would use neither index.

Args: area: A GEOS polygon, or a (west, south, east, north) tuple in degrees. user: The viewer, or None for a visitor who is not signed in.

Returns: The measurements inside the area.

within_distance(point, distance, user=None)#

Return the measurements whose effective location is within a distance of a point.

Built from the same two subqueries as :meth:inside.

Args: point: A GEOS point. distance: A D measure, or a number of metres. user: The viewer, or None for a visitor who is not signed in.

Returns: The measurements within the distance, measured on the ground.

nearest(point, user=None)#

Order the measurements by the distance of their effective location from a point.

Args: point: A GEOS point. user: The viewer, or None for a visitor who is not signed in.

Returns: The measurements nearest first, each carrying distance as a D measure, or None for one with no effective location, which comes last.

with_metadata()#

Prefetch the descriptions, dates and identifiers.

Returns: The queryset with the prefetches applied.