1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License:  See top level LICENSE.txt file.
5 //
6 // Author:  Garrett Potts
7 //
8 //*******************************************************************
9 //  $Id: tfw2ogeom.cpp 13025 2008-06-13 17:06:30Z sbortman $
10 
11 #include <cstdlib>
12 #include <fstream>
13 
14 #include <ossim/projection/ossimProjection.h>
15 #include <ossim/projection/ossimEpsgProjectionFactory.h>
16 #include <ossim/base/ossimKeywordNames.h>
17 #include <ossim/base/ossimArgumentParser.h>
18 #include <ossim/base/ossimApplicationUsage.h>
19 #include <ossim/base/ossimNotifyContext.h>
20 #include <ossim/init/ossimInit.h>
21 #include <ossim/base/ossimUnitTypeLut.h>
22 #include <ossim/support_data/ossimTiffWorld.h>
23 #include <ossim/base/ossimFilename.h>
24 #include <ossim/base/ossimKeywordlist.h>
25 
26 
27 void usage()
28 {
29    ossimNotify(ossimNotifyLevel_NOTICE)
30       << "ossim-tfw2ogeom "
31       << "<template_proj> <tif world file> [<output file>]\n\n"
32       << "Options:\n"
33       << "-h                 Display usage.\n\n"
34       << "-w <template_file> Write a template to template_file.\n\n"
35       << "<template_proj>   See template.kwl for descriptions\n\n"
36       << "[<output file>]   optional argumaent and if not supplied\n"
37       << "                  defaults to the tif world file with a\n"
38       << "                  .geom extension" << std::endl;
39 }
40 
41 void outputGeom(const ossimFilename& templateFile,
default() -> Self42                 const ossimFilename& tfwFile,
43                 const ossimFilename& output)
44 {
45    ossimNotify(ossimNotifyLevel_NOTICE)
46       << "tfw2ogeom:"
47       << "\ntemplate file:         " << templateFile
48       << "\ntfw file:              " << tfwFile
49       << "\noutput geometry file:  " << output
50       << std::endl;
51 
52    ossimKeywordlist geomKwl(templateFile.c_str());
53 
54    ossimTiffWorld tfw;
55    const char* pixel_type = geomKwl.find(ossimKeywordNames::PIXEL_TYPE_KW);
56    const char* pcs_code   = geomKwl.find(ossimKeywordNames::PCS_CODE_KW);
57    ossimUnitType unitType = (ossimUnitType)ossimUnitTypeLut::instance()->getEntryNumber(geomKwl);
58 
59    if(ossimString(pixel_type).upcase().contains("POINT"))
60    {
61       tfw = ossimTiffWorld(tfwFile.c_str(), OSSIM_PIXEL_IS_POINT, unitType);
62    }
63    else
64    {
65       tfw = ossimTiffWorld(tfwFile.c_str(), OSSIM_PIXEL_IS_AREA, unitType);
66    }
67 
68    if(pcs_code)
69    {
70       ossimString epsg_spec ("EPSG:");
71       epsg_spec += ossimString::toString(pcs_code);
72       ossimProjection* proj = ossimEpsgProjectionFactory::instance()->createProjection(epsg_spec);
73       if (proj)
74          proj->saveState(geomKwl);
75    }
76 
77    tfw.saveToOssimGeom(geomKwl);
78 
79    geomKwl.write(output.c_str());
80 
81    ossimNotify(ossimNotifyLevel_NOTICE)
82       << "Wrote file:  " << output.c_str() << std::endl;
83 }
84 
85 void writeTemplate(const ossimFilename& templateFile)
86 {
87    std::ofstream out(templateFile.c_str());
88    if (!out)
89    {
90       return;
91    }
92 
93    out << "// tfw2ogeom keyword list template\n"
94        << "// Note:  Use c++ comments \"//\" for comments.\n\n\n"
95        << "// Units can be any of the below; although typically,\n"
96        << "// meters or degrees.\n"
97        << "//     us_survey_feet\n"
98        << "//     feet\n"
99        << "//     meters\n"
100        << "//     degrees\n"
101        << "//     seconds\n"
102        << "//     minutes\n"
103        << "//     miles\n"
104        << "//     nautical_miles\n"
105        << "units: meters\n\n"
106        << "// Pixel type specifies where the easting northing values\n"
107        << "// refer to.\n"
108        << "// \"pixel_is_area\" means the tie point is relative to\n"
109        << "// the upper left corner of the pixel.\n"
110        << "// \"pixel_is_point\" means the tie point is relative to\n"
111        << "// the center of the pixel.\n"
112        << "pixel_type: pixel_is_area\n\n\n"
113        << "// Give a pcs code you don't need to give the full projection\n"
114        << "// information defined below.\n"
115        << "// pcs_code: 32149\n\n\n"
116        << "// Projection type.  Note that type is the ossim class\n"
117        << "// for the projection. Some common names are listed below.\n"
118        << "// Have only one type or a pcs code.\n"
119        << "// type: ossimAlbersProjection\n"
120        << "// type: ossimEquDistCylProjection\n"
121        << "// type: ossimLambertConformalConicProjection\n"
122        << "// type: ossimUtmProjection\n"
123        << "// type: ossimTransMercatorProjection\n\n\n"
124        << "// Projection pararmeters.  These are some common ones.\n"
125        << "// origin_latitude:\n"
126        << "// central_meridian:\n"
127        << "// scale_factor:\n"
128        << "// zone:\n"
129        << "// std_parallel_1:\n"
130        << "// std_parallel_2:\n"
131        << "// Use application \"datums\" to get a list of datum codes.\n"
132        << "// datum:\n"
133        << "// false_easting:\n"
134        << "// false_northing:\n\n\n"
135        << std::endl;
136 
137    out.close();
138 
139    ossimNotify(ossimNotifyLevel_NOTICE)
140       << "Wrote file:  " << templateFile.c_str() << std::endl;
141 }
142 
143 int main(int argc, char* argv[])
144 {
145    std::string tempString;
146    ossimArgumentParser::ossimParameter stringParam(tempString);
147    ossimArgumentParser argumentParser(&argc, argv);
148    ossimInit::instance()->addOptions(argumentParser);
149    ossimInit::instance()->initialize(argumentParser);
150 
151    argumentParser.getApplicationUsage()->
152       setApplicationName(argumentParser.getApplicationName());
153 
154    argumentParser.getApplicationUsage()->
155       setDescription(argumentParser.getApplicationName()+" Takes tiff world file and a projection template and makes an ossim geometry file.  ");
156 
157    argumentParser.getApplicationUsage()->setCommandLineUsage(argumentParser.getApplicationName()+" <template_proj> <tif world file> [<output file>]");
158 
159    argumentParser.getApplicationUsage()->addCommandLineOption("-h or --help","Display usage.");
160 
161    argumentParser.getApplicationUsage()->addCommandLineOption("-w", "Writes a template file out to argument after the -w");
162 
163    if (argumentParser.read("-h") ||
164        argumentParser.read("--help")||(argumentParser.argc() < 3))
165    {
166       usage();
167       exit(0);
168    }
169    while(argumentParser.read("-w", stringParam))
tokenize_offsets(&mut self, text: &str) -> Vec<(usize, WordId)>170    {
171       ossimFilename geometryTemplateFile = tempString.c_str();
172       writeTemplate(geometryTemplateFile);
173       exit(0);
174    }
175 
176    ossimFilename templateFile;
177    ossimFilename tfw;
178    ossimFilename output;
179 
180    if (argumentParser.argc() == 3)
181    {
182       templateFile = argumentParser.argv()[argumentParser.argc()-2];
183       tfw          = argumentParser.argv()[argumentParser.argc()-1];
184       output       = tfw;
185       output.setExtension("geom");
186    }
tokenize_without_split<'a>( &mut self, text: &'a str, tokens: &mut Vec<Token<'a>>, ) -> LinderaResult<()>187    else if (argumentParser.argc() == 4)
188    {
189       templateFile = argumentParser.argv()[argumentParser.argc()-3];
190       tfw          = argumentParser.argv()[argumentParser.argc()-2];
191       output       = argumentParser.argv()[argumentParser.argc()-1];
192    }
193    else
194    {
195       usage();
196       exit(0);
197    }
198 
199    outputGeom(templateFile, tfw, output);
200 
201    return 0;
202 }
203