Create a geom only layer, the table will added ID column automatically
| 342 | |
| 343 | // Create a geom only layer, the table will added ID column automatically |
| 344 | OGRLayerProxy* |
| 345 | OGRDatasourceProxy::CreateLayer(wxString layer_name, |
| 346 | OGRwkbGeometryType eGType, |
| 347 | std::vector<OGRGeometry*>& geometries, |
| 348 | TableInterface* table, |
| 349 | std::vector<int>& selected_rows, |
| 350 | OGRSpatialReference* spatial_ref, |
| 351 | wxString cpg_encode) |
| 352 | { |
| 353 | wxString error_message; |
| 354 | if(!ds->TestCapability(ODsCCreateLayer)) { |
| 355 | // driver failed to load |
| 356 | error_message << _("GeoDa can't create a layer."); |
| 357 | error_message << _("\n\nDetails: "); |
| 358 | error_message << CPLGetLastErrorMsg(); |
| 359 | throw GdaException(error_message.mb_str()); |
| 360 | } |
| 361 | |
| 362 | // PRECISION is for database e.g. MSSQL |
| 363 | // OVERWRITE: This may be "YES" to force an existing layer of the |
| 364 | // desired name to be destroyed before creating the requested layer. |
| 365 | // LAUNDER is for database: rename desired field name |
| 366 | char** papszLCO = NULL; |
| 367 | papszLCO = CSLAddString(papszLCO, "OVERWRITE=yes"); |
| 368 | papszLCO = CSLAddString(papszLCO, "LAUNDER=no"); |
| 369 | |
| 370 | OGRSpatialReference *poOutputSRS = spatial_ref; |
| 371 | if (ds_type == GdaConst::ds_mysql || ds_type == GdaConst::ds_postgresql || |
| 372 | ds_type == GdaConst::ds_ms_sql || ds_type == GdaConst::ds_sqlite ) { |
| 373 | papszLCO = CSLAddString(papszLCO, "PRECISION=no"); |
| 374 | } else { |
| 375 | if (ds_type == GdaConst::ds_shapefile || ds_type == GdaConst::ds_dbf) { |
| 376 | if (cpg_encode.IsEmpty() == false) { |
| 377 | wxString opt = "ENCODING="; |
| 378 | opt << cpg_encode; |
| 379 | papszLCO = CSLAddString(papszLCO, opt.c_str()); |
| 380 | } |
| 381 | } else if (ds_type == GdaConst::ds_csv && GdaConst::gda_create_csvt) { |
| 382 | wxString opt = "CREATE_CSVT=YES"; |
| 383 | papszLCO = CSLAddString(papszLCO, opt.c_str()); |
| 384 | } |
| 385 | } |
| 386 | OGRLayer *poDstLayer = ds->CreateLayer(layer_name.mb_str(), poOutputSRS, |
| 387 | eGType, papszLCO); |
| 388 | if( poDstLayer == NULL ) { |
| 389 | error_message << _("Can't write/create layer \""); |
| 390 | error_message << layer_name; |
| 391 | error_message << _("\". \n\nDetails: Attemp to write a readonly database, or "); |
| 392 | error_message << CPLGetLastErrorMsg(); |
| 393 | throw GdaException(error_message.mb_str()); |
| 394 | } |
| 395 | OGRFeatureDefn* poFeatDef = poDstLayer->GetLayerDefn(); |
| 396 | std::map<wxString, std::pair<int, int> >::iterator field_it; |
| 397 | // create fields using TableInterface:table |
| 398 | if ( table != NULL ) { |
| 399 | std::vector<int> col_id_map; // using orders in wxGrid |
| 400 | table->FillColIdMap(col_id_map); |
| 401 | for (int _id=0; _id < table->GetNumberCols(); _id++) { |
no test coverage detected