Skip to content

Commit

Permalink
fix: calculate bbod of spatial indexing with using actual coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
HideBa committed Jan 25, 2025
1 parent be99da7 commit 025f18e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
29 changes: 25 additions & 4 deletions src/rust/fcb_core/src/writer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::MAGIC_BYTES;
use anyhow::Result;
use attribute::AttributeSchema;
use cjseq::{CityJSON, CityJSONFeature};
use cjseq::{CityJSON, CityJSONFeature, Transform as CjTransform};
use feature_writer::FeatureWriter;
use header_writer::{HeaderWriter, HeaderWriterOptions};
use packed_rtree::{calc_extent, hilbert_sort, NodeItem, PackedRTree};
Expand All @@ -28,6 +28,9 @@ pub struct FcbWriter<'a> {
/// Optional writer for features
feat_writer: Option<FeatureWriter<'a>>,
/// Offset of the feature in the feature data section
///
/// transform: CjTransform
transform: CjTransform,
feat_offsets: Vec<FeatureOffset>,
feat_nodes: Vec<NodeItem>,
attr_schema: AttributeSchema,
Expand Down Expand Up @@ -57,10 +60,11 @@ impl<'a> FcbWriter<'a> {
attr_schema: Option<AttributeSchema>,
) -> Result<Self> {
let attr_schema = attr_schema.unwrap_or_default();

let transform = cj.transform.clone();
let header_writer = HeaderWriter::new(cj, header_option, attr_schema.clone());
Ok(Self {
header_writer,
transform,
feat_writer: None,
tmpout: BufWriter::new(tempfile::tempfile()?),
attr_schema,
Expand All @@ -75,12 +79,15 @@ impl<'a> FcbWriter<'a> {
///
/// A Result indicating success or failure of the write operation
fn write_feature(&mut self) -> Result<()> {
let transform = &self.transform;

if let Some(feat_writer) = &mut self.feat_writer {
let mut node = feat_writer.bbox.clone();
let feat_buf = feat_writer.finish_to_feature();
let mut node = Self::actual_bbox(transform, &feat_writer.bbox);

node.offset = self.feat_offsets.len() as u64;
self.feat_nodes.push(node);

let feat_buf = feat_writer.finish_to_feature();
let tempoffset = self
.feat_offsets
.last()
Expand All @@ -96,6 +103,19 @@ impl<'a> FcbWriter<'a> {
Ok(())
}

fn actual_bbox(transform: &CjTransform, bbox: &NodeItem) -> NodeItem {
let scale_x = transform.scale[0];
let scale_y = transform.scale[1];
let translate_x = transform.translate[0];
let translate_y = transform.translate[1];
NodeItem::new(
bbox.min_x * scale_x + translate_x,
bbox.min_y * scale_y + translate_y,
bbox.max_x * scale_x + translate_x,
bbox.max_y * scale_y + translate_y,
)
}

/// Adds a new feature to be written
///
/// # Arguments
Expand Down Expand Up @@ -140,6 +160,7 @@ impl<'a> FcbWriter<'a> {

if index_node_size > 0 && !self.feat_nodes.is_empty() {
let extent = calc_extent(&self.feat_nodes);
println!("extent: {:?}", extent);
hilbert_sort(&mut self.feat_nodes, &extent);
let mut offset = 0;
let index_nodes = self
Expand Down
9 changes: 4 additions & 5 deletions src/rust/fcb_core/tests/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ fn read_bbox() -> Result<()> {
.join("delft_bbox.fcb");
let mut filein = BufReader::new(File::open(input_file.clone())?);

let minx = -200000.0;
let miny = -200000.0;
let maxx = 200000.0;
let maxy = 200000.0;
let minx = 84227.77;
let miny = 445377.33;
let maxx = 85323.23;
let maxy = 446334.69;

let mut fcb = FcbReader::open(&mut filein)?.select_bbox(minx, miny, maxx, maxy)?;

assert_ne!(fcb.features_count(), None);

let mut features = Vec::new();
let mut bbox_cnt = 0;
while let Some(feature) = fcb.next()? {
Expand Down
7 changes: 6 additions & 1 deletion src/rust/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ check-wasm:

.PHONY: ser
ser:
cargo run -p fcb_cli ser -i fcb_core/tests/data/delft.city.jsonl -o temp/delft_attr.fcb
cargo run -p fcb_cli ser -i fcb_core/tests/data/delft.city.jsonl -o fcb_core/tests/data/delft_bbox.fcb
# cargo run -p fcb_cli ser -i fcb_core/tests/data/delft.city.jsonl -o temp/delft_bbox.fcb
# cargo run --bin flatcitybuf_cli serialize -i tests/data/delft.city.jsonl -o temp/delft.fcb

.PHONY: deser
deser:
cargo run -p fcb_cli deser -i temp/delft_attr.fcb -o temp/delft_attr.city.jsonl
# cargo run --bin flatcitybuf_cli deserialize -i temp/small.fcb -o temp/small.city.jsonl

.PHONY: fcb_info
fcb_info:
cargo run -p fcb_cli info -i fcb_core/tests/data/delft_bbox.fcb

.PHONY: bench
bench:
cargo bench -p fcb_core --bench read
Expand Down
Empty file removed test/.gitkeep
Empty file.

0 comments on commit 025f18e

Please sign in to comment.