Skip to main content

spatialbench_arrow/
building.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::conversions::string_view_array_from_display_iter;
19use crate::{RecordBatchIterator, DEFAULT_BATCH_SIZE};
20use arrow::array::{BinaryArray, Int64Array, RecordBatch};
21use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
22use geo::Geometry;
23use geozero::{CoordDimensions, ToWkb};
24use spatialbench::generators::{BuildingGenerator, BuildingGeneratorIterator};
25use std::sync::{Arc, LazyLock};
26
27/// Generate [`Building`]s in [`RecordBatch`] format
28///
29/// [`Building`]: spatialbench::generators::Building
30///
31/// # Example
32/// ```
33/// # use spatialbench::generators::{BuildingGenerator};
34/// # use spatialbench_arrow::BuildingArrow;
35///
36/// // Create a SF=1.0 generator and wrap it in an Arrow generator
37/// let generator = BuildingGenerator::new(1.0, 1, 1);
38/// let mut arrow_generator = BuildingArrow::new(generator)
39///   .with_batch_size(10);
40/// // Read the first batch
41/// let batch = arrow_generator.next().unwrap();
42/// ```
43pub struct BuildingArrow {
44    inner: BuildingGeneratorIterator<'static>,
45    batch_size: usize,
46}
47
48impl BuildingArrow {
49    pub fn new(generator: BuildingGenerator<'static>) -> Self {
50        Self {
51            inner: generator.iter(),
52            batch_size: DEFAULT_BATCH_SIZE,
53        }
54    }
55
56    /// Set the batch size
57    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
58        self.batch_size = batch_size;
59        self
60    }
61}
62
63impl RecordBatchIterator for BuildingArrow {
64    fn schema(&self) -> &SchemaRef {
65        &BUILDING_SCHEMA
66    }
67}
68
69impl Iterator for BuildingArrow {
70    type Item = RecordBatch;
71
72    fn next(&mut self) -> Option<Self::Item> {
73        // Get next rows to convert
74        let rows: Vec<_> = self.inner.by_ref().take(self.batch_size).collect();
75        if rows.is_empty() {
76            return None;
77        }
78
79        let buildingkey = Int64Array::from_iter_values(rows.iter().map(|r| r.b_buildingkey));
80        let name = string_view_array_from_display_iter(rows.iter().map(|r| &r.b_name));
81
82        // Convert geo::Polygon to WKB binary format
83        let wkb_array = BinaryArray::from_iter_values(rows.iter().map(|r| {
84            Geometry::Polygon(r.b_boundary.clone())
85                .to_wkb(CoordDimensions::xy())
86                .unwrap()
87        }));
88
89        let batch = RecordBatch::try_new(
90            Arc::clone(self.schema()),
91            vec![Arc::new(buildingkey), Arc::new(name), Arc::new(wkb_array)],
92        )
93        .unwrap();
94        Some(batch)
95    }
96}
97
98/// Schema for the Building
99static BUILDING_SCHEMA: LazyLock<SchemaRef> = LazyLock::new(make_building_schema);
100fn make_building_schema() -> SchemaRef {
101    Arc::new(Schema::new(vec![
102        Field::new("b_buildingkey", DataType::Int64, false),
103        Field::new("b_name", DataType::Utf8View, false),
104        Field::new("b_boundary", DataType::Binary, false),
105    ]))
106}