world_file/lib.rs
1// Copyright 2015 The GeoRust Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![no_std]
16
17//! ```rust
18//! use world_file::WorldFile;
19//!
20//! let world_file_txt = "\
21//! 32.0
22//! 0.0
23//! 0.0
24//! -32.0
25//! 691200.0
26//! 4576000.0";
27//!
28//! let world_file: WorldFile = world_file_txt.parse().unwrap();
29//!
30//! assert_eq!(world_file.x_scale, 32.0);
31//! assert_eq!(world_file.y_skew, 0.0);
32//! assert_eq!(world_file.x_skew, 0.0);
33//! assert_eq!(world_file.y_scale, -32.0);
34//! assert_eq!(world_file.x_coord, 691200.0);
35//! assert_eq!(world_file.y_coord, 4576000.0);
36//! ```
37
38use core::str::FromStr;
39
40pub struct WorldFile {
41 pub x_scale: f64,
42 pub y_skew: f64,
43 pub x_skew: f64,
44 pub y_scale: f64,
45 pub x_coord: f64,
46 pub y_coord: f64,
47}
48
49impl FromStr for WorldFile {
50 type Err = ();
51 fn from_str(s: &str) -> Result<Self, ()> {
52 let mut lines = s.lines();
53
54 // size of pixel in x direction (x_scale)
55 let x_scale: f64 = match lines.next() {
56 Some(s) => match FromStr::from_str(s) {
57 Ok(i) => i,
58 Err(_) => return Err(()),
59 },
60 None => return Err(()),
61 };
62
63 // rotation term for row (y_skew)
64 let y_skew: f64 = match lines.next() {
65 Some(s) => match FromStr::from_str(s) {
66 Ok(i) => i,
67 Err(_) => return Err(()),
68 },
69 None => return Err(()),
70 };
71
72 // rotation term for column (x_skew)
73 let x_skew: f64 = match lines.next() {
74 Some(s) => match FromStr::from_str(s) {
75 Ok(i) => i,
76 Err(_) => return Err(()),
77 },
78 None => return Err(()),
79 };
80
81 // size of pixel in y direction (y_scale)
82 let y_scale: f64 = match lines.next() {
83 Some(s) => match FromStr::from_str(s) {
84 Ok(i) => i,
85 Err(_) => return Err(()),
86 },
87 None => return Err(()),
88 };
89
90 // x coordinate of centre of upper left pixel in map units (x_coord)
91 let x_coord: f64 = match lines.next() {
92 Some(s) => match FromStr::from_str(s) {
93 Ok(i) => i,
94 Err(_) => return Err(()),
95 },
96 None => return Err(()),
97 };
98
99 // y coordinate of centre of upper left pixel in map units (y_coord)
100 let y_coord: f64 = match lines.next() {
101 Some(s) => match FromStr::from_str(s) {
102 Ok(i) => i,
103 Err(_) => return Err(()),
104 },
105 None => return Err(()),
106 };
107
108 Ok(WorldFile {
109 x_scale: x_scale,
110 y_skew: y_skew,
111 x_skew: x_skew,
112 y_scale: y_scale,
113 x_coord: x_coord,
114 y_coord: y_coord,
115 })
116 }
117}