1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::{
    conversion::coords::{CoordType, FromSFCGALGeom, ToSFCGALGeom},
    CoordSeq, Point2d, Point3d, Result, SFCGeometry, ToCoordinates, ToSFCGAL,
};
use anyhow::Error;
use geojson::Value as GeometryValue;

/// Conversion from [`SFCGeometry`] (implemented on [geo-types](https://docs.rs/geo-types/) geometries)
///
/// [`SFCGeometry`]: struct.SFCGeometry.html
pub trait TryIntoCoords<T> {
    type Err;
    fn try_into(&self) -> Result<T>;
}

/// Convert coordinates to `sfcgal_geometry_t`.
pub trait FromSlice {
    fn from_slice(pt: &[f64]) -> Result<Self>
    where
        Self: std::marker::Sized;
}

/// Convert coordinates to `sfcgal_geometry_t`.
pub trait ToVec {
    fn to_vec(&self) -> Vec<f64>;
}

impl ToVec for Point2d {
    fn to_vec(&self) -> Vec<f64> {
        vec![self.0, self.1]
    }
}

impl ToVec for Point3d {
    fn to_vec(&self) -> Vec<f64> {
        vec![self.0, self.1, self.2]
    }
}

impl FromSlice for Point2d {
    fn from_slice(pt: &[f64]) -> Result<Point2d> {
        let mut it = pt.iter();
        Ok((*it.next().unwrap(), *it.next().unwrap()))
    }
}

impl FromSlice for Point3d {
    fn from_slice(pt: &[f64]) -> Result<Point3d> {
        let mut it = pt.iter();
        Ok((
            *it.next().unwrap(),
            *it.next().unwrap(),
            *it.next().unwrap_or(&(0.0f64)),
        ))
    }
}

/// Implements conversion from CoordSeq to geo_types::Geometry
/// (better use TryInto<geo_types::Geometry> for SFCGeometry if the intend
/// is to convert SFCGAL Geometries to geo_types ones)
impl<T> TryIntoCoords<CoordSeq<T>> for GeometryValue
where
    T: FromSlice + CoordType,
{
    type Err = Error;
    fn try_into(&self) -> Result<CoordSeq<T>>
    where
        T: FromSlice + CoordType,
    {
        match *self {
            GeometryValue::Point(ref pt) => Ok(CoordSeq::Point(T::from_slice(pt)?)),
            GeometryValue::MultiPoint(ref pts) => {
                let _pts = pts
                    .iter()
                    .map(|p| T::from_slice(p))
                    .collect::<Result<Vec<T>>>();
                Ok(CoordSeq::Multipoint(_pts?))
            }
            GeometryValue::LineString(ref pts) => {
                let _pts = pts
                    .iter()
                    .map(|p| T::from_slice(p))
                    .collect::<Result<Vec<T>>>();
                Ok(CoordSeq::Linestring(_pts?))
            }
            GeometryValue::MultiLineString(ref lines) => {
                let _pts = lines
                    .iter()
                    .map(|pts| pts.iter().map(|p| T::from_slice(p)).collect())
                    .collect::<Result<Vec<Vec<T>>>>();
                Ok(CoordSeq::Multilinestring(_pts?))
            }
            GeometryValue::Polygon(ref rings) => {
                let _pts = rings
                    .iter()
                    .map(|pts| pts.iter().map(|p| T::from_slice(p)).collect())
                    .collect::<Result<Vec<Vec<T>>>>();
                Ok(CoordSeq::Polygon(_pts?))
            }
            GeometryValue::MultiPolygon(ref polygons) => {
                let _pts = polygons
                    .iter()
                    .map(|rings| {
                        rings
                            .iter()
                            .map(|pts| pts.iter().map(|p| T::from_slice(p)).collect())
                            .collect()
                    })
                    .collect::<Result<Vec<Vec<Vec<T>>>>>();
                Ok(CoordSeq::Multipolygon(_pts?))
            }
            GeometryValue::GeometryCollection(ref geoms) => {
                let _geoms = geoms
                    .iter()
                    .map(|geom| TryIntoCoords::try_into(&geom.value))
                    .collect::<Result<Vec<CoordSeq<T>>>>();
                Ok(CoordSeq::Geometrycollection(_geoms?))
            }
        }
    }
}

