Skip to content
Merged
2 changes: 1 addition & 1 deletion crates/data/src/citygml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ fn read_with(file_name: &str, bytes: &[u8], given: Option<Crs>, keep_named: bool
}
let Reading { counts, mut built, rows, reach, mut said, relations, .. } = reading;
said.object_links += relate(&mut built, relations);
let mut notes = Vec::new();
let mut notes = crate::loss::Notes::new();
let crs = coordinate_system(file_name, given, named, index.vertical_named, &reach, &mut notes)?;
if let Some(other) = &index.other {
notes.push(format!(
Expand Down
2 changes: 1 addition & 1 deletion crates/data/src/cityjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn read_with(file_name: &str, bytes: &[u8], given: Option<Crs>, keep_named: bool
for v in &mut vertices {
*v = [v[0] * s[0] + t[0], v[1] * s[1] + t[1], v[2] * s[2] + t[2]];
}
let mut notes = Vec::new();
let mut notes = crate::loss::Notes::new();
// A compound URN is read by its horizontal part; its vertical part, said when its heights
// aren't metres upward, gives the heights' unit.
let system = named_system(doc.metadata.as_ref());
Expand Down
13 changes: 8 additions & 5 deletions crates/data/src/citymodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ pub(crate) fn coordinate_system(
named: Option<&str>,
vertical_named: bool,
reach: &Reach,
notes: &mut Vec<String>,
notes: &mut crate::loss::Notes,
) -> Result<Crs> {
let crs = match (given, named) {
(Some(crs), named) => {
Expand All @@ -489,7 +489,7 @@ pub(crate) fn coordinate_system(
DataError::Format(format!("{file_name}: it names a coordinate system RustGIS doesn't know ({system}); give one with the crs parameter of layer.addData (from the command line, Python or the AI Assistant)"))
})?,
(None, None) if reach.within(180.0, 90.0) => {
notes.push("the file names no coordinate system: its coordinates are read as longitude/latitude".into());
notes.push(crate::loss::Note::loss(crate::loss::LossCode::AssumedCrs, "the file names no coordinate system: its coordinates are read as longitude/latitude"));
Comment thread
Mavrick-F marked this conversation as resolved.
Resolved
return Ok(Crs::LonLat);
}
(None, None) => {
Expand All @@ -502,7 +502,7 @@ pub(crate) fn coordinate_system(
return Err(DataError::Format(format!("{file_name}: {} is a geocentric coordinate system, which isn't a map's", crs.label())));
}
if crs == Crs::LonLat && !reach.within(180.5, 90.5) {
notes.push("coordinates are not longitude/latitude; RustGIS reads them as WGS 84 and they may draw out of place".into());
notes.push("coordinates are not longitude/latitude; RustGIS reads them as WGS 84 and they may draw out of place");
}
if let Some(unit) = crs.height_unit_not_metre().filter(|_| !vertical_named) {
notes.push(format!(
Expand Down Expand Up @@ -564,7 +564,7 @@ pub(crate) fn finish(
mut rows: Vec<Row>,
mut built: Vec<Built>,
mut counts: Counts,
notes: Vec<String>,
notes: crate::loss::Notes,
crs: Crs,
) -> Result<Vec<(Dataset, ReadReport)>> {
// Converted once every object is in: a reader that streams its objects knows its system, and
Expand Down Expand Up @@ -1082,7 +1082,10 @@ fn layers(stem: &str, rows: Vec<Row>, built: &[Built], crs: Crs, counts: &mut Co
if total > MAX_LAYERS
&& let Some((_, report)) = out.first_mut()
{
report.messages.push(format!("{} of {total} layers are left out: only the first {MAX_LAYERS} are read", total - MAX_LAYERS));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedOverLimit,
format!("{} of {total} layers are left out: only the first {MAX_LAYERS} are read", total - MAX_LAYERS),
));
}
out
}
Expand Down
14 changes: 7 additions & 7 deletions crates/data/src/dbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ fn ldid_code_page(ldid: u8) -> Option<u16> {
})
}

pub(crate) fn encoding(cpg: Option<&[u8]>, ldid: u8, notes: &mut Vec<String>) -> DbfEncoding {
pub(crate) fn encoding(cpg: Option<&[u8]>, ldid: u8, notes: &mut crate::loss::Notes) -> DbfEncoding {
if let Some(bytes) = cpg {
let label = if bytes.len() <= MAX_CPG_BYTES { std::str::from_utf8(bytes).unwrap_or("") } else { "" };
let label = label.trim().trim_start_matches('\u{feff}').trim();
if let Some(encoding) = DbfEncoding::from_name(label) {
return encoding;
}
notes.push("Shapefile: unsupported or empty .cpg; using the .dbf code page, or UTF-8 if unavailable".into());
notes.push("Shapefile: unsupported or empty .cpg; using the .dbf code page, or UTF-8 if unavailable");
}
// No language driver: UTF-8, as it always was, without a note.
if ldid == 0 {
Expand Down Expand Up @@ -418,7 +418,7 @@ pub(crate) fn date(field: &[u8]) -> std::result::Result<Option<crate::date::Date
}

/// A code-page label should be a few bytes. Bound reads even for a hostile sidecar.
pub(crate) fn read_cpg(path: &std::path::Path, notes: &mut Vec<String>) -> Option<Vec<u8>> {
pub(crate) fn read_cpg(path: &std::path::Path, notes: &mut crate::loss::Notes) -> Option<Vec<u8>> {
match try_read_cpg(path) {
Ok(bytes) => bytes,
Err(e) => {
Expand Down Expand Up @@ -583,10 +583,10 @@ mod tests {

#[test]
fn a_page_rustgis_cant_decode_is_named_in_the_note() {
let mut notes = Vec::new();
let mut notes = crate::loss::Notes::new();
assert!(matches!(encoding(None, 0x6C, &mut notes), DbfEncoding::Utf8));
assert_eq!(notes, ["Shapefile: unsupported .dbf code page 0x6C (code page 863, DOS French Canadian); using UTF-8"]);
let mut notes = Vec::new();
let mut notes = crate::loss::Notes::new();
encoding(None, 0xFF, &mut notes);
assert_eq!(notes, ["Shapefile: unsupported .dbf code page 0xFF; using UTF-8"]);
}
Expand All @@ -595,7 +595,7 @@ mod tests {
/// and with a code page that can't be decoded; a code page that is decoded is named by it.
#[test]
fn utf8_is_what_text_is_read_as_unless_another_encoding_is_named() {
let read = |cpg: Option<&[u8]>, ldid| encoding(cpg, ldid, &mut Vec::new()).name();
let read = |cpg: Option<&[u8]>, ldid| encoding(cpg, ldid, &mut crate::loss::Notes::new()).name();
for (cpg, ldid) in [(None, 0), (None, 0xF0), (Some(b"UTF-8".as_slice()), 0x03), (Some(b"65001".as_slice()), 0), (None, 0x6C)] {
assert_eq!(read(cpg, ldid), "UTF-8");
}
Expand All @@ -613,7 +613,7 @@ mod tests {
/// aren't. A DOS page gives every byte a character.
#[test]
fn bytes_that_cant_be_read_are_those_the_declared_decoder_replaces() {
let page = |cpg: &[u8]| encoding(Some(cpg), 0, &mut Vec::new());
let page = |cpg: &[u8]| encoding(Some(cpg), 0, &mut crate::loss::Notes::new());
let (sjis, big5, utf8) = (page(b"932"), page(b"950"), page(b"UTF-8"));
assert!(sjis.malformed(b"X\x82") && sjis.malformed(b"\x82 ") && sjis.malformed(b"\xff"));
assert!(!sjis.malformed(b"\x93\x8c\x8b\x9e") && !sjis.malformed(b"plain") && !sjis.malformed(b""));
Expand Down
47 changes: 30 additions & 17 deletions crates/data/src/dxf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,26 +1636,36 @@ pub fn read(file_name: &str, bytes: &[u8], opts: &Options) -> Result<Vec<(Datase
report.messages.push(why);
}
if opts.crs.is_none() {
report
.messages
.push("a DXF drawing doesn't say its coordinate system (give \"crs\", such as EPSG:2263): coordinates are kept as they are");
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
"a DXF drawing doesn't say its coordinate system (give \"crs\", such as EPSG:2263): coordinates are kept as they are",
));
}
if out.capped {
report.messages.push(format!("the drawing makes more than {MAX_FEATURES} features: the rest are left out"));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedOverLimit,
format!("the drawing makes more than {MAX_FEATURES} features: the rest are left out"),
));
}
if out.vertex_capped {
report.messages.push(format!("the drawing's features hold more than {MAX_TOTAL_VERTICES} coordinates: the rest are left out"));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedOverLimit,
format!("the drawing's features hold more than {MAX_TOTAL_VERTICES} coordinates: the rest are left out"),
));
}
if out.paper > 0 {
report.messages.push(format!(
"{} entities of the drawing's layouts (paper space) were left out; add the drawing with \"paperSpace\" to draw them",
out.paper
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedFeature,
format!(
"{} entities of the drawing's layouts (paper space) were left out; add the drawing with \"paperSpace\" to draw them",
out.paper
),
));
}
if out.blocked > 0 {
report.messages.push(format!(
"{} inserted blocks (图块) were left out; add the drawing with \"blocks\": true to draw their contents",
out.blocked
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedFeature,
format!("{} inserted blocks (图块) were left out; add the drawing with \"blocks\": true to draw their contents", out.blocked),
));
}
if out.trimmed {
Expand Down Expand Up @@ -1871,7 +1881,7 @@ mod tests {
]);
let layers = read("a.dxf", &bytes, &Options { crs: Some(Crs::LonLat), ..Options::default() }).unwrap();
assert_eq!(layers[0].0.features[0].geometry, Some(Geometry::Line(vec![vec![Coord::new(0., 0.), Coord::new(2., 2.)]])));
assert_eq!(layers[0].1.messages, ["1 vertex without Y was left out of spline paths"]);
assert_eq!(layers[0].1.messages.texts().collect::<Vec<_>>(), ["1 vertex without Y was left out of spline paths"]);
assert_eq!(layers[0].1.skipped, 0);
}
}
Expand Down Expand Up @@ -1928,7 +1938,10 @@ mod tests {
Some(Geometry::Polygon(vec![vec![vec![Coord::new(0., 0.), Coord::new(2., 0.), Coord::new(0., 2.), Coord::new(0., 0.)]]]))
);
let subject = if count == 1 { "ring was" } else { "rings were" };
assert_eq!(layers[0].1.messages, [format!("{count} short HATCH boundary {subject} left out of polygons")]);
assert_eq!(
layers[0].1.messages.texts().collect::<Vec<_>>(),
[format!("{count} short HATCH boundary {subject} left out of polygons")]
);
assert_eq!(layers[0].1.skipped, 0);
}
}
Expand Down Expand Up @@ -2035,7 +2048,7 @@ mod tests {
assert_eq!(layers.iter().map(|(d, _)| d.features.len()).sum::<usize>(), 1);
assert_eq!(layers[0].1.skipped, 1);
assert_eq!(
layers[0].1.messages,
layers[0].1.messages.texts().collect::<Vec<_>>(),
["1 entities couldn't be read or placed (missing blocks, blocks nested too deep or in a cycle) and were left out"]
);
}
Expand All @@ -2048,7 +2061,7 @@ mod tests {
let layers = read("a.dxf", text.as_bytes(), &Options { layer: Some("keep"), ..opts }).unwrap();
assert_eq!(layers[0].0.features.len(), 2);
assert_eq!(
layers[0].1.messages,
layers[0].1.messages.texts().collect::<Vec<_>>(),
[
"1 entities of the drawing's layouts (paper space) were left out; add the drawing with \"paperSpace\" to draw them",
"2 vertices without Y were left out of spline paths",
Expand Down Expand Up @@ -2959,7 +2972,7 @@ mod tests {
let kept = layers[0].0.features.len();
assert_eq!(kept, MAX_TOTAL_VERTICES / 300);
assert_eq!(
layers[0].1.messages,
layers[0].1.messages.texts().collect::<Vec<_>>(),
[
format!("the drawing's features hold more than {MAX_TOTAL_VERTICES} coordinates: the rest are left out"),
format!("{kept} unreadable POLYLINE vertices were left out of their lines"),
Expand Down Expand Up @@ -3303,7 +3316,7 @@ mod tests {
);
assert_eq!(report.skipped, 0);
assert_eq!(
report.messages[0],
report.messages[0].as_str(),
"a DXF drawing doesn't say its coordinate system (give \"crs\", such as EPSG:2263): coordinates are kept as they are"
);
assert_eq!(report.messages.len(), if end == "9" { 2 } else { 1 });
Expand Down
16 changes: 11 additions & 5 deletions crates/data/src/esripbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ impl Losses {
pub fn messages(&self) -> crate::loss::Notes {
let mut out = crate::loss::Notes::new();
if self.no_crs {
out.push("the answer names no coordinate system; coordinates are read as longitude/latitude");
out.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
"the answer names no coordinate system; coordinates are read as longitude/latitude",
));
}
if self.unreadable > 0 {
out.push(format!("{} values don't fit their field's type and are left empty", self.unreadable));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is changed by the PR, but three confirmed losses in it stay plain:

  • 167: "values don't fit their field's type and are left empty" is a value loss, which unread_dates_note codes as DroppedValue.
  • 176: "polygon rings have no area and are left out" is DroppedRing, the code the GML reader already uses for rings left out.
  • 170: values with bytes replaced by U+FFFD are ChangedEncoding.

A strict run passes all three today. Confidence: medium. Category: bug
Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, these three are losses with no code. They were plain before this PR, which doesn't set out to code every notice, so they go in the next #2371 batch: unreadable values as dropped.value, rings without area as dropped.ring, U+FFFD values as changed.encoding.

Expand Down Expand Up @@ -200,9 +203,9 @@ pub fn read(bytes: &[u8], name: &str) -> Result<(Dataset, ReadReport)> {
let mut page = read_page(bytes, name)?;
page.report.messages.extend(page.losses.messages());
if page.exceeded_transfer_limit {
page.report.messages.push(format!(
"the service sent {} features only (its record limit); the rest aren't in this answer",
page.dataset.features.len()
page.report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedOverLimit,
format!("the service sent {} features only (its record limit); the rest aren't in this answer", page.dataset.features.len()),
));
}
Ok((page.dataset, page.report))
Expand Down Expand Up @@ -337,7 +340,10 @@ pub fn read_page_within(bytes: &[u8], name: &str, limit: usize) -> Result<Page>
|| esri_wkt_over_code(code, wkt.as_deref()).is_some()
|| !wkt.as_deref().is_some_and(crate::raster::wkt::parses))
{
report.messages.push("the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD");
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::ChangedEncoding,
"the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD",
));
}
let said_before = report.messages.len();
let (crs, to_lonlat) = crate::filegdb::esri_crs(code, wkt.as_deref(), &mut report)?;
Expand Down
26 changes: 21 additions & 5 deletions crates/data/src/esripbf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,22 @@ fn an_unknown_coordinate_system_is_said() {
let bytes = answer(&[message(8, &number(1, 999_999)), transform(false, 1.0, 0.0, 0.0), feature(&[geometry(&[], &[1, 2])])]);
let (ds, report) = read(&bytes, "t").unwrap();
assert!(ds.crs.is_none());
assert_eq!(report.messages, ["coordinate system 999999 can't be used; coordinates are read as longitude/latitude"]);
assert_eq!(
report.messages.texts().collect::<Vec<_>>(),
["coordinate system 999999 can't be used; coordinates are read as longitude/latitude"]
);
}

#[test]
fn an_answer_without_a_coordinate_system_is_said_to_be_read_as_longitude_latitude() {
let bytes = answer(&[transform(false, 1.0, 0.0, 0.0), feature(&[geometry(&[], &[1, 2])])]);
let (ds, report) = read(&bytes, "t").unwrap();
assert_eq!(ds.features[0].geometry, Some(Geometry::Point(c(1.0, 2.0))));
assert_eq!(report.messages, ["the answer names no coordinate system; coordinates are read as longitude/latitude"]);
assert_eq!(
report.messages.texts().collect::<Vec<_>>(),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
["the answer names no coordinate system; coordinates are read as longitude/latitude"]
);
assert_eq!(report.messages[0].loss, Some(crate::loss::LossCode::AssumedCrs));
}

#[test]
Expand Down Expand Up @@ -326,7 +333,10 @@ fn a_page_cut_at_the_record_limit_says_so() {
let bytes = answer(&[number(9, 1), field("OBJECTID", OID), feature(&[uint(1)])]);
assert!(read_page(&bytes, "t").unwrap().exceeded_transfer_limit);
let (_, report) = read(&bytes, "t").unwrap();
assert_eq!(report.messages, ["the service sent 1 features only (its record limit); the rest aren't in this answer"]);
assert_eq!(
report.messages.texts().collect::<Vec<_>>(),
["the service sent 1 features only (its record limit); the rest aren't in this answer"]
);
}

#[test]
Expand Down Expand Up @@ -494,15 +504,21 @@ fn a_wkts_own_shift_wins_over_its_wkid() {
assert!(!crate::raster::wkt::parses(&String::from_utf8_lossy(&broken)));
let (p, messages) = at(&page([number(1, 27700), message(5, &broken)].concat()));
assert!((p.x - by_code.x).abs() < 1e-9 && (p.y - by_code.y).abs() < 1e-9, "{p:?} {by_code:?}");
assert_eq!(messages, ["the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD"]);
assert_eq!(
messages.texts().collect::<Vec<_>>(),
["the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD"]
);
// A non-EPSG Esri code: its WKT is read only for another body's system, so a byte that
// breaks the WKT is said, and a byte in a name is not. The WKT's shift (OSGB 1936's) is set
// aside for the code's, which is said.
assert!(Crs::from_epsg(102033).is_none() && crate::filegdb::crs_of_code(102033).is_some());
let albers = crate::filegdb::crs_of_code(102033).unwrap().transformer().unwrap().to_lonlat(xy).unwrap();
let (p, messages) = at(&page([number(1, 102033), message(5, &broken)].concat()));
assert!((p.x - albers.x).abs() < 1e-9 && (p.y - albers.y).abs() < 1e-9, "{p:?} {albers:?}");
assert_eq!(messages, ["the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD"]);
assert_eq!(
messages.texts().collect::<Vec<_>>(),
["the coordinate system's WKT isn't valid UTF-8; the bytes that can't be read are replaced with U+FFFD"]
);
let (p, messages) = at(&page([number(1, 102033), message(5, &not_utf8)].concat()));
assert!((p.x - albers.x).abs() < 1e-9 && (p.y - albers.y).abs() < 1e-9, "{p:?} {albers:?}");
assert_eq!(
Expand Down
15 changes: 12 additions & 3 deletions crates/data/src/filegdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ pub(crate) fn esri_crs(code: Option<u32>, wkt: Option<&str>, report: &mut ReadRe
Ok(t) if c != Crs::LonLat => (Some(c), Some(t)),
Ok(_) => (None, None),
Err(e) => {
report.messages.push(format!("coordinate system: {e}; coordinates are read as longitude/latitude"));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
format!("coordinate system: {e}; coordinates are read as longitude/latitude"),
));
(None, None)
}
}
Expand All @@ -270,7 +273,10 @@ pub(crate) fn esri_crs(code: Option<u32>, wkt: Option<&str>, report: &mut ReadRe
if let Some(refused) = planetary::refused_wkt(wkt, &e) {
return Err(DataError::Format(format!("coordinate system: {refused}")));
}
report.messages.push(format!("coordinate system: {e}; coordinates are read as longitude/latitude"));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::AssumedCrs,
format!("coordinate system: {e}; coordinates are read as longitude/latitude"),
));
(None, None)
}
},
Expand Down Expand Up @@ -433,7 +439,10 @@ fn read_table(name: &str, t: &[u8], x: &[u8], wkid: Option<u32>) -> Result<Optio
report.unread_dates = crate::source::unread_dates_note(&ds.fields, &unread_dates);
report.messages.extend(report.unread_dates.clone());
if curves > 0 {
report.messages.push(format!("{curves} curved segments are drawn as straight lines"));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::ApproximatedCurve,
format!("{curves} curved segments are drawn as straight lines"),
));
}
if let Some(c) = crs.filter(|_| to_lonlat.is_some()) {
report.messages.extend(crate::io::beyond_lonlat_note("features", beyond, c));
Expand Down
Loading
Loading