| 204 | } |
| 205 | |
| 206 | void OGRWriter::readyFile(const std::string& filename, const SpatialReference& srs) |
| 207 | { |
| 208 | m_curCount = 0; |
| 209 | m_outputFilename = filename; |
| 210 | |
| 211 | // Dataset |
| 212 | m_ds = m_driver->Create(filename.data(), 0, 0, 0, GDT_Unknown, nullptr); |
| 213 | if (!m_ds) |
| 214 | throwError("Unable to open OGR datasource '" + filename + "': " + CPLGetLastErrorMsg()); |
| 215 | |
| 216 | // CRS |
| 217 | if (!srs.empty()) |
| 218 | { |
| 219 | if (m_srs.importFromWkt(srs.getWKT().data()) != OGRERR_NONE) |
| 220 | throwError(std::string("Can't initialise OGR SRS: ") + CPLGetLastErrorMsg()); |
| 221 | } |
| 222 | |
| 223 | // Creation options |
| 224 | std::vector<const char*> ogr_create_options; |
| 225 | for(auto&& o:m_ogrOptions) |
| 226 | ogr_create_options.push_back(o.c_str()); |
| 227 | ogr_create_options.push_back(nullptr); |
| 228 | |
| 229 | // Layer |
| 230 | m_layer = m_ds->CreateLayer("points", &m_srs, m_geomType, |
| 231 | const_cast<char**>(ogr_create_options.data())); |
| 232 | if (!m_layer) |
| 233 | throwError(std::string("Can't create OGR layer: ") + CPLGetLastErrorMsg()); |
| 234 | |
| 235 | // Fields |
| 236 | for(auto& attr : m_attrs) |
| 237 | { |
| 238 | auto& ogrField = std::get<2>(attr); |
| 239 | if (m_layer->CreateField(&ogrField) != OGRERR_NONE) |
| 240 | throwError(std::string("Can't create OGR field: ") + ogrField.GetNameRef()); |
| 241 | } |
| 242 | |
| 243 | // Reusable template feature |
| 244 | m_feature = OGRFeature::CreateFeature(m_layer->GetLayerDefn()); |
| 245 | if (!m_feature) |
| 246 | throwError(std::string("Can't create template OGR feature: ") + CPLGetLastErrorMsg()); |
| 247 | |
| 248 | // Try to use a transaction for data sources that support it (e.g. GPKG), |
| 249 | // otherwise new points may get auto-committed after each insert (very slow) |
| 250 | if (m_ds->TestCapability( ODsCTransactions ) && m_ds->StartTransaction() == OGRERR_NONE) |
| 251 | m_inTransaction = true; |
| 252 | } |
| 253 | |
| 254 | void OGRWriter::writeView(const PointViewPtr view) |
| 255 | { |