/// Conversion from GeoJson to SFCGAL Geometries.
pub trait FromGeoJSON {
    type Err;
    fn from_geojson<T: FromSlice + CoordType + ToSFCGALGeom>(
        geom: &GeometryValue,
    ) -> Result<SFCGeometry>;
}

/// Conversion from SFCGAL Geometries to GeoJson.
pub trait ToGeoJSON {
    type Err;
    fn to_geojson<T: FromSlice + CoordType + ToVec + FromSFCGALGeom>(
        &self,
    ) -> Result<GeometryValue>;
}

/// Conversion from SFCGAL Geometries to GeoJson.
///
/// Allows to choose if coordinates of constructed geojson have to be 2d or 3d.
/// ``` rust
/// use sfcgal::{SFCGeometry, ToGeoJSON};
/// type Point3d = (f64, f64, f64);
///
/// let input_wkt = "POINT (0.1 0.9 1.0)";
/// let pt_sfcgal = SFCGeometry::new(input_wkt).unwrap();
/// let pt_geojson = pt_sfcgal.to_geojson::<Point3d>().unwrap();
/// ```
impl ToGeoJSON for SFCGeometry {
    type Err = Error;
    fn to_geojson<T: FromSlice + CoordType + ToVec + FromSFCGALGeom>(
        &self,
    ) -> Result<GeometryValue> {
        let cs: CoordSeq<T> = self.to_coordinates()?;
        cs.to_geojson::<T>()
    }
}

impl<U> ToGeoJSON for CoordSeq<U>
where
    U: FromSlice + CoordType + ToVec + FromSFCGALGeom,
{
    type Err = Error;
    fn to_geojson<T: FromSlice + CoordType + ToVec + FromSFCGALGeom>(
        &self,
    ) -> Result<GeometryValue> {
        match self {
            CoordSeq::Point(pt) => Ok(GeometryValue::Point(pt.to_vec())),
            CoordSeq::Multipoint(pts) => Ok(GeometryValue::MultiPoint(
                pts.iter().map(|p| p.to_vec()).collect(),
            )),
            CoordSeq::Linestring(pts) => Ok(GeometryValue::LineString(
                pts.iter().map(|p| p.to_vec()).collect(),
            )),
            CoordSeq::Multilinestring(lines) => Ok(GeometryValue::MultiLineString(
                lines
                    .iter()
                    .map(|pts| pts.iter().map(|p| p.to_vec()).collect())
                    .collect(),
            )),
            CoordSeq::Polygon(rings) => Ok(GeometryValue::Polygon(
                rings
                    .iter()
                    .map(|pts| pts.iter().map(|p| p.to_vec()).collect())
                    .collect(),
            )),
            CoordSeq::Multipolygon(polygons) => Ok(GeometryValue::MultiPolygon(
                polygons
                    .iter()
                    .map(|rings| {
                        rings
                            .iter()
                            .map(|pts| pts.iter().map(|p| p.to_vec()).collect())
                            .collect()
                    })
                    .collect(),
            )),
            CoordSeq::Geometrycollection(geoms) => Ok(GeometryValue::GeometryCollection(
                geoms
                    .iter()
                    .map(|geom| {
                        Ok(geojson::Geometry {
                            bbox: None,
                            value: geom.to_geojson::<T>()?,
                            foreign_members: None,
                        })
                    })
                    .collect::<Result<Vec<_>>>()?,
            )),
            _ => unimplemented!(),
        }
    }
}

/// Conversion from GeoJson to SFCGAL geometries.
///
/// Allows to choose if the constructed SFCGAL geometries will be 2d or 3d.
/// ``` rust
/// use sfcgal::{SFCGeometry, FromGeoJSON};
/// type Point2d = (f64, f64);
///
/// let geom = geojson::Geometry {
///     bbox: None,
///     value: geojson::Value::LineString(vec![vec![101.0, 0.0], vec![102.0, 1.0]]),
///     foreign_members: None,
/// };
/// let line_sfcgal = SFCGeometry::from_geojson::<Point2d>(&geom.value).unwrap();
/// ```
impl FromGeoJSON for SFCGeometry {
    type Err = Error;
    fn from_geojson<T: FromSlice + CoordType + ToSFCGALGeom>(
        geom: &GeometryValue,
    ) -> Result<SFCGeometry> {
        let coords: CoordSeq<T> = TryIntoCoords::try_into(geom)?;
        coords.to_sfcgal()
    }
}

