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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! EWKT values and entry points over the checked XY text codec.
//!
//! Prefix parsing retains effective `PostGIS` SRIDs. Body errors are rebased
//! onto the original input without rewriting or erasing any geometry tokens.
use String;
use Cartesian;
use ;
use ;
use ;
use crateEwktError;
use crateEwktWriteError;
use crate;
use Srid;
/// A concrete 2D Cartesian point — the coordinate type every parsed
/// geometry is built from, as in the WKT crate's parser.
type Pt = ;
/// A geometry together with the spatial-reference id that prefixed it.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{Ewkt, Srid, from_ewkt, to_ewkt};
///
/// let e = from_ewkt("SRID=4326;POINT(1 2)").unwrap();
/// assert_eq!(e.srid, Some(Srid::new(4326)));
/// assert_eq!(to_ewkt(&e.geometry, e.srid).unwrap(), "SRID=4326;POINT(1 2)");
///
/// let bare: Ewkt<_> = from_ewkt("POINT(1 2)").unwrap();
/// assert_eq!(bare.srid, None);
/// ```
/// Scan the prefix and delegate without changing the body text.
/// Read EWKT of any kind into a [`DynGeometry`] and its SRID.
///
/// # Errors
///
/// [`EwktError::InvalidSrid`] when the input is claimed as prefixed but
/// the prefix is malformed, and [`EwktError::Wkt`] for anything the WKT
/// crate rejects in the body, with byte offsets rebased onto the input.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{Srid, from_ewkt};
/// use geometry_model::DynKind;
///
/// let e = from_ewkt("SRID=4326;POINT(1 2)").unwrap();
/// assert_eq!(e.srid, Some(Srid::new(4326)));
/// assert_eq!(e.geometry.kind(), DynKind::Point);
/// ```
/// Read EWKT while retaining empty points and multipoint members.
///
/// # Errors
///
/// Returns prefix errors or the errors of [`geometry_io_wkt::from_wkt_2d`],
/// with positions referring to the original EWKT input.
///
/// ```
/// use geometry_io_ewkt::{from_ewkt_2d, Srid};
/// use geometry_model::GeometryValue;
/// let value = from_ewkt_2d("SRID=4326;POINT EMPTY").unwrap();
/// assert_eq!(value.geometry, GeometryValue::Point(None));
/// assert_eq!(value.srid, Some(Srid::new(4326)));
/// ```
/// Read EWKT known to be a `POINT`.
///
/// # Errors
///
/// [`EwktError::InvalidSrid`] for a malformed prefix, and
/// [`EwktError::Wkt`] for a body the WKT crate rejects — including
/// `WktError::TypeMismatch` when the body is a different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{Srid, parse_point, to_ewkt};
///
/// let e = parse_point("SRID=4326;POINT(1 2)").unwrap();
/// assert_eq!(to_ewkt(&e.geometry, e.srid).unwrap(), "SRID=4326;POINT(1 2)");
/// ```
/// Read EWKT known to be a `LINESTRING`.
///
/// # Errors
///
/// As [`parse_point`], including `WktError::TypeMismatch` for a body of a
/// different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{parse_linestring, to_ewkt};
///
/// let e = parse_linestring("SRID=4326;LINESTRING(1 2,4 5)").unwrap();
/// assert_eq!(to_ewkt(&e.geometry, e.srid).unwrap(), "SRID=4326;LINESTRING(1 2,4 5)");
/// ```
/// Read EWKT known to be a `POLYGON`.
///
/// # Errors
///
/// As [`parse_point`], including `WktError::TypeMismatch` for a body of a
/// different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{parse_polygon, to_ewkt};
///
/// let e = parse_polygon("SRID=4326;POLYGON((0 0,1 0,0 1,0 0))").unwrap();
/// assert_eq!(
/// to_ewkt(&e.geometry, e.srid).unwrap(),
/// "SRID=4326;POLYGON((0 0,1 0,0 1,0 0))"
/// );
/// ```
/// Read EWKT known to be a `MULTIPOINT`.
///
/// # Errors
///
/// As [`parse_point`], including `WktError::TypeMismatch` for a body of a
/// different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{parse_multi_point, to_ewkt};
///
/// let e = parse_multi_point("SRID=4326;MULTIPOINT(1 2,4 5)").unwrap();
/// assert_eq!(to_ewkt(&e.geometry, e.srid).unwrap(), "SRID=4326;MULTIPOINT((1 2),(4 5))");
/// ```
/// Read EWKT known to be a `MULTILINESTRING`.
///
/// # Errors
///
/// As [`parse_point`], including `WktError::TypeMismatch` for a body of a
/// different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{parse_multi_linestring, to_ewkt};
///
/// let e = parse_multi_linestring("SRID=4326;MULTILINESTRING((1 2,4 5))").unwrap();
/// assert_eq!(
/// to_ewkt(&e.geometry, e.srid).unwrap(),
/// "SRID=4326;MULTILINESTRING((1 2,4 5))"
/// );
/// ```
/// Read EWKT known to be a `MULTIPOLYGON`.
///
/// # Errors
///
/// As [`parse_point`], including `WktError::TypeMismatch` for a body of a
/// different kind.
///
/// # Examples
///
/// ```
/// use geometry_io_ewkt::{parse_multi_polygon, to_ewkt};
///
/// let e = parse_multi_polygon("SRID=4326;MULTIPOLYGON(((0 0,1 0,0 1,0 0)))").unwrap();
/// assert_eq!(
/// to_ewkt(&e.geometry, e.srid).unwrap(),
/// "SRID=4326;MULTIPOLYGON(((0 0,1 0,0 1,0 0)))"
/// );
/// ```
/// Serialise a geometry and its SRID to canonical EWKT.
///
/// With `srid` of `None` the output is byte-identical to the WKT crate's
/// `to_wkt`.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_io_ewkt::{Srid, to_ewkt};
/// use geometry_model::Point2D;
///
/// let p = Point2D::<f64, Cartesian>::new(1.0, 2.0);
/// assert_eq!(to_ewkt(&p, Some(Srid::new(4326))).unwrap(), "SRID=4326;POINT(1 2)");
/// assert_eq!(to_ewkt(&p, None).unwrap(), "POINT(1 2)");
/// ```
///
/// # Errors
///
/// Returns an error for values outside the supported XY profile or a failing
/// custom writer. See the crate documentation for validation and migration rules.
Sized>
/// Serialise a geometry and its SRID into any [`core::fmt::Write`] sink.
///
/// The streaming counterpart to [`to_ewkt`].
///
/// # Errors
///
/// Returns SRID or geometry validation errors and wraps sink failures.
/// On error the sink may contain partial output; discard it.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_io_ewkt::{Srid, write_ewkt};
/// use geometry_model::Point2D;
///
/// let p = Point2D::<f64, Cartesian>::new(1.0, 2.0);
/// let mut s = String::new();
/// write_ewkt(&p, Some(Srid::UNKNOWN), &mut s).unwrap();
/// assert_eq!(s, "SRID=0;POINT(1 2)");
/// ```
/// Serialise any polygon implementing the geometry traits, plus its SRID,
/// to canonical EWKT.
///
/// The bring-your-own-type counterpart to [`to_ewkt`]. There is no
/// streaming variant, because the WKT crate's polygon writer is private.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_io_ewkt::{Srid, to_ewkt_polygon};
/// use geometry_tag::{PointTag, PolygonTag, RingTag};
/// use geometry_trait::{Geometry, Point, Polygon, Ring};
///
/// struct Coordinate(f64, f64);
///
/// impl Geometry for Coordinate {
/// type Kind = PointTag;
/// type Point = Self;
/// }
///
/// impl Point for Coordinate {
/// type Scalar = f64;
/// type Cs = Cartesian;
/// const DIM: usize = 2;
///
/// fn get<const D: usize>(&self) -> f64 {
/// match D {
/// 0 => self.0,
/// 1 => self.1,
/// _ => unreachable!("a Coordinate has two dimensions"),
/// }
/// }
/// }
///
/// struct Boundary(Vec<Coordinate>);
///
/// impl Geometry for Boundary {
/// type Kind = RingTag;
/// type Point = Coordinate;
/// }
///
/// impl Ring for Boundary {
/// fn points(&self) -> impl ExactSizeIterator<Item = &Coordinate> + Clone {
/// self.0.iter()
/// }
/// }
///
/// struct Parcel(Boundary);
///
/// impl Geometry for Parcel {
/// type Kind = PolygonTag;
/// type Point = Coordinate;
/// }
///
/// impl Polygon for Parcel {
/// type Ring = Boundary;
///
/// fn exterior(&self) -> &Boundary {
/// &self.0
/// }
///
/// fn interiors(&self) -> impl ExactSizeIterator<Item = &Boundary> {
/// [].iter()
/// }
/// }
///
/// let parcel = Parcel(Boundary(vec![
/// Coordinate(0.0, 0.0),
/// Coordinate(0.0, 2.0),
/// Coordinate(2.0, 2.0),
/// Coordinate(0.0, 0.0),
/// ]));
///
/// assert_eq!(
/// to_ewkt_polygon(&parcel, Some(Srid::new(4326))).unwrap(),
/// "SRID=4326;POLYGON((0 0,0 2,2 2,0 0))"
/// );
/// ```
///
/// # Errors
///
/// Returns an error for values outside the supported XY profile or a failing
/// custom writer. See the crate documentation for validation and migration rules.
/// Write a caller-defined XY polygon and optional SRID into a text sink.
///
/// # Errors
///
/// Returns SRID, geometry or sink errors. An error can leave partial output.
///
/// ```
/// use geometry_io_ewkt::{write_ewkt_polygon, Srid};
/// use geometry_model::{Point2D, Polygon};
/// let mut text = String::new();
/// write_ewkt_polygon(&Polygon::<Point2D<f64>>::default(), Some(Srid::new(4326)), &mut text).unwrap();
/// assert_eq!(text, "SRID=4326;POLYGON EMPTY");
/// ```