• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
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 //     https://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 #include <cstdlib>
16 #include <iostream>
17 #include <optional>
18 #include <string>
19 
20 #include "get_raster_data.h"  // NOLINT(build/include)
21 #include "gtiff_converter.h"  // NOLINT(build/include)
22 #include "sandboxed_api/util/fileops.h"
23 #include "sandboxed_api/util/path.h"
24 #include "utils.h"  // NOLINT(build/include)
25 
26 namespace {
27 
SaveToGTiff(gdal::sandbox::parser::RasterDataset bands_data,std::string out_file)28 absl::Status SaveToGTiff(gdal::sandbox::parser::RasterDataset bands_data,
29                          std::string out_file) {
30   std::optional<std::string> proj_db_path =
31       gdal::sandbox::utils::FindProjDbPath();
32 
33   if (proj_db_path == std::nullopt) {
34     return absl::FailedPreconditionError("Specified proj.db does not exist");
35   }
36 
37   gdal::sandbox::RasterToGTiffProcessor processor(
38       std::move(out_file), std::move(proj_db_path.value()),
39       std::move(bands_data));
40 
41   return processor.Run();
42 }
43 
Usage()44 void Usage() {
45   std::cerr << "Example application that converts raster data to GTiff"
46                " format inside the sandbox. Usage:\n"
47                "raster_to_gtiff input_filename output_filename\n"
48                "output_filename must be absolute"
49             << std::endl;
50 }
51 
52 }  // namespace
53 
main(int argc,char * argv[])54 int main(int argc, char* argv[]) {
55   if (argc < 3 || !sandbox2::file::IsAbsolutePath(argv[2])) {
56     Usage();
57     return EXIT_FAILURE;
58   }
59 
60   std::string input_data_path = std::string(argv[1]);
61   std::string output_data_path = std::string(argv[2]);
62 
63   if (absl::Status status = gdal::sandbox::SaveToGTiff(
64           gdal::sandbox::parser::GetRasterBandsFromFile(
65               std::move(input_data_path)),
66           std::move(output_data_path));
67       !status.ok()) {
68     std::cerr << status.ToString() << std::endl;
69     return EXIT_FAILURE;
70   }
71 
72   return EXIT_SUCCESS;
73 }
74