@@ -310,56 +310,75 @@ where
310
310
}
311
311
}
312
312
313
- fn process_kml < T > ( k : Kml < T > ) -> Result < Vec < geo_types:: Geometry < T > > , Error >
313
+ #[ cfg_attr( docsrs, doc( cfg( feature = "geo-types" ) ) ) ]
314
+ impl < T > TryFrom < Kml < T > > for Vec < geo_types:: Geometry < T > >
314
315
where
315
316
T : CoordType ,
316
317
{
317
- match k {
318
- Kml :: KmlDocument ( d) => Ok ( d
319
- . elements
320
- . into_iter ( )
321
- . flat_map ( process_kml)
322
- . flatten ( )
323
- . collect ( ) ) ,
324
- Kml :: Point ( p) => Ok ( vec ! [
325
- geo_types:: Geometry :: Point ( geo_types:: Point :: from( p) ) ;
326
- 1
327
- ] ) ,
328
- Kml :: LineString ( l) => Ok ( vec ! [
329
- geo_types:: Geometry :: LineString (
330
- geo_types:: LineString :: from( l) ,
331
- ) ;
332
- 1
333
- ] ) ,
334
- Kml :: LinearRing ( l) => Ok ( vec ! [
335
- geo_types:: Geometry :: LineString (
336
- geo_types:: LineString :: from( l) ,
337
- ) ;
338
- 1
339
- ] ) ,
340
- Kml :: Polygon ( p) => Ok ( vec ! [
341
- geo_types:: Geometry :: Polygon ( geo_types:: Polygon :: from(
342
- p
343
- ) ) ;
344
- 1
345
- ] ) ,
346
- Kml :: MultiGeometry ( g) => Ok ( geo_types:: GeometryCollection :: try_from ( g) ?. 0 ) ,
347
- Kml :: Placemark ( p) => Ok ( if let Some ( g) = p. geometry {
348
- vec ! [ geo_types:: Geometry :: try_from( g) ?; 1 ]
349
- } else {
350
- vec ! [ ]
351
- } ) ,
352
- Kml :: Document { elements, .. } => Ok ( elements
353
- . into_iter ( )
354
- . flat_map ( process_kml)
355
- . flatten ( )
356
- . collect ( ) ) ,
357
- Kml :: Folder { elements, .. } => Ok ( elements
358
- . into_iter ( )
359
- . flat_map ( process_kml)
360
- . flatten ( )
361
- . collect ( ) ) ,
362
- _ => Ok ( vec ! [ ] ) ,
318
+ type Error = Error ;
319
+
320
+ fn try_from ( k : Kml < T > ) -> Result < Vec < geo_types:: Geometry < T > > , Self :: Error > {
321
+ match k {
322
+ Kml :: KmlDocument ( d) => Ok ( d
323
+ . elements
324
+ . into_iter ( )
325
+ . flat_map ( Vec :: < geo_types:: Geometry < T > > :: try_from)
326
+ . flatten ( )
327
+ . collect :: < Vec < geo_types:: Geometry < T > > > ( ) ) ,
328
+ Kml :: Point ( p) => Ok ( vec ! [
329
+ geo_types:: Geometry :: Point ( geo_types:: Point :: from( p) ) ;
330
+ 1
331
+ ] ) ,
332
+ Kml :: LineString ( l) => Ok ( vec ! [
333
+ geo_types:: Geometry :: LineString (
334
+ geo_types:: LineString :: from( l) ,
335
+ ) ;
336
+ 1
337
+ ] ) ,
338
+ Kml :: LinearRing ( l) => Ok ( vec ! [
339
+ geo_types:: Geometry :: LineString (
340
+ geo_types:: LineString :: from( l) ,
341
+ ) ;
342
+ 1
343
+ ] ) ,
344
+ Kml :: Polygon ( p) => Ok ( vec ! [
345
+ geo_types:: Geometry :: Polygon ( geo_types:: Polygon :: from(
346
+ p
347
+ ) ) ;
348
+ 1
349
+ ] ) ,
350
+ Kml :: MultiGeometry ( g) => Ok ( geo_types:: GeometryCollection :: try_from ( g) ?. 0 ) ,
351
+ Kml :: Placemark ( p) => Ok ( if let Some ( g) = p. geometry {
352
+ vec ! [ geo_types:: Geometry :: try_from( g) ?; 1 ]
353
+ } else {
354
+ vec ! [ ]
355
+ } ) ,
356
+ Kml :: Document { elements, .. } => Ok ( elements
357
+ . into_iter ( )
358
+ . flat_map ( Vec :: < geo_types:: Geometry < T > > :: try_from)
359
+ . flatten ( )
360
+ . collect ( ) ) ,
361
+ Kml :: Folder { elements, .. } => Ok ( elements
362
+ . into_iter ( )
363
+ . flat_map ( Vec :: < geo_types:: Geometry < T > > :: try_from)
364
+ . flatten ( )
365
+ . collect ( ) ) ,
366
+ _ => Ok ( vec ! [ ] ) ,
367
+ }
368
+ }
369
+ }
370
+
371
+ #[ cfg_attr( docsrs, doc( cfg( feature = "geo-types" ) ) ) ]
372
+ impl < T > TryFrom < Kml < T > > for geo_types:: GeometryCollection < T >
373
+ where
374
+ T : CoordType ,
375
+ {
376
+ type Error = Error ;
377
+
378
+ fn try_from ( k : Kml < T > ) -> Result < geo_types:: GeometryCollection < T > , Self :: Error > {
379
+ Ok ( geo_types:: GeometryCollection (
380
+ Vec :: < geo_types:: Geometry < T > > :: try_from ( k) ?,
381
+ ) )
363
382
}
364
383
}
365
384
@@ -387,12 +406,16 @@ where
387
406
/// // Turn the KML string into a geo_types GeometryCollection
388
407
/// let mut collection: GeometryCollection<f64> = quick_collection(k).unwrap();
389
408
/// ```
409
+ #[ deprecated(
410
+ since = "0.8.7" ,
411
+ note = "use `geo_types::GeometryCollection::try_from(&k)` instead"
412
+ ) ]
390
413
#[ cfg_attr( docsrs, doc( cfg( feature = "geo-types" ) ) ) ]
391
414
pub fn quick_collection < T > ( k : Kml < T > ) -> Result < geo_types:: GeometryCollection < T > , Error >
392
415
where
393
416
T : CoordType ,
394
417
{
395
- Ok ( geo_types:: GeometryCollection ( process_kml ( k ) ? ) )
418
+ geo_types:: GeometryCollection :: try_from ( k )
396
419
}
397
420
398
421
#[ cfg( test) ]
@@ -402,7 +425,7 @@ mod tests {
402
425
use std:: collections:: HashMap ;
403
426
404
427
#[ test]
405
- fn test_quick_collection ( ) {
428
+ fn test_try_from_collection ( ) {
406
429
let k = KmlDocument {
407
430
elements : vec ! [
408
431
Kml :: Point ( Point :: from( Coord :: from( ( 1. , 1. ) ) ) ) ,
@@ -425,6 +448,9 @@ mod tests {
425
448
geo_types:: Geometry :: LineString ( geo_types:: LineString :: from( vec![ ( 1. , 1. ) , ( 2. , 2. ) ] ) ) ,
426
449
geo_types:: Geometry :: Point ( geo_types:: Point :: from( ( 3. , 3. ) ) ) ,
427
450
] ) ;
428
- assert_eq ! ( quick_collection( Kml :: KmlDocument ( k) ) . unwrap( ) , gc) ;
451
+ assert_eq ! (
452
+ geo_types:: GeometryCollection :: try_from( Kml :: KmlDocument ( k) ) . unwrap( ) ,
453
+ gc
454
+ ) ;
429
455
}
430
456
}
0 commit comments