/src/proj/src/crs_to_crs.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * Project: PROJ |
3 | | * Purpose: Implements proj_create_crs_to_crs() and the like |
4 | | * |
5 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2018-2024, Even Rouault, <even.rouault at spatialys.com> |
9 | | * |
10 | | * Permission is hereby granted, free of charge, to any person obtaining a |
11 | | * copy of this software and associated documentation files (the "Software"), |
12 | | * to deal in the Software without restriction, including without limitation |
13 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
14 | | * and/or sell copies of the Software, and to permit persons to whom the |
15 | | * Software is furnished to do so, subject to the following conditions: |
16 | | * |
17 | | * The above copyright notice and this permission notice shall be included |
18 | | * in all copies or substantial portions of the Software. |
19 | | * |
20 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
21 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
23 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
25 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
26 | | * DEALINGS IN THE SOFTWARE. |
27 | | *****************************************************************************/ |
28 | | |
29 | | #define FROM_PROJ_CPP |
30 | | |
31 | | #include "proj.h" |
32 | | #include "proj_internal.h" |
33 | | #include <math.h> |
34 | | |
35 | | #include <algorithm> |
36 | | #include <limits> |
37 | | |
38 | | #include "proj/internal/internal.hpp" |
39 | | |
40 | | using namespace NS_PROJ::internal; |
41 | | |
42 | | /** Adds a " +type=crs" suffix to a PROJ string (if it is a PROJ string) */ |
43 | 142 | std::string pj_add_type_crs_if_needed(const std::string &str) { |
44 | 142 | std::string ret(str); |
45 | 142 | if ((starts_with(str, "proj=") || starts_with(str, "+proj=") || |
46 | 142 | starts_with(str, "+init=") || starts_with(str, "+title=")) && |
47 | 0 | str.find("type=crs") == std::string::npos) { |
48 | 0 | ret += " +type=crs"; |
49 | 0 | } |
50 | 142 | return ret; |
51 | 142 | } |
52 | | |
53 | | /*****************************************************************************/ |
54 | | static void reproject_bbox(PJ *pjGeogToCrs, double west_lon, double south_lat, |
55 | | double east_lon, double north_lat, double &minx, |
56 | 0 | double &miny, double &maxx, double &maxy) { |
57 | | /*****************************************************************************/ |
58 | |
|
59 | 0 | minx = -std::numeric_limits<double>::max(); |
60 | 0 | miny = -std::numeric_limits<double>::max(); |
61 | 0 | maxx = std::numeric_limits<double>::max(); |
62 | 0 | maxy = std::numeric_limits<double>::max(); |
63 | |
|
64 | 0 | if (!(west_lon == -180.0 && east_lon == 180.0 && south_lat == -90.0 && |
65 | 0 | north_lat == 90.0)) { |
66 | 0 | minx = -minx; |
67 | 0 | miny = -miny; |
68 | 0 | maxx = -maxx; |
69 | 0 | maxy = -maxy; |
70 | |
|
71 | 0 | constexpr int N_STEPS = 20; |
72 | 0 | constexpr int N_STEPS_P1 = N_STEPS + 1; |
73 | 0 | constexpr int XY_SIZE = N_STEPS_P1 * 4; |
74 | 0 | std::vector<double> x(XY_SIZE); |
75 | 0 | std::vector<double> y(XY_SIZE); |
76 | 0 | const double step_lon = (east_lon - west_lon) / N_STEPS; |
77 | 0 | const double step_lat = (north_lat - south_lat) / N_STEPS; |
78 | 0 | for (int j = 0; j <= N_STEPS; j++) { |
79 | 0 | x[j] = west_lon + j * step_lon; |
80 | 0 | y[j] = south_lat; |
81 | 0 | x[N_STEPS_P1 + j] = x[j]; |
82 | 0 | y[N_STEPS_P1 + j] = north_lat; |
83 | 0 | x[N_STEPS_P1 * 2 + j] = west_lon; |
84 | 0 | y[N_STEPS_P1 * 2 + j] = south_lat + j * step_lat; |
85 | 0 | x[N_STEPS_P1 * 3 + j] = east_lon; |
86 | 0 | y[N_STEPS_P1 * 3 + j] = y[N_STEPS_P1 * 2 + j]; |
87 | 0 | } |
88 | 0 | proj_trans_generic(pjGeogToCrs, PJ_FWD, &x[0], sizeof(double), XY_SIZE, |
89 | 0 | &y[0], sizeof(double), XY_SIZE, nullptr, 0, 0, |
90 | 0 | nullptr, 0, 0); |
91 | 0 | for (int j = 0; j < XY_SIZE; j++) { |
92 | 0 | if (x[j] != HUGE_VAL && y[j] != HUGE_VAL) { |
93 | 0 | minx = std::min(minx, x[j]); |
94 | 0 | miny = std::min(miny, y[j]); |
95 | 0 | maxx = std::max(maxx, x[j]); |
96 | 0 | maxy = std::max(maxy, y[j]); |
97 | 0 | } |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | | /*****************************************************************************/ |
103 | | static PJ *add_coord_op_to_list( |
104 | | int idxInOriginalList, PJ *op, double west_lon, double south_lat, |
105 | | double east_lon, double north_lat, PJ *pjGeogToSrc, PJ *pjGeogToDst, |
106 | | const PJ *pjSrcGeocentricToLonLat, const PJ *pjDstGeocentricToLonLat, |
107 | 0 | const char *areaName, std::vector<PJCoordOperation> &altCoordOps) { |
108 | | /*****************************************************************************/ |
109 | |
|
110 | 0 | double minxSrc; |
111 | 0 | double minySrc; |
112 | 0 | double maxxSrc; |
113 | 0 | double maxySrc; |
114 | 0 | double minxDst; |
115 | 0 | double minyDst; |
116 | 0 | double maxxDst; |
117 | 0 | double maxyDst; |
118 | |
|
119 | 0 | double w = west_lon / 180 * M_PI; |
120 | 0 | double s = south_lat / 180 * M_PI; |
121 | 0 | double e = east_lon / 180 * M_PI; |
122 | 0 | double n = north_lat / 180 * M_PI; |
123 | 0 | if (w > e) { |
124 | 0 | e += 2 * M_PI; |
125 | 0 | } |
126 | | // Integrate cos(lat) between south_lat and north_lat |
127 | 0 | const double pseudoArea = (e - w) * (std::sin(n) - std::sin(s)); |
128 | |
|
129 | 0 | if (pjSrcGeocentricToLonLat) { |
130 | 0 | minxSrc = west_lon; |
131 | 0 | minySrc = south_lat; |
132 | 0 | maxxSrc = east_lon; |
133 | 0 | maxySrc = north_lat; |
134 | 0 | } else { |
135 | 0 | reproject_bbox(pjGeogToSrc, west_lon, south_lat, east_lon, north_lat, |
136 | 0 | minxSrc, minySrc, maxxSrc, maxySrc); |
137 | 0 | } |
138 | |
|
139 | 0 | if (pjDstGeocentricToLonLat) { |
140 | 0 | minxDst = west_lon; |
141 | 0 | minyDst = south_lat; |
142 | 0 | maxxDst = east_lon; |
143 | 0 | maxyDst = north_lat; |
144 | 0 | } else { |
145 | 0 | reproject_bbox(pjGeogToDst, west_lon, south_lat, east_lon, north_lat, |
146 | 0 | minxDst, minyDst, maxxDst, maxyDst); |
147 | 0 | } |
148 | |
|
149 | 0 | if (minxSrc <= maxxSrc && minxDst <= maxxDst) { |
150 | 0 | const char *c_name = proj_get_name(op); |
151 | 0 | std::string name(c_name ? c_name : ""); |
152 | |
|
153 | 0 | const double accuracy = proj_coordoperation_get_accuracy(op->ctx, op); |
154 | 0 | altCoordOps.emplace_back( |
155 | 0 | idxInOriginalList, minxSrc, minySrc, maxxSrc, maxySrc, minxDst, |
156 | 0 | minyDst, maxxDst, maxyDst, op, name, accuracy, pseudoArea, areaName, |
157 | 0 | pjSrcGeocentricToLonLat, pjDstGeocentricToLonLat); |
158 | 0 | op = nullptr; |
159 | 0 | } |
160 | 0 | return op; |
161 | 0 | } |
162 | | |
163 | | namespace { |
164 | | struct ObjectKeeper { |
165 | | PJ *m_obj = nullptr; |
166 | 0 | explicit ObjectKeeper(PJ *obj) : m_obj(obj) {} |
167 | 0 | ~ObjectKeeper() { proj_destroy(m_obj); } |
168 | | ObjectKeeper(const ObjectKeeper &) = delete; |
169 | | ObjectKeeper &operator=(const ObjectKeeper &) = delete; |
170 | | }; |
171 | | } // namespace |
172 | | |
173 | | /*****************************************************************************/ |
174 | 0 | static PJ *create_operation_to_geog_crs(PJ_CONTEXT *ctx, const PJ *crs) { |
175 | | /*****************************************************************************/ |
176 | |
|
177 | 0 | std::unique_ptr<ObjectKeeper> keeper; |
178 | 0 | if (proj_get_type(crs) == PJ_TYPE_COORDINATE_METADATA) { |
179 | 0 | auto tmp = proj_get_source_crs(ctx, crs); |
180 | 0 | assert(tmp); |
181 | 0 | keeper.reset(new ObjectKeeper(tmp)); |
182 | 0 | crs = tmp; |
183 | 0 | } |
184 | 0 | (void)keeper; |
185 | | |
186 | | // Create a geographic 2D long-lat degrees CRS that is related to the |
187 | | // CRS |
188 | 0 | auto geodetic_crs = proj_crs_get_geodetic_crs(ctx, crs); |
189 | 0 | if (!geodetic_crs) { |
190 | 0 | proj_context_log_debug(ctx, "Cannot find geodetic CRS matching CRS"); |
191 | 0 | return nullptr; |
192 | 0 | } |
193 | | |
194 | 0 | auto geodetic_crs_type = proj_get_type(geodetic_crs); |
195 | 0 | if (geodetic_crs_type == PJ_TYPE_GEOCENTRIC_CRS || |
196 | 0 | geodetic_crs_type == PJ_TYPE_GEOGRAPHIC_2D_CRS || |
197 | 0 | geodetic_crs_type == PJ_TYPE_GEOGRAPHIC_3D_CRS) { |
198 | 0 | auto datum = proj_crs_get_datum_forced(ctx, geodetic_crs); |
199 | 0 | assert(datum); |
200 | 0 | auto cs = proj_create_ellipsoidal_2D_cs( |
201 | 0 | ctx, PJ_ELLPS2D_LONGITUDE_LATITUDE, nullptr, 0); |
202 | 0 | auto ellps = proj_get_ellipsoid(ctx, datum); |
203 | 0 | proj_destroy(datum); |
204 | 0 | double semi_major_metre = 0; |
205 | 0 | double inv_flattening = 0; |
206 | 0 | proj_ellipsoid_get_parameters(ctx, ellps, &semi_major_metre, nullptr, |
207 | 0 | nullptr, &inv_flattening); |
208 | | // It is critical to set the prime meridian to 0 |
209 | 0 | auto temp = proj_create_geographic_crs( |
210 | 0 | ctx, "unnamed crs", "unnamed datum", proj_get_name(ellps), |
211 | 0 | semi_major_metre, inv_flattening, "Reference prime meridian", 0, |
212 | 0 | nullptr, 0, cs); |
213 | 0 | proj_destroy(ellps); |
214 | 0 | proj_destroy(cs); |
215 | 0 | proj_destroy(geodetic_crs); |
216 | 0 | geodetic_crs = temp; |
217 | 0 | geodetic_crs_type = proj_get_type(geodetic_crs); |
218 | 0 | } |
219 | 0 | if (geodetic_crs_type != PJ_TYPE_GEOGRAPHIC_2D_CRS) { |
220 | | // Shouldn't happen |
221 | 0 | proj_context_log_debug(ctx, "Cannot find geographic CRS matching CRS"); |
222 | 0 | proj_destroy(geodetic_crs); |
223 | 0 | return nullptr; |
224 | 0 | } |
225 | | |
226 | | // Create the transformation from this geographic 2D CRS to the source CRS |
227 | 0 | auto operation_ctx = proj_create_operation_factory_context(ctx, nullptr); |
228 | 0 | proj_operation_factory_context_set_spatial_criterion( |
229 | 0 | ctx, operation_ctx, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION); |
230 | 0 | proj_operation_factory_context_set_grid_availability_use( |
231 | 0 | ctx, operation_ctx, |
232 | 0 | PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID); |
233 | 0 | auto target_crs_2D = proj_crs_demote_to_2D(ctx, nullptr, crs); |
234 | 0 | auto op_list_to_geodetic = |
235 | 0 | proj_create_operations(ctx, geodetic_crs, target_crs_2D, operation_ctx); |
236 | 0 | proj_destroy(target_crs_2D); |
237 | 0 | proj_operation_factory_context_destroy(operation_ctx); |
238 | 0 | proj_destroy(geodetic_crs); |
239 | |
|
240 | 0 | const int nOpCount = op_list_to_geodetic == nullptr |
241 | 0 | ? 0 |
242 | 0 | : proj_list_get_count(op_list_to_geodetic); |
243 | 0 | if (nOpCount == 0) { |
244 | 0 | proj_context_log_debug( |
245 | 0 | ctx, "Cannot compute transformation from geographic CRS to CRS"); |
246 | 0 | proj_list_destroy(op_list_to_geodetic); |
247 | 0 | return nullptr; |
248 | 0 | } |
249 | 0 | PJ *opGeogToCrs = nullptr; |
250 | | // Use in priority operations *without* grids |
251 | 0 | for (int i = 0; i < nOpCount; i++) { |
252 | 0 | auto op = proj_list_get(ctx, op_list_to_geodetic, i); |
253 | 0 | assert(op); |
254 | 0 | if (proj_coordoperation_get_grid_used_count(ctx, op) == 0) { |
255 | 0 | opGeogToCrs = op; |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | proj_destroy(op); |
259 | 0 | } |
260 | 0 | if (opGeogToCrs == nullptr) { |
261 | 0 | opGeogToCrs = proj_list_get(ctx, op_list_to_geodetic, 0); |
262 | 0 | assert(opGeogToCrs); |
263 | 0 | } |
264 | 0 | proj_list_destroy(op_list_to_geodetic); |
265 | 0 | return opGeogToCrs; |
266 | 0 | } |
267 | | |
268 | | /*****************************************************************************/ |
269 | | static PJ *create_operation_geocentric_crs_to_geog_crs(PJ_CONTEXT *ctx, |
270 | | const PJ *geocentric_crs) |
271 | | /*****************************************************************************/ |
272 | 0 | { |
273 | 0 | assert(proj_get_type(geocentric_crs) == PJ_TYPE_GEOCENTRIC_CRS); |
274 | |
|
275 | 0 | auto datum = proj_crs_get_datum_forced(ctx, geocentric_crs); |
276 | 0 | assert(datum); |
277 | 0 | auto cs = proj_create_ellipsoidal_2D_cs(ctx, PJ_ELLPS2D_LONGITUDE_LATITUDE, |
278 | 0 | nullptr, 0); |
279 | 0 | auto ellps = proj_get_ellipsoid(ctx, datum); |
280 | 0 | proj_destroy(datum); |
281 | 0 | double semi_major_metre = 0; |
282 | 0 | double inv_flattening = 0; |
283 | 0 | proj_ellipsoid_get_parameters(ctx, ellps, &semi_major_metre, nullptr, |
284 | 0 | nullptr, &inv_flattening); |
285 | | // It is critical to set the prime meridian to 0 |
286 | 0 | auto lon_lat_crs = proj_create_geographic_crs( |
287 | 0 | ctx, "unnamed crs", "unnamed datum", proj_get_name(ellps), |
288 | 0 | semi_major_metre, inv_flattening, "Reference prime meridian", 0, |
289 | 0 | nullptr, 0, cs); |
290 | 0 | proj_destroy(ellps); |
291 | 0 | proj_destroy(cs); |
292 | | |
293 | | // Create the transformation from this geocentric CRS to the lon-lat one |
294 | 0 | auto operation_ctx = proj_create_operation_factory_context(ctx, nullptr); |
295 | 0 | proj_operation_factory_context_set_spatial_criterion( |
296 | 0 | ctx, operation_ctx, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION); |
297 | 0 | proj_operation_factory_context_set_grid_availability_use( |
298 | 0 | ctx, operation_ctx, |
299 | 0 | PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID); |
300 | 0 | auto op_list = |
301 | 0 | proj_create_operations(ctx, geocentric_crs, lon_lat_crs, operation_ctx); |
302 | 0 | proj_operation_factory_context_destroy(operation_ctx); |
303 | 0 | proj_destroy(lon_lat_crs); |
304 | |
|
305 | 0 | const int nOpCount = op_list == nullptr ? 0 : proj_list_get_count(op_list); |
306 | 0 | if (nOpCount != 1) { |
307 | 0 | proj_context_log_debug(ctx, "Cannot compute transformation from " |
308 | 0 | "geocentric CRS to geographic CRS"); |
309 | 0 | proj_list_destroy(op_list); |
310 | 0 | return nullptr; |
311 | 0 | } |
312 | | |
313 | 0 | auto op = proj_list_get(ctx, op_list, 0); |
314 | 0 | assert(op); |
315 | 0 | proj_list_destroy(op_list); |
316 | 0 | return op; |
317 | 0 | } |
318 | | |
319 | | /*****************************************************************************/ |
320 | | PJ *proj_create_crs_to_crs(PJ_CONTEXT *ctx, const char *source_crs, |
321 | 0 | const char *target_crs, PJ_AREA *area) { |
322 | | /****************************************************************************** |
323 | | Create a transformation pipeline between two known coordinate reference |
324 | | systems. |
325 | | |
326 | | See docs/source/development/reference/functions.rst |
327 | | |
328 | | ******************************************************************************/ |
329 | 0 | if (!ctx) { |
330 | 0 | ctx = pj_get_default_ctx(); |
331 | 0 | } |
332 | |
|
333 | 0 | PJ *src; |
334 | 0 | PJ *dst; |
335 | 0 | try { |
336 | 0 | std::string source_crs_modified(pj_add_type_crs_if_needed(source_crs)); |
337 | 0 | std::string target_crs_modified(pj_add_type_crs_if_needed(target_crs)); |
338 | |
|
339 | 0 | src = proj_create(ctx, source_crs_modified.c_str()); |
340 | 0 | if (!src) { |
341 | 0 | proj_context_log_debug(ctx, "Cannot instantiate source_crs"); |
342 | 0 | return nullptr; |
343 | 0 | } |
344 | | |
345 | 0 | dst = proj_create(ctx, target_crs_modified.c_str()); |
346 | 0 | if (!dst) { |
347 | 0 | proj_context_log_debug(ctx, "Cannot instantiate target_crs"); |
348 | 0 | proj_destroy(src); |
349 | 0 | return nullptr; |
350 | 0 | } |
351 | 0 | } catch (const std::exception &) { |
352 | 0 | return nullptr; |
353 | 0 | } |
354 | | |
355 | 0 | auto ret = proj_create_crs_to_crs_from_pj(ctx, src, dst, area, nullptr); |
356 | 0 | proj_destroy(src); |
357 | 0 | proj_destroy(dst); |
358 | 0 | return ret; |
359 | 0 | } |
360 | | |
361 | | /*****************************************************************************/ |
362 | | std::vector<PJCoordOperation> |
363 | | pj_create_prepared_operations(PJ_CONTEXT *ctx, const PJ *source_crs, |
364 | | const PJ *target_crs, PJ_OBJ_LIST *op_list) |
365 | | /*****************************************************************************/ |
366 | 0 | { |
367 | 0 | PJ *pjGeogToSrc = nullptr; |
368 | 0 | PJ *pjSrcGeocentricToLonLat = nullptr; |
369 | 0 | if (proj_get_type(source_crs) == PJ_TYPE_GEOCENTRIC_CRS) { |
370 | 0 | pjSrcGeocentricToLonLat = |
371 | 0 | create_operation_geocentric_crs_to_geog_crs(ctx, source_crs); |
372 | 0 | if (!pjSrcGeocentricToLonLat) { |
373 | | // shouldn't happen |
374 | 0 | return {}; |
375 | 0 | } |
376 | 0 | } else { |
377 | 0 | pjGeogToSrc = create_operation_to_geog_crs(ctx, source_crs); |
378 | 0 | if (!pjGeogToSrc) { |
379 | 0 | proj_context_log_debug( |
380 | 0 | ctx, "Cannot create transformation from geographic " |
381 | 0 | "CRS of source CRS to source CRS"); |
382 | 0 | return {}; |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | 0 | PJ *pjGeogToDst = nullptr; |
387 | 0 | PJ *pjDstGeocentricToLonLat = nullptr; |
388 | 0 | if (proj_get_type(target_crs) == PJ_TYPE_GEOCENTRIC_CRS) { |
389 | 0 | pjDstGeocentricToLonLat = |
390 | 0 | create_operation_geocentric_crs_to_geog_crs(ctx, target_crs); |
391 | 0 | if (!pjDstGeocentricToLonLat) { |
392 | | // shouldn't happen |
393 | 0 | proj_destroy(pjSrcGeocentricToLonLat); |
394 | 0 | proj_destroy(pjGeogToSrc); |
395 | 0 | return {}; |
396 | 0 | } |
397 | 0 | } else { |
398 | 0 | pjGeogToDst = create_operation_to_geog_crs(ctx, target_crs); |
399 | 0 | if (!pjGeogToDst) { |
400 | 0 | proj_context_log_debug( |
401 | 0 | ctx, "Cannot create transformation from geographic " |
402 | 0 | "CRS of target CRS to target CRS"); |
403 | 0 | proj_destroy(pjSrcGeocentricToLonLat); |
404 | 0 | proj_destroy(pjGeogToSrc); |
405 | 0 | return {}; |
406 | 0 | } |
407 | 0 | } |
408 | | |
409 | 0 | try { |
410 | 0 | std::vector<PJCoordOperation> preparedOpList; |
411 | | |
412 | | // Iterate over source->target candidate transformations and reproject |
413 | | // their long-lat bounding box into the source CRS. |
414 | 0 | const auto op_count = proj_list_get_count(op_list); |
415 | 0 | for (int i = 0; i < op_count; i++) { |
416 | 0 | auto op = proj_list_get(ctx, op_list, i); |
417 | 0 | assert(op); |
418 | 0 | double west_lon = 0.0; |
419 | 0 | double south_lat = 0.0; |
420 | 0 | double east_lon = 0.0; |
421 | 0 | double north_lat = 0.0; |
422 | |
|
423 | 0 | const char *areaName = nullptr; |
424 | 0 | if (!proj_get_area_of_use(ctx, op, &west_lon, &south_lat, &east_lon, |
425 | 0 | &north_lat, &areaName)) { |
426 | 0 | west_lon = -180; |
427 | 0 | south_lat = -90; |
428 | 0 | east_lon = 180; |
429 | 0 | north_lat = 90; |
430 | 0 | } |
431 | |
|
432 | 0 | if (west_lon <= east_lon) { |
433 | 0 | op = add_coord_op_to_list( |
434 | 0 | i, op, west_lon, south_lat, east_lon, north_lat, |
435 | 0 | pjGeogToSrc, pjGeogToDst, pjSrcGeocentricToLonLat, |
436 | 0 | pjDstGeocentricToLonLat, areaName, preparedOpList); |
437 | 0 | } else { |
438 | 0 | auto op_clone = proj_clone(ctx, op); |
439 | |
|
440 | 0 | op = add_coord_op_to_list( |
441 | 0 | i, op, west_lon, south_lat, 180, north_lat, pjGeogToSrc, |
442 | 0 | pjGeogToDst, pjSrcGeocentricToLonLat, |
443 | 0 | pjDstGeocentricToLonLat, areaName, preparedOpList); |
444 | 0 | op_clone = add_coord_op_to_list( |
445 | 0 | i, op_clone, -180, south_lat, east_lon, north_lat, |
446 | 0 | pjGeogToSrc, pjGeogToDst, pjSrcGeocentricToLonLat, |
447 | 0 | pjDstGeocentricToLonLat, areaName, preparedOpList); |
448 | 0 | proj_destroy(op_clone); |
449 | 0 | } |
450 | |
|
451 | 0 | proj_destroy(op); |
452 | 0 | } |
453 | |
|
454 | 0 | proj_destroy(pjGeogToSrc); |
455 | 0 | proj_destroy(pjGeogToDst); |
456 | 0 | proj_destroy(pjSrcGeocentricToLonLat); |
457 | 0 | proj_destroy(pjDstGeocentricToLonLat); |
458 | 0 | return preparedOpList; |
459 | 0 | } catch (const std::exception &) { |
460 | 0 | proj_destroy(pjGeogToSrc); |
461 | 0 | proj_destroy(pjGeogToDst); |
462 | 0 | proj_destroy(pjSrcGeocentricToLonLat); |
463 | 0 | proj_destroy(pjDstGeocentricToLonLat); |
464 | 0 | return {}; |
465 | 0 | } |
466 | 0 | } |
467 | | |
468 | | // --------------------------------------------------------------------------- |
469 | | |
470 | | //! @cond Doxygen_Suppress |
471 | | static const char *getOptionValue(const char *option, |
472 | 0 | const char *keyWithEqual) noexcept { |
473 | 0 | if (ci_starts_with(option, keyWithEqual)) { |
474 | 0 | return option + strlen(keyWithEqual); |
475 | 0 | } |
476 | 0 | return nullptr; |
477 | 0 | } |
478 | | //! @endcond |
479 | | |
480 | | /*****************************************************************************/ |
481 | | PJ *proj_create_crs_to_crs_from_pj(PJ_CONTEXT *ctx, const PJ *source_crs, |
482 | | const PJ *target_crs, PJ_AREA *area, |
483 | 0 | const char *const *options) { |
484 | | /****************************************************************************** |
485 | | Create a transformation pipeline between two known coordinate reference |
486 | | systems. |
487 | | |
488 | | See docs/source/development/reference/functions.rst |
489 | | |
490 | | ******************************************************************************/ |
491 | 0 | if (!ctx) { |
492 | 0 | ctx = pj_get_default_ctx(); |
493 | 0 | } |
494 | 0 | pj_load_ini( |
495 | 0 | ctx); // to set ctx->errorIfBestTransformationNotAvailableDefault |
496 | |
|
497 | 0 | const char *authority = nullptr; |
498 | 0 | double accuracy = -1; |
499 | 0 | bool allowBallparkTransformations = true; |
500 | 0 | bool forceOver = false; |
501 | 0 | bool warnIfBestTransformationNotAvailable = |
502 | 0 | ctx->warnIfBestTransformationNotAvailableDefault; |
503 | 0 | bool errorIfBestTransformationNotAvailable = |
504 | 0 | ctx->errorIfBestTransformationNotAvailableDefault; |
505 | 0 | for (auto iter = options; iter && iter[0]; ++iter) { |
506 | 0 | const char *value; |
507 | 0 | if ((value = getOptionValue(*iter, "AUTHORITY="))) { |
508 | 0 | authority = value; |
509 | 0 | } else if ((value = getOptionValue(*iter, "ACCURACY="))) { |
510 | 0 | accuracy = pj_atof(value); |
511 | 0 | } else if ((value = getOptionValue(*iter, "ALLOW_BALLPARK="))) { |
512 | 0 | if (ci_equal(value, "yes")) |
513 | 0 | allowBallparkTransformations = true; |
514 | 0 | else if (ci_equal(value, "no")) |
515 | 0 | allowBallparkTransformations = false; |
516 | 0 | else { |
517 | 0 | ctx->logger(ctx->logger_app_data, PJ_LOG_ERROR, |
518 | 0 | "Invalid value for ALLOW_BALLPARK option."); |
519 | 0 | return nullptr; |
520 | 0 | } |
521 | 0 | } else if ((value = getOptionValue(*iter, "ONLY_BEST="))) { |
522 | 0 | warnIfBestTransformationNotAvailable = false; |
523 | 0 | if (ci_equal(value, "yes")) |
524 | 0 | errorIfBestTransformationNotAvailable = true; |
525 | 0 | else if (ci_equal(value, "no")) |
526 | 0 | errorIfBestTransformationNotAvailable = false; |
527 | 0 | else { |
528 | 0 | ctx->logger(ctx->logger_app_data, PJ_LOG_ERROR, |
529 | 0 | "Invalid value for ONLY_BEST option."); |
530 | 0 | return nullptr; |
531 | 0 | } |
532 | 0 | } else if ((value = getOptionValue(*iter, "FORCE_OVER="))) { |
533 | 0 | if (ci_equal(value, "yes")) { |
534 | 0 | forceOver = true; |
535 | 0 | } |
536 | 0 | } else { |
537 | 0 | std::string msg("Unknown option :"); |
538 | 0 | msg += *iter; |
539 | 0 | ctx->logger(ctx->logger_app_data, PJ_LOG_ERROR, msg.c_str()); |
540 | 0 | return nullptr; |
541 | 0 | } |
542 | 0 | } |
543 | | |
544 | 0 | auto operation_ctx = proj_create_operation_factory_context(ctx, authority); |
545 | 0 | if (!operation_ctx) { |
546 | 0 | return nullptr; |
547 | 0 | } |
548 | | |
549 | 0 | proj_operation_factory_context_set_allow_ballpark_transformations( |
550 | 0 | ctx, operation_ctx, allowBallparkTransformations); |
551 | |
|
552 | 0 | if (accuracy >= 0) { |
553 | 0 | proj_operation_factory_context_set_desired_accuracy(ctx, operation_ctx, |
554 | 0 | accuracy); |
555 | 0 | } |
556 | |
|
557 | 0 | if (area && area->bbox_set) { |
558 | 0 | proj_operation_factory_context_set_area_of_interest( |
559 | 0 | ctx, operation_ctx, area->west_lon_degree, area->south_lat_degree, |
560 | 0 | area->east_lon_degree, area->north_lat_degree); |
561 | |
|
562 | 0 | if (!area->name.empty()) { |
563 | 0 | proj_operation_factory_context_set_area_of_interest_name( |
564 | 0 | ctx, operation_ctx, area->name.c_str()); |
565 | 0 | } |
566 | 0 | } |
567 | |
|
568 | 0 | proj_operation_factory_context_set_spatial_criterion( |
569 | 0 | ctx, operation_ctx, PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION); |
570 | 0 | proj_operation_factory_context_set_grid_availability_use( |
571 | 0 | ctx, operation_ctx, |
572 | 0 | (errorIfBestTransformationNotAvailable || |
573 | 0 | warnIfBestTransformationNotAvailable || |
574 | 0 | proj_context_is_network_enabled(ctx)) |
575 | 0 | ? PROJ_GRID_AVAILABILITY_KNOWN_AVAILABLE |
576 | 0 | : PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID); |
577 | |
|
578 | 0 | auto op_list = |
579 | 0 | proj_create_operations(ctx, source_crs, target_crs, operation_ctx); |
580 | 0 | proj_operation_factory_context_destroy(operation_ctx); |
581 | |
|
582 | 0 | if (!op_list) { |
583 | 0 | return nullptr; |
584 | 0 | } |
585 | | |
586 | 0 | auto op_count = proj_list_get_count(op_list); |
587 | 0 | if (op_count == 0) { |
588 | 0 | proj_list_destroy(op_list); |
589 | |
|
590 | 0 | proj_context_log_debug(ctx, "No operation found matching criteria"); |
591 | 0 | return nullptr; |
592 | 0 | } |
593 | | |
594 | 0 | ctx->forceOver = forceOver; |
595 | |
|
596 | 0 | const int old_debug_level = ctx->debug_level; |
597 | 0 | if (errorIfBestTransformationNotAvailable || |
598 | 0 | warnIfBestTransformationNotAvailable) |
599 | 0 | ctx->debug_level = PJ_LOG_NONE; |
600 | 0 | PJ *P = proj_list_get(ctx, op_list, 0); |
601 | 0 | ctx->debug_level = old_debug_level; |
602 | 0 | assert(P); |
603 | |
|
604 | 0 | if (P != nullptr) { |
605 | 0 | P->errorIfBestTransformationNotAvailable = |
606 | 0 | errorIfBestTransformationNotAvailable; |
607 | 0 | P->warnIfBestTransformationNotAvailable = |
608 | 0 | warnIfBestTransformationNotAvailable; |
609 | 0 | P->skipNonInstantiable = warnIfBestTransformationNotAvailable; |
610 | 0 | } |
611 | |
|
612 | 0 | const bool mayNeedToReRunWithDiscardMissing = |
613 | 0 | (errorIfBestTransformationNotAvailable || |
614 | 0 | warnIfBestTransformationNotAvailable) && |
615 | 0 | !proj_context_is_network_enabled(ctx); |
616 | 0 | int singleOpIsInstanciable = -1; |
617 | 0 | if (P != nullptr && op_count == 1 && mayNeedToReRunWithDiscardMissing) { |
618 | 0 | singleOpIsInstanciable = proj_coordoperation_is_instantiable(ctx, P); |
619 | 0 | } |
620 | |
|
621 | 0 | const auto backup_errno = proj_context_errno(ctx); |
622 | 0 | if (P == nullptr || |
623 | 0 | (op_count == 1 && (!mayNeedToReRunWithDiscardMissing || |
624 | 0 | errorIfBestTransformationNotAvailable || |
625 | 0 | singleOpIsInstanciable == static_cast<int>(true)))) { |
626 | 0 | proj_list_destroy(op_list); |
627 | 0 | ctx->forceOver = false; |
628 | |
|
629 | 0 | if (P != nullptr && (errorIfBestTransformationNotAvailable || |
630 | 0 | warnIfBestTransformationNotAvailable)) { |
631 | 0 | if (singleOpIsInstanciable < 0) { |
632 | 0 | singleOpIsInstanciable = |
633 | 0 | proj_coordoperation_is_instantiable(ctx, P); |
634 | 0 | } |
635 | 0 | if (!singleOpIsInstanciable) { |
636 | 0 | pj_warn_about_missing_grid(P); |
637 | 0 | if (errorIfBestTransformationNotAvailable) { |
638 | 0 | proj_destroy(P); |
639 | 0 | return nullptr; |
640 | 0 | } |
641 | 0 | } |
642 | 0 | } |
643 | | |
644 | 0 | if (P != nullptr) { |
645 | 0 | P->over = forceOver; |
646 | 0 | } |
647 | 0 | return P; |
648 | 0 | } else if (op_count == 1 && mayNeedToReRunWithDiscardMissing && |
649 | 0 | !singleOpIsInstanciable) { |
650 | 0 | pj_warn_about_missing_grid(P); |
651 | 0 | } |
652 | | |
653 | 0 | if (errorIfBestTransformationNotAvailable || |
654 | 0 | warnIfBestTransformationNotAvailable) |
655 | 0 | ctx->debug_level = PJ_LOG_NONE; |
656 | 0 | auto preparedOpList = |
657 | 0 | pj_create_prepared_operations(ctx, source_crs, target_crs, op_list); |
658 | 0 | ctx->debug_level = old_debug_level; |
659 | |
|
660 | 0 | ctx->forceOver = false; |
661 | 0 | proj_list_destroy(op_list); |
662 | |
|
663 | 0 | if (preparedOpList.empty()) { |
664 | 0 | proj_destroy(P); |
665 | 0 | return nullptr; |
666 | 0 | } |
667 | | |
668 | 0 | bool foundInstanciableAndNonBallpark = false; |
669 | |
|
670 | 0 | for (auto &op : preparedOpList) { |
671 | 0 | op.pj->over = forceOver; |
672 | 0 | op.pj->errorIfBestTransformationNotAvailable = |
673 | 0 | errorIfBestTransformationNotAvailable; |
674 | 0 | op.pj->warnIfBestTransformationNotAvailable = |
675 | 0 | warnIfBestTransformationNotAvailable; |
676 | 0 | if (mayNeedToReRunWithDiscardMissing && |
677 | 0 | !foundInstanciableAndNonBallpark) { |
678 | 0 | if (!proj_coordoperation_has_ballpark_transformation(op.pj->ctx, |
679 | 0 | op.pj) && |
680 | 0 | op.isInstantiable()) { |
681 | 0 | foundInstanciableAndNonBallpark = true; |
682 | 0 | } |
683 | 0 | } |
684 | 0 | } |
685 | 0 | if (mayNeedToReRunWithDiscardMissing && !foundInstanciableAndNonBallpark) { |
686 | | // Re-run proj_create_operations with |
687 | | // PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID |
688 | | // Can happen for example for NAD27->NAD83 transformation when we |
689 | | // have no grid and thus have fallback to Helmert transformation and |
690 | | // a WGS84 intermediate. |
691 | 0 | operation_ctx = proj_create_operation_factory_context(ctx, authority); |
692 | 0 | if (operation_ctx) { |
693 | 0 | proj_operation_factory_context_set_allow_ballpark_transformations( |
694 | 0 | ctx, operation_ctx, allowBallparkTransformations); |
695 | |
|
696 | 0 | if (accuracy >= 0) { |
697 | 0 | proj_operation_factory_context_set_desired_accuracy( |
698 | 0 | ctx, operation_ctx, accuracy); |
699 | 0 | } |
700 | |
|
701 | 0 | if (area && area->bbox_set) { |
702 | 0 | proj_operation_factory_context_set_area_of_interest( |
703 | 0 | ctx, operation_ctx, area->west_lon_degree, |
704 | 0 | area->south_lat_degree, area->east_lon_degree, |
705 | 0 | area->north_lat_degree); |
706 | |
|
707 | 0 | if (!area->name.empty()) { |
708 | 0 | proj_operation_factory_context_set_area_of_interest_name( |
709 | 0 | ctx, operation_ctx, area->name.c_str()); |
710 | 0 | } |
711 | 0 | } |
712 | |
|
713 | 0 | proj_operation_factory_context_set_spatial_criterion( |
714 | 0 | ctx, operation_ctx, |
715 | 0 | PROJ_SPATIAL_CRITERION_PARTIAL_INTERSECTION); |
716 | 0 | proj_operation_factory_context_set_grid_availability_use( |
717 | 0 | ctx, operation_ctx, |
718 | 0 | PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID); |
719 | |
|
720 | 0 | op_list = proj_create_operations(ctx, source_crs, target_crs, |
721 | 0 | operation_ctx); |
722 | 0 | proj_operation_factory_context_destroy(operation_ctx); |
723 | |
|
724 | 0 | if (op_list) { |
725 | 0 | ctx->forceOver = forceOver; |
726 | 0 | ctx->debug_level = PJ_LOG_NONE; |
727 | 0 | auto preparedOpList2 = pj_create_prepared_operations( |
728 | 0 | ctx, source_crs, target_crs, op_list); |
729 | 0 | ctx->debug_level = old_debug_level; |
730 | 0 | ctx->forceOver = false; |
731 | 0 | proj_list_destroy(op_list); |
732 | |
|
733 | 0 | if (!preparedOpList2.empty()) { |
734 | | // Append new lists of operations to previous one |
735 | 0 | std::vector<PJCoordOperation> newOpList; |
736 | 0 | for (auto &&op : preparedOpList) { |
737 | 0 | if (!proj_coordoperation_has_ballpark_transformation( |
738 | 0 | op.pj->ctx, op.pj)) { |
739 | 0 | newOpList.emplace_back(std::move(op)); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | for (auto &&op : preparedOpList2) { |
743 | 0 | op.pj->over = forceOver; |
744 | 0 | op.pj->errorIfBestTransformationNotAvailable = |
745 | 0 | errorIfBestTransformationNotAvailable; |
746 | 0 | op.pj->warnIfBestTransformationNotAvailable = |
747 | 0 | warnIfBestTransformationNotAvailable; |
748 | 0 | newOpList.emplace_back(std::move(op)); |
749 | 0 | } |
750 | 0 | preparedOpList = std::move(newOpList); |
751 | 0 | } else { |
752 | | // We get there in "cs2cs --only-best --no-ballpark |
753 | | // EPSG:4326+3855 EPSG:4979" use case, where the initial |
754 | | // create_operations returned 1 operation, and the retry |
755 | | // with |
756 | | // PROJ_GRID_AVAILABILITY_DISCARD_OPERATION_IF_MISSING_GRID |
757 | | // returned 0. |
758 | 0 | if (op_count == 1 && |
759 | 0 | errorIfBestTransformationNotAvailable) { |
760 | 0 | if (singleOpIsInstanciable < 0) { |
761 | 0 | singleOpIsInstanciable = |
762 | 0 | proj_coordoperation_is_instantiable(ctx, P); |
763 | 0 | } |
764 | 0 | if (!singleOpIsInstanciable) { |
765 | 0 | proj_destroy(P); |
766 | 0 | proj_context_errno_set(ctx, backup_errno); |
767 | 0 | return nullptr; |
768 | 0 | } |
769 | 0 | } |
770 | 0 | } |
771 | 0 | } |
772 | 0 | } |
773 | 0 | } |
774 | | |
775 | | // If there's finally juste a single result, return it directly |
776 | 0 | if (preparedOpList.size() == 1) { |
777 | 0 | auto retP = preparedOpList[0].pj; |
778 | 0 | preparedOpList[0].pj = nullptr; |
779 | 0 | proj_destroy(P); |
780 | 0 | return retP; |
781 | 0 | } |
782 | | |
783 | 0 | P->alternativeCoordinateOperations = std::move(preparedOpList); |
784 | | // The returned P is rather dummy |
785 | 0 | P->descr = "Set of coordinate operations"; |
786 | 0 | P->over = forceOver; |
787 | 0 | P->iso_obj = nullptr; |
788 | 0 | P->fwd = nullptr; |
789 | 0 | P->inv = nullptr; |
790 | 0 | P->fwd3d = nullptr; |
791 | 0 | P->inv3d = nullptr; |
792 | 0 | P->fwd4d = nullptr; |
793 | 0 | P->inv4d = nullptr; |
794 | |
|
795 | 0 | return P; |
796 | 0 | } |