Skip to content
Merged
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
125 changes: 94 additions & 31 deletions crates/data/src/source/shp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,27 @@ impl ShpSource {
}
}

/// How a request narrows a read of the file ([`ShpSource::narrowing`]), the same for a sampled
/// read and a visit in chunks.
struct Narrowing {
/// The request's box in the file's own system, when it is one (a box in another system is
/// read as if absent).
own: Option<Extent>,
/// A box over the whole plane in its own system with no extent beside it, which narrows
/// nothing: every record, those without a box too, as a whole read.
everywhere: bool,
/// A read of an area, in longitude/latitude or in the file's own system, which holds no
/// shape without geometry.
by_area: bool,
}

impl ShpSource {
fn narrowing(&self, req: &Request) -> Narrowing {
let own = self.reproject.own_box(req);
let everywhere = req.extent.is_none() && own == Some(super::whole_plane());
Narrowing { own, everywhere, by_area: req.extent.is_some() || (own.is_some() && !everywhere) }
}

/// A complete visit of a request's records, or of those numbered `numbered` (from 0, in
/// order), parsed and given a chunk at a time ([`FeatureSource::visit_request_chunks`],
/// [`FeatureSource::visit_ids_chunks`]). Only a whole read looks at the stored boxes.
Expand All @@ -284,11 +304,7 @@ impl ShpSource {
let encoding = self.encoding.decoder();
let mut chunks = super::chunk_memory::Chunker::new(bytes, visit)?;
let req = Request { limit: usize::MAX, ..*req };
// The request's box in the file's own system, when it is one (a box in another system
// is read as if absent), and whether it is one over the whole plane, which narrows
// nothing: every record, those without a box too, as a whole read.
let own = self.reproject.own_box(&req);
let everywhere = own == Some(super::whole_plane());
let Narrowing { own, everywhere, by_area } = self.narrowing(&req);
let extent = self.reproject.request_box(&req).filter(|_| !everywhere);
if extent.is_some_and(|e| e.is_empty()) {
return Ok(Batch { complete: true, ..Batch::default() });
Expand All @@ -312,6 +328,9 @@ impl ShpSource {
let (mut skipped, mut unread_dates, mut null_text, mut boxed_off, mut not_utf8) = (0, Vec::new(), Vec::new(), 0u64, Vec::new());
let mut nonfinite = Vec::new();
let mut deleted = 0usize;
// Shapes whose coordinates aren't numbers given without geometry, and of them those under
// a box the index takes.
let (mut kept_no_number, mut kept_boxed) = (0usize, 0usize);
// The same directory, ordering, extent rules and read-only scan fallback as sampled
// reads, with no interaction row cap. The index visits records without materializing
// their directory or the complete feature collection in memory.
Expand All @@ -320,6 +339,8 @@ impl ShpSource {
crate::progress::begin(self.info.count.and_then(|n| usize::try_from(n).ok()).unwrap_or(0));
let mut each = |i: u32, place: Place| -> Result<()> {
crate::progress::step(1)?;
// A shape whose coordinates aren't numbers: whether its stored box is one the index takes.
let mut no_number: Option<bool> = None;
if u64::from(place.len) > MAX_RECORD {
skipped += 1;
return Ok(());
Expand All @@ -331,14 +352,14 @@ impl ShpSource {
return Ok(());
}
let geometry = match parse_shape(&buf) {
// A coordinate that isn't a number (NaN or infinite) has no place: the record is
// left out and counted, as one that can't be converted (#1985). One under a stored
// box the index takes ([`index::usable`]) is counted apart too, as the index framed
// it by that box; a backwards one it can't frame, and counts as unboxed instead.
// A coordinate that isn't a number (NaN or infinite) has no place: the record keeps
// its attributes without its geometry, as a whole read keeps it (#1985, #2032
// C2f), and is counted as the read gives it, or leaves it out of an area. One under
// a stored box the index takes ([`index::usable`]) is said with it, as the index
// framed it by that box; a backwards one it can't frame, and counts as unboxed.
Ok(Some(g)) if !g.is_finite() => {
skipped += 1;
boxed_off += u64::from(index::usable(record_box(&buf, u64::from(place.len))));
return Ok(());
no_number = Some(index::usable(record_box(&buf, u64::from(place.len))));
None
}
Ok(Some(g)) => {
// A box the index took as stored (one in order) that misses part of the shape.
Expand All @@ -363,7 +384,8 @@ impl ShpSource {
// As in `read`, the shape itself is checked.
// As a read by area, its box in the file's own system included, gives no rows
// without a shape.
if geometry.as_ref().map_or(if own.is_some() { !everywhere } else { req.extent.is_some() }, |g| !g.inside) {
if geometry.as_ref().map_or(by_area, |g| !g.inside) {
boxed_off += u64::from(no_number == Some(true));
return Ok(());
}
let geometry = geometry.map(|p| p.geometry);
Expand All @@ -382,6 +404,8 @@ impl ShpSource {
} else {
vec![Value::Null; self.table.columns.len()]
};
kept_no_number += usize::from(no_number.is_some());
kept_boxed += usize::from(no_number == Some(true));
chunks.push(Feature { id: u64::from(i) + 1, geometry, attrs })
};
let (matched, complete) = match numbered {
Expand All @@ -402,6 +426,7 @@ impl ShpSource {
null_text,
notes: boxed_off_note(boxed_off, false, late)
.into_iter()
.chain(kept_no_number_note(kept_no_number, kept_boxed, false))
.chain(deleted_note(deleted, false).map(crate::loss::Note::from))
.chain(nonfinite_note(&self.info.fields, &nonfinite, false))
.collect(),
Expand Down Expand Up @@ -451,17 +476,23 @@ impl FeatureSource for ShpSource {
let (mut skipped, mut unread_dates, mut null_text, mut boxed_off, mut not_utf8) = (0, Vec::new(), Vec::new(), 0u64, Vec::new());
let mut nonfinite = Vec::new();
let mut deleted = 0usize;
// Shapes whose coordinates aren't numbers given without geometry, and of them those under
// a box the index takes.
let (mut kept_no_number, mut kept_boxed) = (0usize, 0usize);
// An extent the file's system couldn't bound (#1925) has no boxes for the index to sample
// by: every record is visited, tested, and the matches sampled as they come.
let late = req.extent.is_some() && bounds.is_none();
let mut sample = super::Sampler::new(limit);
let Narrowing { everywhere, by_area, .. } = self.narrowing(req);
if late {
crate::progress::begin(self.info.count.and_then(|n| usize::try_from(n).ok()).unwrap_or(0));
}
let mut each = |i: u32, place: Place| {
if late {
crate::progress::step(1)?;
}
// A shape whose coordinates aren't numbers: whether its stored box is one the index takes.
let mut no_number: Option<bool> = None;
if u64::from(place.len) > MAX_RECORD {
skipped += 1;
return Ok(());
Expand All @@ -472,14 +503,14 @@ impl FeatureSource for ShpSource {
return Ok(());
}
let geometry = match parse_shape(&buf) {
// A coordinate that isn't a number (NaN or infinite) has no place: the record is
// left out and counted, as one that can't be converted (#1985). One under a stored
// box the index takes ([`index::usable`]) is counted apart too, as the index framed
// it by that box; a backwards one it can't frame, and counts as unboxed instead.
// A coordinate that isn't a number (NaN or infinite) has no place: the record keeps
// its attributes without its geometry, as a whole read keeps it (#1985, #2032
// C2f), and is counted as the read gives it, or leaves it out of an area. One under
// a stored box the index takes ([`index::usable`]) is said with it, as the index
// framed it by that box; a backwards one it can't frame, and counts as unboxed.
Ok(Some(g)) if !g.is_finite() => {
skipped += 1;
boxed_off += u64::from(index::usable(record_box(&buf, u64::from(place.len))));
return Ok(());
no_number = Some(index::usable(record_box(&buf, u64::from(place.len))));
None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
Ok(Some(g)) => match self.reproject.place(g, req) {
Some(placed) => Some(placed),
Expand All @@ -496,7 +527,8 @@ impl FeatureSource for ShpSource {
};
// The index's boxes are rounded outward to f32, so it can match a shape just outside. A
// shape without geometry doesn't match an extent, bounded natively or not.
if geometry.as_ref().map_or(req.extent.is_some(), |g| !g.inside) {
if geometry.as_ref().map_or(by_area, |g| !g.inside) {
boxed_off += u64::from(no_number == Some(true));
return Ok(());
}
let geometry = geometry.map(|p| p.geometry);
Expand All @@ -522,9 +554,12 @@ impl FeatureSource for ShpSource {
vec![Value::Null; self.table.columns.len()]
};
let f = Feature { id: u64::from(i) + 1, geometry, attrs };
// A late read is one by area, which gives no feature without geometry.
if late {
sample.keep(f)
} else {
kept_no_number += usize::from(no_number.is_some());
kept_boxed += usize::from(no_number == Some(true));
features.push(f)
}
Ok(())
Expand All @@ -534,7 +569,9 @@ impl FeatureSource for ShpSource {
features = std::mem::take(&mut sample.items);
(sample.seen(), sample.complete())
} else {
self.index.visit(bounds, limit, &mut each)?
// A box over the whole plane narrows nothing: every record, those without a usable
// box too, as `visit_with` reads them.
self.index.visit(bounds.filter(|_| !everywhere), limit, &mut each)?
};
Ok(Batch {
features,
Expand All @@ -548,6 +585,7 @@ impl FeatureSource for ShpSource {
// visits the whole layer, so its count is of the layer.
notes: boxed_off_note(boxed_off, !late && !complete, late)
.into_iter()
.chain(kept_no_number_note(kept_no_number, kept_boxed, !late && !complete))
.chain(deleted_note(deleted, !late && !complete).map(crate::loss::Note::from))
// The values a late read decodes are of the records its sample keeps (geometry checks
// aside), so its count is of a sample whenever the sample isn't the whole layer.
Expand Down Expand Up @@ -594,14 +632,16 @@ impl FeatureSource for ShpSource {
}
}

/// Says how many shapes a read left out for a coordinate that isn't a number while their stored
/// box is one the index takes (finite and in order, [`index::usable`]): [`record_box`] doesn't read
/// the points, so the layer's extent and its index still take in those boxes. A backwards stored
/// box over such a point isn't counted here: the index can't frame it from the points, so it's
/// left out of the extent and counted with the shapes that have no usable box. A `sampled` read visited only some of the matches, so its count is of
/// the shapes it read, not of the layer. A `late` read (an area the layer's system couldn't bound,
/// #1925) visited every record and can't place a NaN shape's stored box in the area, so its count
/// is of the whole layer, and says so.
/// Says how many shapes with a coordinate that isn't a number a read by area left out (a shape
/// without geometry is in no area) while their stored box is one the index takes (finite and in
/// order, [`index::usable`]): [`record_box`] doesn't read the points, so the layer's extent and
/// its index still take in those boxes, and the area seemed to hold them. A read that gives them
/// says them with [`kept_no_number_note`]. A backwards stored box over such a point isn't counted
/// here: the index can't frame it from the points, so it's left out of the extent and counted with
/// the shapes that have no usable box. A `sampled` read visited only some of the matches, so its
/// count is of the shapes it read, not of the layer. A `late` read (an area the layer's system
/// couldn't bound, #1925) visited every record and can't place a NaN shape's stored box in the
/// area, so its count is of the whole layer, and says so.
fn boxed_off_note(n: u64, sampled: bool, late: bool) -> Vec<crate::loss::Note> {
if n == 0 {
return Vec::new();
Expand All @@ -616,11 +656,34 @@ fn boxed_off_note(n: u64, sampled: bool, late: bool) -> Vec<crate::loss::Note> {
vec![crate::loss::Note::loss(
crate::loss::LossCode::DroppedFeature,
format!(
"{what} have a coordinate that isn't a number under a bounding box that is one: they're left out of the read, but the layer's extent and its index still take in their stored boxes"
"{what} have a coordinate that isn't a number under a bounding box that is one: they have no geometry, so the read leaves them out, but the layer's extent and its index still take in their stored boxes"
),
)]
}

/// Says how many shapes with a coordinate that isn't a number a read gave without geometry,
/// keeping their attributes, in the words a whole read says it with
/// ([`crate::io`]'s Shapefile reader), and of them how many lie under a stored box the index
/// takes (`boxed`, [`boxed_off_note`]); of those read when the read is `sampled`.
fn kept_no_number_note(n: usize, boxed: usize, sampled: bool) -> Option<crate::loss::Note> {
let what = match n {
0 => return None,
n if sampled => format!("{n} of the shapes read (a sample of the layer, which may hold more)"),
n => format!("{n} shape(s)"),
};
Some(crate::loss::Note::loss(
crate::loss::LossCode::DroppedGeometry,
format!(
"{what} have a coordinate that isn't a number (NaN or infinite): their features are kept without geometry{}",
match boxed {
0 => String::new(),
b if b == n => ", but the layer's extent and its index still take in their stored boxes".to_string(),
b => format!(", but the layer's extent and its index still take in the stored boxes of {b} of them"),
}
),
))
}

/// The numbers in the `.dbf` that aren't finite, read as empty ([`crate::io::nonfinite_note`]); a
/// `sampled` read counts only the records it read, and says so, as [`deleted_note`] does.
fn nonfinite_note(fields: &[crate::Field], counts: &[usize], sampled: bool) -> Option<crate::loss::Note> {
Expand Down
Loading
Loading