#[cfg(test)]
mod tests {
    use super::{Point2d, Point3d, ToGeoJSON, TryIntoCoords};
    use crate::*;

    #[test]
    fn point_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "POINT (0.1 0.9)";
        let pt_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let pt_geojson = pt_sfcgal.to_geojson::<Point2d>().unwrap();
        let pt_sfcgal_new = SFCGeometry::from_geojson::<Point2d>(&pt_geojson).unwrap();
        assert_eq!(
            pt_sfcgal.to_wkt_decim(1).unwrap(),
            pt_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point2d> = TryIntoCoords::try_into(&pt_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            pt_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }

    #[test]
    fn point_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "POINT (0.1 0.9 1.0)";
        let pt_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let pt_geojson = pt_sfcgal.to_geojson::<Point3d>().unwrap();
        let pt_sfcgal_new = SFCGeometry::from_geojson::<Point3d>(&pt_geojson).unwrap();
        assert_eq!(
            pt_sfcgal.to_wkt_decim(1).unwrap(),
            pt_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&pt_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            pt_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn multipoint_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTIPOINT ((0.1 0.9),(2.3 3.4))";
        let multipt_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipt_geojson = multipt_sfcgal.to_geojson::<Point2d>().unwrap();
        let multipt_sfcgal_new = SFCGeometry::from_geojson::<Point2d>(&multipt_geojson).unwrap();
        assert_eq!(
            multipt_sfcgal.to_wkt_decim(1).unwrap(),
            multipt_sfcgal_new.to_wkt_decim(1).unwrap()
        );
    }

    #[test]
    fn multipoint_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTIPOINT ((0.1 0.9 1.1),(2.3 3.4 1.1))";
        let multipt_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipt_geojson = multipt_sfcgal.to_geojson::<Point3d>().unwrap();
        let multipt_sfcgal_new = SFCGeometry::from_geojson::<Point3d>(&multipt_geojson).unwrap();
        assert_eq!(
            multipt_sfcgal.to_wkt_decim(1).unwrap(),
            multipt_sfcgal_new.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn line_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "LINESTRING (10.0 1.0, 1.0 2.0)";
        let line_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let line_geojson = line_sfcgal.to_geojson::<Point2d>().unwrap();
        let line_sfcgal_new = SFCGeometry::from_geojson::<Point2d>(&line_geojson).unwrap();
        assert_eq!(
            line_sfcgal.to_wkt_decim(1).unwrap(),
            line_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point2d> = TryIntoCoords::try_into(&line_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            line_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn line_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "LINESTRING (10.0 1.0 2.0, 1.0 2.0 1.7)";
        let line_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let line_geojson = line_sfcgal.to_geojson::<Point3d>().unwrap();
        let line_sfcgal_new = SFCGeometry::from_geojson::<Point3d>(&line_geojson).unwrap();
        assert_eq!(
            line_sfcgal.to_wkt_decim(1).unwrap(),
            line_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&line_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            line_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn multiline_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTILINESTRING (\
                         (-0.0 -0.0,0.5 0.5),\
                         (1.0 -0.0,0.5 0.5),\
                         (1.0 1.0,0.5 0.5),\
                         (-0.0 1.0,0.5 0.5))";
        let multiline_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multiline_geojson = multiline_sfcgal.to_geojson::<Point2d>().unwrap();
        let multiline_sfcgal_new =
            SFCGeometry::from_geojson::<Point2d>(&multiline_geojson).unwrap();
        assert_eq!(
            multiline_sfcgal.to_wkt_decim(1).unwrap(),
            multiline_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point2d> = TryIntoCoords::try_into(&multiline_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            multiline_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn multiline_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTILINESTRING (\
                         (-0.0 -0.0 1.3,0.5 0.5 1.3),\
                         (1.0 -0.0 1.3,0.5 0.5 1.3),\
                         (1.0 1.0 1.3,0.5 0.5 1.3),\
                         (-0.0 1.0 1.3,0.5 0.5 1.3))";
        let multiline_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multiline_geojson = multiline_sfcgal.to_geojson::<Point3d>().unwrap();
        let multiline_sfcgal_new =
            SFCGeometry::from_geojson::<Point3d>(&multiline_geojson).unwrap();
        assert_eq!(
            multiline_sfcgal.to_wkt_decim(1).unwrap(),
            multiline_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&multiline_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            multiline_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn polyg_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "POLYGON ((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0))";
        let polyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let polyg_geojson = polyg_sfcgal.to_geojson::<Point2d>().unwrap();
        let polyg_sfcgal_new = SFCGeometry::from_geojson::<Point2d>(&polyg_geojson).unwrap();
        assert_eq!(
            polyg_sfcgal.to_wkt_decim(1).unwrap(),
            polyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point2d> = TryIntoCoords::try_into(&polyg_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            polyg_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn polyg_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "POLYGON ((0.0 0.0 2.0, 1.0 0.0 2.0, 1.0 1.0 2.0, 0.0 0.0 2.0))";
        let polyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let polyg_geojson = polyg_sfcgal.to_geojson::<Point3d>().unwrap();
        let polyg_sfcgal_new = SFCGeometry::from_geojson::<Point3d>(&polyg_geojson).unwrap();
        assert_eq!(
            polyg_sfcgal.to_wkt_decim(1).unwrap(),
            polyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&polyg_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            polyg_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn multipolyg_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTIPOLYGON (((0.0 0.0, 1.0 0.0, 1.0 1.0, 0.0 0.0)))";
        let multipolyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipolyg_geojson = multipolyg_sfcgal.to_geojson::<Point2d>().unwrap();
        let multipolyg_sfcgal_new =
            SFCGeometry::from_geojson::<Point2d>(&multipolyg_geojson).unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            multipolyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn multipolyg_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "MULTIPOLYGON (((0.0 0.0 2.0, 1.0 0.0 2.0, 1.0 1.0 2.0, 0.0 0.0 2.0)))";
        let multipolyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipolyg_geojson = multipolyg_sfcgal.to_geojson::<Point3d>().unwrap();
        let multipolyg_sfcgal_new =
            SFCGeometry::from_geojson::<Point3d>(&multipolyg_geojson).unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            multipolyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&multipolyg_geojson).unwrap();
        let b = a.to_sfcgal().unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn geomcollection_2d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt = "GEOMETRYCOLLECTION (POINT (1.0 1.0),LINESTRING (10.0 1.0,1.0 2.0))";
        let multipolyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipolyg_geojson = multipolyg_sfcgal.to_geojson::<Point2d>().unwrap();
        let multipolyg_sfcgal_new =
            SFCGeometry::from_geojson::<Point2d>(&multipolyg_geojson).unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            multipolyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
    }
    #[test]
    fn geomcollection_3d_sfcgal_to_geojson_to_sfcgal() {
        let input_wkt =
            "GEOMETRYCOLLECTION (POINT (1.0 1.0 4.0),LINESTRING (10.0 1.0 4.0,1.0 2.0 4.0))";
        let multipolyg_sfcgal = SFCGeometry::new(input_wkt).unwrap();
        let multipolyg_geojson = multipolyg_sfcgal.to_geojson::<Point3d>().unwrap();
        let multipolyg_sfcgal_new =
            SFCGeometry::from_geojson::<Point3d>(&multipolyg_geojson).unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            multipolyg_sfcgal_new.to_wkt_decim(1).unwrap()
        );
        let a: CoordSeq<Point3d> = TryIntoCoords::try_into(&multipolyg_geojson).unwrap();
        let c = a.to_geojson::<Point3d>().unwrap();
        let b = a.to_sfcgal().unwrap();
        let d = SFCGeometry::from_geojson::<Point3d>(&c).unwrap();
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            b.to_wkt_decim(1).unwrap()
        );
        assert_eq!(
            multipolyg_sfcgal.to_wkt_decim(1).unwrap(),
            d.to_wkt_decim(1).unwrap()
        );
    }
}