Repository navigation
fix(data): read GeoParquet 2.0 and native Parquet geometry in the system the file says #3408
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
Changes from all commits
3e6ced9
85c9efa
233abc8
79c7f71
cd545b7
2f114f3
9f244b4
a01fb25
81192b1
1d3594f
3a32bfa
1c3a683
cb6328d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3528,17 +3528,26 @@ fn a_csv_export_takes_x_and_y_in_a_system_asked_for() { | |
| } | ||
|
|
||
| /// A catalog index as AlphaEarth's is (a `bbox` struct, a nested `assets.data.href`), and patch | ||
| /// embeddings as Earth Index's are (a fixed-size list of floats a point), in row groups of 50. | ||
| fn write_index_and_vectors() -> Vec<u8> { | ||
| /// embeddings as Earth Index's are (a fixed-size list of floats a point), in row groups of 50, with | ||
| /// `geo` metadata. | ||
| fn write_index_and_vectors(geo: &str, native: Option<::parquet::basic::LogicalType>) -> Vec<u8> { | ||
| write_index_and_vectors_with(geo, native, |_| {}) | ||
| } | ||
|
|
||
| /// As [`write_index_and_vectors`], with the first point's WKB changed. | ||
| fn write_index_and_vectors_with(geo: &str, native: Option<::parquet::basic::LogicalType>, first: impl FnOnce(&mut Vec<u8>)) -> Vec<u8> { | ||
| use arrow_array::builder::{FixedSizeListBuilder, Float32Builder}; | ||
| use arrow_array::{BinaryArray, Float64Array, RecordBatch, StringArray, StructArray}; | ||
| use arrow_schema::{DataType, Field as AField, Fields, Schema}; | ||
| let pts = points(); | ||
| let wkbs: Vec<Vec<u8>> = pts.iter().map(|(c, ..)| wkb::encode(&Geometry::Point(*c))).collect(); | ||
| let mut wkbs: Vec<Vec<u8>> = pts.iter().map(|(c, ..)| wkb::encode(&Geometry::Point(*c))).collect(); | ||
| first(&mut wkbs[0]); | ||
| let bbox_fields: Fields = | ||
| ["xmin", "ymin", "xmax", "ymax"].map(|n| Arc::new(AField::new(n, DataType::Float64, false))).into_iter().collect(); | ||
| let coord = | ||
| |f: fn(&Coord) -> f64| Arc::new(Float64Array::from(pts.iter().map(|(c, ..)| f(c)).collect::<Vec<_>>())) as arrow_array::ArrayRef; | ||
| let coord = |f: fn(&Coord) -> f64| { | ||
| Arc::new(Float64Array::from(pts.iter().map(|(c, ..)| f(c) * if geo.contains("metres") { 1e5 } else { 1.0 }).collect::<Vec<_>>())) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This helper chooses the fixture's coordinate scale and whether statistics are written by searching the Category: quality
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left for the release quality pass: a test helper's arguments. |
||
| as arrow_array::ArrayRef | ||
| }; | ||
| let bbox = StructArray::new(bbox_fields.clone(), vec![coord(|c| c.x), coord(|c| c.y), coord(|c| c.x), coord(|c| c.y)], None); | ||
| let href = | ||
| Arc::new(StringArray::from(pts.iter().map(|p| format!("s3://bucket/{}.tiff", p.1)).collect::<Vec<_>>())) as arrow_array::ArrayRef; | ||
|
|
@@ -3566,21 +3575,43 @@ fn write_index_and_vectors() -> Vec<u8> { | |
| Arc::new(assets), | ||
| Arc::new(emb), | ||
| ]; | ||
| let geo = r#"{"version":"1.0.0","primary_column":"geometry","columns":{"geometry":{"encoding":"WKB","geometry_types":["Point"]}}}"#; | ||
| let props = ::parquet::file::properties::WriterProperties::builder() | ||
| .set_max_row_group_row_count(Some(50)) | ||
| .set_statistics_enabled(if geo.contains("no-statistics") { | ||
| ::parquet::file::properties::EnabledStatistics::None | ||
| } else { | ||
| ::parquet::file::properties::EnabledStatistics::Chunk | ||
| }) | ||
| .set_key_value_metadata(Some(vec![::parquet::file::metadata::KeyValue::new("geo".to_string(), geo.to_string())])) | ||
| .build(); | ||
| let mut out = Vec::new(); | ||
| let mut w = ::parquet::arrow::ArrowWriter::try_new(&mut out, schema.clone(), Some(props)).unwrap(); | ||
| // The arrow writer gives a geometry column no native type, so it is put in the schema. | ||
| let parquet_schema = ::parquet::arrow::ArrowSchemaConverter::new().convert(&schema).unwrap(); | ||
| let fields = parquet_schema.root_schema().get_fields().iter().map(|f| match native.clone().filter(|_| f.name() == "geometry") { | ||
| Some(ty) => Arc::new( | ||
| ::parquet::schema::types::Type::primitive_type_builder("geometry", ::parquet::basic::Type::BYTE_ARRAY) | ||
| .with_repetition(::parquet::basic::Repetition::REQUIRED) | ||
| .with_logical_type(Some(ty)) | ||
| .build() | ||
| .unwrap(), | ||
| ), | ||
| None => f.clone(), | ||
| }); | ||
| let root = ::parquet::schema::types::Type::group_type_builder("schema").with_fields(fields.collect()).build().unwrap(); | ||
| let described = ::parquet::schema::types::SchemaDescriptor::new(Arc::new(root)); | ||
| let options = ::parquet::arrow::arrow_writer::ArrowWriterOptions::new().with_properties(props).with_parquet_schema(described); | ||
| let mut w = ::parquet::arrow::ArrowWriter::try_new_with_options(&mut out, schema.clone(), options).unwrap(); | ||
| w.write(&RecordBatch::try_new(schema, cols).unwrap()).unwrap(); | ||
| w.close().unwrap(); | ||
| out | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_columns_and_vectors_by_address() { | ||
| let bytes = write_index_and_vectors(); | ||
| let bytes = write_index_and_vectors( | ||
| r#"{"version":"1.0.0","primary_column":"geometry","columns":{"geometry":{"encoding":"WKB","geometry_types":["Point"]}}}"#, | ||
| None, | ||
| ); | ||
| let url = "https://test.example/index.parquet"; | ||
| remote::testing::serve(url, bytes.clone()); | ||
| let e = Extent::new(10.0, 10.0, 40.0, 30.0); | ||
|
|
@@ -3614,6 +3645,121 @@ fn nested_columns_and_vectors_by_address() { | |
| } | ||
| } | ||
|
|
||
| /// Points and rows found by place are in the file's own system, and refused when the file doesn't | ||
| /// say it. | ||
| #[test] | ||
| fn vectors_and_index_rows_by_address_keep_to_the_files_system() { | ||
| let geo = |crs: &str| { | ||
| format!( | ||
| r#"{{"version":"2.0.0","primary_column":"geometry","columns":{{"geometry":{{"encoding":"WKB","geometry_types":["Point"],"crs":{crs}}}}}}}"# | ||
| ) | ||
| }; | ||
| // Degrees' numbers as metres: all of them within a few metres of 0, 0. | ||
| let mercator = "https://test.example/mercator.parquet"; | ||
| remote::testing::serve(mercator, write_index_and_vectors(&geo(r#"{"id":{"authority":"EPSG","code":3857}}"#), None)); | ||
| let near = Extent::new(-0.01, -0.01, 0.01, 0.01); | ||
| let v = vectors_url_blocking(mercator, near, "embedding".into(), (100, 10_000)).unwrap(); | ||
| assert_eq!(v.points.len(), points().len()); | ||
| assert!(v.points.iter().all(|(x, y)| near.contains(Coord::new(*x, *y)))); | ||
| let far = Extent::new(10.0, 10.0, 40.0, 30.0); | ||
| assert!(vectors_url_blocking(mercator, far, "embedding".into(), (100, 10_000)).unwrap().points.is_empty()); | ||
| let refused = query_url_blocking(mercator, vec![], Request { extent: Some(near), limit: 10, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("not WGS 84 longitude/latitude"), "{refused}"); | ||
| for (name, more, why) in [ | ||
| ("nad27", r#"{"id":{"authority":"OGC","code":"CRS27"}}"#, "not WGS 84"), | ||
| ("zm", r#"{"id":{"authority":"EPSG","code":4326}},"bbox":[0,0,0,1,1,1]"#, "Z"), | ||
| ] { | ||
| let url = format!("https://test.example/{name}.parquet"); | ||
| remote::testing::serve(&url, write_index_and_vectors(&geo(more), None)); | ||
| let refused = query_url_blocking(&url, vec![], Request { extent: Some(far), limit: 10, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains(why), "{refused}"); | ||
| } | ||
| let dated = "https://test.example/dated.parquet"; | ||
| remote::testing::serve(dated, write_index_and_vectors(&geo(r#"{"id":{"authority":"EPSG","code":4326}},"epoch":2021.5"#), None)); | ||
| let refused = query_url_blocking(dated, vec![], Request { extent: Some(far), limit: 10, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("epoch"), "{refused}"); | ||
| let unknown = "https://test.example/unknown.parquet"; | ||
| remote::testing::serve(unknown, write_index_and_vectors(&geo("null"), None)); | ||
| // Geography points have no edges to approximate, and a native type gives the system. | ||
| let native = |crs: &str| Some(::parquet::basic::LogicalType::geography(Some(crs.into()), None)); | ||
| let geography = "https://test.example/geography.parquet"; | ||
| remote::testing::serve(geography, write_index_and_vectors("{}", native("OGC:CRS84"))); | ||
| assert_eq!(vectors_url_blocking(geography, far, "embedding".into(), (100, 10_000)).unwrap().points.len(), in_box(&far)); | ||
| let native_mercator = "https://test.example/native-mercator.parquet"; | ||
| remote::testing::serve( | ||
| native_mercator, | ||
| write_index_and_vectors("{}", Some(::parquet::basic::LogicalType::geometry(Some("EPSG:3857".into())))), | ||
| ); | ||
| assert_eq!(vectors_url_blocking(native_mercator, near, "embedding".into(), (100, 10_000)).unwrap().points.len(), points().len()); | ||
| // A point with a height, and one with no place, are refused rather than lost without a word. | ||
| let wgs84 = geo(r#"{"id":{"authority":"EPSG","code":4326}}"#); | ||
| let height = "https://test.example/height.parquet"; | ||
| let with_z = |b: &mut Vec<u8>| { | ||
| // The ISO code of a Point Z, and its Z after x and y. | ||
| b[1..5].copy_from_slice(&1001u32.to_le_bytes()); | ||
| b.extend(5f64.to_le_bytes()); | ||
| }; | ||
| remote::testing::serve(height, write_index_and_vectors_with(&wgs84, None, with_z)); | ||
| let refused = vectors_url_blocking(height, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("1 of its points have Z or M values"), "{refused}"); | ||
| let refused = query_url_blocking(height, vec![], Request { extent: None, limit: 1000, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("Z or M"), "{refused}"); | ||
| // Points of a geography have no edges to say anything of; a line has. | ||
| assert_eq!( | ||
| query_url_blocking(geography, vec![], Request { extent: Some(far), limit: 1000, own: None }).unwrap().1.features.len(), | ||
| in_box(&far) | ||
| ); | ||
| let curved = "https://test.example/curved.parquet"; | ||
| let line = |b: &mut Vec<u8>| *b = wkb::encode(&Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]])); | ||
| remote::testing::serve(curved, write_index_and_vectors_with("{}", native("OGC:CRS84"), line)); | ||
| let refused = query_url_blocking(curved, vec![], Request { extent: None, limit: 1000, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("straight lines"), "{refused}"); | ||
| let refused = vectors_url_blocking(curved, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("1 of its geometries aren't single points"), "{refused}"); | ||
| let lost = "https://test.example/lost.parquet"; | ||
| let nowhere = |b: &mut Vec<u8>| b[5..13].copy_from_slice(&f64::INFINITY.to_le_bytes()); | ||
| // Boxes in metres with no statistics to say so as the file opens, and the same in degrees. | ||
| for (writer, refused) in [("no-statistics metres", true), ("no-statistics", false)] { | ||
| let url = format!("https://test.example/{}.parquet", writer.replace(' ', "-")); | ||
| let more = format!(r#"{{"id":{{"authority":"EPSG","code":4326}}}},"writer":"{writer}""#); | ||
| remote::testing::serve(&url, write_index_and_vectors(&geo(&more), None)); | ||
| let read = query_url_blocking(&url, vec![], Request { extent: Some(far), limit: 1000, own: None }); | ||
| match refused { | ||
| true => assert!(read.unwrap_err().to_string().contains("not longitude/latitude")), | ||
| false => assert_eq!(read.unwrap().1.features.len(), in_box(&far)), | ||
| } | ||
| } | ||
| let east = "https://test.example/one-point-in-metres.parquet"; | ||
| let beyond = |b: &mut Vec<u8>| b[5..13].copy_from_slice(&500_000f64.to_le_bytes()); | ||
| remote::testing::serve(east, write_index_and_vectors_with(&geo(r#"{"id":{"authority":"EPSG","code":4326}}"#), None, beyond)); | ||
| let refused = query_url_blocking(east, vec![], Request { extent: None, limit: 1000, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("not longitude/latitude"), "{refused}"); | ||
| // A box beyond degrees in a file that says it is in degrees. | ||
| let metres = "https://test.example/metres.parquet"; | ||
| let bounded = geo(r#"{"id":{"authority":"EPSG","code":4326}},"bbox":[222146,3893937,222147,3893938]"#); | ||
| remote::testing::serve(metres, write_index_and_vectors(&bounded, None)); | ||
| let refused = query_url_blocking(metres, vec![], Request { extent: Some(far), limit: 10, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("not longitude/latitude"), "{refused}"); | ||
| let polar = "https://test.example/beyond-the-pole.parquet"; | ||
| let beyond = |b: &mut Vec<u8>| b[13..21].copy_from_slice(&95f64.to_le_bytes()); | ||
| remote::testing::serve(polar, write_index_and_vectors_with(&wgs84, None, beyond)); | ||
| let east = "https://test.example/beyond-the-edge.parquet"; | ||
| let beyond = |b: &mut Vec<u8>| b[5..13].copy_from_slice(&500_000f64.to_le_bytes()); | ||
| remote::testing::serve(east, write_index_and_vectors_with(&wgs84, None, beyond)); | ||
| let refused = vectors_url_blocking(east, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("1 of its rows have no place"), "{refused}"); | ||
| let refused = vectors_url_blocking(polar, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("1 of its rows have no place"), "{refused}"); | ||
| let utm = geo(r#"{"id":{"authority":"EPSG","code":32616}}"#); | ||
| remote::testing::serve(lost, write_index_and_vectors_with(&utm, None, nowhere)); | ||
| let refused = vectors_url_blocking(lost, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("1 of its rows have no place"), "{refused}"); | ||
| let refused = vectors_url_blocking(unknown, far, "embedding".into(), (100, 10_000)).err().unwrap().to_string(); | ||
| assert!(refused.contains("coordinate system is unknown"), "{refused}"); | ||
| let refused = query_url_blocking(unknown, vec![], Request { extent: Some(far), limit: 10, own: None }).unwrap_err().to_string(); | ||
| assert!(refused.contains("coordinate system is unknown"), "{refused}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn shapefile_complete_chunks_keep_all_rows_order_attributes_and_budget() { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
|
|
@@ -3827,6 +3973,10 @@ fn geopackage_flatgeobuf_and_geoparquet_visit_what_a_whole_read_gives() { | |
| let error = capped.visit_chunks(None, 4096, &mut |_| Ok(())).unwrap_err().to_string(); | ||
| assert!(error.contains("row group 0 has 50 rows, more than the 40"), "{error}"); | ||
| // A capped row group outside the extent is passed over, not refused. | ||
| // A catalog's read refuses the group, whichever of its rows would match. | ||
| let exact = parquet::ParquetSource::open_path(&pq, "grid").unwrap().with_group_rows(40).exact(); | ||
| let world = Request { extent: Some(Extent::new(-180.0, -90.0, 180.0, 90.0)), limit: 1000, own: None }; | ||
| assert!(exact.read(&world).unwrap_err().to_string().contains("more than the 40 read from one")); | ||
| let capped = parquet::ParquetSource::open_path(&pq, "grid").unwrap().with_group_rows(40); | ||
| assert!(visit_all(&capped, Some(Extent::new(178.0, 0.0, 179.0, 1.0)), 4096).0.is_empty()); | ||
| // A GeoPackage put in place of the one added is refused, not read as the layer. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every projected GeoParquet is now refused by a catalog query, not only the rows that can't be placed. Before this PR,
query_nowread such a file andreadalready converted each row to longitude/latitude (place,parquet.rsaround line 1014). A UTM file therefore returned its rows; now it fails with "its coordinates are in ..., not WGS 84 longitude/latitude". The testvectors_and_index_rows_by_address_keep_to_the_files_systemasserts that refusal for EPSG:3857, so it looks deliberate, but the PR text gives no reason to refuse a projected file as a whole.The silent loss this guards against is the rows
placecannot place. Those are already counted inbatch.skipped(parquet.rsline 1015). Refusing on that count keeps projected files working and still says what was dropped. Suggested change: drop the reprojection case fromrefusal()and, after the read, refuse whenbatch.skipped > 0with the count.Confidence: medium. I did not run the code. The refusal is intentional per the test, so the author may want to keep it. Please confirm.
Category: bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept: the refusal is needed.
Reproject::placeconverts only the box it tests and returns the geometry as the file holds it (Fitted { geometry: g, inside }), so a UTM index would hand its metres to a caller that takes them for longitude/latitude (foundintiles/embeddings.rsuses them for footprints and distances). The result carries no system to convert by. Refusing onskippedwouldn't catch it, because those rows are placed and matched.