-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprojection.rs
234 lines (222 loc) · 9.73 KB
/
projection.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
use std::sync::Arc;
use nusamai_citygml::schema::Schema;
use nusamai_plateau::Entity;
use nusamai_projection::{
crs::*, etmerc::ExtendedTransverseMercatorProjection, jprect::JPRZone, vshift::Jgd2011ToWgs84,
};
use crate::{pipeline::Feedback, transformer::Transform};
/// Coordinate transformation
#[derive(Clone)]
pub struct ProjectionTransform {
jgd2wgs: Arc<Jgd2011ToWgs84>,
output_epsg: EpsgCode,
jpr_zone_proj: Option<ExtendedTransverseMercatorProjection>,
}
impl Transform for ProjectionTransform {
fn transform(&mut self, _feedback: &Feedback, entity: Entity, out: &mut Vec<Entity>) {
let input_epsg = {
let geom_store = entity.geometry_store.read().unwrap();
geom_store.epsg
};
match input_epsg {
EPSG_JGD2011_GEOGRAPHIC_3D => self.transform_from_jgd2011(&entity, None),
EPSG_WGS84_GEOGRAPHIC_3D => self.transform_from_wgs84(&entity, None),
EPSG_JGD2011_JPRECT_I_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_II_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_III_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IV_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_V_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VIII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IX_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_X_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XIII_JGD2011_HEIGHT => {
self.transform_from_jgd2011(&entity, Some(input_epsg));
}
_ => {
panic!("Unsupported input CRS: {}", input_epsg);
}
}
out.push(entity);
}
fn transform_schema(&self, schema: &mut Schema) {
schema.epsg = Some(self.output_epsg);
}
}
impl ProjectionTransform {
pub fn new(jgd2wgs: Arc<Jgd2011ToWgs84>, output_epsg: EpsgCode) -> Self {
// For Japan Plane Rectangular CS
let jpr_zone_proj = JPRZone::from_epsg(output_epsg).map(|zone| zone.projection());
Self {
jgd2wgs,
output_epsg,
jpr_zone_proj,
}
}
fn rectangular_to_lnglat(x: f64, y: f64, height: f64, input_epsg: EpsgCode) -> (f64, f64, f64) {
let zone = JPRZone::from_epsg(input_epsg).unwrap();
let proj = zone.projection();
let (lng, lat, height) = proj.project_inverse(x, y, height).unwrap();
(lng, lat, height)
}
fn transform_from_jgd2011(&mut self, entity: &Entity, rectangular: Option<EpsgCode>) {
match self.output_epsg {
EPSG_JGD2011_GEOGRAPHIC_3D => {
let mut geom_store = entity.geometry_store.write().unwrap();
geom_store.vertices.iter_mut().for_each(|v| {
// Swap x and y (lat, lng -> lng, lat)
(v[0], v[1], v[2]) = (v[1], v[0], v[2]);
if let Some(input_epsg) = rectangular {
(v[0], v[1], v[2]) =
Self::rectangular_to_lnglat(v[0], v[1], v[2], input_epsg);
};
});
geom_store.epsg = self.output_epsg;
}
EPSG_WGS84_GEOGRAPHIC_3D => {
let mut geom_store = entity.geometry_store.write().unwrap();
geom_store.vertices.iter_mut().for_each(|v| {
// Swap x and y (lat, lng -> lng, lat)
(v[0], v[1], v[2]) = (v[1], v[0], v[2]);
if let Some(input_epsg) = rectangular {
(v[0], v[1], v[2]) =
Self::rectangular_to_lnglat(v[0], v[1], v[2], input_epsg);
};
// JGD2011 to WGS 84 (elevation to ellipsoidal height)
(v[0], v[1], v[2]) = self.jgd2wgs.convert(v[0], v[1], v[2]);
});
geom_store.epsg = self.output_epsg;
}
EPSG_WEB_MERCATOR => {
let mut geom_store = entity.geometry_store.write().unwrap();
geom_store.vertices.iter_mut().for_each(|v| {
// Swap x and y (lat, lng -> lng, lat)
let (lng, lat) = (v[1], v[0]);
if let Some(input_epsg) = rectangular {
(v[0], v[1], v[2]) =
Self::rectangular_to_lnglat(v[0], v[1], v[2], input_epsg);
};
// LngLat to Web Mercator
(v[0], v[1]) = tinymvt::webmercator::lnglat_to_web_mercator_meters(lng, lat)
});
geom_store.epsg = self.output_epsg;
}
EPSG_JGD2011_JPRECT_I_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_II_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_III_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IV_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_V_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VIII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IX_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_X_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XIII_JGD2011_HEIGHT => {
// To Japan Plane Rectangular CS + JGD2011 (vertical) height
let proj = self.jpr_zone_proj.as_ref().unwrap();
let mut geom_store = entity.geometry_store.write().unwrap();
geom_store.vertices.iter_mut().for_each(|v| {
let (lng, lat) = (v[1], v[0]);
if let Some(input_epsg) = rectangular {
(v[0], v[1], v[2]) =
Self::rectangular_to_lnglat(v[0], v[1], v[2], input_epsg);
};
// Change x and y; keep the height
// TODO: error handling
(v[0], v[1], _) = proj.project_forward(lng, lat, 0.).unwrap();
});
geom_store.epsg = self.output_epsg;
}
EPSG_JGD2011_JPRECT_I
| EPSG_JGD2011_JPRECT_II
| EPSG_JGD2011_JPRECT_III
| EPSG_JGD2011_JPRECT_IV
| EPSG_JGD2011_JPRECT_V
| EPSG_JGD2011_JPRECT_VI
| EPSG_JGD2011_JPRECT_VII
| EPSG_JGD2011_JPRECT_VIII
| EPSG_JGD2011_JPRECT_IX
| EPSG_JGD2011_JPRECT_X
| EPSG_JGD2011_JPRECT_XI
| EPSG_JGD2011_JPRECT_XII
| EPSG_JGD2011_JPRECT_XIII
| EPSG_JGD2011_JPRECT_XIV
| EPSG_JGD2011_JPRECT_XV
| EPSG_JGD2011_JPRECT_XVI
| EPSG_JGD2011_JPRECT_XVII
| EPSG_JGD2011_JPRECT_XVIII
| EPSG_JGD2011_JPRECT_XIX => {
// To Japan Plane Rectangular CS
let proj = self.jpr_zone_proj.as_ref().unwrap();
let mut geom_store = entity.geometry_store.write().unwrap();
geom_store.vertices.iter_mut().for_each(|v| {
let (lng, lat) = (v[1], v[0]);
if let Some(input_epsg) = rectangular {
(v[0], v[1], v[2]) =
Self::rectangular_to_lnglat(v[0], v[1], v[2], input_epsg);
};
// Change x and y; keep the height
// TODO: error handling
(v[0], v[1], _) = proj.project_forward(lng, lat, 0.).unwrap();
});
geom_store.epsg = self.output_epsg;
}
_ => {
panic!("Unsupported output CRS: {}", self.output_epsg);
}
};
}
fn transform_from_wgs84(&mut self, _entity: &Entity, _rectangular: Option<EpsgCode>) {
match self.output_epsg {
EPSG_WGS84_GEOGRAPHIC_3D => {
// Do nothing
}
EPSG_JGD2011_JPRECT_I_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_II_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_III_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IV_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_V_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_VIII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_IX_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_X_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XI_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XII_JGD2011_HEIGHT
| EPSG_JGD2011_JPRECT_XIII_JGD2011_HEIGHT => {
// TODO: implement
unimplemented!("WGS84 to EPSG:{} not supported yet", self.output_epsg);
}
EPSG_JGD2011_JPRECT_I
| EPSG_JGD2011_JPRECT_II
| EPSG_JGD2011_JPRECT_III
| EPSG_JGD2011_JPRECT_IV
| EPSG_JGD2011_JPRECT_V
| EPSG_JGD2011_JPRECT_VI
| EPSG_JGD2011_JPRECT_VII
| EPSG_JGD2011_JPRECT_VIII
| EPSG_JGD2011_JPRECT_IX
| EPSG_JGD2011_JPRECT_X
| EPSG_JGD2011_JPRECT_XI
| EPSG_JGD2011_JPRECT_XII
| EPSG_JGD2011_JPRECT_XIII
| EPSG_JGD2011_JPRECT_XIV
| EPSG_JGD2011_JPRECT_XV
| EPSG_JGD2011_JPRECT_XVI
| EPSG_JGD2011_JPRECT_XVII
| EPSG_JGD2011_JPRECT_XVIII
| EPSG_JGD2011_JPRECT_XIX => {
// TODO: implement
unimplemented!("WGS84 to EPSG:{} not supported yet", self.output_epsg);
}
_ => {
panic!("Unsupported output CRS: {}", self.output_epsg);
}
}
}
}