-
Notifications
You must be signed in to change notification settings - Fork 95
/
srs.rs
962 lines (869 loc) · 35.2 KB
/
srs.rs
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
use crate::utils::{_last_null_pointer_err, _string};
use gdal_sys::{self, OGRErr};
use std::ffi::{CStr, CString};
use std::ptr::{self};
use std::str::FromStr;
use crate::errors::*;
/// A OpenGIS Spatial Reference System definition.
///
/// Used in geo-referencing raster and vector data, and in coordinate transformations.
///
/// # Notes
/// * See also: [OGR Coordinate Reference Systems and Coordinate Transformation Tutorial](https://gdal.org/tutorials/osr_api_tut.html)
/// * Consult the [OGC WKT Coordinate System Issues](https://gdal.org/tutorials/wktproblems.html)
/// page for implementation details of WKT in OGR.
#[derive(Debug)]
pub struct SpatialRef(gdal_sys::OGRSpatialReferenceH);
impl Drop for SpatialRef {
fn drop(&mut self) {
unsafe { gdal_sys::OSRRelease(self.0) };
self.0 = ptr::null_mut();
}
}
impl Clone for SpatialRef {
fn clone(&self) -> SpatialRef {
let n_obj = unsafe { gdal_sys::OSRClone(self.0) };
SpatialRef(n_obj)
}
}
impl PartialEq for SpatialRef {
fn eq(&self, other: &SpatialRef) -> bool {
unsafe { gdal_sys::OSRIsSame(self.0, other.0) == 1 }
}
}
impl SpatialRef {
pub fn new() -> Result<SpatialRef> {
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(ptr::null()) };
if c_obj.is_null() {
return Err(_last_null_pointer_err("OSRNewSpatialReference"));
}
Ok(SpatialRef(c_obj))
}
/// Returns a wrapped `SpatialRef` from a raw C API handle.
///
/// # Safety
/// The handle passed to this function must be valid.
pub unsafe fn from_c_obj(c_obj: gdal_sys::OGRSpatialReferenceH) -> Result<SpatialRef> {
let mut_c_obj = gdal_sys::OSRClone(c_obj);
if mut_c_obj.is_null() {
Err(_last_null_pointer_err("OSRClone"))
} else {
Ok(SpatialRef(mut_c_obj))
}
}
/// Returns a C pointer to the allocated [`gdal_sys::OGRSpatialReferenceH`] memory.
pub fn to_c_hsrs(&self) -> gdal_sys::OGRSpatialReferenceH {
self.0
}
/// Set spatial reference from various text formats.
///
/// This method will examine the provided input, and try to deduce the format,
/// and then use it to initialize the spatial reference system. See the [C++ API docs][CPP]
/// for details on these forms.
///
/// [CPP]: https://gdal.org/api/ogrspatialref.html#_CPPv4N19OGRSpatialReference16SetFromUserInputEPKc
pub fn from_definition(definition: &str) -> Result<SpatialRef> {
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(ptr::null()) };
if c_obj.is_null() {
return Err(_last_null_pointer_err("OSRNewSpatialReference"));
}
let rv =
unsafe { gdal_sys::OSRSetFromUserInput(c_obj, CString::new(definition)?.as_ptr()) };
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: rv,
method_name: "OSRSetFromUserInput",
});
}
Ok(SpatialRef(c_obj))
}
pub fn from_wkt(wkt: &str) -> Result<SpatialRef> {
let c_str = CString::new(wkt)?;
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(c_str.as_ptr()) };
if c_obj.is_null() {
return Err(_last_null_pointer_err("OSRNewSpatialReference"));
}
Ok(SpatialRef(c_obj))
}
pub fn from_epsg(epsg_code: u32) -> Result<SpatialRef> {
let null_ptr = ptr::null_mut();
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(null_ptr) };
let rv = unsafe { gdal_sys::OSRImportFromEPSG(c_obj, epsg_code as libc::c_int) };
if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRImportFromEPSG",
})
} else {
Ok(SpatialRef(c_obj))
}
}
pub fn from_proj4(proj4_string: &str) -> Result<SpatialRef> {
let c_str = CString::new(proj4_string)?;
let null_ptr = ptr::null_mut();
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(null_ptr) };
let rv = unsafe { gdal_sys::OSRImportFromProj4(c_obj, c_str.as_ptr()) };
if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRImportFromProj4",
})
} else {
Ok(SpatialRef(c_obj))
}
}
pub fn from_esri(esri_wkt: &str) -> Result<SpatialRef> {
let c_str = CString::new(esri_wkt)?;
let mut ptrs = vec![c_str.as_ptr() as *mut libc::c_char, ptr::null_mut()];
let null_ptr = ptr::null_mut();
let c_obj = unsafe { gdal_sys::OSRNewSpatialReference(null_ptr) };
let rv = unsafe { gdal_sys::OSRImportFromESRI(c_obj, ptrs.as_mut_ptr()) };
if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRImportFromESRI",
})
} else {
Ok(SpatialRef(c_obj))
}
}
pub fn to_wkt(&self) -> Result<String> {
let mut c_wkt = ptr::null_mut();
let rv = unsafe { gdal_sys::OSRExportToWkt(self.0, &mut c_wkt) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToWkt",
})
} else {
Ok(_string(c_wkt))
};
unsafe { gdal_sys::VSIFree(c_wkt.cast::<std::ffi::c_void>()) };
res
}
pub fn morph_to_esri(&self) -> Result<()> {
let rv = unsafe { gdal_sys::OSRMorphToESRI(self.0) };
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: rv,
method_name: "OSRMorphToESRI",
});
}
Ok(())
}
pub fn to_pretty_wkt(&self) -> Result<String> {
let mut c_wkt = ptr::null_mut();
let rv =
unsafe { gdal_sys::OSRExportToPrettyWkt(self.0, &mut c_wkt, false as libc::c_int) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToPrettyWkt",
})
} else {
Ok(_string(c_wkt))
};
unsafe { gdal_sys::VSIFree(c_wkt.cast::<std::ffi::c_void>()) };
res
}
pub fn to_xml(&self) -> Result<String> {
let mut c_raw_xml = ptr::null_mut();
let rv = unsafe { gdal_sys::OSRExportToXML(self.0, &mut c_raw_xml, ptr::null()) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToXML",
})
} else {
Ok(_string(c_raw_xml))
};
unsafe { gdal_sys::VSIFree(c_raw_xml.cast::<std::ffi::c_void>()) };
res
}
pub fn to_proj4(&self) -> Result<String> {
let mut c_proj4str = ptr::null_mut();
let rv = unsafe { gdal_sys::OSRExportToProj4(self.0, &mut c_proj4str) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToProj4",
})
} else {
Ok(_string(c_proj4str))
};
unsafe { gdal_sys::VSIFree(c_proj4str.cast::<std::ffi::c_void>()) };
res
}
#[cfg(any(major_ge_4, all(major_ge_3, minor_ge_1)))]
pub fn to_projjson(&self) -> Result<String> {
let mut c_projjsonstr = ptr::null_mut();
let options = ptr::null();
let rv = unsafe { gdal_sys::OSRExportToPROJJSON(self.0, &mut c_projjsonstr, options) };
let res = if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRExportToPROJJSON",
})
} else {
Ok(_string(c_projjsonstr))
};
unsafe { gdal_sys::VSIFree(c_projjsonstr.cast::<std::ffi::c_void>()) };
res
}
pub fn auth_name(&self) -> Result<String> {
let c_ptr = unsafe { gdal_sys::OSRGetAuthorityName(self.0, ptr::null()) };
if c_ptr.is_null() {
Err(_last_null_pointer_err("SRGetAuthorityName"))
} else {
Ok(_string(c_ptr))
}
}
pub fn auth_code(&self) -> Result<i32> {
let c_ptr = unsafe { gdal_sys::OSRGetAuthorityCode(self.0, ptr::null()) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("OSRGetAuthorityCode"));
}
let c_str = unsafe { CStr::from_ptr(c_ptr) };
let epsg = i32::from_str(c_str.to_str()?);
match epsg {
Ok(n) => Ok(n),
Err(_) => Err(GdalError::OgrError {
err: OGRErr::OGRERR_UNSUPPORTED_SRS,
method_name: "OSRGetAuthorityCode",
}),
}
}
pub fn authority(&self) -> Result<String> {
let c_ptr = unsafe { gdal_sys::OSRGetAuthorityName(self.0, ptr::null()) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("SRGetAuthorityName"));
}
let name = unsafe { CStr::from_ptr(c_ptr) }.to_str()?;
let c_ptr = unsafe { gdal_sys::OSRGetAuthorityCode(self.0, ptr::null()) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("OSRGetAuthorityCode"));
}
let code = unsafe { CStr::from_ptr(c_ptr) }.to_str()?;
Ok(format!("{name}:{code}"))
}
pub fn auto_identify_epsg(&mut self) -> Result<()> {
let rv = unsafe { gdal_sys::OSRAutoIdentifyEPSG(self.0) };
if rv != OGRErr::OGRERR_NONE {
Err(GdalError::OgrError {
err: rv,
method_name: "OSRAutoIdentifyEPSG",
})
} else {
Ok(())
}
}
#[cfg(major_ge_3)]
pub fn name(&self) -> Result<String> {
let c_ptr = unsafe { gdal_sys::OSRGetName(self.0) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("OSRGetName"));
}
Ok(_string(c_ptr))
}
pub fn angular_units_name(&self) -> Result<String> {
let mut c_ptr = ptr::null_mut();
unsafe { gdal_sys::OSRGetAngularUnits(self.0, &mut c_ptr) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("OSRGetAngularUnits"));
}
Ok(_string(c_ptr))
}
pub fn angular_units(&self) -> f64 {
unsafe { gdal_sys::OSRGetAngularUnits(self.0, ptr::null_mut()) }
}
pub fn linear_units_name(&self) -> Result<String> {
let mut c_ptr = ptr::null_mut();
unsafe { gdal_sys::OSRGetLinearUnits(self.0, &mut c_ptr) };
if c_ptr.is_null() {
return Err(_last_null_pointer_err("OSRGetLinearUnits"));
}
Ok(_string(c_ptr))
}
pub fn linear_units(&self) -> f64 {
unsafe { gdal_sys::OSRGetLinearUnits(self.0, ptr::null_mut()) }
}
#[inline]
pub fn is_geographic(&self) -> bool {
unsafe { gdal_sys::OSRIsGeographic(self.0) == 1 }
}
#[inline]
#[cfg(all(major_ge_3, minor_ge_1))]
pub fn is_derived_geographic(&self) -> bool {
unsafe { gdal_sys::OSRIsDerivedGeographic(self.0) == 1 }
}
#[inline]
pub fn is_local(&self) -> bool {
unsafe { gdal_sys::OSRIsLocal(self.0) == 1 }
}
#[inline]
pub fn is_projected(&self) -> bool {
unsafe { gdal_sys::OSRIsProjected(self.0) == 1 }
}
#[inline]
pub fn is_compound(&self) -> bool {
unsafe { gdal_sys::OSRIsCompound(self.0) == 1 }
}
#[inline]
pub fn is_geocentric(&self) -> bool {
unsafe { gdal_sys::OSRIsGeocentric(self.0) == 1 }
}
#[inline]
pub fn is_vertical(&self) -> bool {
unsafe { gdal_sys::OSRIsVertical(self.0) == 1 }
}
pub fn axis_orientation(
&self,
target_key: &str,
axis: i32,
) -> Result<super::AxisOrientationType> {
let mut orientation = gdal_sys::OGRAxisOrientation::OAO_Other;
let c_ptr = unsafe {
gdal_sys::OSRGetAxis(
self.0,
CString::new(target_key)?.as_ptr(),
axis as libc::c_int,
&mut orientation,
)
};
// null ptr indicate a failure (but no CPLError) see Gdal documentation.
if c_ptr.is_null() {
Err(GdalError::AxisNotFoundError {
key: target_key.into(),
method_name: "OSRGetAxis",
})
} else {
Ok(orientation)
}
}
pub fn axis_name(&self, target_key: &str, axis: i32) -> Result<String> {
// See get_axis_orientation
let c_ptr = unsafe {
gdal_sys::OSRGetAxis(
self.0,
CString::new(target_key)?.as_ptr(),
axis as libc::c_int,
ptr::null_mut(),
)
};
if c_ptr.is_null() {
Err(GdalError::AxisNotFoundError {
key: target_key.into(),
method_name: "OSRGetAxis",
})
} else {
Ok(_string(c_ptr))
}
}
#[cfg(all(major_ge_3, minor_ge_1))]
pub fn axes_count(&self) -> i32 {
unsafe { gdal_sys::OSRGetAxesCount(self.0) }
}
#[cfg(major_ge_3)]
pub fn set_axis_mapping_strategy(&self, strategy: gdal_sys::OSRAxisMappingStrategy::Type) {
unsafe {
gdal_sys::OSRSetAxisMappingStrategy(self.0, strategy);
}
}
#[cfg(major_ge_3)]
#[deprecated(note = "use `axis_mapping_strategy` instead")]
pub fn get_axis_mapping_strategy(&self) -> gdal_sys::OSRAxisMappingStrategy::Type {
self.axis_mapping_strategy()
}
#[cfg(major_ge_3)]
pub fn axis_mapping_strategy(&self) -> gdal_sys::OSRAxisMappingStrategy::Type {
unsafe { gdal_sys::OSRGetAxisMappingStrategy(self.0) }
}
#[cfg(major_ge_3)]
/// Get the valid use bounding area for this `SpatialRef`.
///
/// See: [`OSRGetAreaOfUse`](https://gdal.org/api/ogr_srs_api.html#_CPPv415OSRGetAreaOfUse20OGRSpatialReferenceHPdPdPdPdPPKc)
pub fn area_of_use(&self) -> Option<AreaOfUse> {
let mut c_area_name: *const libc::c_char = ptr::null_mut();
let (mut w_long, mut s_lat, mut e_long, mut n_lat): (f64, f64, f64, f64) =
(0.0, 0.0, 0.0, 0.0);
let ret_val = unsafe {
gdal_sys::OSRGetAreaOfUse(
self.0,
&mut w_long,
&mut s_lat,
&mut e_long,
&mut n_lat,
&mut c_area_name,
) == 1
};
if ret_val {
Some(AreaOfUse {
west_lon_degree: w_long,
south_lat_degree: s_lat,
east_lon_degree: e_long,
north_lat_degree: n_lat,
name: _string(c_area_name),
})
} else {
None
}
}
/// Get spheroid semi-major axis.
///
/// Returns an error if the semi-major axis can't be found
///
/// See: [`OSRGetSemiMajor`](https://gdal.org/api/ogr_srs_api.html#_CPPv415OSRGetSemiMajor20OGRSpatialReferenceHP6OGRErr)
pub fn semi_major(&self) -> Result<f64> {
let mut err_code = OGRErr::OGRERR_NONE;
let a = unsafe { gdal_sys::OSRGetSemiMajor(self.0, &mut err_code as *mut u32) };
if err_code != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: err_code,
method_name: "OSRGetSemiMajor",
});
}
Ok(a)
}
/// Get spheroid semi-minor axis.
///
/// Returns an error if the semi-minor axis can't be found
///
/// See: [`OSRGetSemiMinor`](https://gdal.org/api/ogr_srs_api.html#_CPPv415OSRGetSemiMinor20OGRSpatialReferenceHP6OGRErr)
pub fn semi_minor(&self) -> Result<f64> {
let mut err_code = OGRErr::OGRERR_NONE;
let b = unsafe { gdal_sys::OSRGetSemiMinor(self.0, &mut err_code as *mut u32) };
if err_code != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: err_code,
method_name: "OSRGetSemiMinor",
});
}
Ok(b)
}
/// Set a projection parameter value.
///
/// Returns an error if there the `PROJCS` node is missing.
///
/// See: [`OSRSetProjParm`](https://gdal.org/api/ogr_srs_api.html#_CPPv414OSRSetProjParm20OGRSpatialReferenceHPKcd)
pub fn set_proj_param(&mut self, name: &str, value: f64) -> Result<()> {
let c_name = CString::new(name)?;
let rv = unsafe { gdal_sys::OSRSetProjParm(self.0, c_name.as_ptr(), value) };
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: rv,
method_name: "OSRSetProjParm",
});
}
Ok(())
}
/// Fetch a projection parameter value.
///
/// Returns:
/// * `Ok(Some(value))` - if parameter is sucessfully found,
/// * `Ok(None)` - if parameter is not found (C library will return [OGRERR_FAILURE](https://gdal.org/api/vector_c_api.html#c.OGRERR_FAILURE)),
/// * `Err(_)` - if there is a string conversion error or on other GDAL OGR error.
///
/// See: [`OSRGetProjParm`](https://gdal.org/api/ogr_srs_api.html#_CPPv414OSRGetProjParm20OGRSpatialReferenceHPKcdP6OGRErr)
pub fn get_proj_param(&self, name: &str) -> Result<Option<f64>> {
let c_name = CString::new(name)?;
let mut err_code = OGRErr::OGRERR_NONE;
let p = unsafe {
gdal_sys::OSRGetProjParm(self.0, c_name.as_ptr(), 0.0, &mut err_code as *mut u32)
};
if err_code != OGRErr::OGRERR_NONE {
match err_code {
OGRErr::OGRERR_FAILURE => Ok(None),
other_err => Err(GdalError::OgrError {
err: other_err,
method_name: "OSRGetProjParm",
}),
}
} else {
Ok(Some(p))
}
}
/// Set attribute value in spatial reference.
///
/// Returns error in case if path was invalid or on GDAL OGR error.
///
/// See: [`OSRSetAttrValue`](https://gdal.org/api/ogr_srs_api.html#_CPPv415OSRSetAttrValue20OGRSpatialReferenceHPKcPKc)
pub fn set_attr_value(&mut self, node_path: &str, new_value: Option<&str>) -> Result<()> {
let c_node_path = CString::new(node_path)?;
let c_new_value = match new_value {
Some(value) => Some(CString::new(value)?),
None => None,
};
let value_ptr = c_new_value
.as_ref()
.map(|s| s.as_ptr())
.unwrap_or(ptr::null());
let rv = unsafe { gdal_sys::OSRSetAttrValue(self.0, c_node_path.as_ptr(), value_ptr) };
if rv != OGRErr::OGRERR_NONE {
return Err(GdalError::OgrError {
err: rv,
method_name: "OSRSetAttrValue",
});
}
Ok(())
}
/// Fetch indicated attribute of named node.
///
/// Returns:
/// * `Ok(Some(value))` - if node and attribute are sucessfully found,
/// * `Ok(None)` - if node or attribute are not found (C library will return `nullptr`) or attribute contains no value,
/// * `Err(_)` - if there is a string conversion error.
///
/// See: [`OSRGetProjParm`](https://gdal.org/api/ogr_srs_api.html#_CPPv415OSRGetAttrValue20OGRSpatialReferenceHPKci)
///
/// # Panics
///
/// Panics if `child` is greater than [`libc::c_int::MAX`].
pub fn get_attr_value(&self, node_path: &str, child: usize) -> Result<Option<String>> {
let child = child.try_into().expect("`child` must fit in `c_int`");
let c_node_path = CString::new(node_path)?;
let rv = unsafe { gdal_sys::OSRGetAttrValue(self.0, c_node_path.as_ptr(), child) };
if rv.is_null() {
Ok(None)
} else {
Ok(Some(_string(rv)))
}
}
/// Make a duplicate of the `GEOGCS` node of this [`SpatialRef`].
///
/// Returns an error if the `GEOGCS` node is missing.
///
/// See: [OSRCloneGeogCS](https://gdal.org/api/ogr_srs_api.html#_CPPv414OSRCloneGeogCS20OGRSpatialReferenceH)
pub fn geog_cs(&self) -> Result<SpatialRef> {
let raw_ret = unsafe { gdal_sys::OSRCloneGeogCS(self.0) };
if raw_ret.is_null() {
return Err(_last_null_pointer_err("OSRCloneGeogCS"));
}
Ok(SpatialRef(raw_ret))
}
}
#[derive(Debug, Clone)]
/// Defines the bounding area of valid use for a [`SpatialRef`].
///
/// See [`area_of_use`][SpatialRef::area_of_use].
pub struct AreaOfUse {
pub west_lon_degree: f64,
pub south_lat_degree: f64,
pub east_lon_degree: f64,
pub north_lat_degree: f64,
pub name: String,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::assert_almost_eq;
#[test]
fn from_wkt_to_proj4() {
let spatial_ref = SpatialRef::from_wkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4326]]").unwrap();
assert_eq!(
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs",
spatial_ref.to_proj4().unwrap().trim()
);
let spatial_ref = SpatialRef::from_definition("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4326]]").unwrap();
assert_eq!(
"+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs",
spatial_ref.to_proj4().unwrap().trim()
);
}
#[test]
fn from_proj4_to_wkt() {
let spatial_ref = SpatialRef::from_proj4(
"+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs",
)
.unwrap();
let wkt = spatial_ref.to_wkt().unwrap();
// Test we the DATUM description has expected tokens
assert!(wkt.contains("GRS") && wkt.contains("80"));
// Test parsing back in gets us functionally the same SRS
let round_trip = SpatialRef::from_wkt(&wkt).unwrap();
assert_eq!(spatial_ref, round_trip);
}
#[test]
fn from_epsg_to_wkt_proj4() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let wkt = spatial_ref.to_wkt().unwrap();
// TODO: handle proj changes on lib level
#[cfg(not(major_ge_3))]
assert_eq!("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]", wkt);
#[cfg(major_ge_3)]
assert_eq!("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]", wkt);
let proj4string = spatial_ref.to_proj4().unwrap();
assert_eq!("+proj=longlat +datum=WGS84 +no_defs", proj4string.trim());
}
#[cfg(any(major_ge_4, all(major_ge_3, minor_ge_1)))]
#[test]
fn from_epsg_to_projjson() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let projjson = spatial_ref.to_projjson().unwrap();
// Testing for exact string equality would be too strict, since the order of keys in JSON is
// unspecified. Ideally, we'd parse the JSON and then compare the values, but adding a JSON
// parser as a dependency just for this one test would be overkill. Thus, we do only a quick
// sanity check.
assert!(
projjson.contains("World Geodetic System 1984"),
"{projjson:?} does not contain expected CRS name",
);
}
#[test]
fn from_esri_to_proj4() {
let spatial_ref = SpatialRef::from_esri("GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]").unwrap();
let proj4string = spatial_ref.to_proj4().unwrap();
assert_eq!("+proj=longlat +datum=WGS84 +no_defs", proj4string.trim());
}
#[test]
fn comparison() {
let spatial_ref1 = SpatialRef::from_wkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4326]]").unwrap();
let spatial_ref2 = SpatialRef::from_epsg(4326).unwrap();
let spatial_ref3 = SpatialRef::from_epsg(3025).unwrap();
let spatial_ref4 = SpatialRef::from_proj4("+proj=longlat +datum=WGS84 +no_defs ").unwrap();
let spatial_ref5 = SpatialRef::from_esri("GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]").unwrap();
assert_eq!(spatial_ref1, spatial_ref2);
assert_ne!(spatial_ref2, spatial_ref3);
assert_eq!(spatial_ref4, spatial_ref2);
assert_eq!(spatial_ref5, spatial_ref4);
}
#[test]
fn authority() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
assert_eq!(spatial_ref.auth_name().unwrap(), "EPSG".to_string());
assert_eq!(spatial_ref.auth_code().unwrap(), 4326);
assert_eq!(spatial_ref.authority().unwrap(), "EPSG:4326".to_string());
let spatial_ref = SpatialRef::from_wkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4326]]").unwrap();
assert_eq!(spatial_ref.auth_name().unwrap(), "EPSG".to_string());
assert_eq!(spatial_ref.auth_code().unwrap(), 4326);
assert_eq!(spatial_ref.authority().unwrap(), "EPSG:4326".to_string());
let spatial_ref = SpatialRef::from_wkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",7030]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",6326]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST]]").unwrap();
assert!(spatial_ref.auth_name().is_err());
assert!(spatial_ref.auth_code().is_err());
assert!(spatial_ref.authority().is_err());
let spatial_ref = SpatialRef::from_proj4(
"+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs",
)
.unwrap();
assert!(spatial_ref.auth_name().is_err());
assert!(spatial_ref.auth_code().is_err());
assert!(spatial_ref.authority().is_err());
}
#[test]
fn auto_identify() {
// retreived from https://epsg.io/32632, but deleted the `AUTHORITY["EPSG","32632"]`
let mut spatial_ref = SpatialRef::from_wkt(
r#"
PROJCS["WGS 84 / UTM zone 32N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",9],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH]]
"#,
)
.unwrap();
assert!(spatial_ref.auth_code().is_err());
spatial_ref.auto_identify_epsg().unwrap();
assert_eq!(spatial_ref.auth_code().unwrap(), 32632);
}
#[cfg(major_ge_3)]
#[test]
fn axis_mapping_strategy() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
assert_eq!(
spatial_ref.axis_mapping_strategy(),
gdal_sys::OSRAxisMappingStrategy::OAMS_AUTHORITY_COMPLIANT
);
spatial_ref.set_axis_mapping_strategy(
gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER,
);
assert_eq!(
spatial_ref.axis_mapping_strategy(),
gdal_sys::OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER
);
}
#[cfg(major_ge_3)]
#[test]
fn area_of_use() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let area_of_use = spatial_ref.area_of_use().unwrap();
assert_almost_eq(area_of_use.west_lon_degree, -180.0);
assert_almost_eq(area_of_use.south_lat_degree, -90.0);
assert_almost_eq(area_of_use.east_lon_degree, 180.0);
assert_almost_eq(area_of_use.north_lat_degree, 90.0);
}
#[cfg(major_ge_3)]
#[test]
fn get_name() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let name = spatial_ref.name().unwrap();
assert_eq!(name, "WGS 84");
}
#[test]
fn get_units_epsg4326() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let angular_units_name = spatial_ref.angular_units_name().unwrap();
assert_eq!(angular_units_name.to_lowercase(), "degree");
let to_radians = spatial_ref.angular_units();
assert_almost_eq(to_radians, 0.01745329);
}
#[test]
fn get_units_epsg2154() {
let spatial_ref = SpatialRef::from_epsg(2154).unwrap();
let linear_units_name = spatial_ref.linear_units_name().unwrap();
assert_eq!(linear_units_name.to_lowercase(), "metre");
let to_meters = spatial_ref.linear_units();
assert_almost_eq(to_meters, 1.0);
}
#[test]
fn predicats_epsg4326() {
let spatial_ref_4326 = SpatialRef::from_epsg(4326).unwrap();
assert!(spatial_ref_4326.is_geographic());
assert!(!spatial_ref_4326.is_local());
assert!(!spatial_ref_4326.is_projected());
assert!(!spatial_ref_4326.is_compound());
assert!(!spatial_ref_4326.is_geocentric());
assert!(!spatial_ref_4326.is_vertical());
#[cfg(all(major_ge_3, minor_ge_1))]
assert!(!spatial_ref_4326.is_derived_geographic());
}
#[test]
fn predicats_epsg2154() {
let spatial_ref_2154 = SpatialRef::from_epsg(2154).unwrap();
assert!(!spatial_ref_2154.is_geographic());
assert!(!spatial_ref_2154.is_local());
assert!(spatial_ref_2154.is_projected());
assert!(!spatial_ref_2154.is_compound());
assert!(!spatial_ref_2154.is_geocentric());
#[cfg(all(major_ge_3, minor_ge_1))]
assert!(!spatial_ref_2154.is_derived_geographic());
}
//XXX Gdal 2 implementation is partial
#[cfg(major_ge_3)]
#[test]
fn crs_axis() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
#[cfg(all(major_ge_3, minor_ge_1))]
assert_eq!(spatial_ref.axes_count(), 2);
let orientation = spatial_ref.axis_orientation("GEOGCS", 0).unwrap();
assert_eq!(orientation, gdal_sys::OGRAxisOrientation::OAO_North);
assert!(spatial_ref.axis_name("GEOGCS", 0).is_ok());
assert!(spatial_ref.axis_name("DO_NO_EXISTS", 0).is_err());
assert!(spatial_ref.axis_orientation("DO_NO_EXISTS", 0).is_err());
}
#[test]
fn semi_major_and_semi_minor() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let semi_major = spatial_ref.semi_major().unwrap();
assert_almost_eq(semi_major, 6_378_137.0);
let semi_minor = spatial_ref.semi_minor().unwrap();
assert_almost_eq(semi_minor, 6_356_752.31);
}
#[test]
fn proj_params() {
let spatial_ref = SpatialRef::from_proj4(
"+proj=geos +lon_0=42 +h=35785831 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs",
)
.unwrap();
let central_meridian = spatial_ref
.get_proj_param("central_meridian")
.unwrap()
.unwrap();
assert_almost_eq(central_meridian, 42.0);
let satellite_height = spatial_ref
.get_proj_param("satellite_height")
.unwrap()
.unwrap();
assert_almost_eq(satellite_height, 35_785_831.0);
}
#[test]
fn setting_proj_param() {
let mut spatial_ref = SpatialRef::from_proj4(
"+proj=geos +lon_0=42 +h=35785831 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs",
)
.unwrap();
spatial_ref
.set_proj_param("central_meridian", -15.0)
.unwrap();
let central_meridian = spatial_ref
.get_proj_param("central_meridian")
.unwrap()
.unwrap();
assert_almost_eq(central_meridian, -15.0);
}
#[test]
fn non_existing_proj_param() {
let spatial_ref = SpatialRef::from_epsg(4326).unwrap();
let spam = spatial_ref.get_proj_param("spam").unwrap();
assert_eq!(spam, None);
}
#[test]
fn attr_values() {
let mut spatial_ref = SpatialRef::from_epsg(4326).unwrap();
assert_eq!(
spatial_ref.get_attr_value("GEOGCS", 0).unwrap().unwrap(),
"WGS 84"
);
assert_eq!(
spatial_ref
.get_attr_value("GEOGCS|UNIT", 0)
.unwrap()
.unwrap(),
"degree"
);
spatial_ref
.set_attr_value("GEOGCS|UNIT", Some("meter"))
.unwrap();
assert_eq!(
spatial_ref
.get_attr_value("GEOGCS|UNIT", 0)
.unwrap()
.unwrap(),
"meter"
);
}
#[test]
fn geog_cs() {
let spatial_ref = SpatialRef::from_proj4(
"+proj=geos +lon_0=42 +h=35785831 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs",
)
.unwrap();
let expected_geog_cs = SpatialRef::from_wkt(
r#"
GEOGCS["unknown",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],
AXIS["Longitude",EAST],
AXIS["Latitude",NORTH]
]
"#,
)
.unwrap();
let geog_cs = spatial_ref.geog_cs().unwrap();
assert_eq!(
geog_cs,
expected_geog_cs,
"GEOGCS of geos spatial reference: \"{:?}\"\n does not equal to expected one: {:?}",
geog_cs.to_wkt(),
expected_geog_cs.to_wkt()
);
}
}