SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
proj_json.h
1#pragma once
2
3#include "proj.h"
4#include "nlohmann/json.hpp"
5
6namespace proj
7{
8 inline void to_json(nlohmann::json &j, const projection_t &p)
9 {
10 if (p.type == ProjType_Equirectangular)
11 j["type"] = "equirec";
12 else if (p.type == ProjType_Stereographic)
13 j["type"] = "stereo";
14 else if (p.type == ProjType_UniversalTransverseMercator)
15 j["type"] = "utm";
16 else if (p.type == ProjType_Geos)
17 j["type"] = "geos";
18 else if (p.type == ProjType_Tpers)
19 j["type"] = "tpers";
20 else if (p.type == ProjType_WebMerc)
21 j["type"] = "webmerc";
22 // else if (p.type == ProjType_LambertConformalConic)
23 // j["type"] = "lamcc";
24
25 if (p.type == ProjType_UniversalTransverseMercator)
26 {
27 j["zone"] = p.params.zone;
28 j["south"] = p.params.south;
29 }
30
31 if (p.type == ProjType_Geos)
32 {
33 j["altitude"] = p.params.altitude;
34 j["sweep_x"] = p.params.sweep_x;
35 }
36
37 if (p.type == ProjType_Tpers)
38 {
39 j["altitude"] = p.params.altitude;
40 j["tilt"] = p.params.tilt;
41 j["azimuth"] = p.params.azimuth;
42 }
43
44 if (p.proj_offset_x != 0)
45 j["offset_x"] = p.proj_offset_x;
46 if (p.proj_offset_y != 0)
47 j["offset_y"] = p.proj_offset_y;
48 if (p.proj_scalar_x != 1)
49 j["scalar_x"] = p.proj_scalar_x;
50 if (p.proj_scalar_y != 1)
51 j["scalar_y"] = p.proj_scalar_y;
52
53 if (p.lam0 != 0)
54 j["lon0"] = p.lam0 * RAD2DEG;
55 if (p.phi0 != 0)
56 j["lat0"] = p.phi0 * RAD2DEG;
57 }
58
59 inline void from_json(const nlohmann::json &j_in, projection_t &p)
60 {
61 nlohmann::json j = j_in;
62
63 if (j["type"].get<std::string>() == "equirec")
64 p.type = ProjType_Equirectangular;
65 else if (j["type"].get<std::string>() == "stereo")
66 p.type = ProjType_Stereographic;
67 else if (j["type"].get<std::string>() == "utm")
68 p.type = ProjType_UniversalTransverseMercator;
69 else if (j["type"].get<std::string>() == "geos")
70 p.type = ProjType_Geos;
71 else if (j["type"].get<std::string>() == "tpers")
72 p.type = ProjType_Tpers;
73 else if (j["type"].get<std::string>() == "webmerc")
74 p.type = ProjType_WebMerc;
75 // else if (j["type"].get<std::string>() == "lamcc")
76 // p.type = ProjType_LambertConformalConic;
77
79
80 if (p.type == ProjType_Stereographic &&
81 j.contains("center_lon") && j.contains("center_lat") && j.contains("scale") &&
82 j.contains("width") && j.contains("height"))
83 {
84 double projections_image_width = j["width"];
85 double projections_image_height = j["height"];
86 double projections_stereo_center_lon = j["center_lon"];
87 double projections_stereo_center_lat = j["center_lat"];
88 double projections_stereo_scale = j["scale"];
89 j["lon0"] = projections_stereo_center_lon;
90 j["lat0"] = projections_stereo_center_lat;
91 j["scalar_x"] = projections_stereo_scale;
92 j["scalar_y"] = -projections_stereo_scale;
93 j["offset_x"] = -projections_image_width * 0.5 * projections_stereo_scale;
94 j["offset_y"] = projections_image_height * 0.5 * projections_stereo_scale;
95 }
96 else if (p.type == ProjType_Tpers &&
97 j.contains("center_lon") && j.contains("center_lat") && j.contains("scale") &&
98 j.contains("width") && j.contains("height"))
99 {
100 double projections_image_width = j["width"];
101 double projections_image_height = j["height"];
102 double projections_tpers_center_lon = j["center_lon"];
103 double projections_tpers_center_lat = j["center_lat"];
104 double projections_tpers_scale = j["scale"];
105 j["lon0"] = projections_tpers_center_lon;
106 j["lat0"] = projections_tpers_center_lat;
107 j["scalar_x"] = projections_tpers_scale;
108 j["scalar_y"] = -projections_tpers_scale;
109 j["offset_x"] = -projections_image_width * 0.5 * projections_tpers_scale;
110 j["offset_y"] = projections_image_height * 0.5 * projections_tpers_scale;
111 }
112 else if (p.type == ProjType_UniversalTransverseMercator &&
113 j.contains("scale") &&
114 j.contains("width") && j.contains("height"))
115 {
116 double projections_image_width = j["width"];
117 double projections_image_height = j["height"];
118 double projections_utm_scale = j["scale"];
119 double projections_utm_offset_y = j.contains("offset_y") ? j["offset_y"].get<double>() : 0.0;
120 j["scalar_x"] = projections_utm_scale;
121 j["scalar_y"] = -projections_utm_scale;
122 j["offset_x"] = -projections_image_width * 0.5 * projections_utm_scale;
123 j["offset_y"] = projections_image_height * 0.5 * projections_utm_scale + projections_utm_offset_y;
124 }
125
127
128 if (p.type == ProjType_UniversalTransverseMercator)
129 {
130 if (j.contains("zone"))
131 p.params.zone = j["zone"];
132 if (j.contains("south"))
133 p.params.south = j["south"];
134 }
135
136 if (p.type == ProjType_Geos)
137 {
138 if (j.contains("altitude"))
139 p.params.altitude = j["altitude"];
140 if (j.contains("sweep_x"))
141 p.params.sweep_x = j["sweep_x"];
142 }
143
144 if (p.type == ProjType_Tpers)
145 {
146 if (j.contains("altitude"))
147 p.params.altitude = j["altitude"];
148 if (j.contains("tilt"))
149 p.params.tilt = j["tilt"];
150 if (j.contains("azimuth"))
151 p.params.azimuth = j["azimuth"];
152 }
153
155
156 if (j.contains("offset_x"))
157 p.proj_offset_x = j["offset_x"];
158 if (j.contains("offset_y"))
159 p.proj_offset_y = j["offset_y"];
160 if (j.contains("scalar_x"))
161 p.proj_scalar_x = j["scalar_x"];
162 if (j.contains("scalar_y"))
163 p.proj_scalar_y = j["scalar_y"];
164
165 if (j.contains("lon0"))
166 p.lam0 = j["lon0"].get<double>() * DEG2RAD;
167 if (j.contains("lat0"))
168 p.phi0 = j["lat0"].get<double>() * DEG2RAD;
169 }
170}
Definition proj.h:36