/src/PROJ/src/transformations/tinshift.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * Project: PROJ |
3 | | * Purpose: Functionality related to triangulation transformation |
4 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
5 | | * |
6 | | ****************************************************************************** |
7 | | * Copyright (c) 2020, Even Rouault, <even.rouault at spatialys.com> |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | | * copy of this software and associated documentation files (the "Software"), |
11 | | * to deal in the Software without restriction, including without limitation |
12 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
13 | | * and/or sell copies of the Software, and to permit persons to whom the |
14 | | * Software is furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included |
17 | | * in all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
20 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
25 | | * DEALINGS IN THE SOFTWARE. |
26 | | *****************************************************************************/ |
27 | | |
28 | | #define PROJ_COMPILATION |
29 | | |
30 | | #include "tinshift.hpp" |
31 | | #include "filemanager.hpp" |
32 | | #include "proj_internal.h" |
33 | | |
34 | | PROJ_HEAD(tinshift, "Triangulation based transformation"); |
35 | | |
36 | | using namespace TINSHIFT_NAMESPACE; |
37 | | |
38 | | namespace { |
39 | | |
40 | | struct tinshiftData { |
41 | | std::unique_ptr<Evaluator> evaluator{}; |
42 | | |
43 | 0 | tinshiftData() = default; |
44 | | |
45 | | tinshiftData(const tinshiftData &) = delete; |
46 | | tinshiftData &operator=(const tinshiftData &) = delete; |
47 | | }; |
48 | | |
49 | | } // namespace |
50 | | |
51 | 3 | static PJ *pj_tinshift_destructor(PJ *P, int errlev) { |
52 | 3 | if (nullptr == P) |
53 | 0 | return nullptr; |
54 | | |
55 | 3 | auto Q = static_cast<struct tinshiftData *>(P->opaque); |
56 | 3 | delete Q; |
57 | 3 | P->opaque = nullptr; |
58 | | |
59 | 3 | return pj_default_destructor(P, errlev); |
60 | 3 | } |
61 | | |
62 | 0 | static void tinshift_forward_4d(PJ_COORD &coo, PJ *P) { |
63 | 0 | auto *Q = (struct tinshiftData *)P->opaque; |
64 | |
|
65 | 0 | if (!Q->evaluator->forward(coo.xyz.x, coo.xyz.y, coo.xyz.z, coo.xyz.x, |
66 | 0 | coo.xyz.y, coo.xyz.z)) { |
67 | 0 | coo = proj_coord_error(); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | 0 | static void tinshift_reverse_4d(PJ_COORD &coo, PJ *P) { |
72 | 0 | auto *Q = (struct tinshiftData *)P->opaque; |
73 | |
|
74 | 0 | if (!Q->evaluator->inverse(coo.xyz.x, coo.xyz.y, coo.xyz.z, coo.xyz.x, |
75 | 0 | coo.xyz.y, coo.xyz.z)) { |
76 | 0 | coo = proj_coord_error(); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | 3 | PJ *PJ_TRANSFORMATION(tinshift, 1) { |
81 | | |
82 | 3 | const char *filename = pj_param(P->ctx, P->params, "sfile").s; |
83 | 3 | if (!filename) { |
84 | 3 | proj_log_error(P, _("+file= should be specified.")); |
85 | 3 | return pj_tinshift_destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG); |
86 | 3 | } |
87 | | |
88 | 0 | auto file = NS_PROJ::FileManager::open_resource_file(P->ctx, filename); |
89 | 0 | if (nullptr == file) { |
90 | 0 | proj_log_error(P, _("Cannot open %s"), filename); |
91 | 0 | return pj_tinshift_destructor( |
92 | 0 | P, PROJ_ERR_INVALID_OP_FILE_NOT_FOUND_OR_INVALID); |
93 | 0 | } |
94 | 0 | file->seek(0, SEEK_END); |
95 | 0 | unsigned long long size = file->tell(); |
96 | | // Arbitrary threshold to avoid ingesting an arbitrarily large JSON file, |
97 | | // that could be a denial of service risk. 100 MB should be sufficiently |
98 | | // large for any valid use ! |
99 | 0 | if (size > 100 * 1024 * 1024) { |
100 | 0 | proj_log_error(P, _("File %s too large"), filename); |
101 | 0 | return pj_tinshift_destructor( |
102 | 0 | P, PROJ_ERR_INVALID_OP_FILE_NOT_FOUND_OR_INVALID); |
103 | 0 | } |
104 | 0 | file->seek(0); |
105 | 0 | std::string jsonStr; |
106 | 0 | try { |
107 | 0 | jsonStr.resize(static_cast<size_t>(size)); |
108 | 0 | } catch (const std::bad_alloc &) { |
109 | 0 | proj_log_error(P, _("Cannot read %s. Not enough memory"), filename); |
110 | 0 | return pj_tinshift_destructor(P, PROJ_ERR_OTHER); |
111 | 0 | } |
112 | 0 | if (file->read(&jsonStr[0], jsonStr.size()) != jsonStr.size()) { |
113 | 0 | proj_log_error(P, _("Cannot read %s"), filename); |
114 | 0 | return pj_tinshift_destructor( |
115 | 0 | P, PROJ_ERR_INVALID_OP_FILE_NOT_FOUND_OR_INVALID); |
116 | 0 | } |
117 | | |
118 | 0 | auto Q = new tinshiftData(); |
119 | 0 | P->opaque = (void *)Q; |
120 | 0 | P->destructor = pj_tinshift_destructor; |
121 | |
|
122 | 0 | try { |
123 | 0 | Q->evaluator.reset(new Evaluator(TINShiftFile::parse(jsonStr))); |
124 | 0 | } catch (const std::exception &e) { |
125 | 0 | proj_log_error(P, _("invalid model: %s"), e.what()); |
126 | 0 | return pj_tinshift_destructor( |
127 | 0 | P, PROJ_ERR_INVALID_OP_FILE_NOT_FOUND_OR_INVALID); |
128 | 0 | } |
129 | | |
130 | 0 | P->fwd4d = tinshift_forward_4d; |
131 | 0 | P->inv4d = tinshift_reverse_4d; |
132 | 0 | P->left = PJ_IO_UNITS_WHATEVER; |
133 | 0 | P->right = PJ_IO_UNITS_WHATEVER; |
134 | |
|
135 | 0 | return P; |
136 | 0 | } |