OGS
ConvertSHPToGLI.cpp File Reference
#include <shapefil.h>
#include <tclap/CmdLine.h>
#include <fstream>
#include <vector>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h"
#include "GeoLib/IO/XmlIO/Qt/XmlStnInterface.h"
#include "GeoLib/Point.h"
#include "GeoLib/Station.h"
#include "InfoLib/GitInfo.h"
Include dependency graph for ConvertSHPToGLI.cpp:

Go to the source code of this file.

Functions

void convertPoints (DBFHandle dbf_handle, std::string const &out_fname, std::size_t x_id, std::size_t y_id, std::size_t z_id, std::vector< std::size_t > const &name_component_ids, std::string &points_group_name, bool station)
void printFieldInformationTable (DBFHandle const &dbf_handle, std::size_t n_fields)
int main (int argc, char *argv[])

Function Documentation

◆ convertPoints()

void convertPoints ( DBFHandle dbf_handle,
std::string const & out_fname,
std::size_t x_id,
std::size_t y_id,
std::size_t z_id,
std::vector< std::size_t > const & name_component_ids,
std::string & points_group_name,
bool station )

Definition at line 20 of file ConvertSHPToGLI.cpp.

28{
29 int n_records(DBFGetRecordCount(dbf_handle));
30 INFO("Reading {:d} records.", n_records);
31
32 std::vector<GeoLib::Point*> points;
33 points.reserve(n_records);
34
35 std::string name;
36 for (int k = 0; k < n_records; k++)
37 {
38 double x(DBFReadDoubleAttribute(dbf_handle, k, x_id));
39 double y(DBFReadDoubleAttribute(dbf_handle, k, y_id));
40 double z(0.0);
41 if (z_id != std::numeric_limits<std::size_t>::max())
42 {
43 z = DBFReadDoubleAttribute(dbf_handle, k, z_id);
44 }
45
46 name.clear();
47 if (!name_component_ids.empty())
48 {
49 for (unsigned long name_component_id : name_component_ids)
50 {
51 if (name_component_id !=
52 std::numeric_limits<std::size_t>::max())
53 {
54 name += DBFReadStringAttribute(dbf_handle, k,
55 name_component_id);
56 name += " ";
57 }
58 }
59 }
60 else
61 {
62 name = std::to_string(k);
63 }
64
65 if (station)
66 {
68 points.push_back(pnt);
69 }
70 else
71 {
72 GeoLib::Point* pnt(new GeoLib::Point(x, y, z));
73 points.push_back(pnt);
74 }
75 }
76
77 GeoLib::GEOObjects geo_objs;
78 if (station)
79 {
80 geo_objs.addStationVec(std::move(points), points_group_name);
81 }
82 else
83 {
84 geo_objs.addPointVec(std::move(points), points_group_name,
86 }
87
88 if (station)
89 {
90 GeoLib::IO::XmlStnInterface xml(geo_objs);
91 xml.export_name = points_group_name;
92 BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname);
93 }
94 else
95 {
96 GeoLib::IO::XmlGmlInterface xml(geo_objs);
97 xml.export_name = points_group_name;
98 BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname);
99 }
100}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
Container class for geometric objects.
Definition GEOObjects.h:46
void addPointVec(std::vector< Point * > &&points, std::string &name, PointVec::NameIdMap &&pnt_id_name_map, double const eps=std::sqrt(std::numeric_limits< double >::epsilon()))
void addStationVec(std::vector< Point * > &&stations, std::string &name)
Adds a vector of stations with the given name and colour to GEOObjects.
Reads and writes GeoObjects to and from XML files.
Reads and writes Observation Sites to and from XML files.
A Station (observation site) is basically a Point with some additional information.
Definition Station.h:26
static Station * createStation(const std::string &line)
Definition Station.cpp:35
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:30
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34

References GeoLib::GEOObjects::addPointVec(), GeoLib::GEOObjects::addStationVec(), GeoLib::Station::createStation(), BaseLib::IO::XMLInterface::export_name, INFO(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by main().

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 153 of file ConvertSHPToGLI.cpp.

154{
155 TCLAP::CmdLine cmd(
156 "Converts points contained in shape file\n\n"
157 "OpenGeoSys-6 software, version " +
159 ".\n"
160 "Copyright (c) 2012-2026, OpenGeoSys Community "
161 "(http://www.opengeosys.org)",
163 TCLAP::ValueArg<std::string> shapefile_arg(
164 "s",
165 "shape-file",
166 "Input (.shp). The name of the input shape "
167 "file",
168 true,
169 "",
170 "INPUT_FILE");
171 cmd.add(shapefile_arg);
172
173 auto log_level_arg = BaseLib::makeLogLevelArg();
174 cmd.add(log_level_arg);
175 cmd.parse(argc, argv);
176
177 BaseLib::MPI::Setup mpi_setup(argc, argv);
178 BaseLib::initOGSLogger(log_level_arg.getValue());
179
180 std::string fname(shapefile_arg.getValue());
181
182 int shape_type;
183 int number_of_elements;
184
185 SHPHandle hSHP = SHPOpen(fname.c_str(), "rb");
186 if (hSHP)
187 {
188 SHPGetInfo(hSHP, &number_of_elements, &shape_type,
189 nullptr /*padfMinBound*/, nullptr /*padfMinBound*/);
190
191 if ((shape_type - 1) % 10 == 0)
192 INFO("Shape file contains {:d} points.", number_of_elements);
193 if (((shape_type - 3) % 10 == 0 || (shape_type - 5) % 10 == 0))
194 {
195 ERR("Shape file contains {:d} polylines.", number_of_elements);
196 ERR("This programme only handles only files containing points.");
197 SHPClose(hSHP);
198 return EXIT_SUCCESS;
199 }
200 SHPClose(hSHP);
201 }
202 else
203 {
204 ERR("Could not open shapefile {:s}.", fname);
205 }
206
207 DBFHandle dbf_handle = DBFOpen(fname.c_str(), "rb");
208 if (dbf_handle)
209 {
210 std::size_t n_fields(DBFGetFieldCount(dbf_handle));
211 printFieldInformationTable(dbf_handle, n_fields);
212
213 std::size_t x_id;
214 std::size_t y_id;
215 std::size_t z_id;
216 INFO(
217 "Please give the field idx that should be used for reading the x "
218 "coordinate: ");
219 std::cin >> x_id;
220 INFO(
221 "Please give the field idx that should be used for reading the y "
222 "coordinate: ");
223 std::cin >> y_id;
224 INFO(
225 "Please give the field idx that should be used for reading the z "
226 "coordinate: ");
227 std::cin >> z_id;
228
229 if (z_id > n_fields)
230 {
231 z_id = std::numeric_limits<std::size_t>::max();
232 }
233
234 std::size_t n_name_components;
235 INFO("Please give the number of fields that should be added to name: ");
236 std::cin >> n_name_components;
237
238 std::vector<std::size_t> name_component_ids(
239 n_name_components, std::numeric_limits<std::size_t>::max());
240 if (n_name_components != 0)
241 {
242 for (std::size_t j(0); j < n_name_components; j++)
243 {
244 INFO(
245 "- please give the field idx that should be used for "
246 "reading the name: ");
247 std::cin >> name_component_ids[j];
248 }
249 }
250 for (std::size_t j(0); j < n_name_components; j++)
251 {
252 if (name_component_ids[j] > n_fields)
253 {
254 name_component_ids[j] = std::numeric_limits<std::size_t>::max();
255 }
256 }
257
258 std::size_t station(0);
259
260 INFO(
261 "Should I read the information as GeoLib::Station (0) or as "
262 "GeoLib::Point (1)? Please give the number: ");
263 std::cin >> station;
264
265 std::string fname_base(fname);
266 if (station == 0)
267 {
268 fname += ".stn";
269 }
270 else
271 {
272 fname += ".gml";
273 }
274
275 INFO("Writing to {:s}.", fname);
276 convertPoints(dbf_handle,
277 fname,
278 x_id,
279 y_id,
280 z_id,
281 name_component_ids,
282 fname_base,
283 station == 0);
284 DBFClose(dbf_handle);
285 INFO("\tok.");
286 }
287 else
288 {
289 ERR("Could not open the database file.");
290 }
291
292 return EXIT_SUCCESS;
293}
void convertPoints(DBFHandle dbf_handle, std::string const &out_fname, std::size_t x_id, std::size_t y_id, std::size_t z_id, std::vector< std::size_t > const &name_component_ids, std::string &points_group_name, bool station)
void printFieldInformationTable(DBFHandle const &dbf_handle, std::size_t n_fields)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
GITINFOLIB_EXPORT const std::string ogs_version

References convertPoints(), ERR(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, and printFieldInformationTable().

◆ printFieldInformationTable()

void printFieldInformationTable ( DBFHandle const & dbf_handle,
std::size_t n_fields )

Definition at line 102 of file ConvertSHPToGLI.cpp.

104{
105 char* field_name(new char[256]);
106 int width(0);
107 int n_decimals(0);
108 std::stringstream out;
109 out << std::endl;
110 out << "************************************************" << std::endl;
111 out << "field idx | name of field | data type of field " << std::endl;
112 out << "------------------------------------------------" << std::endl;
113 for (std::size_t field_idx(0); field_idx < n_fields; field_idx++)
114 {
115 DBFGetFieldInfo(dbf_handle, field_idx, field_name, &width, &n_decimals);
116 if (field_idx < 10)
117 {
118 out << " " << field_idx << " |";
119 }
120 else
121 {
122 out << " " << field_idx << " |";
123 }
124 std::string field_name_str(field_name);
125 for (int k(0); k < (14 - static_cast<int>(field_name_str.size())); k++)
126 {
127 out << " ";
128 }
129 out << field_name_str << " |";
130
131 char native_field_type(DBFGetNativeFieldType(dbf_handle, field_idx));
132 switch (native_field_type)
133 {
134 case 'C':
135 out << " string" << std::endl;
136 break;
137 case 'F':
138 out << " float" << std::endl;
139 break;
140 case 'N':
141 out << " numeric" << std::endl;
142 break;
143 default:
144 out << " n_decimal " << n_decimals << std::endl;
145 break;
146 }
147 }
148 delete[] field_name;
149 out << "************************************************" << std::endl;
150 INFO("{:s}", out.str());
151}

References INFO().

Referenced by main().