Skip to content
Snippets Groups Projects
Commit 923006ab authored by Tom Fischer's avatar Tom Fischer
Browse files

Using logog and TCLAP in ConvertSHPToGLI.cpp

parent 1d131ee2
No related branches found
No related tags found
No related merge requests found
......@@ -12,15 +12,22 @@
*
*/
//ShapeLib includes
#include "shapefil.h"
// STL
#include <fstream>
#include <iostream>
#include <vector>
// Base
// TCLAP
#include "tclap/CmdLine.h"
// ThirdParty/logog
#include "logog/include/logog.hpp"
// ShapeLib
#include "shapefil.h"
// BaseLib
#include "LogogSimpleFormatter.h"
#include "StringTools.h"
// FileIO
......@@ -32,6 +39,7 @@
#include "Point.h"
#include "Station.h"
// OgsLib
#include "OGS/ProjectData.h"
void convertPoints (DBFHandle dbf_handle,
......@@ -44,31 +52,7 @@ void convertPoints (DBFHandle dbf_handle,
bool station)
{
int n_records (DBFGetRecordCount (dbf_handle));
std::cout << "writing " << n_records << " records" << std::endl;
// out << "#POINTS" << std::endl;
//
// for (int k(0); k<n_records; k++) {
// double x (DBFReadDoubleAttribute( dbf_handle, k, x_id));
// double y (DBFReadDoubleAttribute( dbf_handle, k, y_id));
// double z (0.0);
// if (z_id != std::numeric_limits<size_t>::max())
// z = DBFReadDoubleAttribute( dbf_handle, k, z_id);
// out.precision (10);
// out.flags (std::ios::fixed);
// out << k << " " << x << " " << y << " " << z << std::flush;
// if (!name_component_ids.empty()) {
// out << " $NAME ";
// for (size_t j(0); j<name_component_ids.size(); j++) {
// if (name_component_ids[j] != std::numeric_limits<size_t>::max()) {
// std::string name (DBFReadStringAttribute( dbf_handle, k, name_component_ids[j]));
// out << name.c_str() << " ";
// }
// }
// }
// out << std::endl;
// }
// out << "#STOP" << std::endl;
INFO("Reading %d records.", n_records);
std::vector<GeoLib::Point*>* points (new std::vector<GeoLib::Point*>);
points->reserve (n_records);
......@@ -79,8 +63,7 @@ void convertPoints (DBFHandle dbf_handle,
double y(DBFReadDoubleAttribute(dbf_handle, k, y_id));
double z(0.0);
if (z_id != std::numeric_limits<size_t>::max())
z = DBFReadDoubleAttribute(dbf_handle, k,
z_id);
z = DBFReadDoubleAttribute(dbf_handle, k, z_id);
name = "";
if (!name_component_ids.empty()) {
......@@ -89,7 +72,9 @@ void convertPoints (DBFHandle dbf_handle,
name += DBFReadStringAttribute(dbf_handle, k, name_component_ids[j]);
name += " ";
}
} else name = BaseLib::number2str(k);
}
else
name = BaseLib::number2str(k);
if (station) {
GeoLib::Station* pnt(GeoLib::Station::createStation(name, x, y, z));
......@@ -126,114 +111,124 @@ void convertPoints (DBFHandle dbf_handle,
delete project_data;
}
int main (int argc, char* argv[])
void printFieldInformationTable(DBFHandle const& dbf_handle, std::size_t n_fields)
{
if (argc == 1)
{
std::cout << "Usage: " << argv[0] << " shape_file_name" << std::endl;
return -1;
char* field_name(new char[256]);
int width(0), n_decimals(0);
std::stringstream out;
out << std::endl;
out << "************************************************" << std::endl;
out << "field idx | name of field | data type of field " << std::endl;
out << "------------------------------------------------" << std::endl;
for (size_t field_idx(0); field_idx < n_fields; field_idx++) {
DBFGetFieldInfo(dbf_handle, field_idx, field_name, &width, &n_decimals);
if (field_idx < 10)
out << " " << field_idx << " |";
else
out << " " << field_idx << " |";
std::string field_name_str(field_name);
for (int k(0); k < (14 - (int) field_name_str.size()); k++)
out << " ";
out << field_name_str << " |";
char native_field_type(DBFGetNativeFieldType(dbf_handle, field_idx));
switch (native_field_type) {
case 'C':
out << " string" << std::endl;
break;
case 'F':
out << " float" << std::endl;
break;
case 'N':
out << " numeric" << std::endl;
break;
default:
out << " n_decimal " << n_decimals << std::endl;
}
}
delete[] field_name;
out << "************************************************" << std::endl;
INFO("%s", out.str().c_str());
}
int main (int argc, char* argv[])
{
LOGOG_INITIALIZE();
logog::Cout* logog_cout (new logog::Cout);
BaseLib::LogogSimpleFormatter *custom_format (new BaseLib::LogogSimpleFormatter);
logog_cout->SetFormatter(*custom_format);
std::string fname (argv[1]);
TCLAP::CmdLine cmd("Converts points contained in shape file", ' ', "0.1");
TCLAP::ValueArg<std::string> shapefile_arg("s",
"shape-file",
"the name of the shape file ",
true,
"",
"shape file");
cmd.add( shapefile_arg );
cmd.parse(argc, argv);
std::string fname (shapefile_arg.getValue());
/* from SHPInterface.cpp */
int shape_type, number_of_elements;
double padfMinBound[4], padfMaxBound[4];
SHPHandle hSHP = SHPOpen(fname.c_str(),"rb");
SHPGetInfo( hSHP, &number_of_elements, &shape_type, padfMinBound, padfMaxBound );
if (hSHP) {
SHPGetInfo( hSHP, &number_of_elements, &shape_type, padfMinBound, padfMaxBound );
if ((shape_type - 1) % 10 == 0)
std::cout << "shape file contains " << number_of_elements << " points" << std::endl;
if ( ((shape_type - 3) % 10 == 0 || (shape_type - 5) % 10 == 0))
{
std::cout << "shape file contains " << number_of_elements << " polylines" <<
std::endl;
std::cout << "this programm only handles point-input files" << std::endl;
if ((shape_type - 1) % 10 == 0)
INFO("Shape file contains %d points.", number_of_elements);
if ( ((shape_type - 3) % 10 == 0 || (shape_type - 5) % 10 == 0))
{
ERR("Shape file contains %d polylines.", number_of_elements);
ERR("This programm only handles only files containing points.");
SHPClose(hSHP);
return 0;
}
SHPClose(hSHP);
return 0;
} else {
ERR("Could not open shapefile %s.", fname.c_str());
}
SHPClose(hSHP);
/* end from SHPInterface */
DBFHandle dbf_handle = DBFOpen(fname.c_str(),"rb");
if(dbf_handle)
{
char* field_name (new char[256]);
int width(0), n_decimals(0);
size_t n_fields (DBFGetFieldCount(dbf_handle));
std::cout << "************************************************" << std::endl;
std::cout << "field idx | name of field | data type of field " << std::endl;
for (size_t field_idx (0); field_idx < n_fields; field_idx++)
{
DBFGetFieldInfo( dbf_handle, field_idx, field_name, &width, &n_decimals);
if (field_idx < 10)
std::cout << " " << field_idx << " |" << std::flush;
else
std::cout << " " << field_idx << " |" << std::flush;
std::string field_name_str (field_name);
for (int k(0); k < (14 - (int)field_name_str.size()); k++)
std::cout << " ";
std::cout << field_name_str << " |" << std::flush;
char native_field_type (DBFGetNativeFieldType (dbf_handle, field_idx));
switch (native_field_type)
{
case 'C':
std::cout << " string" << std::endl;
break;
case 'F':
std::cout << " float" << std::endl;
break;
case 'N':
std::cout << " numeric" << std::endl;
break;
default:
std::cout << " n_decimal " << n_decimals << std::endl;
}
}
delete [] field_name;
std::cout << "************************************************" << std::endl;
std::size_t n_fields(DBFGetFieldCount(dbf_handle));
printFieldInformationTable(dbf_handle, n_fields);
size_t x_id, y_id, z_id;
std::cout <<
"please give the field idx that should be used for reading the x coordinate: " <<
std::flush;
INFO("Please give the field idx that should be used for reading the x coordinate: ");
std::cin >> x_id;
std::cout <<
"please give the field idx that should be used for reading the y coordinate: " <<
std::flush;
INFO("Please give the field idx that should be used for reading the y coordinate: ");
std::cin >> y_id;
std::cout <<
"please give the field idx that should be used for reading the z coordinate: " <<
std::flush;
INFO("Please give the field idx that should be used for reading the z coordinate: ");
std::cin >> z_id;
if (z_id > n_fields)
z_id = std::numeric_limits<size_t>::max();
size_t n_name_components;
std::cout << "please give the number of fields that should be added to name: " <<
std::flush;
INFO("Please give the number of fields that should be added to name: ");
std::cin >> n_name_components;
std::vector<size_t> name_component_ids (n_name_components,
std::numeric_limits<size_t>::max());
if (n_name_components != 0)
if (n_name_components != 0) {
for (size_t j(0); j < n_name_components; j++)
{
std::cout <<
"- please give the field idx that should be used for reading the name: "
<< std::flush;
INFO("- please give the field idx that should be used for reading the name: ");
std::cin >> name_component_ids[j];
}
}
for (size_t j(0); j < n_name_components; j++)
if (name_component_ids[j] > n_fields)
name_component_ids[j] = std::numeric_limits<size_t>::max();
size_t station (0);
std::cout <<
"Should I read the information as GeoLib::Station (0) or as GeoLib::Point (1)? Please give the number: "
<< std::flush;
INFO("Should I read the information as GeoLib::Station (0) or as GeoLib::Point (1)? Please give the number: ");
std::cin >> station;
std::string fname_base (fname);
......@@ -242,7 +237,7 @@ int main (int argc, char* argv[])
else
fname += ".gml";
std::cout << "writing " << fname << " ... " << std::flush;
INFO("Writing to %s.", fname.c_str());
convertPoints (dbf_handle,
fname,
x_id,
......@@ -252,10 +247,14 @@ int main (int argc, char* argv[])
fname_base,
station == 0 ? true : false);
DBFClose (dbf_handle);
std::cout << "ok" << std::endl;
INFO("\tok.");
} else {
ERR("Could not open the database file.");
}
else
std::cout << "error" << std::endl;
delete custom_format;
delete logog_cout;
LOGOG_SHUTDOWN();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment