Loading [MathJax]/extensions/tex2jax.js
OGS
ConvertSHPToGLI.cpp File Reference

Detailed Description

Implementation of the shp to gli converter tool.

Author
Thomas Fischer
Date
2010-05-03

Definition in file ConvertSHPToGLI.cpp.

#include <tclap/CmdLine.h>
#include <fstream>
#include <vector>
#include <shapefil.h>
#include "BaseLib/MPI.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 32 of file ConvertSHPToGLI.cpp.

40{
41 int n_records(DBFGetRecordCount(dbf_handle));
42 INFO("Reading {:d} records.", n_records);
43
44 std::vector<GeoLib::Point*> points;
45 points.reserve(n_records);
46
47 std::string name;
48 for (int k = 0; k < n_records; k++)
49 {
50 double x(DBFReadDoubleAttribute(dbf_handle, k, x_id));
51 double y(DBFReadDoubleAttribute(dbf_handle, k, y_id));
52 double z(0.0);
53 if (z_id != std::numeric_limits<std::size_t>::max())
54 {
55 z = DBFReadDoubleAttribute(dbf_handle, k, z_id);
56 }
57
58 name.clear();
59 if (!name_component_ids.empty())
60 {
61 for (unsigned long name_component_id : name_component_ids)
62 {
63 if (name_component_id !=
64 std::numeric_limits<std::size_t>::max())
65 {
66 name += DBFReadStringAttribute(dbf_handle, k,
67 name_component_id);
68 name += " ";
69 }
70 }
71 }
72 else
73 {
74 name = std::to_string(k);
75 }
76
77 if (station)
78 {
80 points.push_back(pnt);
81 }
82 else
83 {
84 GeoLib::Point* pnt(new GeoLib::Point(x, y, z));
85 points.push_back(pnt);
86 }
87 }
88
89 GeoLib::GEOObjects geo_objs;
90 if (station)
91 {
92 geo_objs.addStationVec(std::move(points), points_group_name);
93 }
94 else
95 {
96 geo_objs.addPointVec(std::move(points), points_group_name,
98 }
99
100 if (station)
101 {
102 GeoLib::IO::XmlStnInterface xml(geo_objs);
103 xml.export_name = points_group_name;
104 BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname);
105 }
106 else
107 {
108 GeoLib::IO::XmlGmlInterface xml(geo_objs);
109 xml.export_name = points_group_name;
110 BaseLib::IO::writeStringToFile(xml.writeToString(), out_fname);
111 }
112}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
Container class for geometric objects.
Definition GEOObjects.h:57
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:37
static Station * createStation(const std::string &line)
Definition Station.cpp:46
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:41
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:45

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 165 of file ConvertSHPToGLI.cpp.

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

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

◆ printFieldInformationTable()

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

Definition at line 114 of file ConvertSHPToGLI.cpp.

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

References INFO().

Referenced by main().