Skip to content

Commit eeb8819

Browse files
docs(geoarrow-geojson): Add some documents for geoarrow-geojson (#1388)
The geoarrow-geojson crate is great! I think it's better than nothing to have some documents about the usage. This pull request adds some minimal examples.
1 parent cfe03b5 commit eeb8819

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

rust/geoarrow-geojson/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
11
# geoarrow-geojson
2+
3+
This crate provides two types of writers.
4+
5+
- [`GeoJsonWriter`][crate::writer::GeoJsonWriter] streams Arrow record batches as a GeoJSON FeatureCollection, writing the collection header once and appending each batch of features. Use it when you need a single GeoJSON document containing all features.
6+
7+
Example output (formatted for readability):
8+
9+
```json
10+
{
11+
"type": "FeatureCollection",
12+
"features": [
13+
{
14+
"type": "Feature",
15+
"geometry": { "type": "Point", "coordinates": [30, 10] },
16+
"properties": { "count": 10 }
17+
},
18+
{
19+
"type": "Feature",
20+
"geometry": { "type": "Point", "coordinates": [40, 20] },
21+
"properties": { "count": 20 }
22+
}
23+
]
24+
}
25+
```
26+
27+
- [`GeoJsonLinesWriter`][crate::writer::GeoJsonWriter] writes each feature as a standalone GeoJSON object separated by newlines, making it suitable for line-delimited sinks and streaming pipelines.
28+
29+
Example output:
30+
31+
```json
32+
{"type":"Feature","geometry":{"type":"Point","coordinates":[30,10]},"properties":{"count":10}}
33+
{"type":"Feature","geometry":{"type":"Point","coordinates":[40,20]},"properties":{"count":20}}
34+
```

rust/geoarrow-geojson/src/writer/mod.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,46 @@ use geoarrow_schema::GeoArrowType;
88

99
use crate::encoder::GeoArrowEncoderFactory;
1010

11+
/// Writer that emits a GeoJSON FeatureCollection to an underlying `Write` implementor.
12+
///
13+
/// ```
14+
/// # use std::sync::Arc;
15+
/// #
16+
/// # use arrow_array::RecordBatch;
17+
/// # use arrow_schema::Schema;
18+
/// # use geoarrow_array::{GeoArrowArray, IntoArrow};
19+
/// # use geoarrow_schema::{CoordType, Dimension};
20+
/// # use geoarrow_geojson::writer::GeoJsonWriter;
21+
/// # fn try_example() -> Result<(), Box<dyn std::error::Error>> {
22+
/// use geoarrow_array::test::point; // This contains ponts data for testing
23+
///
24+
/// // Pick two points and convert it to the geometry field
25+
/// let point_arr = point::array(CoordType::Interleaved, Dimension::XY).slice(0, 2);
26+
/// let field = point_arr.extension_type().to_field("geometry", true);
27+
/// // Create a Record batch with the geometry data
28+
/// let batch = RecordBatch::try_new(
29+
/// Arc::new(Schema::new(vec![Arc::new(field)])),
30+
/// vec![point_arr.to_array_ref()],
31+
/// )?;
32+
///
33+
/// let mut buffer = Vec::new();
34+
/// let mut writer = GeoJsonWriter::new(&mut buffer)?;
35+
/// writer.write(&batch)?;
36+
/// writer.finish()?;
37+
///
38+
/// assert_eq!(
39+
/// String::from_utf8(buffer)?,
40+
/// concat!(
41+
/// r#"{"type":"FeatureCollection","features":["#,
42+
/// r#"{"type":"Feature","geometry":{"type":"Point","coordinates":[30,10]},"properties":{}},"#,
43+
/// r#"{"type":"Feature","geometry":{"type":"Point","coordinates":[40,20]},"properties":{}}"#,
44+
/// r#"]}"#
45+
/// )
46+
/// );
47+
/// # Ok(())
48+
/// # }
49+
/// # try_example().unwrap();
50+
/// ```
1151
pub struct GeoJsonWriter<W: Write> {
1252
/// Underlying writer to use to write bytes
1353
writer: ArrayWriter<W>,
@@ -26,6 +66,7 @@ impl<W: Write> GeoJsonWriter<W> {
2666
})
2767
}
2868

69+
/// Write the leading GeoJSON FeatureCollection bytes to the output sink.
2970
fn write_header(w: &mut W) -> std::io::Result<()> {
3071
// Don't include the initial `[` because the ArrayWriter will write the open brace
3172
let s = br#"{"type":"FeatureCollection","features":"#;
@@ -61,6 +102,42 @@ impl<W: Write> GeoJsonWriter<W> {
61102
}
62103
}
63104

105+
/// Writer that emits newline-delimited GeoJSON features to an underlying `Write` implementor.
106+
///
107+
/// ```
108+
/// # use std::sync::Arc;
109+
/// #
110+
/// # use arrow_array::RecordBatch;
111+
/// # use arrow_schema::Schema;
112+
/// # use geoarrow_array::{GeoArrowArray, IntoArrow};
113+
/// # use geoarrow_schema::{CoordType, Dimension};
114+
/// # use geoarrow_geojson::writer::GeoJsonLinesWriter;
115+
/// # fn try_example() -> Result<(), Box<dyn std::error::Error>> {
116+
/// use geoarrow_array::test::point; // This contains points data for testing
117+
///
118+
/// // Pick two points and convert it to the geometry field
119+
/// let point_arr = point::array(CoordType::Interleaved, Dimension::XY).slice(0, 2);
120+
/// let field = point_arr.extension_type().to_field("geometry", true);
121+
/// let batch = RecordBatch::try_new(
122+
/// Arc::new(Schema::new(vec![Arc::new(field)])),
123+
/// vec![point_arr.to_array_ref()],
124+
/// )?;
125+
///
126+
/// let mut buffer = Vec::new();
127+
/// let mut writer = GeoJsonLinesWriter::new(&mut buffer);
128+
/// writer.write(&batch)?;
129+
/// writer.finish()?;
130+
///
131+
/// assert_eq!(
132+
/// String::from_utf8(buffer)?,
133+
/// r#"{"type":"Feature","geometry":{"type":"Point","coordinates":[30,10]},"properties":{}}
134+
/// {"type":"Feature","geometry":{"type":"Point","coordinates":[40,20]},"properties":{}}
135+
/// "#.to_string()
136+
/// );
137+
/// # Ok(())
138+
/// # }
139+
/// # try_example().unwrap();
140+
/// ```
64141
pub struct GeoJsonLinesWriter<W: Write> {
65142
/// Underlying writer to use to write bytes
66143
writer: LineDelimitedWriter<W>,

0 commit comments

Comments
 (0)