@@ -8,6 +8,46 @@ use geoarrow_schema::GeoArrowType;
88
99use 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+ /// ```
1151pub 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+ /// ```
64141pub struct GeoJsonLinesWriter < W : Write > {
65142 /// Underlying writer to use to write bytes
66143 writer : LineDelimitedWriter < W > ,
0 commit comments