-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_layer.cpp
168 lines (150 loc) · 4.92 KB
/
output_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* output_layer.cpp
*
* Created on: 11.10.2016
* Author: michael
*
* This file contains code from osmium library v1 (include/osmium/export/shapefile.hpp).
*/
#include <memory>
#include "output_layer.hpp"
OutputLayer::OutputLayer(std::string&& out_directory, std::string&& layer_name, int output_epsg, bool tile_ids,
std::string& output_format) :
m_current_index(0),
m_directory(out_directory),
m_layer_name(layer_name),
m_tile_ids(tile_ids),
m_output_format(output_format) {
m_output_srs.importFromEPSG(output_epsg);
m_web_mercator_srs.importFromEPSG(3857);
setup_data_source();
open_layer();
}
OutputLayer::~OutputLayer() {
GDALClose(m_dataset);
OGRCleanupAll();
}
void OutputLayer::add_field(const std::string& field_name, OGRFieldType type, int width) {
OGRFieldDefn sequence_field(field_name.c_str(), type);
m_dbf_entry_size += width;
m_current_dbf_size += 32;
sequence_field.SetWidth(width);
int ogrerr = m_layer->CreateField(&sequence_field);
if (ogrerr != OGRERR_NONE) {
std::cerr << "Creating " << field_name << " field failed.\n";
exit(1);
}
}
void OutputLayer::close_layer() {
if (m_layer->CommitTransaction() != OGRERR_NONE) {
std::cerr << "Error commit transation\n";
exit(1);
}
m_layer->SyncToDisk();
}
void OutputLayer::open_layer() {
m_current_index++;
m_current_dbf_size = 33;
m_dbf_entry_size = 1;
std::string current_layer_name = m_layer_name;
current_layer_name += "_";
current_layer_name += std::to_string(m_current_index);
m_layer = m_dataset->CreateLayer(current_layer_name.c_str(), &m_output_srs, wkbPolygon, NULL);
if (!m_layer) {
std::cerr << "Creating layer " << m_layer_name << " failed.\n";
exit(1);
}
add_field("sequence", OFTString, 12);
add_field("zoom", OFTInteger, 3);
if (m_tile_ids) {
add_field("x", OFTInteger, 8);
add_field("y", OFTInteger, 8);
}
if (m_output_format == "ESRI Shapefile") {
write_cpg_file();
}
m_current_shp_size = 100;
if (m_layer->StartTransaction() != OGRERR_NONE) {
std::cerr << "Error starting transaction\n";
exit(1);
}
}
void OutputLayer::write_cpg_file() {
std::ofstream file;
std::string cpgname = m_directory;
cpgname += m_layer_name;
cpgname += "_";
cpgname += std::to_string(m_current_index);
cpgname += ".cpg";
file.open(cpgname.c_str());
if (file.fail()) {
throw std::runtime_error("Can't open shapefile: " + cpgname);
}
file << "UTF-8" << std::endl;
file.close();
}
void OutputLayer::setup_data_source() {
OGRRegisterAll();
GDALDriver* m_driver = GetGDALDriverManager()->GetDriverByName(m_output_format.c_str());
if (!m_driver) {
std::cerr << "Driver for " << m_output_format << " is not available.\n";
exit(1);
}
if (m_output_format == "ESRI Shapfile") {
CPLSetConfigOption("SHAPE_ENCODING", "UTF8");
}
m_dataset = m_driver->Create(m_directory.c_str(), 0, 0, 0, GDT_Unknown, NULL);
if (!m_dataset) {
std::cerr << "Creation of output file failed.\n";
exit(1);
}
}
void OutputLayer::set_directory(std::string& path) {
size_t last_slash = path.find_last_of("/");
if (last_slash == std::string::npos) {
m_directory = ".";
} else {
m_directory = path.substr(0, last_slash+1);
}
}
void OutputLayer::set_filename(std::string& path) {
size_t last_slash = path.find_last_of("/");
size_t shp_suffix = path.find_last_of(".shp");
if (last_slash == std::string::npos) {
m_layer_name = path.substr(0, shp_suffix);
} else {
m_layer_name = path.substr(last_slash+1, shp_suffix).c_str();
}
}
void OutputLayer::ensure_layer_writeable() {
if (m_layer)
{
if ((m_current_dbf_size < m_max_shape_size) && (m_current_shp_size < m_max_shape_size)) return;
close_layer();
}
open_layer();
}
void OutputLayer::write_tile(int x, int y, int zoom, std::string& sequence, bool tile_ids) {
ensure_layer_writeable();
Tile tile(x, y, zoom);
std::unique_ptr<OGRPolygon> polygon = tile.get_square();
OGRFeature* feature = OGRFeature::CreateFeature(m_layer->GetLayerDefn());
if (m_web_mercator_srs.GetEPSGGeogCS() != m_output_srs.GetEPSGGeogCS()) {
polygon->assignSpatialReference(&m_web_mercator_srs);
polygon->transformTo(&m_output_srs);
}
feature->SetGeometry(polygon.get());
feature->SetField("sequence", sequence.c_str());
feature->SetField("zoom", zoom);
if (tile_ids) {
feature->SetField("x", x);
feature->SetField("y", y);
}
if (m_layer->CreateFeature(feature) != OGRERR_NONE) {
std::cerr << "Failed to create feature.\n";
exit(1);
}
OGRFeature::DestroyFeature(feature);
m_current_shp_size += 120;
m_current_dbf_size += m_dbf_entry_size;
}