| 1 | /********************************************************************** |
| 2 | VectorOgr.cc: class to hold OGR features, typically read with readNextFeature |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2024 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include <unordered_map> |
| 22 | #include "VectorOgr.h" |
| 23 | #include "base/Optionjl.h" |
| 24 | #include "imageclasses/Jim.h" |
| 25 | |
| 26 | using namespace std; |
| 27 | using namespace statfactory; |
| 28 | |
| 29 | namespace svm{ |
| 30 | enum SVM_TYPE {C_SVC=0, nu_SVC=1,one_class=2, epsilon_SVR=3, nu_SVR=4}; |
| 31 | enum KERNEL_TYPE {linear=0,polynomial=1,radial=2,sigmoid=3}; |
| 32 | } |
| 33 | |
| 34 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) |
| 35 | |
| 36 | ///Default constructor |
| 37 | // VectorOgr::VectorOgr(void) : m_gds(NULL), m_layer(NULL), m_access(GDAL_OF_READONLY), m_projection(std::string()){} |
| 38 | VectorOgr::VectorOgr(void) : m_gds(NULL), m_access(GDAL_OF_READONLY){m_filename.clear();} |
| 39 | |
| 40 | ///Copy constructor |
| 41 | VectorOgr::VectorOgr(VectorOgr& other, app::AppFactory &app) : m_gds(NULL){ |
| 42 | Optionjl<std::string> filename_opt("fn", "filename", "filename"); |
| 43 | Optionjl<std::string> ogrformat_opt("f", "oformat", "Output sample dataset format","SQLite"); |
| 44 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 45 | |
| 46 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 47 | try{ |
| 48 | doProcess=filename_opt.retrieveOption(app); |
| 49 | ogrformat_opt.retrieveOption(app); |
| 50 | verbose_opt.retrieveOption(app); |
| 51 | } |
| 52 | catch(std::string predefinedString){ |
| 53 | std::cout << predefinedString << std::endl; |
| 54 | } |
| 55 | if(!doProcess){ |
| 56 | std::cout << std::endl; |
| 57 | std::ostringstream helpStream; |
| 58 | helpStream << "exception thrown due to help info"; |
| 59 | throw(helpStream.str());//help was invoked, stop processing |
| 60 | } |
| 61 | |
| 62 | if(filename_opt.empty()){ |
| 63 | std::ostringstream helpStream; |
| 64 | helpStream << "Error: VectorOgr constructor needs filename key"; |
| 65 | throw(helpStream.str());//help was invoked, stop processing |
| 66 | } |
| 67 | |
| 68 | open(filename_opt[0], ogrformat_opt[0], 1); |
| 69 | copy(other,app); |
| 70 | } |
| 71 | |
| 72 | ///Copy constructor |
| 73 | // VectorOgr::VectorOgr(VectorOgr& other, const std::string& filename, const std::string& imageType, char** options){ |
| 74 | // open(filename, imageType); |
| 75 | // copy(other); |
| 76 | // } |
| 77 | |
| 78 | ///Destructor |
| 79 | VectorOgr::~VectorOgr(void){ |
| 80 | destroyAll(); |
| 81 | close(); |
| 82 | m_filename.clear(); |
| 83 | }; |
| 84 | |
| 85 | ///assignment operator |
| 86 | // VectorOgr& VectorOgr::operator=(VectorOgr& other) |
| 87 | // { |
| 88 | // //check for assignment to self (of the form v=v) |
| 89 | // if(this==&other) |
| 90 | // return *this; |
| 91 | // else{ |
| 92 | // copy(other,copyData); |
| 93 | // return *this; |
| 94 | // } |
| 95 | // } |
| 96 | |
| 97 | // ///reset all features, releasing memory from heap |
| 98 | // void VectorOgr::destroyFeatures(size_t ilayer){ |
| 99 | // for(std::vector<OGRFeature*>::iterator fit=m_features[ilayer].begin();fit!=m_features[ilayer].end();++fit){ |
| 100 | // if(*fit){ |
| 101 | // OGRFeature::DestroyFeature( *fit ); |
| 102 | // *fit=NULL; |
| 103 | // } |
| 104 | // else |
| 105 | // std::cerr << "Error: cannot destroy NULL feature" << std::endl; |
| 106 | // } |
| 107 | // m_features[ilayer].clear(); |
| 108 | // m_features.erase(m_features.begin()+ilayer); |
| 109 | // m_layer.erase(m_layer.begin()+ilayer); |
| 110 | // } |
| 111 | |
| 112 | ///reset all features, releasing memory from heap |
| 113 | void VectorOgr::destroyAll(){ |
| 114 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 115 | destroyFeatures(ilayer); |
| 116 | } |
| 117 | m_features.clear(); |
| 118 | m_layer.clear(); |
| 119 | } |
| 120 | |
| 121 | bool VectorOgr::isEmpty(size_t ilayer){ |
| 122 | if(m_features.size()>ilayer){ |
| 123 | std::vector<OGRFeature*>::iterator fit=m_features[ilayer].begin(); |
| 124 | while(fit!=m_features[ilayer].end()){ |
| 125 | if(*fit){ |
| 126 | return false; |
| 127 | } |
| 128 | ++fit; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void VectorOgr::destroyEmptyFeatures(size_t ilayer){ |
| 135 | if(m_features.size()>ilayer){ |
| 136 | std::vector<OGRFeature*>::iterator fit=m_features[ilayer].begin(); |
| 137 | while(fit!=m_features[ilayer].end()){ |
| 138 | if(*fit){ |
| 139 | ++fit; |
| 140 | continue; |
| 141 | } |
| 142 | else{ |
| 143 | m_features[ilayer].erase(fit); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void VectorOgr::destroyFeatures(size_t ilayer){ |
| 150 | if(m_features.size()>ilayer){ |
| 151 | for(std::vector<OGRFeature*>::iterator fit=m_features[ilayer].begin();fit!=m_features[ilayer].end();++fit){ |
| 152 | if(*fit){ |
| 153 | OGRFeature::DestroyFeature( *fit ); |
| 154 | *fit=NULL; |
| 155 | } |
| 156 | // else |
| 157 | // std::cerr << "Error: cannot destroy NULL feature" << std::endl; |
| 158 | } |
| 159 | m_features[ilayer].clear(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // ///Assignment operator |
| 164 | // VectorOgr& VectorOgr::operator=(VectorOgr& other){ |
| 165 | // if(this!=&other){ |
| 166 | // destroyFeatures(); |
| 167 | // for(unsigned int index=0;index<other.getFeatureCount();++index) |
| 168 | // m_features.push_back(other.cloneFeature(index)); |
| 169 | // } |
| 170 | // return(*this); |
| 171 | // } |
| 172 | |
| 173 | ///open a GDAL vector dataset for reading |
| 174 | OGRErr VectorOgr::open(const std::string& ogrFilename, const std::vector<std::string>& layernames, bool noread, const std::vector<std::string>& ooptions){ |
| 175 | try{ |
| 176 | m_filename = ogrFilename; |
| 177 | #if GDAL_VERSION_MAJOR < 2 |
| 178 | //register the drivers |
| 179 | OGRRegisterAll(); |
| 180 | //open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string. |
| 181 | m_gds = OGRSFDriverRegistrar::Open(ogrFilename, FALSE);//FALSE: do not update |
| 182 | #else |
| 183 | //open options |
| 184 | char **papszOptions=NULL; |
| 185 | for(std::vector<std::string>::const_iterator optionIt=ooptions.begin();optionIt!=ooptions.end();++optionIt) |
| 186 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 187 | papszOptions=CSLAddString(papszOptions,NULL); |
| 188 | //register the drivers |
| 189 | GDALAllRegister(); |
| 190 | //open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string. |
| 191 | m_gds = (GDALDataset*) GDALOpenEx(ogrFilename.c_str(), GDAL_OF_VECTOR||GDAL_OF_READONLY, NULL, papszOptions, NULL); |
| 192 | // m_gds = (GDALDataset*) GDALOpenEx(ogrFilename.c_str(), GDAL_OF_VECTOR||GDAL_OF_READONLY, NULL, NULL, NULL); |
| 193 | #endif |
| 194 | if( m_gds == NULL ){ |
| 195 | #if GDAL_VERSION_MAJOR < 2 |
| 196 | std::string errorString="Open failed"; |
| 197 | throw(errorString); |
| 198 | #else |
| 199 | m_gds = (GDALDataset*) GDALOpenEx(ogrFilename.c_str(), GDAL_OF_VECTOR, NULL, papszOptions, NULL); |
| 200 | // m_gds = (GDALDataset*) GDALOpenEx(ogrFilename.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL); |
| 201 | if( m_gds == NULL ){ |
| 202 | ostringstream errorStream; |
| 203 | errorStream << "Open failed for file " << ogrFilename << std::endl; |
| 204 | throw(errorStream.str()); |
| 205 | } |
| 206 | #endif |
| 207 | } |
| 208 | for(size_t ilayer=0;ilayer<getGDSLayerCount();++ilayer){ |
| 209 | OGRLayer *readLayer=m_gds->GetLayer(ilayer); |
| 210 | string currentLayername=readLayer->GetName(); |
| 211 | if(layernames.size()){ |
| 212 | vector<string>::const_iterator it=find(layernames.begin(),layernames.end(),currentLayername); |
| 213 | if(it==layernames.end()){ |
| 214 | continue; |
| 215 | } |
| 216 | } |
| 217 | m_layer.push_back(readLayer); |
| 218 | m_features.resize(m_layer.size()); |
| 219 | if(!noread) |
| 220 | readFeatures(m_layer.size()-1); |
| 221 | } |
| 222 | } |
| 223 | catch(std::string errorString){ |
| 224 | std::cerr << errorString << std::endl; |
| 225 | throw; |
| 226 | } |
| 227 | return(OGRERR_NONE); |
| 228 | // return(setProjection(m_gds->GetProjectionRef())); |
| 229 | } |
| 230 | |
| 231 | ///open a GDAL vector dataset for writing with layers to be pushed later |
| 232 | OGRErr VectorOgr::open(const std::string& filename, const std::string& imageType, unsigned int access){ |
| 233 | setAccess(access); |
| 234 | m_filename = filename; |
| 235 | setCodec(filename,imageType); |
| 236 | if( m_gds == NULL ){ |
| 237 | std::string errorString="Open failed (1)"; |
| 238 | throw(errorString); |
| 239 | } |
| 240 | return(OGRERR_NONE); |
| 241 | } |
| 242 | |
| 243 | ///open a GDAL vector dataset for writing |
| 244 | OGRErr VectorOgr::open(const std::string& filename, const std::vector<std::string>& layernames, const std::string& imageType, const OGRwkbGeometryType& geometryType, OGRSpatialReference* theSRS, char** options){ |
| 245 | open(filename,imageType); |
| 246 | for(size_t ilayer=0;ilayer<layernames.size();++ilayer) |
| 247 | pushLayer(layernames[ilayer],theSRS,geometryType,options); |
| 248 | return(OGRERR_NONE); |
| 249 | } |
| 250 | |
| 251 | ///open a GDAL vector dataset for writing |
| 252 | OGRErr VectorOgr::open(const std::string& filename, const std::vector<std::string>& layernames, const std::string& imageType, const OGRwkbGeometryType& geometryType, const std::string& theProjection, char** options){ |
| 253 | open(filename,imageType); |
| 254 | for(size_t ilayer=0;ilayer<layernames.size();++ilayer) |
| 255 | pushLayer(layernames[ilayer],theProjection,geometryType,options); |
| 256 | return(OGRERR_NONE); |
| 257 | } |
| 258 | |
| 259 | ///open a GDAL vector dataset for writing |
| 260 | OGRErr VectorOgr::open(const std::string& filename, const std::vector<std::string>& layernames, const std::string& imageType, const std::string& geometryType, const std::string& theProjection, char** options){ |
| 261 | open(filename,imageType); |
| 262 | for(size_t ilayer=0;ilayer<layernames.size();++ilayer) |
| 263 | pushLayer(layernames[ilayer],theProjection,geometryType,options); |
| 264 | return(OGRERR_NONE); |
| 265 | } |
| 266 | |
| 267 | ///open vector dataset for reading/writing |
| 268 | OGRErr VectorOgr::open(app::AppFactory& app){ |
| 269 | Optionjl<std::string> filename_opt("fn", "filename", "filename"); |
| 270 | Optionjl<std::string> layer_opt("ln", "ln", "Layer name"); |
| 271 | Optionjl<std::string> projection_opt("a_srs", "a_srs", "Assign projection"); |
| 272 | Optionjl<std::string> geometryType_opt("gtype", "gtype", "Geometry type","wkbUnknown"); |
| 273 | Optionjl<std::string> coptions_opt("co", "co", "format dependent options controlling creation of the output file"); |
| 274 | Optionjl<std::string> ooptions_opt("oo", "oo", "Open option(s) for output file. Multiple options can be specified."); |
| 275 | Optionjl<std::string> ogrformat_opt("f", "oformat", "Output sample dataset format"); |
| 276 | Optionjl<unsigned int> access_opt("access", "access", "Access (0: GDAL_OF_READ_ONLY, 1: GDAL_OF_UPDATE)",0); |
| 277 | Optionjl<bool> noread_opt("noread", "noread", "do not read features when opening)",false); |
| 278 | Optionjl<std::string> attributeFilter_opt("af", "attributeFilter", "attribute filter"); |
| 279 | Optionjl<double> ulx_opt("ulx", "ulx", "Upper left x value bounding box"); |
| 280 | Optionjl<double> uly_opt("uly", "uly", "Upper left y value bounding box"); |
| 281 | Optionjl<double> lrx_opt("lrx", "lrx", "Lower right x value bounding box"); |
| 282 | Optionjl<double> lry_opt("lry", "lry", "Lower right y value bounding box"); |
| 283 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 284 | |
| 285 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 286 | try{ |
| 287 | doProcess=filename_opt.retrieveOption(app); |
| 288 | layer_opt.retrieveOption(app); |
| 289 | projection_opt.retrieveOption(app); |
| 290 | geometryType_opt.retrieveOption(app); |
| 291 | coptions_opt.retrieveOption(app); |
| 292 | ooptions_opt.retrieveOption(app); |
| 293 | ogrformat_opt.retrieveOption(app); |
| 294 | access_opt.retrieveOption(app); |
| 295 | noread_opt.retrieveOption(app); |
| 296 | attributeFilter_opt.retrieveOption(app); |
| 297 | ulx_opt.retrieveOption(app); |
| 298 | uly_opt.retrieveOption(app); |
| 299 | lrx_opt.retrieveOption(app); |
| 300 | lry_opt.retrieveOption(app); |
| 301 | verbose_opt.retrieveOption(app); |
| 302 | } |
| 303 | catch(std::string predefinedString){ |
| 304 | std::cout << predefinedString << std::endl; |
| 305 | } |
| 306 | if(!doProcess){ |
| 307 | std::cout << std::endl; |
| 308 | std::ostringstream helpStream; |
| 309 | helpStream << "exception thrown due to help info"; |
| 310 | throw(helpStream.str());//help was invoked, stop processing |
| 311 | } |
| 312 | |
| 313 | // std::vector<std::string> badKeys; |
| 314 | // app.badKeys(badKeys); |
| 315 | // if(badKeys.size()){ |
| 316 | // std::ostringstream errorStream; |
| 317 | // if(badKeys.size()>1) |
| 318 | // errorStream << "Error: unknown keys: "; |
| 319 | // else |
| 320 | // errorStream << "Error: unknown key: "; |
| 321 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 322 | // errorStream << badKeys[ikey] << " "; |
| 323 | // } |
| 324 | // errorStream << std::endl; |
| 325 | // throw(errorStream.str()); |
| 326 | // } |
| 327 | if(filename_opt.empty()){ |
| 328 | std::ostringstream helpStream; |
| 329 | helpStream << "Error: VectorOgr constructor needs filename key"; |
| 330 | throw(helpStream.str());//help was invoked, stop processing |
| 331 | } |
| 332 | setAccess(access_opt[0]); |
| 333 | if(getAccess()==GDAL_OF_READONLY && ogrformat_opt.empty()){ |
| 334 | if(verbose_opt[0]) |
| 335 | std::cout << "open in read access mode" << std::endl; |
| 336 | bool noread=true; |
| 337 | //do not read features yet, only initialize layers |
| 338 | open(filename_opt[0], layer_opt, noread, ooptions_opt); |
| 339 | if(m_gds){ |
| 340 | // for(size_t ilayer=0;ilayer<getGDSLayerCount();++ilayer){ |
| 341 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 342 | // OGRLayer *readLayer=m_gds->GetLayer(ilayer); |
| 343 | // OGRLayer *readLayer=getLayer(ilayer); |
| 344 | // string currentLayername=readLayer->GetName(); |
| 345 | // if(layer_opt.size()){ |
| 346 | // vector<string>::const_iterator it=find(layer_opt.begin(),layer_opt.end(),currentLayername); |
| 347 | // if(it==layer_opt.end()) |
| 348 | // continue; |
| 349 | // } |
| 350 | //we already initialized the layers in the open above |
| 351 | // if(ilayer<getLayerCount()) |
| 352 | // m_layer[ilayer]=readLayer; |
| 353 | // else |
| 354 | // m_layer.push_back(readLayer); |
| 355 | // m_features.resize(m_layer.size()); |
| 356 | unsigned int nfeatures=0; |
| 357 | if(attributeFilter_opt.size()) |
| 358 | setAttributeFilter(attributeFilter_opt[0],ilayer); |
| 359 | if(ulx_opt.size()&&uly_opt.size()&&lrx_opt.size()&&lry_opt.size()) |
| 360 | setSpatialFilterRect(ulx_opt[0],uly_opt[0],lrx_opt[0],lry_opt[0],ilayer); |
| 361 | if(!noread_opt[0]) |
| 362 | nfeatures=readFeatures(ilayer); |
| 363 | if(verbose_opt[0]) |
| 364 | std::cout << "read " << nfeatures << " features in layer " << getLayerName(ilayer) << std::endl; |
| 365 | // unsigned int nfeatures=0; |
| 366 | // if(attributeFilter_opt.size()) |
| 367 | // setAttributeFilter(attributeFilter_opt[0],ilayer); |
| 368 | // if(!noread_opt[0]) |
| 369 | // nfeatures=readFeatures(ilayer); |
| 370 | // if(verbose_opt[0]) |
| 371 | // std::cout << "read " << nfeatures << " features in layer " << ilayer << std::endl; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | //open for writing |
| 376 | if(!m_gds){ |
| 377 | setAccess(GDAL_OF_UPDATE); |
| 378 | } |
| 379 | if(getAccess()==GDAL_OF_UPDATE){ |
| 380 | if(ogrformat_opt.empty()) |
| 381 | ogrformat_opt.push_back("SQLite"); |
| 382 | char **papszOptions=NULL; |
| 383 | for(std::vector<std::string>::const_iterator optionIt=coptions_opt.begin();optionIt!=coptions_opt.end();++optionIt){ |
| 384 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 385 | } |
| 386 | if(verbose_opt[0]) |
| 387 | std::cout << "open in update mode" << std::endl; |
| 388 | if(layer_opt.size()){ |
| 389 | if(projection_opt.size()){ |
| 390 | return(open(filename_opt[0], layer_opt, ogrformat_opt[0], geometryType_opt[0], projection_opt[0], papszOptions)); |
| 391 | } |
| 392 | else{ |
| 393 | OGRwkbGeometryType gType=string2geotype(geometryType_opt[0]); |
| 394 | return(open(filename_opt[0], layer_opt, ogrformat_opt[0], gType, NULL, papszOptions)); |
| 395 | } |
| 396 | } |
| 397 | else{ |
| 398 | return(open(filename_opt[0],ogrformat_opt[0]),getAccess()); |
| 399 | } |
| 400 | } |
| 401 | if(verbose_opt[0]) |
| 402 | std::cout << "Opened vector dataset" << std::endl; |
| 403 | return(OGRERR_NONE); |
| 404 | } |
| 405 | |
| 406 | ///prepare driver for writing vector dataset |
| 407 | void VectorOgr::setCodec(const std::string& filename, const std::string& imageType){ |
| 408 | if(m_gds){ |
| 409 | std::string errorString="Warning: vector dataset is already open"; |
| 410 | throw(errorString); |
| 411 | } |
| 412 | #if GDAL_VERSION_MAJOR < 2 |
| 413 | //register the drivers |
| 414 | OGRRegisterAll(); |
| 415 | //fetch the OGR file driver |
| 416 | OGRSFDriver *poDriver; |
| 417 | poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(imageType.c_str()); |
| 418 | #else |
| 419 | //register the drivers |
| 420 | GDALAllRegister(); |
| 421 | GDALDriver *poDriver; |
| 422 | poDriver = GetGDALDriverManager()->GetDriverByName(imageType.c_str()); |
| 423 | #endif |
| 424 | if( poDriver == NULL ){ |
| 425 | std::string errorString="Error: FileOpenError"; |
| 426 | throw(errorString); |
| 427 | } |
| 428 | if(getAccess()==GDAL_OF_UPDATE){ |
| 429 | //check if file already exists |
| 430 | #if GDAL_VERSION_MAJOR < 2 |
| 431 | m_gds = OGRSFDriverRegistrar::Open( filename.c_str(), TRUE ); |
| 432 | #else |
| 433 | m_gds = (GDALDataset*) GDALOpenEx(filename.c_str(), GDAL_OF_UPDATE||GDAL_OF_VECTOR, NULL, NULL, NULL); |
| 434 | if(!m_gds) |
| 435 | m_gds=poDriver->Create(filename.c_str(),0,0,0,GDT_Unknown,NULL); |
| 436 | #endif |
| 437 | } |
| 438 | else{//(overwrite existing layer) |
| 439 | #if GDAL_VERSION_MAJOR < 2 |
| 440 | m_gds = OGRSFDriverRegistrar::Open( filename.c_str(), FALSE ); |
| 441 | #else |
| 442 | m_gds = (GDALDataset*) GDALOpenEx(filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL); |
| 443 | #endif |
| 444 | if(m_gds){ |
| 445 | #if GDAL_VERSION_MAJOR < 2 |
| 446 | OGRDataSource::DestroyDataSource(m_gds); |
| 447 | #else |
| 448 | GDALClose(m_gds); |
| 449 | #endif |
| 450 | } |
| 451 | #if GDAL_VERSION_MAJOR < 2 |
| 452 | m_gds=poDriver->CreateDataSource(filename.c_str(),NULL); |
| 453 | #else |
| 454 | m_gds=poDriver->Create(filename.c_str(),0,0,0,GDT_Unknown,NULL); |
| 455 | #endif |
| 456 | } |
| 457 | if(!m_gds){ |
| 458 | std::string errorString="m_gds is 0"; |
| 459 | throw(errorString); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | //close the vector dataset |
| 464 | void VectorOgr::close(void) |
| 465 | { |
| 466 | // destroyFeatures();//already done in destructor |
| 467 | if(m_gds){ |
| 468 | #if GDAL_VERSION_MAJOR < 2 |
| 469 | OGRDataSource::DestroyDataSource(m_gds); |
| 470 | #else |
| 471 | GDALClose(m_gds); |
| 472 | #endif |
| 473 | m_gds=NULL; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | ///Create a layer |
| 478 | OGRErr VectorOgr::pushLayer(const std::string& layername, const OGRSpatialReference* theSRS, const OGRwkbGeometryType& geometryType, char** papszOptions){ |
| 479 | if( !m_gds->TestCapability( ODsCCreateLayer ) ){ |
| 480 | // std::ostringstream errorStream; |
| 481 | // errorStream << "Error: Test capability to create layer " << layername << " failed (1)" << std::endl; |
| 482 | // throw(errorStream.str()); |
| 483 | std::cerr << "Error: Test capability to create layer " << layername << " failed (1)" << std::endl; |
| 484 | std::string errorString="pushLayer failed, file already exists?"; |
| 485 | throw(errorString); |
| 486 | } |
| 487 | //if no constraints on the types geometry to be written: use wkbUnknown |
| 488 | OGRSpatialReference* srsCopy = theSRS ? theSRS->Clone() : nullptr; |
| 489 | m_layer.push_back(m_gds->CreateLayer(layername.c_str(), srsCopy, geometryType, papszOptions)); |
| 490 | if (srsCopy) { |
| 491 | srsCopy->Release(); |
| 492 | } |
| 493 | |
| 494 | m_features.resize(m_layer.size()); |
| 495 | if(!m_layer.back()){ |
| 496 | std::string errorString="Open failed"; |
| 497 | throw(errorString); |
| 498 | } |
| 499 | return(OGRERR_NONE); |
| 500 | } |
| 501 | |
| 502 | ///Create a layer |
| 503 | OGRErr VectorOgr::pushLayer(const std::string& layername, const std::string& theProjection, const OGRwkbGeometryType& geometryType, char** papszOptions){ |
| 504 | OGRErr result=OGRERR_NONE; |
| 505 | if( !m_gds){ |
| 506 | std::string errorString="pushLayer failed due to no associated GDAL dataset"; |
| 507 | throw(errorString); |
| 508 | } |
| 509 | if( !m_gds->TestCapability( ODsCCreateLayer ) ){ |
| 510 | // std::ostringstream errorStream; |
| 511 | // errorStream << "Error: Test capability to create layer " << layername << " failed (2)" << std::endl; |
| 512 | // throw(errorStream.str()); |
| 513 | std::cerr << "Error: Test capability to create layer " << layername << " failed (2)" << std::endl; |
| 514 | std::string errorString="pushLayer failed, file already exists?"; |
| 515 | throw(errorString); |
| 516 | } |
| 517 | //if no constraints on the types geometry to be written: use wkbUnknown |
| 518 | if(theProjection.size()){ |
| 519 | OGRSpatialReference* poSRS = new OGRSpatialReference(); |
| 520 | #if GDAL_VERSION_MAJOR > 2 |
| 521 | poSRS->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 522 | #endif |
| 523 | // OGRSpatialReference oSRS; |
| 524 | poSRS->SetFromUserInput(theProjection.c_str()); |
| 525 | result=pushLayer(layername.c_str(), poSRS, geometryType, papszOptions); |
| 526 | poSRS->Release(); |
| 527 | } |
| 528 | else |
| 529 | result=pushLayer(layername.c_str(), NULL, geometryType, papszOptions); |
| 530 | return(result); |
| 531 | } |
| 532 | |
| 533 | ///Create a layer |
| 534 | OGRErr VectorOgr::pushLayer(const std::string& layername, const std::string& theProjection, const std::string& geometryType, char** papszOptions){ |
| 535 | OGRErr result=OGRERR_NONE; |
| 536 | if( !m_gds->TestCapability( ODsCCreateLayer ) ){ |
| 537 | // std::ostringstream errorStream; |
| 538 | // errorStream << "Error: Test capability to create layer " << layername << " failed (3)" << std::endl; |
| 539 | // throw(errorStream.str()); |
| 540 | std::cerr << "Error: Test capability to create layer " << layername << " failed (3)" << std::endl; |
| 541 | std::string errorString="pushLayer failed, file already exists?"; |
| 542 | throw(errorString); |
| 543 | } |
| 544 | //if no constraints on the types geometry to be written: use wkbUnknown |
| 545 | OGRwkbGeometryType eGType=wkbUnknown; |
| 546 | if(geometryType.size()) |
| 547 | eGType=string2geotype(geometryType); |
| 548 | |
| 549 | if(theProjection.size()){ |
| 550 | OGRSpatialReference* poSRS = new OGRSpatialReference(); |
| 551 | #if GDAL_VERSION_MAJOR > 2 |
| 552 | poSRS->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 553 | #endif |
| 554 | // OGRSpatialReference oSRS; |
| 555 | poSRS->SetFromUserInput(theProjection.c_str()); |
| 556 | result=pushLayer(layername.c_str(), poSRS, eGType, papszOptions); |
| 557 | poSRS->Release(); |
| 558 | } |
| 559 | else |
| 560 | result=pushLayer(layername.c_str(), NULL, eGType, papszOptions); |
| 561 | //check if destroy is needed?! |
| 562 | // CSLDestroy( papszOptions ); |
| 563 | return(result); |
| 564 | } |
| 565 | |
| 566 | std::shared_ptr<VectorOgr> VectorOgr::intersect(OGRPolygon *pGeom, app::AppFactory& app){ |
| 567 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 568 | if(intersect(pGeom, *ogrWriter, app)!=OGRERR_NONE){ |
| 569 | std::ostringstream errorStream; |
| 570 | errorStream << "Error: failed to intersect" << std::endl; |
| 571 | std::cerr << errorStream.str() << std::endl; |
| 572 | throw(errorStream.str()); |
| 573 | } |
| 574 | return(ogrWriter); |
| 575 | } |
| 576 | |
| 577 | std::shared_ptr<VectorOgr> VectorOgr::intersect(const Jim& aJim, app::AppFactory& app){ |
| 578 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 579 | if(intersect(aJim, *ogrWriter, app)!=OGRERR_NONE){ |
| 580 | std::ostringstream errorStream; |
| 581 | errorStream << "Error: failed to intersect" << std::endl; |
| 582 | std::cerr << errorStream.str() << std::endl; |
| 583 | throw(errorStream.str()); |
| 584 | } |
| 585 | return(ogrWriter); |
| 586 | } |
| 587 | |
| 588 | OGRErr VectorOgr::intersect(OGRPolygon *pGeom, VectorOgr& ogrWriter, app::AppFactory& app){ |
| 589 | Optionjl<string> output_opt("o", "output", "Output sample dataset"); |
| 590 | Optionjl<string> ogrformat_opt("f", "oformat", "Output vector dataset format","SQLite"); |
| 591 | Optionjl<unsigned int> access_opt("access", "access", "Access (0: GDAL_OF_READ_ONLY, 1: GDAL_OF_UPDATE)",1); |
| 592 | Optionjl<std::string> options_opt("co", "co", "format dependent options controlling creation of the output file"); |
| 593 | // Optionjl<bool> allCovered_opt("ac", "all_covered", "Set this flag to include only those polygons that are entirely covered by the raster", false); |
| 594 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 595 | |
| 596 | // allCovered_opt.setHide(1); |
| 597 | options_opt.setHide(1); |
| 598 | |
| 599 | |
| 600 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 601 | try{ |
| 602 | doProcess=output_opt.retrieveOption(app); |
| 603 | ogrformat_opt.retrieveOption(app); |
| 604 | access_opt.retrieveOption(app); |
| 605 | options_opt.retrieveOption(app); |
| 606 | // allCovered_opt.retrieveOption(app); |
| 607 | verbose_opt.retrieveOption(app); |
| 608 | if(!doProcess){ |
| 609 | cout << endl; |
| 610 | std::ostringstream helpStream; |
| 611 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 612 | throw(helpStream.str());//help was invoked, stop processing |
| 613 | } |
| 614 | char **papszOptions=NULL; |
| 615 | for(std::vector<std::string>::const_iterator optionIt=options_opt.begin();optionIt!=options_opt.end();++optionIt){ |
| 616 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 617 | } |
| 618 | |
| 619 | if(output_opt.empty()){ |
| 620 | std::ostringstream errorStream; |
| 621 | errorStream << "Error: no output file provided" << std::endl; |
| 622 | throw(errorStream.str()); |
| 623 | } |
| 624 | |
| 625 | ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 626 | |
| 627 | for(int ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 628 | ogrWriter.pushLayer(getLayerName(ilayer),getProjection(ilayer),getGeometryType(ilayer),papszOptions); |
| 629 | ogrWriter.copyFields(*this,std::vector<std::string>(),ilayer, ilayer); |
| 630 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 631 | ogrWriter.resize(getFeatureCount(ilayer),ilayer); |
| 632 | #endif |
| 633 | |
| 634 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 635 | #pragma omp parallel for |
| 636 | #else |
| 637 | #endif |
| 638 | for(size_t ifeature=0;ifeature<getFeatureCount(ilayer);++ifeature){ |
| 639 | if(verbose_opt[0]>1) |
| 640 | std::cout << "feature " << ifeature << endl; |
| 641 | OGRFeature *readFeature=getFeatureRef(ifeature,ilayer); |
| 642 | if(readFeature){ |
| 643 | if(readFeature->GetGeometryRef()->Intersects(pGeom)){ |
| 644 | if(verbose_opt[0]>1) |
| 645 | std::cout << "write valid feature " << ifeature << endl; |
| 646 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 647 | writeFeature->SetFrom(readFeature); |
| 648 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 649 | ogrWriter.setFeature(ifeature,writeFeature,ilayer); |
| 650 | #else |
| 651 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 652 | #endif |
| 653 | } |
| 654 | else{ |
| 655 | if(verbose_opt[0]>1) |
| 656 | std::cerr << "Warning: " << ifeature << " is not intersecting" << std::endl; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | //todo: check if needed? |
| 661 | // ogrWriter.destroyEmptyFeatures(ilayer); |
| 662 | } |
| 663 | } |
| 664 | catch(std::string errorString){ |
| 665 | std::cerr << "Error: " << errorString << std::endl; |
| 666 | throw; |
| 667 | } |
| 668 | catch(...){ |
| 669 | std::cerr << "Error: undefined" << std::endl; |
| 670 | throw; |
| 671 | } |
| 672 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 673 | ogrWriter.destroyEmptyFeatures(); |
| 674 | #endif |
| 675 | return(OGRERR_NONE); |
| 676 | } |
| 677 | |
| 678 | OGRErr VectorOgr::intersect(const Jim& aJim, VectorOgr& ogrWriter, app::AppFactory& app){ |
| 679 | OGRErr result=OGRERR_NONE; |
| 680 | OGRPolygon *pGeom = (OGRPolygon*) OGRGeometryFactory::createGeometry(wkbPolygon); |
| 681 | OGRSpatialReference imgSpatialRef=aJim.getSpatialRef(); |
| 682 | #if GDAL_VERSION_MAJOR > 2 |
| 683 | imgSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 684 | #endif |
| 685 | // OGRSpatialReference *thisSpatialRef=getLayer()->GetSpatialRef(); |
| 686 | OGRSpatialReference thisSpatialRef=this->getSpatialRef(); |
| 687 | #if GDAL_VERSION_MAJOR > 2 |
| 688 | thisSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 689 | #endif |
| 690 | |
| 691 | OGRCoordinateTransformation *img2vector = OGRCreateCoordinateTransformation(&imgSpatialRef, &thisSpatialRef); |
| 692 | aJim.getBoundingBox(pGeom,img2vector); |
| 693 | result=intersect(pGeom,ogrWriter,app); |
| 694 | OGRGeometryFactory::destroyGeometry(pGeom ); |
| 695 | return(result); |
| 696 | } |
| 697 | |
| 698 | std::shared_ptr<VectorOgr> VectorOgr::convexHull(app::AppFactory& app){ |
| 699 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 700 | if(convexHull(*ogrWriter, app)!=OGRERR_NONE){ |
| 701 | std::cerr << "Failed to convexHull" << std::endl; |
| 702 | } |
| 703 | return(ogrWriter); |
| 704 | } |
| 705 | |
| 706 | OGRErr VectorOgr::convexHull(VectorOgr& ogrWriter, app::AppFactory& app){ |
| 707 | Optionjl<string> output_opt("o", "output", "Output sample dataset"); |
| 708 | Optionjl<string> ogrformat_opt("f", "oformat", "Output vector dataset format","SQLite"); |
| 709 | Optionjl<unsigned int> access_opt("access", "access", "Access (0: GDAL_OF_READ_ONLY, 1: GDAL_OF_UPDATE)",1); |
| 710 | Optionjl<std::string> options_opt("co", "co", "format dependent options controlling creation of the output file"); |
| 711 | // Optionjl<bool> allCovered_opt("ac", "all_covered", "Set this flag to include only those polygons that are entirely covered by the raster", false); |
| 712 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 713 | |
| 714 | // allCovered_opt.setHide(1); |
| 715 | options_opt.setHide(1); |
| 716 | |
| 717 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 718 | try{ |
| 719 | doProcess=output_opt.retrieveOption(app); |
| 720 | ogrformat_opt.retrieveOption(app); |
| 721 | access_opt.retrieveOption(app); |
| 722 | options_opt.retrieveOption(app); |
| 723 | // allCovered_opt.retrieveOption(app); |
| 724 | verbose_opt.retrieveOption(app); |
| 725 | if(!doProcess){ |
| 726 | cout << endl; |
| 727 | std::ostringstream helpStream; |
| 728 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 729 | throw(helpStream.str());//help was invoked, stop processing |
| 730 | } |
| 731 | char **papszOptions=NULL; |
| 732 | for(std::vector<std::string>::const_iterator optionIt=options_opt.begin();optionIt!=options_opt.end();++optionIt){ |
| 733 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 734 | } |
| 735 | |
| 736 | if(output_opt.empty()){ |
| 737 | std::ostringstream errorStream; |
| 738 | errorStream << "Error: no output file provided" << std::endl; |
| 739 | throw(errorStream.str()); |
| 740 | } |
| 741 | |
| 742 | ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 743 | for(int ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 744 | OGRSpatialReference thisSpatialRef=this->getSpatialRef(); |
| 745 | #if GDAL_VERSION_MAJOR > 2 |
| 746 | thisSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 747 | #endif |
| 748 | ogrWriter.pushLayer(getLayerName(ilayer),&thisSpatialRef,wkbPolygon,papszOptions); |
| 749 | // ogrWriter.pushLayer(getLayerName(ilayer),getLayer(ilayer)->GetSpatialRef(),wkbPolygon,papszOptions); |
| 750 | // #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 751 | // #pragma omp parallel for |
| 752 | // #else |
| 753 | // #endif |
| 754 | ogrWriter.createField("id",OFTInteger,ilayer); |
| 755 | OGRGeometryCollection geomColl; |
| 756 | for(size_t ifeature=0;ifeature<getFeatureCount(ilayer);++ifeature){ |
| 757 | OGRFeature *readFeature=getFeatureRef(ifeature,ilayer); |
| 758 | if(readFeature) |
| 759 | geomColl.addGeometry(readFeature->GetGeometryRef()); |
| 760 | } |
| 761 | |
| 762 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 763 | //todo: only set convexHulled features. check if NULL features are a problem when writing |
| 764 | // ogrWriter.pushFeature(writeFeature,ilayer); |
| 765 | OGRGeometry *pGeom=geomColl.ConvexHull(); |
| 766 | OGRFeature *poFeature=ogrWriter.createFeature(ilayer); |
| 767 | poFeature->SetField("id",1); |
| 768 | poFeature->SetGeometry(pGeom); |
| 769 | ogrWriter.pushFeature(poFeature,ilayer); |
| 770 | //todo:check if need to destroy feature? |
| 771 | // destroyEmptyFeatures(ilayer); |
| 772 | } |
| 773 | } |
| 774 | catch(std::string errorString){ |
| 775 | std::cerr << "Error: " << errorString << std::endl; |
| 776 | throw; |
| 777 | } |
| 778 | catch(...){ |
| 779 | std::cerr << "Error: undefined" << std::endl; |
| 780 | throw; |
| 781 | } |
| 782 | return(OGRERR_NONE); |
| 783 | } |
| 784 | |
| 785 | OGRErr VectorOgr::createField(const std::string& fieldname, const OGRFieldType& fieldType, size_t ilayer){ |
| 786 | OGRFieldDefn oField( fieldname.c_str(), fieldType ); |
| 787 | if(fieldType==OFTString) |
| 788 | oField.SetWidth(32); |
| 789 | return(getLayer(ilayer)->CreateField( &oField )); |
| 790 | } |
| 791 | |
| 792 | ///create field |
| 793 | OGRErr VectorOgr::createField(OGRFieldDefn* poField, size_t ilayer){ |
| 794 | if(! m_gds){ |
| 795 | std::cerr << "Error: createField failed, no dataset" << std::endl; |
| 796 | std::string errorString="createField failed, no dataset"; |
| 797 | throw(errorString); |
| 798 | // return(OGRERR_FAILURE); |
| 799 | } |
| 800 | m_gds->GetLayer(ilayer)->CreateField(poField); |
| 801 | return(OGRERR_NONE); |
| 802 | } |
| 803 | |
| 804 | ///copy fields from other VectorOgr instance |
| 805 | OGRErr VectorOgr::copyFields(const VectorOgr& vectorOgr,const vector<std::string>& fieldnames, size_t fromLayer, size_t toLayer){ |
| 806 | try{ |
| 807 | if(!m_gds){ |
| 808 | std::string errorString="Error: no GDAL dataset"; |
| 809 | throw(errorString); |
| 810 | } |
| 811 | //get fields from vectorOgr |
| 812 | std::vector<OGRFieldDefn*> fields; |
| 813 | vectorOgr.getFields(fields,fromLayer); |
| 814 | |
| 815 | for(unsigned int iField=0;iField<fields.size();++iField){ |
| 816 | if(fieldnames.size()){ |
| 817 | std::string fieldname=fields[iField]->GetNameRef(); |
| 818 | std::vector<std::string>::const_iterator fit = std::find(fieldnames.begin(), fieldnames.end(), fieldname); |
| 819 | if(fit!=fieldnames.end()){ |
| 820 | if(!m_gds->GetLayer(toLayer)) |
| 821 | std::cerr << "Warning: could not get layer" << std::endl; |
| 822 | if(createField(fields[iField],toLayer)!=OGRERR_NONE){ |
| 823 | std::string errorString="Error: could not create field"; |
| 824 | throw(errorString); |
| 825 | } |
| 826 | } |
| 827 | else |
| 828 | continue; |
| 829 | } |
| 830 | else{ |
| 831 | if(createField(fields[iField],toLayer)!=OGRERR_NONE){ |
| 832 | std::string errorString="Error: could not create field"; |
| 833 | throw(errorString); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | catch(std::string errorString){ |
| 839 | std::cerr << errorString << std::endl; |
| 840 | throw; |
| 841 | } |
| 842 | return(OGRERR_NONE); |
| 843 | } |
| 844 | |
| 845 | ///perform a deep copy, including layers and features |
| 846 | OGRErr VectorOgr::copy(VectorOgr& other, app::AppFactory &app){ |
| 847 | Optionjl<std::string> options_opt("co", "co", "format dependent options controlling creation of the output file"); |
| 848 | Optionjl<std::string> newfield_opt("newfield", "newfield", "create new field(s)"); |
| 849 | Optionjl<std::string> ftype_opt("newtype", "newtype", "Field type (only Inter, Real, or String)", "Integer"); |
| 850 | Optionjl<std::string> newvalue_opt("newvalue", "newvalue", "value(s) to set for new field(s)"); |
| 851 | Optionjl<std::string> targetSRS_opt("t_srs", "t_srs", "Target spatial reference, e.g., epsg:3035 to use European projection and force to European grid"); |
| 852 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 853 | |
| 854 | options_opt.retrieveOption(app); |
| 855 | newfield_opt.retrieveOption(app); |
| 856 | ftype_opt.retrieveOption(app); |
| 857 | newvalue_opt.retrieveOption(app); |
| 858 | targetSRS_opt.retrieveOption(app); |
| 859 | verbose_opt.retrieveOption(app); |
| 860 | |
| 861 | OGRFieldType fieldType; |
| 862 | int ogr_typecount=11;//hard coded for now! |
| 863 | if(verbose_opt[0]>1) |
| 864 | std::cout << "field and label types can be: "; |
| 865 | for(int iType = 0; iType < ogr_typecount; ++iType){ |
| 866 | if(verbose_opt[0]>1) |
| 867 | std::cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType); |
| 868 | if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 869 | && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType), |
| 870 | ftype_opt[0].c_str())) |
| 871 | fieldType=(OGRFieldType) iType; |
| 872 | } |
| 873 | switch( fieldType ){ |
| 874 | case OFTInteger: |
| 875 | case OFTReal: |
| 876 | case OFTRealList: |
| 877 | case OFTString: |
| 878 | if(verbose_opt[0]>1) |
| 879 | std::cout << std::endl << "field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl; |
| 880 | break; |
| 881 | default: |
| 882 | cerr << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported" << std::endl; |
| 883 | std::ostringstream errorStream; |
| 884 | errorStream << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported"; |
| 885 | std::cerr << errorStream.str(); |
| 886 | throw(errorStream.str()); |
| 887 | } |
| 888 | |
| 889 | char **papszOptions=NULL; |
| 890 | for(std::vector<std::string>::const_iterator optionIt=options_opt.begin();optionIt!=options_opt.end();++optionIt) |
| 891 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 892 | if(newfield_opt.size() && newvalue_opt.size()){ |
| 893 | while(newvalue_opt.size()<newfield_opt.size()) |
| 894 | newvalue_opt.push_back(newvalue_opt.back()); |
| 895 | } |
| 896 | |
| 897 | OGRSpatialReference sourceSpatialRef; |
| 898 | OGRSpatialReference targetSpatialRef; |
| 899 | #if GDAL_VERSION_MAJOR > 2 |
| 900 | sourceSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 901 | targetSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 902 | #endif |
| 903 | |
| 904 | try{ |
| 905 | for(size_t ilayer=0;ilayer<other.getLayerCount();++ilayer){ |
| 906 | sourceSpatialRef.SetFromUserInput(other.getProjection(ilayer).c_str()); |
| 907 | if(targetSRS_opt.size()){ |
| 908 | targetSpatialRef.SetFromUserInput(targetSRS_opt[0].c_str()); |
| 909 | } |
| 910 | else{ |
| 911 | targetSpatialRef.SetFromUserInput(other.getProjection(ilayer).c_str()); |
| 912 | } |
| 913 | |
| 914 | char *targetWKT=0; |
| 915 | targetSpatialRef.exportToWkt(&targetWKT); |
| 916 | |
| 917 | OGRCoordinateTransformation *transform = OGRCreateCoordinateTransformation(&sourceSpatialRef, &targetSpatialRef); |
| 918 | |
| 919 | if(sourceSpatialRef.IsSame(&targetSpatialRef)){ |
| 920 | if(verbose_opt[0]) |
| 921 | std::cout << "spatial reference of vector target is same as raster" << std::endl; |
| 922 | transform=0; |
| 923 | } |
| 924 | else{ |
| 925 | if(verbose_opt[0]) |
| 926 | std::cout << "spatial reference of vector target is different from raster, transform: " << transform << std::endl; |
| 927 | if(!transform){ |
| 928 | std::ostringstream errorStream; |
| 929 | errorStream << "Error: cannot create OGRCoordinateTransformation" << std::endl; |
| 930 | throw(errorStream.str()); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | pushLayer(other.getLayerName(ilayer),&targetSpatialRef,other.getGeometryType(ilayer),papszOptions); |
| 935 | destroyFeatures(ilayer); |
| 936 | copyFields(other,std::vector<std::string>(),ilayer,ilayer); |
| 937 | for(std::vector<std::string>::const_iterator fieldit=newfield_opt.begin();fieldit!=newfield_opt.end();++fieldit) |
| 938 | createField(*fieldit,fieldType,ilayer); |
| 939 | |
| 940 | m_features[ilayer].resize(other.getFeatureCount(ilayer)); |
| 941 | |
| 942 | for(size_t ifeature=0;ifeature<other.getFeatureCount(ilayer);++ifeature){ |
| 943 | OGRFeature *writeFeature=createFeature(ilayer); |
| 944 | |
| 945 | OGRFeature *otherFeature=other.getFeatureRef(ifeature,ilayer); |
| 946 | if(otherFeature){ |
| 947 | writeFeature->SetFrom(otherFeature); |
| 948 | OGRGeometry* writeGeometry = writeFeature->GetGeometryRef(); |
| 949 | //coordinate transform |
| 950 | if(!VectorOgr::transform(writeGeometry,transform)){ |
| 951 | std::string errorString="Error: coordinate transform not successful"; |
| 952 | throw(errorString); |
| 953 | } |
| 954 | writeFeature->SetGeometry(writeGeometry); |
| 955 | for(size_t ifield=0;ifield<newfield_opt.size();++ifield){ |
| 956 | if(newvalue_opt.size()){ |
| 957 | switch( fieldType ){ |
| 958 | case OFTInteger: |
| 959 | writeFeature->SetField(newfield_opt[ifield].c_str(),string2type<GIntBig>(newvalue_opt[ifield])); |
| 960 | break; |
| 961 | case OFTReal: |
| 962 | case OFTRealList: |
| 963 | writeFeature->SetField(newfield_opt[ifield].c_str(),string2type<double>(newvalue_opt[ifield])); |
| 964 | break; |
| 965 | case OFTString: |
| 966 | writeFeature->SetField(newfield_opt[ifield].c_str(),newvalue_opt[ifield].c_str()); |
| 967 | break; |
| 968 | default: |
| 969 | cerr << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported" << std::endl; |
| 970 | std::ostringstream errorStream; |
| 971 | errorStream << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported"; |
| 972 | std::cerr << errorStream.str(); |
| 973 | throw(errorStream.str()); |
| 974 | } |
| 975 | } |
| 976 | else{ |
| 977 | writeFeature->SetField(newfield_opt[ifield].c_str(),static_cast<GIntBig>(ifeature)); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | m_features[ilayer][ifeature]=writeFeature; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | catch(std::string errorString){ |
| 986 | std::cout << errorString << std::endl; |
| 987 | } |
| 988 | write(); |
| 989 | return(OGRERR_NONE); |
| 990 | } |
| 991 | |
| 992 | ///merge another vector |
| 993 | //todo: handle different definitions or layers |
| 994 | //todo: handle multi-layers |
| 995 | // OGRErr VectorOgr::merge(VectorOgr& theVector){ |
| 996 | // //better not to parallellize here? |
| 997 | // // #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 998 | // // #pragma omp parallel for |
| 999 | // // #else |
| 1000 | // // #endif |
| 1001 | // for(unsigned int index=0;index<theVector.getFeatureCount();++index){ |
| 1002 | // OGRFeature *poFeature; |
| 1003 | // poFeature=theVector.cloneFeature(index); |
| 1004 | // pushFeature(poFeature); |
| 1005 | // } |
| 1006 | // } |
| 1007 | |
| 1008 | ///write features to the vector dataset |
| 1009 | OGRErr VectorOgr::write(const std::string& filename){ |
| 1010 | if(filename.size()){ |
| 1011 | vector<unsigned char> vbytes; |
| 1012 | size_t nbytes=serialize(vbytes); |
| 1013 | std::ofstream outputStream(filename, std::ios::out|std::ios::binary); |
| 1014 | outputStream.write(nbytes ? (char*)&vbytes[0] : 0, std::streamsize(nbytes)); |
| 1015 | outputStream.close(); |
| 1016 | // FILE *file = fopen(filename.c_str(),"wb"); |
| 1017 | // if(file){ |
| 1018 | // fwrite(vbytes.data(),vbytes.size(),1,file); |
| 1019 | // fclose(file); |
| 1020 | // } |
| 1021 | } |
| 1022 | else if(m_gds){ |
| 1023 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 1024 | auto fit=m_features[ilayer].begin(); |
| 1025 | size_t ifeature=0; |
| 1026 | while(fit!=m_features[ilayer].end()){ |
| 1027 | if(*fit){ |
| 1028 | if(getLayer(ilayer)->CreateFeature(*fit)!=OGRERR_NONE){ |
| 1029 | // std::ostringstream errorStream; |
| 1030 | // errorStream << "Warning: could not create feature " << ifeature << std::endl; |
| 1031 | // std::cerr << errorStream.str(); |
| 1032 | } |
| 1033 | ++ifeature; |
| 1034 | ++fit; |
| 1035 | } |
| 1036 | else{ |
| 1037 | // std::string errorString="Warning: NULL feature in m_feature"; |
| 1038 | // std::cout << errorString << std::endl; |
| 1039 | m_features[ilayer].erase(fit); |
| 1040 | ++ifeature; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | return(OGRERR_NONE); |
| 1046 | } |
| 1047 | |
| 1048 | ///copy all features from existing VectorOgr and write to vector dataset |
| 1049 | //not tested yet |
| 1050 | // void VectorOgr::write(VectorOgr& vectorOgr, const std::string& filename, const std::vector<std::string>& layernames, const::std::string& imageType, char** papszOptions){ |
| 1051 | // m_layer.clear(); |
| 1052 | // setCodec(filename,imageType); |
| 1053 | // if( !m_gds->TestCapability( ODsCCreateLayer ) ){ |
| 1054 | // std::string errorString="Error: Test capability to create layer failed"; |
| 1055 | // throw(errorString); |
| 1056 | // } |
| 1057 | // for(size_t ilayer=0; ilayer<vectorOgr.getLayerCount();++ilayer){ |
| 1058 | // //if no constraints on the types geometry to be written: use wkbUnknown |
| 1059 | // OGRLayer* poLayer; |
| 1060 | // m_layer.push_back(m_gds->CreateLayer(vectorogr.getLayer(ilayer)->GetName(), vectorOgr.getLayer(ilayer)->GetSpatialRef(), vectorOgr.getLayer(ilayer)->GetGeomType() ,papszOptions)); |
| 1061 | // copyFields(vectorOgr,ilayer); |
| 1062 | // for(unsigned int index=0;index<vectorOgr.getFeatureCount(ilayer);++index){ |
| 1063 | // OGRFeature *poFeature; |
| 1064 | // poFeature=OGRFeature::CreateFeature(getLayer(ilayer)->GetLayerDefn()); |
| 1065 | // poFeature=vectorOgr.cloneFeature(index,ilayer); |
| 1066 | // if(getLayer(ilayer)->CreateFeature(poFeature)!=OGRERR_NONE){ |
| 1067 | // std::string errorString="Error: could not create feature"; |
| 1068 | // throw(errorString); |
| 1069 | // } |
| 1070 | // OGRFeature::DestroyFeature(poFeature); |
| 1071 | // } |
| 1072 | // } |
| 1073 | // } |
| 1074 | |
| 1075 | ///get projection |
| 1076 | std::string VectorOgr::getProjection(size_t ilayer) const{ |
| 1077 | char *wktString; |
| 1078 | OGRLayer* thisLayer=getLayer(ilayer); |
| 1079 | if(thisLayer){ |
| 1080 | // getSpatialRef().exportToWkt(&wktString); |
| 1081 | thisLayer->GetSpatialRef()->exportToWkt(&wktString); |
| 1082 | } |
| 1083 | else{ |
| 1084 | std::string errorString("Error: could not getLayer"); |
| 1085 | throw(errorString); |
| 1086 | } |
| 1087 | std::string projectionString(wktString); |
| 1088 | CPLFree(wktString); |
| 1089 | return(projectionString); |
| 1090 | }; |
| 1091 | |
| 1092 | // void VectorOgr::setProjection(const std::string& projection) |
| 1093 | // { |
| 1094 | // if(projection.size()){ |
| 1095 | // OGRSpatialReference theRef; |
| 1096 | // theRef.SetFromUserInput(projection.c_str()); |
| 1097 | // char *wktString; |
| 1098 | // theRef.exportToWkt(&wktString); |
| 1099 | // if(m_gds&&m_access==GDAL_OF_UPDATE) |
| 1100 | // m_gds->SetProjection(wktString); |
| 1101 | // else{ |
| 1102 | // std::ostringstream errorStream; |
| 1103 | // errorStream << "Error: could not set projection, no m_gds and access is " << m_access << std::endl; |
| 1104 | // throw(errorStream.str()); |
| 1105 | // } |
| 1106 | // } |
| 1107 | // } |
| 1108 | |
| 1109 | ///get extent of all layers |
| 1110 | bool VectorOgr::getExtent(double& ulx, double& uly, double& lrx, double& lry, OGRCoordinateTransformation *poCT) const{ |
| 1111 | bool result=true; |
| 1112 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 1113 | double layer_ulx=0; |
| 1114 | double layer_uly=0; |
| 1115 | double layer_lrx=0; |
| 1116 | double layer_lry=0; |
| 1117 | if(!getExtent(layer_ulx,layer_uly,layer_lrx,layer_lry, ilayer, poCT)){ |
| 1118 | result=false; |
| 1119 | } |
| 1120 | if(!ilayer){ |
| 1121 | ulx=layer_ulx; |
| 1122 | uly=layer_uly; |
| 1123 | lrx=layer_lrx; |
| 1124 | lry=layer_lry; |
| 1125 | } |
| 1126 | else{ |
| 1127 | ulx=std::min(ulx,layer_ulx); |
| 1128 | uly=std::max(uly,layer_uly); |
| 1129 | lrx=std::max(lrx,layer_lrx); |
| 1130 | lry=std::min(lry,layer_lry); |
| 1131 | } |
| 1132 | } |
| 1133 | return result; |
| 1134 | } |
| 1135 | |
| 1136 | bool VectorOgr::getExtent(OGRPolygon *bbPolygon, size_t ilayer, OGRCoordinateTransformation *poCT) const{ |
| 1137 | bool result = true; |
| 1138 | OGRLinearRing bbRing; |
| 1139 | double layer_ulx=0; |
| 1140 | double layer_uly=0; |
| 1141 | double layer_lrx=0; |
| 1142 | double layer_lry=0; |
| 1143 | if(!getExtent(layer_ulx, layer_uly, layer_lrx, layer_lry, ilayer, poCT)){ |
| 1144 | result = false; |
| 1145 | } |
| 1146 | OGRPoint ul; |
| 1147 | OGRPoint ur; |
| 1148 | OGRPoint lr; |
| 1149 | OGRPoint ll; |
| 1150 | ul.setX(layer_ulx); |
| 1151 | ul.setY(layer_uly); |
| 1152 | ur.setX(layer_lrx); |
| 1153 | ur.setY(layer_uly); |
| 1154 | lr.setX(layer_lrx); |
| 1155 | lr.setY(layer_lry); |
| 1156 | ll.setX(layer_ulx); |
| 1157 | ll.setY(layer_lry); |
| 1158 | bbRing.addPoint(&ul); |
| 1159 | bbRing.addPoint(&ur); |
| 1160 | bbRing.addPoint(&lr); |
| 1161 | bbRing.addPoint(&ll); |
| 1162 | bbRing.addPoint(&ul); |
| 1163 | bbPolygon->addRing(&bbRing); |
| 1164 | bbPolygon->closeRings(); |
| 1165 | return result; |
| 1166 | } |
| 1167 | |
| 1168 | ///get extent of the layer |
| 1169 | bool VectorOgr::getExtent(double& ulx, double& uly, double& lrx, double& lry, size_t ilayer, OGRCoordinateTransformation *poCT) const{ |
| 1170 | // try{ |
| 1171 | OGREnvelope oExt; |
| 1172 | OGRLayer* thisLayer=getLayer(ilayer); |
| 1173 | if(thisLayer){ |
| 1174 | int nGeomFieldCount = thisLayer->GetLayerDefn()->GetGeomFieldCount(); |
| 1175 | if(nGeomFieldCount<1){ |
| 1176 | std::ostringstream errorStream; |
| 1177 | errorStream << "Error: layer does not contain geometry" << std::endl; |
| 1178 | throw(errorStream.str()); |
| 1179 | } |
| 1180 | else if(nGeomFieldCount>1){ |
| 1181 | for(int iGeom = 0;iGeom < nGeomFieldCount; ++iGeom){ |
| 1182 | OGRGeomFieldDefn* poGFldDefn = thisLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); |
| 1183 | if (thisLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE){ |
| 1184 | ulx=oExt.MinX; |
| 1185 | uly=oExt.MaxY; |
| 1186 | lrx=oExt.MaxX; |
| 1187 | lry=oExt.MinY; |
| 1188 | } |
| 1189 | else{ |
| 1190 | std::ostringstream errorStream; |
| 1191 | errorStream << "Error: could not get extent from layer with multiple geometry field count" << std::endl; |
| 1192 | throw(errorStream.str()); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | else{ |
| 1197 | if(thisLayer->GetExtent(&oExt, true) == OGRERR_NONE){ |
| 1198 | ulx=oExt.MinX; |
| 1199 | uly=oExt.MaxY; |
| 1200 | lrx=oExt.MaxX; |
| 1201 | lry=oExt.MinY; |
| 1202 | } |
| 1203 | else{ |
| 1204 | std::ostringstream errorStream; |
| 1205 | errorStream << "Error: could not get extent from layer with 1 geometry field count" << std::endl; |
| 1206 | throw(errorStream.str()); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | if(poCT){ |
| 1211 | std::vector<double> xvector(4);//ulx,urx,llx,lrx |
| 1212 | std::vector<double> yvector(4);//uly,ury,lly,lry |
| 1213 | xvector[0]=ulx; |
| 1214 | xvector[1]=lrx; |
| 1215 | xvector[2]=lrx; |
| 1216 | xvector[3]=ulx; |
| 1217 | yvector[0]=uly; |
| 1218 | yvector[1]=uly; |
| 1219 | yvector[2]=lry; |
| 1220 | yvector[3]=lry; |
| 1221 | if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){ |
| 1222 | std::ostringstream errorStream; |
| 1223 | errorStream << "Error: cannot apply OGRCoordinateTransformation in VectorOgr::getExtent" << std::endl; |
| 1224 | throw(errorStream.str()); |
| 1225 | } |
| 1226 | |
| 1227 | ulx=std::min(xvector[0],xvector[3]); |
| 1228 | uly=std::max(yvector[0],yvector[1]); |
| 1229 | lrx=std::max(xvector[1],xvector[2]); |
| 1230 | lry=std::min(yvector[2],yvector[3]); |
| 1231 | } |
| 1232 | return true; |
| 1233 | } |
| 1234 | |
| 1235 | // bool VectorOgr::getExtent(std::vector<double> &bbvector, size_t ilayer, OGRCoordinateTransformation *poCT) const{ |
| 1236 | // bbvector.resize(4); |
| 1237 | // return getExtent(bbvector[0],bbvector[1],bbvector[2],bbvector[3],ilayer,poCT); |
| 1238 | // } |
| 1239 | |
| 1240 | ///set feature to the object |
| 1241 | OGRErr VectorOgr::setFeature(unsigned int index, OGRFeature *poFeature, size_t ilayer){ |
| 1242 | if(index>=0&&index<m_features[ilayer].size()){ |
| 1243 | m_features[ilayer][index]=poFeature; |
| 1244 | } |
| 1245 | else{ |
| 1246 | std::cerr << "Error: setFeature failed" << std::endl; |
| 1247 | std::string errorString="setFeature failed"; |
| 1248 | throw(errorString); |
| 1249 | } |
| 1250 | return(OGRERR_NONE); |
| 1251 | } |
| 1252 | |
| 1253 | ///push feature to the object |
| 1254 | OGRErr VectorOgr::pushFeature(OGRFeature *poFeature, size_t ilayer){ |
| 1255 | if(ilayer<m_features.size()) |
| 1256 | m_features[ilayer].push_back(poFeature); |
| 1257 | else{ |
| 1258 | std::string errorString="Error: pushFeature failed"; |
| 1259 | std::cerr << errorString << std::endl; |
| 1260 | throw(errorString); |
| 1261 | } |
| 1262 | return(OGRERR_NONE); |
| 1263 | } |
| 1264 | |
| 1265 | ///read all features from an OGR dataset, specifying layer, attribute filter and spatial filter optionally |
| 1266 | unsigned int VectorOgr::readFeatures(){ |
| 1267 | unsigned int nfeatures=0; |
| 1268 | destroyAll(); |
| 1269 | m_layer.resize(getGDSLayerCount()); |
| 1270 | m_features.resize(m_layer.size()); |
| 1271 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 1272 | OGRFeature *poFeature; |
| 1273 | //start reading features from the layer |
| 1274 | m_layer[ilayer]->ResetReading(); |
| 1275 | while( (poFeature = m_layer[ilayer]->GetNextFeature()) != NULL ){ |
| 1276 | m_features[ilayer].push_back(poFeature); |
| 1277 | ++nfeatures; |
| 1278 | } |
| 1279 | } |
| 1280 | return(nfeatures); |
| 1281 | } |
| 1282 | |
| 1283 | ///read all features from an OGR dataset, specifying layer, attribute filter and spatial filter optionally |
| 1284 | unsigned int VectorOgr::readFeatures(size_t ilayer){ |
| 1285 | if(ilayer>=m_features.size()){ |
| 1286 | std::cout << "Warning: resize m_features" << std::endl; |
| 1287 | m_features.resize(ilayer+1); |
| 1288 | } |
| 1289 | unsigned int nfeatures=0; |
| 1290 | OGRFeature *poFeature; |
| 1291 | //start reading features from the layer |
| 1292 | m_layer[ilayer]->ResetReading(); |
| 1293 | while( (poFeature = m_layer[ilayer]->GetNextFeature()) != NULL ){ |
| 1294 | m_features[ilayer].push_back(poFeature); |
| 1295 | ++nfeatures; |
| 1296 | } |
| 1297 | return(nfeatures); |
| 1298 | } |
| 1299 | |
| 1300 | ///clone feature. The newly created feature is owned by the caller, and will have it's own reference to the OGRFeatureDefn. |
| 1301 | OGRFeature* VectorOgr::cloneFeature(unsigned int index, size_t ilayer){ |
| 1302 | OGRFeature* poFeature=NULL; |
| 1303 | if(index>=0&&index<m_features[ilayer].size()){ |
| 1304 | poFeature=(m_features[ilayer][index])->Clone(); |
| 1305 | } |
| 1306 | return(poFeature); |
| 1307 | } |
| 1308 | |
| 1309 | ///get feature reference (feature should not be deleted) |
| 1310 | OGRFeature* VectorOgr::getFeatureRef(unsigned int index, size_t ilayer){ |
| 1311 | OGRFeature* poFeature=NULL; |
| 1312 | if(m_features.size()<=ilayer){ |
| 1313 | std::ostringstream errorStream; |
| 1314 | errorStream << "Error: m_features not initialized for layer " << ilayer << std::endl; |
| 1315 | std::cerr << errorStream.str() << std::endl; |
| 1316 | throw(errorStream.str()); |
| 1317 | } |
| 1318 | if(index>=0&&index<m_features[ilayer].size()){ |
| 1319 | poFeature=m_features[ilayer][index]; |
| 1320 | } |
| 1321 | return(poFeature); |
| 1322 | } |
| 1323 | |
| 1324 | OGRErr VectorOgr::getFields(std::vector<OGRFieldDefn*>& fields, size_t ilayer) const{ |
| 1325 | OGRFeatureDefn *poFeatureDefn = getLayer(ilayer)->GetLayerDefn(); |
| 1326 | fields.clear(); |
| 1327 | fields.resize(poFeatureDefn->GetFieldCount()); |
| 1328 | for(int iField=0;iField<poFeatureDefn->GetFieldCount();++iField) |
| 1329 | fields[iField]=poFeatureDefn->GetFieldDefn(iField); |
| 1330 | return(OGRERR_NONE); |
| 1331 | } |
| 1332 | |
| 1333 | void VectorOgr::getFieldNames(std::vector<std::string>& fieldnames, size_t ilayer) const{ |
| 1334 | OGRFeatureDefn *poFeatureDefn = getLayer(ilayer)->GetLayerDefn(); |
| 1335 | fieldnames.clear(); |
| 1336 | fieldnames.resize(poFeatureDefn->GetFieldCount()); |
| 1337 | for(int iField=0;iField<poFeatureDefn->GetFieldCount();++iField) |
| 1338 | fieldnames[iField]=poFeatureDefn->GetFieldDefn(iField)->GetNameRef(); |
| 1339 | } |
| 1340 | |
| 1341 | OGRErr VectorOgr::addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, std::string fieldName, int theId, size_t ilayer){ |
| 1342 | OGRFeature *poFeature=createFeature(ilayer); |
| 1343 | OGRPoint pt; |
| 1344 | if(pointAttributes.size()+1!=poFeature->GetFieldCount()){ |
| 1345 | std::ostringstream ess; |
| 1346 | ess << "Failed to add feature: " << pointAttributes.size() << " != " << poFeature->GetFieldCount() << std::endl; |
| 1347 | throw(ess.str()); |
| 1348 | } |
| 1349 | poFeature->SetField( fieldName.c_str(), theId); |
| 1350 | for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){ |
| 1351 | poFeature->SetField((mit->first).c_str(),mit->second); |
| 1352 | } |
| 1353 | pt.setX(x); |
| 1354 | pt.setY(y); |
| 1355 | poFeature->SetGeometry( &pt ); |
| 1356 | return(pushFeature(poFeature,ilayer)); |
| 1357 | } |
| 1358 | |
| 1359 | OGRErr VectorOgr::addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, size_t ilayer){ |
| 1360 | OGRFeature *poFeature=createFeature(ilayer); |
| 1361 | OGRPoint pt; |
| 1362 | if(pointAttributes.size()!=poFeature->GetFieldCount()){ |
| 1363 | std::ostringstream ess; |
| 1364 | ess << "Failed to add feature: " << pointAttributes.size() << " != " << poFeature->GetFieldCount() << std::endl; |
| 1365 | throw(ess.str()); |
| 1366 | } |
| 1367 | for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){ |
| 1368 | poFeature->SetField((mit->first).c_str(),mit->second); |
| 1369 | } |
| 1370 | pt.setX(x); |
| 1371 | pt.setY(y); |
| 1372 | poFeature->SetGeometry( &pt ); |
| 1373 | return(pushFeature(poFeature,ilayer)); |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | ///serialize vector in vector of unsigned chars |
| 1378 | size_t VectorOgr::serialize(vector<unsigned char> &vbytes){ |
| 1379 | VSIStatBufL statbuf; |
| 1380 | if( VSIStatL(getFileName().c_str(), &statbuf) == 0 ){ |
| 1381 | size_t filesize = static_cast<size_t>(statbuf.st_size); |
| 1382 | if( filesize > 0 ){ |
| 1383 | vbytes.resize(filesize); |
| 1384 | VSILFILE *file = VSIFOpenL(getFileName().c_str(),"rb"); |
| 1385 | if( file ){ |
| 1386 | VSIFReadL(vbytes.data(),filesize,1,file); |
| 1387 | VSIFCloseL(file); |
| 1388 | } |
| 1389 | } |
| 1390 | return(filesize); |
| 1391 | } |
| 1392 | else{ |
| 1393 | std::cerr << "Error: VSIStatL failed" << std::endl; |
| 1394 | return(0); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | void VectorOgr::dumpOgr(app::AppFactory& app){ |
| 1399 | Optionjl<std::string> fname_opt("n", "name", "the field name to dump"); |
| 1400 | Optionjl<string> output_opt("o", "output", "Output ascii file (Default is empty: dump to standard output)"); |
| 1401 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 1402 | |
| 1403 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 1404 | try{ |
| 1405 | doProcess=fname_opt.retrieveOption(app); |
| 1406 | output_opt.retrieveOption(app); |
| 1407 | verbose_opt.retrieveOption(app); |
| 1408 | |
| 1409 | if(!doProcess){ |
| 1410 | cout << endl; |
| 1411 | std::ostringstream helpStream; |
| 1412 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 1413 | throw(helpStream.str());//help was invoked, stop processing |
| 1414 | } |
| 1415 | |
| 1416 | ofstream outputStream; |
| 1417 | if(output_opt.size()) |
| 1418 | outputStream.open(output_opt[0].c_str()); |
| 1419 | |
| 1420 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 1421 | std::vector<OGRFieldDefn*> fields; |
| 1422 | std::vector<size_t> fieldindexes; |
| 1423 | getFields(fields,ilayer); |
| 1424 | for(unsigned int ifield=0;ifield<fields.size();++ifield){ |
| 1425 | if(fname_opt.size()){ |
| 1426 | if(std::find(fname_opt.begin(),fname_opt.end(),fields[ifield]->GetNameRef())!=fname_opt.end()) |
| 1427 | fieldindexes.push_back(ifield); |
| 1428 | } |
| 1429 | else |
| 1430 | fieldindexes.push_back(ifield); |
| 1431 | } |
| 1432 | if(verbose_opt[0]) |
| 1433 | std::cout << "ilayer is: " << ilayer << std::endl; |
| 1434 | for(size_t ifeature = 0; ifeature < getFeatureCount(ilayer); ++ifeature) { |
| 1435 | OGRFeature *thisFeature=getFeatureRef(ifeature,ilayer); |
| 1436 | if(!thisFeature){ |
| 1437 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1438 | continue; |
| 1439 | } |
| 1440 | for(std::vector<size_t>::const_iterator fit=fieldindexes.begin();fit!=fieldindexes.end();++fit){ |
| 1441 | if(output_opt.empty()) |
| 1442 | std::cout << thisFeature->GetFieldAsString(*fit) << " "; |
| 1443 | else |
| 1444 | outputStream << thisFeature->GetFieldAsString(*fit) << " "; |
| 1445 | } |
| 1446 | if(output_opt.empty()) |
| 1447 | std::cout << std::endl; |
| 1448 | else |
| 1449 | outputStream << std::endl; |
| 1450 | } |
| 1451 | } |
| 1452 | if(!output_opt.empty()) |
| 1453 | outputStream.close(); |
| 1454 | } |
| 1455 | catch(std::string errorString){ |
| 1456 | std::cerr << errorString << std::endl; |
| 1457 | throw; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | shared_ptr<VectorOgr> VectorOgr::join(VectorOgr &ogrReader, app::AppFactory& app){ |
| 1462 | std::shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 1463 | join(ogrReader, *ogrWriter, app); |
| 1464 | return(ogrWriter); |
| 1465 | } |
| 1466 | |
| 1467 | OGRErr VectorOgr::join(VectorOgr &ogrReader, VectorOgr &ogrWriter, app::AppFactory& app){ |
| 1468 | Optionjl<string> output_opt("o", "output", "Filename of joined vector dataset"); |
| 1469 | Optionjl<string> ogrformat_opt("f", "oformat", "Output ogr format for joined vector dataset","SQLite"); |
| 1470 | Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 1471 | Optionjl<unsigned int> access_opt("access", "access", "Access (0: GDAL_OF_READ_ONLY, 1: GDAL_OF_UPDATE)",1); |
| 1472 | Optionjl<std::string> key_opt("key", "key", "Key(s) used to join", "fid"); |
| 1473 | Optionjl<std::string> method_opt("method", "method", "Join method (INNER, OUTER_LEFT, OUTER_RIGHT, OUTER_FULL)", "INNER"); |
| 1474 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 1475 | |
| 1476 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 1477 | try{ |
| 1478 | OGRErr result=OGRERR_NONE; |
| 1479 | doProcess=output_opt.retrieveOption(app); |
| 1480 | ogrformat_opt.retrieveOption(app); |
| 1481 | access_opt.retrieveOption(app); |
| 1482 | option_opt.retrieveOption(app); |
| 1483 | key_opt.retrieveOption(app); |
| 1484 | method_opt.retrieveOption(app); |
| 1485 | verbose_opt.retrieveOption(app); |
| 1486 | |
| 1487 | if(!doProcess||output_opt.empty()){ |
| 1488 | cout << endl; |
| 1489 | std::ostringstream helpStream; |
| 1490 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 1491 | throw(helpStream.str());//help was invoked, stop processing |
| 1492 | } |
| 1493 | char **papszOptions=NULL; |
| 1494 | for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 1495 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 1496 | bool initWriter=true; |
| 1497 | |
| 1498 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0],access_opt[0])!=OGRERR_NONE) |
| 1499 | if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE) |
| 1500 | initWriter=false; |
| 1501 | if(isEmpty() || ogrReader.isEmpty()){ |
| 1502 | ostringstream errorStream; |
| 1503 | errorStream << "Error: features are empty"; |
| 1504 | throw(errorStream.str()); |
| 1505 | } |
| 1506 | if(verbose_opt[0]){ |
| 1507 | std::cout << "join this vector containing " << getFeatureCount() << " features with vector containing " << ogrReader.getFeatureCount() << std::endl; |
| 1508 | |
| 1509 | if(initWriter) |
| 1510 | std::cout << "initWriter is true" << std::endl; |
| 1511 | else |
| 1512 | std::cout << "initWriter is false" << std::endl; |
| 1513 | } |
| 1514 | // if(output_opt.size()){ |
| 1515 | // if(verbose_opt[0]) |
| 1516 | // std::cout << "opening ogrWriter " << output_opt[0] << std::endl; |
| 1517 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE){ |
| 1518 | // if(verbose_opt[0]) |
| 1519 | // std::cout << "Warning: could not open ogrWriter " << output_opt[0] << std::endl; |
| 1520 | // } |
| 1521 | // } |
| 1522 | // if(getLayerCount()!=ogrReader.getLayerCount()){ |
| 1523 | // ostringstream errorStream; |
| 1524 | // errorStream << "Warning: number of layers in this layer and ogrReader are not equal: " << getLayerCount() << " != " << ogrReader.getLayerCount() << std::endl; |
| 1525 | // throw(errorStream.str()); |
| 1526 | // } |
| 1527 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 1528 | if(verbose_opt[0]) |
| 1529 | std::cout << "ilayer is: " << ilayer << std::endl; |
| 1530 | // if(output_opt.size()){ |
| 1531 | if(initWriter){ |
| 1532 | if(verbose_opt[0]){ |
| 1533 | std::cout << "push layer " << getLayerName(ilayer) << std::endl; |
| 1534 | std::cout << "projection: " << getProjection(ilayer) << std::endl; |
| 1535 | std::cout << "GeometryType: " << getGeometryType(ilayer) << std::endl; |
| 1536 | } |
| 1537 | if(ogrWriter.pushLayer(getLayerName(ilayer),getProjection(ilayer),getGeometryType(ilayer),papszOptions)!=OGRERR_NONE){ |
| 1538 | ostringstream fs; |
| 1539 | fs << "push layer to ogrWriter with polygons failed "; |
| 1540 | fs << "layer name: "<< getLayerName(ilayer) << std::endl; |
| 1541 | throw(fs.str()); |
| 1542 | } |
| 1543 | if(verbose_opt[0]) |
| 1544 | std::cout << "destroy features" << getLayerName(ilayer) << std::endl; |
| 1545 | ogrWriter.destroyFeatures(ilayer); |
| 1546 | // ogrWriter.pushLayer(getLayer()->GetName(),getProjection(),getGeometryType(),NULL); |
| 1547 | std::vector<std::string> thisfields; |
| 1548 | this->getFieldNames(thisfields,ilayer); |
| 1549 | if(verbose_opt[0]){ |
| 1550 | cout << "copy thisfields: "; |
| 1551 | for(unsigned int iField=0;iField<thisfields.size();++iField) |
| 1552 | cout << " " << thisfields[iField]; |
| 1553 | cout << std::endl; |
| 1554 | } |
| 1555 | if(verbose_opt[0]){ |
| 1556 | cout << "Fields in ogrWriter before copy are: "; |
| 1557 | std::vector<OGRFieldDefn*> fields; |
| 1558 | ogrWriter.getFields(fields,ilayer); |
| 1559 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1560 | cout << " " << fields[iField]->GetNameRef(); |
| 1561 | cout << std::endl; |
| 1562 | } |
| 1563 | if(verbose_opt[0]) |
| 1564 | std::cout << "copyFields from this" << std::endl; |
| 1565 | |
| 1566 | ogrWriter.copyFields(*this,thisfields,ilayer,ilayer); |
| 1567 | if(verbose_opt[0]){ |
| 1568 | cout << "Fields in ogrWriter after copy from this are: "; |
| 1569 | std::vector<OGRFieldDefn*> fields; |
| 1570 | ogrWriter.getFields(fields,ilayer); |
| 1571 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1572 | cout << " " << fields[iField]->GetNameRef(); |
| 1573 | cout << std::endl; |
| 1574 | } |
| 1575 | std::vector<std::string> thatfields; |
| 1576 | ogrReader.getFieldNames(thatfields,ilayer); |
| 1577 | for(auto itr=thisfields.begin();itr!=thisfields.end();++itr){ |
| 1578 | auto thatitr = std::find(thatfields.begin(), thatfields.end(), *itr); |
| 1579 | if (thatitr != thatfields.end()) thatfields.erase(thatitr); |
| 1580 | } |
| 1581 | if(verbose_opt[0]){ |
| 1582 | std::cout << "copy new fields from that" << std::endl; |
| 1583 | for(unsigned int iField=0;iField<thatfields.size();++iField) |
| 1584 | std::cout << " " << thatfields[iField]; |
| 1585 | std::cout << std::endl; |
| 1586 | } |
| 1587 | if(thatfields.size()){ |
| 1588 | if(verbose_opt[0]){ |
| 1589 | std::cout << "copy new fields from that" << std::endl; |
| 1590 | for(unsigned int iField=0;iField<thatfields.size();++iField) |
| 1591 | std::cout << " " << thatfields[iField]; |
| 1592 | std::cout << std::endl; |
| 1593 | } |
| 1594 | ogrWriter.copyFields(ogrReader,thatfields,ilayer,ilayer); |
| 1595 | } |
| 1596 | } |
| 1597 | if(verbose_opt[0]){ |
| 1598 | cout << "Fields are: "; |
| 1599 | std::vector<OGRFieldDefn*> fields; |
| 1600 | ogrWriter.getFields(fields,ilayer); |
| 1601 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1602 | cout << " " << fields[iField]->GetNameRef(); |
| 1603 | cout << std::endl; |
| 1604 | } |
| 1605 | //make sure to use resize and setFeature instead of pushFeature when in processing in parallel!!! |
| 1606 | std::string theKey1=key_opt[0]; |
| 1607 | std::string theKey2=(key_opt.size()>1)? key_opt[1] : key_opt[0]; |
| 1608 | switch(string2method(method_opt[0])){ |
| 1609 | case(OUTER_FULL):{ |
| 1610 | if(verbose_opt[0]) |
| 1611 | std::cout << "in OUTER_FULL method" << std::endl; |
| 1612 | std::unordered_multimap<std::string, size_t> hashmap; |
| 1613 | // hash |
| 1614 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(ilayer); ++ifeature) { |
| 1615 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 1616 | if(!thatFeature){ |
| 1617 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1618 | continue; |
| 1619 | } |
| 1620 | int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 1621 | if(iField<0){ |
| 1622 | std::ostringstream errorStream; |
| 1623 | errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 1624 | errorStream << "fields are: " << std::endl; |
| 1625 | std::vector<OGRFieldDefn*> fields; |
| 1626 | ogrReader.getFields(fields,ilayer); |
| 1627 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1628 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1629 | errorStream << std::endl; |
| 1630 | throw(errorStream.str()); |
| 1631 | } |
| 1632 | hashmap.insert(std::make_pair(thatFeature->GetFieldAsString(iField), ifeature)); |
| 1633 | if(verbose_opt[0]>1) |
| 1634 | std::cout << "hash ifeature: " << ifeature << ": " << thatFeature->GetFieldAsString(iField) << "," << ifeature << std::endl; |
| 1635 | } |
| 1636 | // map |
| 1637 | if(verbose_opt[0]>1) |
| 1638 | std::cout << "map" << std::endl; |
| 1639 | std::vector<size_t> thatFeaturesAdded; |
| 1640 | for(size_t ifeature = 0; ifeature < getFeatureCount(ilayer); ++ifeature) { |
| 1641 | OGRFeature *thisFeature=getFeatureRef(ifeature,ilayer); |
| 1642 | if(!thisFeature){ |
| 1643 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1644 | continue; |
| 1645 | } |
| 1646 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 1647 | if(verbose_opt[0]>1) |
| 1648 | std::cout << "set from this" << std::endl; |
| 1649 | writeFeature->SetFrom(thisFeature); |
| 1650 | if(verbose_opt[0]>1) |
| 1651 | std::cout << "map ifeature: " << ifeature << std::endl; |
| 1652 | int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 1653 | if(iField<0){ |
| 1654 | std::ostringstream errorStream; |
| 1655 | errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 1656 | errorStream << "fields are: " << std::endl; |
| 1657 | std::vector<OGRFieldDefn*> fields; |
| 1658 | getFields(fields,ilayer); |
| 1659 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1660 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1661 | errorStream << std::endl; |
| 1662 | throw(errorStream.str()); |
| 1663 | } |
| 1664 | auto range = hashmap.equal_range(thisFeature->GetFieldAsString(iField)); |
| 1665 | bool thatFeatureAdded=false; |
| 1666 | for(auto it = range.first; it != range.second; ++it) { |
| 1667 | if(verbose_opt[0]>1) |
| 1668 | std::cout << "set from this " << it->first << " " << it->second << std::endl; |
| 1669 | writeFeature->SetFrom(ogrReader.getFeatureRef(it->second,ilayer)); |
| 1670 | thatFeaturesAdded.push_back(it->second); |
| 1671 | } |
| 1672 | if(verbose_opt[0]>1) |
| 1673 | std::cout << "pushFeature" << std::endl; |
| 1674 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 1675 | } |
| 1676 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(); ++ifeature) { |
| 1677 | if(find(thatFeaturesAdded.begin(),thatFeaturesAdded.end(),ifeature)!=thatFeaturesAdded.end()){ |
| 1678 | if(verbose_opt[0]>1) |
| 1679 | std::cout << "skip feature " << ifeature << "already added" << std::endl; |
| 1680 | continue; |
| 1681 | } |
| 1682 | else if(verbose_opt[0]>1) |
| 1683 | std::cout << "adding feature " << ifeature << std::endl; |
| 1684 | |
| 1685 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 1686 | if(!thatFeature){ |
| 1687 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1688 | continue; |
| 1689 | } |
| 1690 | int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 1691 | if(iField<0){ |
| 1692 | std::ostringstream errorStream; |
| 1693 | errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 1694 | errorStream << "fields are: " << std::endl; |
| 1695 | std::vector<OGRFieldDefn*> fields; |
| 1696 | ogrReader.getFields(fields,ilayer); |
| 1697 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1698 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1699 | errorStream << std::endl; |
| 1700 | throw(errorStream.str()); |
| 1701 | } |
| 1702 | // auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 1703 | // bool skip=false;//check if we already added this feature |
| 1704 | // for(auto it = range.first; it != range.second; ++it) { |
| 1705 | // if(verbose_opt[0]>1) |
| 1706 | // std::cout << "feature " << ifeature << " with key (" << it->first << ", " << it->second << ") was already added" << std::endl; |
| 1707 | // skip=true;//feature was already added |
| 1708 | // break; |
| 1709 | // } |
| 1710 | // if(skip) |
| 1711 | // continue; |
| 1712 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 1713 | if(verbose_opt[0]>1) |
| 1714 | std::cout << "set from ogrReader" << std::endl; |
| 1715 | writeFeature->SetFrom(thatFeature); |
| 1716 | if(verbose_opt[0]>1) |
| 1717 | std::cout << "map ifeature: " << ifeature << std::endl; |
| 1718 | if(verbose_opt[0]>1) |
| 1719 | std::cout << "pushFeature" << std::endl; |
| 1720 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 1721 | } |
| 1722 | break; |
| 1723 | } |
| 1724 | case(OUTER_LEFT):{ |
| 1725 | if(verbose_opt[0]) |
| 1726 | std::cout << "in OUTER_LEFT method" << std::endl; |
| 1727 | std::unordered_multimap<std::string, size_t> hashmap; |
| 1728 | // hash |
| 1729 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(ilayer); ++ifeature) { |
| 1730 | if(verbose_opt[0]>1) |
| 1731 | std::cout << "hash ifeature: " << ifeature << std::endl; |
| 1732 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 1733 | if(!thatFeature){ |
| 1734 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1735 | continue; |
| 1736 | } |
| 1737 | int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 1738 | if(iField<0){ |
| 1739 | std::ostringstream errorStream; |
| 1740 | errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 1741 | errorStream << "fields are: " << std::endl; |
| 1742 | std::vector<OGRFieldDefn*> fields; |
| 1743 | ogrReader.getFields(fields,ilayer); |
| 1744 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1745 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1746 | errorStream << std::endl; |
| 1747 | throw(errorStream.str()); |
| 1748 | } |
| 1749 | hashmap.insert(std::make_pair(thatFeature->GetFieldAsString(iField), ifeature)); |
| 1750 | } |
| 1751 | // map |
| 1752 | if(verbose_opt[0]>1) |
| 1753 | std::cout << "map" << std::endl; |
| 1754 | for(size_t ifeature = 0; ifeature < getFeatureCount(ilayer); ++ifeature) { |
| 1755 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 1756 | OGRFeature *thisFeature=getFeatureRef(ifeature,ilayer); |
| 1757 | if(!thisFeature){ |
| 1758 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1759 | continue; |
| 1760 | } |
| 1761 | if(verbose_opt[0]>1) |
| 1762 | std::cout << "set from this" << std::endl; |
| 1763 | writeFeature->SetFrom(thisFeature); |
| 1764 | if(verbose_opt[0]>1) |
| 1765 | std::cout << "map ifeature: " << ifeature << std::endl; |
| 1766 | int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 1767 | if(iField<0){ |
| 1768 | std::ostringstream errorStream; |
| 1769 | errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 1770 | errorStream << "fields are: " << std::endl; |
| 1771 | std::vector<OGRFieldDefn*> fields; |
| 1772 | getFields(fields,ilayer); |
| 1773 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1774 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1775 | errorStream << std::endl; |
| 1776 | throw(errorStream.str()); |
| 1777 | } |
| 1778 | auto range = hashmap.equal_range(thisFeature->GetFieldAsString(iField)); |
| 1779 | for(auto it = range.first; it != range.second; ++it) { |
| 1780 | if(verbose_opt[0]>1) |
| 1781 | std::cout << "set from this" << std::endl; |
| 1782 | writeFeature->SetFrom(ogrReader.getFeatureRef(it->second,ilayer)); |
| 1783 | } |
| 1784 | if(verbose_opt[0]>1) |
| 1785 | std::cout << "pushFeature" << std::endl; |
| 1786 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 1787 | } |
| 1788 | break; |
| 1789 | } |
| 1790 | case(OUTER_RIGHT):{ |
| 1791 | if(verbose_opt[0]) |
| 1792 | std::cout << "in OUTER_RIGHT method" << std::endl; |
| 1793 | std::unordered_multimap<std::string, size_t> hashmap; |
| 1794 | // hash |
| 1795 | for(size_t ifeature = 0; ifeature < getFeatureCount(ilayer); ++ifeature) { |
| 1796 | if(verbose_opt[0]>1) |
| 1797 | std::cout << "hash ifeature: " << ifeature << std::endl; |
| 1798 | OGRFeature *thisFeature=getFeatureRef(ifeature,ilayer); |
| 1799 | if(!thisFeature){ |
| 1800 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1801 | continue; |
| 1802 | } |
| 1803 | int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 1804 | if(iField<0){ |
| 1805 | std::ostringstream errorStream; |
| 1806 | errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 1807 | errorStream << "fields are: " << std::endl; |
| 1808 | std::vector<OGRFieldDefn*> fields; |
| 1809 | this->getFields(fields,ilayer); |
| 1810 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1811 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1812 | errorStream << std::endl; |
| 1813 | throw(errorStream.str()); |
| 1814 | } |
| 1815 | hashmap.insert(std::make_pair(thisFeature->GetFieldAsString(iField), ifeature)); |
| 1816 | } |
| 1817 | // map |
| 1818 | if(verbose_opt[0]>1) |
| 1819 | std::cout << "map" << std::endl; |
| 1820 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(ilayer); ++ifeature) { |
| 1821 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 1822 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 1823 | if(!thatFeature){ |
| 1824 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1825 | continue; |
| 1826 | } |
| 1827 | if(verbose_opt[0]>1) |
| 1828 | std::cout << "set from ogrReader" << std::endl; |
| 1829 | writeFeature->SetFrom(thatFeature); |
| 1830 | if(verbose_opt[0]>1) |
| 1831 | std::cout << "map ifeature: " << ifeature << std::endl; |
| 1832 | int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 1833 | if(iField<0){ |
| 1834 | std::ostringstream errorStream; |
| 1835 | errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 1836 | errorStream << "fields are: " << std::endl; |
| 1837 | std::vector<OGRFieldDefn*> fields; |
| 1838 | ogrReader.getFields(fields,ilayer); |
| 1839 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1840 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1841 | errorStream << std::endl; |
| 1842 | throw(errorStream.str()); |
| 1843 | } |
| 1844 | auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 1845 | for(auto it = range.first; it != range.second; ++it) { |
| 1846 | if(verbose_opt[0]>1) |
| 1847 | std::cout << "set from this" << std::endl; |
| 1848 | writeFeature->SetFrom(this->getFeatureRef(it->second,ilayer)); |
| 1849 | } |
| 1850 | if(verbose_opt[0]>1) |
| 1851 | std::cout << "pushFeature" << std::endl; |
| 1852 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 1853 | } |
| 1854 | break; |
| 1855 | } |
| 1856 | case(INNER):{ |
| 1857 | if(verbose_opt[0]) |
| 1858 | std::cout << "in INNER method" << std::endl; |
| 1859 | std::unordered_multimap<std::string, size_t> hashmap; |
| 1860 | // hash |
| 1861 | for(size_t ifeature = 0; ifeature < getFeatureCount(ilayer); ++ifeature) { |
| 1862 | if(verbose_opt[0]>1) |
| 1863 | std::cout << "hash ifeature: " << ifeature << std::endl; |
| 1864 | OGRFeature *thisFeature=getFeatureRef(ifeature,ilayer); |
| 1865 | if(!thisFeature){ |
| 1866 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1867 | continue; |
| 1868 | } |
| 1869 | int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 1870 | if(verbose_opt[0]>1){ |
| 1871 | std::cout << "theKey1: " << theKey1 << std::endl; |
| 1872 | std::cout << "theKey2: " << theKey2 << std::endl; |
| 1873 | std::cout << "key for this feature: " << thisFeature->GetFieldAsString(iField) << std::endl; |
| 1874 | } |
| 1875 | if(iField<0){ |
| 1876 | std::ostringstream errorStream; |
| 1877 | errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 1878 | errorStream << "fields are: " << std::endl; |
| 1879 | std::vector<OGRFieldDefn*> fields; |
| 1880 | this->getFields(fields,ilayer); |
| 1881 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1882 | errorStream << " " << fields[iField]->GetNameRef(); |
| 1883 | errorStream << std::endl; |
| 1884 | throw(errorStream.str()); |
| 1885 | } |
| 1886 | else if(verbose_opt[0]>1){ |
| 1887 | std::vector<OGRFieldDefn*> fields; |
| 1888 | this->getFields(fields,ilayer); |
| 1889 | std::cout << "fields are: " << std::endl; |
| 1890 | for(unsigned int iField=0;iField<fields.size();++iField) |
| 1891 | std::cout << " " << fields[iField]->GetNameRef(); |
| 1892 | std::cout << std::endl; |
| 1893 | } |
| 1894 | hashmap.insert(std::make_pair(thisFeature->GetFieldAsString(iField), ifeature)); |
| 1895 | } |
| 1896 | // map |
| 1897 | if(verbose_opt[0]>1) |
| 1898 | std::cout << "map" << std::endl; |
| 1899 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(ilayer); ++ifeature) { |
| 1900 | if(verbose_opt[0]>1) |
| 1901 | std::cout << "map ifeature: " << ifeature << std::endl; |
| 1902 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 1903 | if(!thatFeature){ |
| 1904 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 1905 | continue; |
| 1906 | } |
| 1907 | int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 1908 | if(iField<0){ |
| 1909 | std::ostringstream errorStream; |
| 1910 | errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 1911 | throw(errorStream.str()); |
| 1912 | } |
| 1913 | if(verbose_opt[0]>1) |
| 1914 | std::cout << "calculate range" << std::endl; |
| 1915 | auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 1916 | for(auto it = range.first; it != range.second; ++it) { |
| 1917 | OGRLayer *writeLayer=ogrWriter.getLayer(ilayer); |
| 1918 | if(!writeLayer){ |
| 1919 | std::ostringstream errorStream; |
| 1920 | errorStream << "Error: could not get layer " << ilayer << std::endl; |
| 1921 | throw(errorStream.str()); |
| 1922 | } |
| 1923 | // OGRFeatureDefn *poFDefn = writeLayer->GetLayerDefn(); |
| 1924 | // if(!poFDefn){ |
| 1925 | // std::ostringstream errorStream; |
| 1926 | // errorStream << "Error: could not get layer definition" << std::endl; |
| 1927 | // throw(errorStream.str()); |
| 1928 | // } |
| 1929 | OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 1930 | OGRFeature *thisFeature=getFeatureRef(it->second,ilayer); |
| 1931 | if(thisFeature){ |
| 1932 | if(verbose_opt[0]>1) |
| 1933 | std::cout << "setFrom thisFeature" << std::endl; |
| 1934 | writeFeature->SetFrom(thisFeature); |
| 1935 | } |
| 1936 | if(thatFeature){ |
| 1937 | if(verbose_opt[0]>1) |
| 1938 | std::cout << "setFrom thatFeature" << std::endl; |
| 1939 | writeFeature->SetFrom(thatFeature); |
| 1940 | } |
| 1941 | if(verbose_opt[0]>1) |
| 1942 | std::cout << "pushing feature" << std::endl; |
| 1943 | ogrWriter.pushFeature(writeFeature,ilayer); |
| 1944 | if(verbose_opt[0]>1) |
| 1945 | std::cout << "pushed feature" << std::endl; |
| 1946 | } |
| 1947 | } |
| 1948 | break; |
| 1949 | } |
| 1950 | default: |
| 1951 | std::ostringstream errorStream; |
| 1952 | errorStream << "Error: join method " << method_opt[0] << " not implemented " << std::endl; |
| 1953 | throw(errorStream.str()); |
| 1954 | break; |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | catch(std::string errorString){ |
| 1959 | std::cerr << errorString << std::endl; |
| 1960 | throw; |
| 1961 | } |
| 1962 | return(OGRERR_NONE); |
| 1963 | } |
| 1964 | |
| 1965 | shared_ptr<VectorOgr> VectorOgr::merge(VectorOgr &ogrReader, app::AppFactory& app){ |
| 1966 | std::shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 1967 | merge(ogrReader, *ogrWriter, app); |
| 1968 | return(ogrWriter); |
| 1969 | } |
| 1970 | |
| 1971 | void VectorOgr::merge(VectorOgr &ogrReader, VectorOgr &ogrWriter, app::AppFactory& app){ |
| 1972 | Optionjl<string> output_opt("o", "output", "Filename of joined vector dataset"); |
| 1973 | Optionjl<string> ogrformat_opt("f", "oformat", "Output ogr format for joined vector dataset","SQLite"); |
| 1974 | Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 1975 | Optionjl<bool> single_opt("single", "single", "If specified, all input vector layers will be merged into a single one.",false); |
| 1976 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 1977 | |
| 1978 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 1979 | try{ |
| 1980 | doProcess=output_opt.retrieveOption(app); |
| 1981 | ogrformat_opt.retrieveOption(app); |
| 1982 | option_opt.retrieveOption(app); |
| 1983 | single_opt.retrieveOption(app); |
| 1984 | verbose_opt.retrieveOption(app); |
| 1985 | |
| 1986 | if(!doProcess||output_opt.empty()){ |
| 1987 | cout << endl; |
| 1988 | std::ostringstream helpStream; |
| 1989 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 1990 | throw(helpStream.str());//help was invoked, stop processing |
| 1991 | } |
| 1992 | char **papszOptions=NULL; |
| 1993 | for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 1994 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 1995 | bool initWriter=true; |
| 1996 | |
| 1997 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0],access_opt[0])!=OGRERR_NONE) |
| 1998 | if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE) |
| 1999 | initWriter=false; |
| 2000 | if(isEmpty() || ogrReader.isEmpty()){ |
| 2001 | ostringstream errorStream; |
| 2002 | errorStream << "Error: features are empty"; |
| 2003 | throw(errorStream.str()); |
| 2004 | } |
| 2005 | if(verbose_opt[0]){ |
| 2006 | if(initWriter) |
| 2007 | std::cout << "initWriter is true" << std::endl; |
| 2008 | else |
| 2009 | std::cout << "initWriter is false" << std::endl; |
| 2010 | } |
| 2011 | size_t nlayer1=getLayerCount(); |
| 2012 | if(verbose_opt[0]) |
| 2013 | std::cout << "copy from this" << std::endl; |
| 2014 | |
| 2015 | ogrWriter.copy(*this,app); |
| 2016 | if(verbose_opt[0]) |
| 2017 | std::cout << "copy from other" << std::endl; |
| 2018 | for(size_t ilayer=0;ilayer<ogrReader.getLayerCount();++ilayer){ |
| 2019 | std::ostringstream layerstream; |
| 2020 | layerstream << ogrReader.getLayerName(ilayer) << getLayerCount(); |
| 2021 | if(verbose_opt[0]) |
| 2022 | std::cout << "layer: " << layerstream.str() << std::endl; |
| 2023 | |
| 2024 | OGRSpatialReference *thatSpatialRef=ogrReader.getLayer(ilayer)->GetSpatialRef()->Clone(); |
| 2025 | #if GDAL_VERSION_MAJOR > 2 |
| 2026 | thatSpatialRef->SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 2027 | #endif |
| 2028 | OGRwkbGeometryType thatGeometryType=ogrReader.getGeometryType(ilayer); |
| 2029 | size_t currentSize=0; |
| 2030 | if(!single_opt[0]){ |
| 2031 | if(verbose_opt[0]) |
| 2032 | std::cout << "push layer " << layerstream.str() << std::endl; |
| 2033 | // char **papszOptions=NULL; |
| 2034 | if(ogrWriter.pushLayer(layerstream.str(),thatSpatialRef,thatGeometryType,papszOptions)!=OGRERR_NONE){ |
| 2035 | std::string errorString="Open failed"; |
| 2036 | throw(errorString); |
| 2037 | } |
| 2038 | } |
| 2039 | else |
| 2040 | currentSize=ogrWriter.getFeatureCount(); |
| 2041 | |
| 2042 | if(verbose_opt[0]) |
| 2043 | std::cout << "resize to " << currentSize+ogrReader.getFeatureCount(ilayer) << std::endl; |
| 2044 | ogrWriter.copyFields(ogrReader,std::vector<std::string>(),ilayer,nlayer1+ilayer); |
| 2045 | ogrWriter.resize(currentSize+ogrReader.getFeatureCount(ilayer),nlayer1+ilayer); |
| 2046 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 2047 | #pragma omp parallel for |
| 2048 | #else |
| 2049 | #endif |
| 2050 | for(size_t ifeature = 0; ifeature < ogrReader.getFeatureCount(ilayer); ++ifeature) { |
| 2051 | if(verbose_opt[0]) |
| 2052 | std::cout << "ifeature: " << ifeature << std::endl; |
| 2053 | try{ |
| 2054 | OGRFeature *thatFeature=ogrReader.getFeatureRef(ifeature,ilayer); |
| 2055 | if(!thatFeature){ |
| 2056 | if(verbose_opt[0]) |
| 2057 | std::cout << "skip empty feature" << std::endl; |
| 2058 | continue; |
| 2059 | } |
| 2060 | if(verbose_opt[0]) |
| 2061 | std::cout << "create new feature" << std::endl; |
| 2062 | OGRFeature *writeFeature=ogrWriter.createFeature(nlayer1+ilayer); |
| 2063 | if(verbose_opt[0]) |
| 2064 | std::cout << "setFrom" << std::endl; |
| 2065 | writeFeature->SetFrom(thatFeature); |
| 2066 | if(verbose_opt[0]) |
| 2067 | std::cout << "setFeature" << std::endl; |
| 2068 | ogrWriter.setFeature(currentSize+ifeature,writeFeature,nlayer1+ilayer); |
| 2069 | } |
| 2070 | catch(std::string errorString){ |
| 2071 | std::cerr << errorString << std::endl; |
| 2072 | continue; |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | catch(std::string errorString){ |
| 2078 | std::cerr << errorString << std::endl; |
| 2079 | throw; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | // OGRErr VectorOgr::sortByLabel(std::map<std::string,Vector2d<float> > &mapPixels, const std::string& label, std::vector<std::string>& bandNames){ |
| 2084 | // //[classNr][pixelNr][bandNr] |
| 2085 | // try{ |
| 2086 | // mapPixels.clear(); |
| 2087 | // int nsample=0; |
| 2088 | // int nband=0; |
| 2089 | // std::vector<OGRFieldDefn*> fields; |
| 2090 | // getFields(fields); |
| 2091 | // int fieldLabel=-1; |
| 2092 | // int fieldId=-1; |
| 2093 | // for(int iField=0;iField<fields.size();++iField){ |
| 2094 | // std::string fieldname=fields[iField]->GetNameRef(); |
| 2095 | // if(fieldname==label){ |
| 2096 | // fieldLabel=iField; |
| 2097 | // } |
| 2098 | // if(fieldname=="fid"){ |
| 2099 | // fieldId=iField; |
| 2100 | // } |
| 2101 | // if(fieldLabel>0&&fieldId>0) |
| 2102 | // break; |
| 2103 | // } |
| 2104 | // if(fieldLabel<0||fieldLabel>=fields.size()){ |
| 2105 | // std::string errorString="Error: label not found"; |
| 2106 | // throw(errorString); |
| 2107 | // } |
| 2108 | |
| 2109 | // for(unsigned int index=0;index<getFeatureCount();++index){ |
| 2110 | // std::vector<float> theFeature; |
| 2111 | // std::string theClass; |
| 2112 | // for(int iField=0;iField<fields.size();++iField){ |
| 2113 | // if(iField==fieldLabel){ |
| 2114 | // theClass=m_features[index]->GetFieldAsString(iField); |
| 2115 | // } |
| 2116 | // else if(iField!=fieldId){ |
| 2117 | // double theValue=m_features[index]->GetFieldAsDouble(iField); |
| 2118 | // theFeature.push_back(theValue); |
| 2119 | // } |
| 2120 | // } |
| 2121 | // mapPixels[theClass].push_back(theFeature); |
| 2122 | // } |
| 2123 | // return(OGRERR_NONE); |
| 2124 | // } |
| 2125 | // catch(std::string errorString){ |
| 2126 | // std::cerr << errorString << std::endl; |
| 2127 | // return(OGRERR_FAILURE); |
| 2128 | // } |
| 2129 | // } |
| 2130 | |
| 2131 | OGRErr VectorOgr::sortByLabel(std::map<std::string,Vector2d<float> > &mapPixels, const std::string& label, const std::vector<std::string>& bandNames){ |
| 2132 | //[classNr][pixelNr][bandNr] |
| 2133 | ///Warning: bands got ordered as they have been stored in this vector and not according to order bandNames have been provided in argument |
| 2134 | try{ |
| 2135 | bool labelFound=false; |
| 2136 | mapPixels.clear(); |
| 2137 | for(size_t ilayer=0;ilayer<getLayerCount();++ilayer){ |
| 2138 | for(unsigned int index=0;index<getFeatureCount(ilayer);++index){ |
| 2139 | std::vector<float> theFeature; |
| 2140 | std::string theClass; |
| 2141 | for(int iField=0;iField<m_features[ilayer][index]->GetFieldCount();++iField){ |
| 2142 | std::string fieldname=m_features[ilayer][index]->GetFieldDefnRef(iField)->GetNameRef(); |
| 2143 | if(fieldname==label){ |
| 2144 | theClass=m_features[ilayer][index]->GetFieldAsString(iField); |
| 2145 | labelFound=true; |
| 2146 | } |
| 2147 | else if(bandNames.size()){ |
| 2148 | if(find(bandNames.begin(),bandNames.end(),fieldname)!=bandNames.end()){ |
| 2149 | double theValue=m_features[ilayer][index]->GetFieldAsDouble(iField); |
| 2150 | theFeature.push_back(theValue); |
| 2151 | } |
| 2152 | } |
| 2153 | else if(fieldname!="fid"){ |
| 2154 | double theValue=m_features[ilayer][index]->GetFieldAsDouble(iField); |
| 2155 | theFeature.push_back(theValue); |
| 2156 | } |
| 2157 | } |
| 2158 | if(!labelFound){ |
| 2159 | std::ostringstream errorStream; |
| 2160 | errorStream << "Error: label " << label << "not found" << std::endl; |
| 2161 | throw(errorStream.str()); |
| 2162 | } |
| 2163 | mapPixels[theClass].push_back(theFeature); |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | catch(std::string errorString){ |
| 2168 | std::cerr << errorString << std::endl; |
| 2169 | throw; |
| 2170 | } |
| 2171 | return(OGRERR_NONE); |
| 2172 | } |
| 2173 | |
| 2174 | // ///static function for coordinate transform based on proj4 parameters |
| 2175 | // bool VectorOgr::transform(OGRGeometry *pGeom, const std::string& outputProj4){ |
| 2176 | // try{ |
| 2177 | // OGRSpatialReference targetSRS; |
| 2178 | // if( targetSRS.importFromProj4(outputProj4.c_str()) != OGRERR_NONE ){ |
| 2179 | // std::ostringstream errorStream; |
| 2180 | // errorStream << "Error: cannot import SRS from Proj4 string: " << outputProj4 << std::endl; |
| 2181 | // throw(errorStream.str()); |
| 2182 | // } |
| 2183 | // return(VectorOgr::transform(pGeom,&targetSRS)); |
| 2184 | // } |
| 2185 | // catch(std::string errorString){ |
| 2186 | // std::cerr << errorString << std::endl; |
| 2187 | // return false; |
| 2188 | // } |
| 2189 | // } |
| 2190 | |
| 2191 | // ///static function for coordinate transform based on EPSG codes |
| 2192 | // bool VectorOgr::transform(OGRGeometry *pGeom, int outputEPSG){ |
| 2193 | // try{ |
| 2194 | // OGRSpatialReference targetSRS; |
| 2195 | // if( targetSRS.importFromEPSG(outputEPSG) != OGRERR_NONE ){ |
| 2196 | // std::ostringstream errorStream; |
| 2197 | // errorStream << "Error: cannot import SRS from EPSG code: " << outputEPSG << std::endl; |
| 2198 | // throw(errorStream.str()); |
| 2199 | // } |
| 2200 | // return(transform(pGeom,&targetSRS)); |
| 2201 | // } |
| 2202 | // catch(std::string errorString){ |
| 2203 | // std::cerr << errorString << std::endl; |
| 2204 | // return false; |
| 2205 | // } |
| 2206 | // } |
| 2207 | |
| 2208 | ///static function for coordinate transform based on OGRCoordinateTransformation |
| 2209 | bool VectorOgr::transform(OGRGeometry *pGeom, OGRCoordinateTransformation *poCT){ |
| 2210 | if(poCT) |
| 2211 | return pGeom->transform(poCT) == OGRERR_NONE; |
| 2212 | else |
| 2213 | return true; |
| 2214 | } |
| 2215 | |
| 2216 | ///static function for coordinate transform based on OGRCoordinateTransformation |
| 2217 | bool VectorOgr::transform(OGREnvelope *pEnv, OGRCoordinateTransformation *poCT){ |
| 2218 | if(poCT){ |
| 2219 | try{ |
| 2220 | std::vector<double> xvector(2);//ulx,lrx |
| 2221 | std::vector<double> yvector(2);//uly,lry |
| 2222 | xvector[0]=pEnv->MinX; |
| 2223 | xvector[1]=pEnv->MaxX; |
| 2224 | yvector[0]=pEnv->MaxY; |
| 2225 | yvector[1]=pEnv->MinY; |
| 2226 | if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){ |
| 2227 | std::ostringstream errorStream; |
| 2228 | errorStream << "Error: cannot apply OGRCoordinateTransformation in VectorOgr::transform" << std::endl; |
| 2229 | throw(errorStream.str()); |
| 2230 | } |
| 2231 | pEnv->MinX=xvector[0]; |
| 2232 | pEnv->MaxX=xvector[1]; |
| 2233 | pEnv->MaxY=yvector[0]; |
| 2234 | pEnv->MinY=yvector[1]; |
| 2235 | return true; |
| 2236 | } |
| 2237 | catch(std::string errorString){ |
| 2238 | std::cerr << errorString << std::endl; |
| 2239 | throw; |
| 2240 | } |
| 2241 | } |
| 2242 | else |
| 2243 | return true; |
| 2244 | } |
| 2245 | ///static function for coordinate transform of a vector of points based on OGRSpatialReference |
| 2246 | // bool VectorOgr::transform(std::vector<double> &xvector, std::vector<double> &yvector, OGRSpatialReference *sourceSRS, OGRSpatialReference *targetSRS){ |
| 2247 | // try{ |
| 2248 | // if(sourceSRS->IsSame(targetSRS)) |
| 2249 | // return true; |
| 2250 | // OGRCoordinateTransformation *poCT = OGRCreateCoordinateTransformation(sourceSRS, targetSRS); |
| 2251 | // if( !poCT ){ |
| 2252 | // std::ostringstream errorStream; |
| 2253 | // errorStream << "Error: cannot create OGRCoordinateTransformation" << std::endl; |
| 2254 | // throw(errorStream.str()); |
| 2255 | // } |
| 2256 | // if(!poCT->Transform(xvector.size(),&xvector[0],&yvector[0])){ |
| 2257 | // std::ostringstream errorStream; |
| 2258 | // errorStream << "Error: cannot apply OGRCoordinateTransformation" << std::endl; |
| 2259 | // throw(errorStream.str()); |
| 2260 | // } |
| 2261 | // } |
| 2262 | // catch(std::string errorString){ |
| 2263 | // std::cerr << errorString << std::endl; |
| 2264 | // return false; |
| 2265 | // } |
| 2266 | // } |
| 2267 | |
| 2268 | ///static function to join |
| 2269 | // OGRErr VectorOgr::join(VectorOgr &ogrReader1, VectorOgr &ogrReader2, VectorOgr &ogrWriter, app::AppFactory& app){ |
| 2270 | // Optionjl<string> output_opt("o", "output", "Filename of joined vector dataset"); |
| 2271 | // Optionjl<string> ogrformat_opt("f", "f", "Output ogr format for joined vector dataset","SQLite"); |
| 2272 | // Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 2273 | // Optionjl<std::string> key_opt("key", "key", "Key(s) used to join", "fid"); |
| 2274 | // Optionjl<std::string> method_opt("method", "method", "Join method (INNER, OUTER_LEFT, OUTER_RIGHT, OUTER_FULL)", "INNER"); |
| 2275 | // Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 2276 | |
| 2277 | // bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 2278 | // try{ |
| 2279 | // doProcess=output_opt.retrieveOption(app); |
| 2280 | // ogrformat_opt.retrieveOption(app); |
| 2281 | // option_opt.retrieveOption(app); |
| 2282 | // key_opt.retrieveOption(app); |
| 2283 | // method_opt.retrieveOption(app); |
| 2284 | // verbose_opt.retrieveOption(app); |
| 2285 | |
| 2286 | // if(!doProcess){ |
| 2287 | // cout << endl; |
| 2288 | // std::ostringstream helpStream; |
| 2289 | // helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 2290 | // throw(helpStream.str());//help was invoked, stop processing |
| 2291 | // } |
| 2292 | // OGRErr result=OGRERR_NONE; |
| 2293 | // char **papszOptions=NULL; |
| 2294 | // for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 2295 | // papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 2296 | // if(output_opt.size()){ |
| 2297 | // if(verbose_opt[0]) |
| 2298 | // std::cout << "opening ogrWriter " << output_opt[0] << std::endl; |
| 2299 | // if(ogrWriter.open(output_opt[0],ogrformat_opt[0])!=OGRERR_NONE){ |
| 2300 | // if(verbose_opt[0]) |
| 2301 | // std::cout << "Warning: could not open ogrWriter " << output_opt[0] << std::endl; |
| 2302 | // } |
| 2303 | // } |
| 2304 | // std::cout << "number of layers in ogrReader1: " << ogrReader1.getLayerCount() << std::endl; |
| 2305 | // std::cout << "number of layers in ogrReader2: " << ogrReader2.getLayerCount() << std::endl; |
| 2306 | // std::cout << "number of layers in ogrWriter: " << ogrWriter.getLayerCount() << std::endl; |
| 2307 | // if(ogrReader1.getLayerCount()!=ogrReader2.getLayerCount()){ |
| 2308 | // ostringstream errorStream; |
| 2309 | // errorStream << "Warning: number of layers in ogrReader1 and ogrReader2 are not equal: " << ogrReader1.getLayerCount() << " != " << ogrReader2.getLayerCount() << std::endl; |
| 2310 | // throw(errorStream.str()); |
| 2311 | // } |
| 2312 | // for(size_t ilayer=0;ilayer<ogrReader1.getLayerCount();++ilayer){ |
| 2313 | // //test |
| 2314 | // std::cout << "ilayer: " << ilayer << std::endl; |
| 2315 | // //test |
| 2316 | // std::cout << "number of layers in ogrWriter: " << ogrWriter.getLayerCount() << std::endl; |
| 2317 | // if(output_opt.size()){ |
| 2318 | // if(ogrWriter.getLayerCount()<=ilayer){ |
| 2319 | // if(verbose_opt[0]) |
| 2320 | // std::cout << "push layer" << ogrReader1.getLayerName(ilayer) << std::endl; |
| 2321 | // if(ogrWriter.pushLayer(ogrReader1.getLayerName(ilayer),ogrReader1.getProjection(ilayer),ogrReader1.getGeometryType(),papszOptions)!=OGRERR_NONE){ |
| 2322 | // ostringstream fs; |
| 2323 | // fs << "push layer to ogrWriter with polygons failed "; |
| 2324 | // fs << "layer name: "<< ogrReader1.getLayerName(ilayer) << std::endl; |
| 2325 | // throw(fs.str()); |
| 2326 | // } |
| 2327 | // } |
| 2328 | // // ogrWriter.pushLayer(getLayer()->GetName(),getProjection(),getGeometryType(),NULL); |
| 2329 | // std::vector<std::string> thisfields; |
| 2330 | // ogrReader1.getFieldNames(thisfields,ilayer); |
| 2331 | // if(verbose_opt[0]) |
| 2332 | // std::cout << "copyFields from ogrReader1" << std::endl; |
| 2333 | |
| 2334 | // //test |
| 2335 | // // if(verbose_opt[0]){ |
| 2336 | // // std::vector<std::string> writefields; |
| 2337 | // // std::cout << "debug0" << std::endl; |
| 2338 | // // ogrWriter.getFieldNames(writefields,ilayer); |
| 2339 | // // std::cout << "debug1" << std::endl; |
| 2340 | // // for(auto fit=writefields.begin();fit!=writefields.end();++fit) |
| 2341 | // // std::cout << *fit << std::endl; |
| 2342 | // // } |
| 2343 | // // std::cout << "debug2" << std::endl; |
| 2344 | |
| 2345 | // ogrWriter.copyFields(ogrReader1,thisfields,ilayer); |
| 2346 | // std::cout << "debug3" << std::endl; |
| 2347 | // std::vector<std::string> thatfields; |
| 2348 | // ogrReader2.getFieldNames(thatfields,ilayer); |
| 2349 | // for(auto itr=thisfields.begin();itr!=thisfields.end();++itr){ |
| 2350 | // auto thatitr = std::find(thatfields.begin(), thatfields.end(), *itr); |
| 2351 | // if (thatitr != thatfields.end()) thatfields.erase(thatitr); |
| 2352 | // } |
| 2353 | // if(verbose_opt[0]){ |
| 2354 | // std::cout << "copyFields from ogrReader2" << std::endl; |
| 2355 | // for(unsigned int iField=0;iField<thatfields.size();++iField) |
| 2356 | // std::cout << " " << thatfields[iField]; |
| 2357 | // std::cout << std::endl; |
| 2358 | // } |
| 2359 | // ogrWriter.copyFields(ogrReader2,thatfields,ilayer); |
| 2360 | // } |
| 2361 | // //make sure to use resize and setFeature instead of pushFeature when in processing in parallel!!! |
| 2362 | // std::string theKey1=key_opt[0]; |
| 2363 | // std::string theKey2=(key_opt.size()>1)? key_opt[1] : key_opt[0]; |
| 2364 | // switch(string2method(method_opt[0])){ |
| 2365 | // case(OUTER_FULL):{ |
| 2366 | // if(verbose_opt[0]) |
| 2367 | // std::cout << "in OUTER_FULL method" << std::endl; |
| 2368 | // std::unordered_multimap<std::string, size_t> hashmap; |
| 2369 | // // hash |
| 2370 | // for(size_t ifeature = 0; ifeature < ogrReader2.getFeatureCount(ilayer); ++ifeature) { |
| 2371 | // if(verbose_opt[0]>1) |
| 2372 | // std::cout << "hash ifeature: " << ifeature << std::endl; |
| 2373 | // OGRFeature *thatFeature=ogrReader2.getFeatureRef(ifeature,ilayer); |
| 2374 | // if(!thatFeature){ |
| 2375 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2376 | // continue; |
| 2377 | // } |
| 2378 | // int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 2379 | // if(iField<0){ |
| 2380 | // std::ostringstream errorStream; |
| 2381 | // errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 2382 | // errorStream << "fields are: " << std::endl; |
| 2383 | // std::vector<OGRFieldDefn*> fields; |
| 2384 | // ogrReader2.getFields(fields,ilayer); |
| 2385 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2386 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2387 | // errorStream << std::endl; |
| 2388 | // throw(errorStream.str()); |
| 2389 | // } |
| 2390 | // hashmap.insert(std::make_pair(thatFeature->GetFieldAsString(iField), ifeature)); |
| 2391 | // } |
| 2392 | // // map |
| 2393 | // if(verbose_opt[0]>1) |
| 2394 | // std::cout << "map" << std::endl; |
| 2395 | // for(size_t ifeature = 0; ifeature < ogrReader1.getFeatureCount(ilayer); ++ifeature) { |
| 2396 | // OGRFeature *thisFeature=ogrReader1.getFeatureRef(ifeature,ilayer); |
| 2397 | // if(!thisFeature){ |
| 2398 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2399 | // continue; |
| 2400 | // } |
| 2401 | // OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 2402 | // if(verbose_opt[0]>1) |
| 2403 | // std::cout << "set from this" << std::endl; |
| 2404 | // writeFeature->SetFrom(thisFeature); |
| 2405 | // if(verbose_opt[0]>1) |
| 2406 | // std::cout << "map ifeature: " << ifeature << std::endl; |
| 2407 | // int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 2408 | // if(iField<0){ |
| 2409 | // std::ostringstream errorStream; |
| 2410 | // errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 2411 | // errorStream << "fields are: " << std::endl; |
| 2412 | // std::vector<OGRFieldDefn*> fields; |
| 2413 | // ogrReader1.getFields(fields,ilayer); |
| 2414 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2415 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2416 | // errorStream << std::endl; |
| 2417 | // throw(errorStream.str()); |
| 2418 | // } |
| 2419 | // auto range = hashmap.equal_range(thisFeature->GetFieldAsString(iField)); |
| 2420 | // for(auto it = range.first; it != range.second; ++it) { |
| 2421 | // if(verbose_opt[0]>1) |
| 2422 | // std::cout << "set from this" << std::endl; |
| 2423 | // writeFeature->SetFrom(ogrReader2.getFeatureRef(it->second,ilayer)); |
| 2424 | // } |
| 2425 | // if(verbose_opt[0]>1) |
| 2426 | // std::cout << "pushFeature" << std::endl; |
| 2427 | // ogrWriter.pushFeature(writeFeature); |
| 2428 | // } |
| 2429 | // for(size_t ifeature = 0; ifeature < ogrReader2.getFeatureCount(ilayer); ++ifeature) { |
| 2430 | // OGRFeature *thatFeature=ogrReader2.getFeatureRef(ifeature,ilayer); |
| 2431 | // if(!thatFeature){ |
| 2432 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2433 | // continue; |
| 2434 | // } |
| 2435 | // int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 2436 | // if(iField<0){ |
| 2437 | // std::ostringstream errorStream; |
| 2438 | // errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 2439 | // errorStream << "fields are: " << std::endl; |
| 2440 | // std::vector<OGRFieldDefn*> fields; |
| 2441 | // ogrReader2.getFields(fields,ilayer); |
| 2442 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2443 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2444 | // errorStream << std::endl; |
| 2445 | // throw(errorStream.str()); |
| 2446 | // } |
| 2447 | // auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 2448 | // bool skip=false;//check if we already added this feature |
| 2449 | // for(auto it = range.first; it != range.second; ++it) { |
| 2450 | // skip=true;//feature was already added |
| 2451 | // break; |
| 2452 | // } |
| 2453 | // if(skip) |
| 2454 | // continue; |
| 2455 | // OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 2456 | // if(verbose_opt[0]>1) |
| 2457 | // std::cout << "set from ogrReader2" << std::endl; |
| 2458 | // writeFeature->SetFrom(thatFeature); |
| 2459 | // if(verbose_opt[0]>1) |
| 2460 | // std::cout << "map ifeature: " << ifeature << std::endl; |
| 2461 | // if(verbose_opt[0]>1) |
| 2462 | // std::cout << "pushFeature" << std::endl; |
| 2463 | // ogrWriter.pushFeature(writeFeature); |
| 2464 | // } |
| 2465 | // break; |
| 2466 | // } |
| 2467 | // case(OUTER_LEFT):{ |
| 2468 | // if(verbose_opt[0]) |
| 2469 | // std::cout << "in OUTER_LEFT method" << std::endl; |
| 2470 | // std::unordered_multimap<std::string, size_t> hashmap; |
| 2471 | // // hash |
| 2472 | // for(size_t ifeature = 0; ifeature < ogrReader2.getFeatureCount(ilayer); ++ifeature) { |
| 2473 | // if(verbose_opt[0]>1) |
| 2474 | // std::cout << "hash ifeature: " << ifeature << std::endl; |
| 2475 | // OGRFeature *thatFeature=ogrReader2.getFeatureRef(ifeature,ilayer); |
| 2476 | // if(!thatFeature){ |
| 2477 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2478 | // continue; |
| 2479 | // } |
| 2480 | // int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 2481 | // if(iField<0){ |
| 2482 | // std::ostringstream errorStream; |
| 2483 | // errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 2484 | // errorStream << "fields are: " << std::endl; |
| 2485 | // std::vector<OGRFieldDefn*> fields; |
| 2486 | // ogrReader2.getFields(fields,ilayer); |
| 2487 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2488 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2489 | // errorStream << std::endl; |
| 2490 | // throw(errorStream.str()); |
| 2491 | // } |
| 2492 | // hashmap.insert(std::make_pair(thatFeature->GetFieldAsString(iField), ifeature)); |
| 2493 | // } |
| 2494 | // // map |
| 2495 | // if(verbose_opt[0]>1) |
| 2496 | // std::cout << "map" << std::endl; |
| 2497 | // for(size_t ifeature = 0; ifeature < ogrReader1.getFeatureCount(ilayer); ++ifeature) { |
| 2498 | // OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 2499 | // OGRFeature *thisFeature=ogrReader1.getFeatureRef(ifeature,ilayer); |
| 2500 | // if(!thisFeature){ |
| 2501 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2502 | // continue; |
| 2503 | // } |
| 2504 | // if(verbose_opt[0]>1) |
| 2505 | // std::cout << "set from this" << std::endl; |
| 2506 | // writeFeature->SetFrom(thisFeature); |
| 2507 | // if(verbose_opt[0]>1) |
| 2508 | // std::cout << "map ifeature: " << ifeature << std::endl; |
| 2509 | // int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 2510 | // if(iField<0){ |
| 2511 | // std::ostringstream errorStream; |
| 2512 | // errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 2513 | // errorStream << "fields are: " << std::endl; |
| 2514 | // std::vector<OGRFieldDefn*> fields; |
| 2515 | // ogrReader1.getFields(fields,ilayer); |
| 2516 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2517 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2518 | // errorStream << std::endl; |
| 2519 | // throw(errorStream.str()); |
| 2520 | // } |
| 2521 | // auto range = hashmap.equal_range(thisFeature->GetFieldAsString(iField)); |
| 2522 | // for(auto it = range.first; it != range.second; ++it) { |
| 2523 | // if(verbose_opt[0]>1) |
| 2524 | // std::cout << "set from this" << std::endl; |
| 2525 | // writeFeature->SetFrom(ogrReader2.getFeatureRef(it->second,ilayer)); |
| 2526 | // } |
| 2527 | // if(verbose_opt[0]>1) |
| 2528 | // std::cout << "pushFeature" << std::endl; |
| 2529 | // ogrWriter.pushFeature(writeFeature); |
| 2530 | // } |
| 2531 | // break; |
| 2532 | // } |
| 2533 | // case(OUTER_RIGHT):{ |
| 2534 | // if(verbose_opt[0]) |
| 2535 | // std::cout << "in OUTER_RIGHT method" << std::endl; |
| 2536 | // std::unordered_multimap<std::string, size_t> hashmap; |
| 2537 | // // hash |
| 2538 | // for(size_t ifeature = 0; ifeature < ogrReader1.getFeatureCount(ilayer); ++ifeature) { |
| 2539 | // if(verbose_opt[0]>1) |
| 2540 | // std::cout << "hash ifeature: " << ifeature << std::endl; |
| 2541 | // OGRFeature *thisFeature=ogrReader1.getFeatureRef(ifeature,ilayer); |
| 2542 | // if(!thisFeature){ |
| 2543 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2544 | // continue; |
| 2545 | // } |
| 2546 | // int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 2547 | // if(iField<0){ |
| 2548 | // std::ostringstream errorStream; |
| 2549 | // errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 2550 | // errorStream << "fields are: " << std::endl; |
| 2551 | // std::vector<OGRFieldDefn*> fields; |
| 2552 | // ogrReader1.getFields(fields,ilayer); |
| 2553 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2554 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2555 | // errorStream << std::endl; |
| 2556 | // throw(errorStream.str()); |
| 2557 | // } |
| 2558 | // hashmap.insert(std::make_pair(thisFeature->GetFieldAsString(iField), ifeature)); |
| 2559 | // } |
| 2560 | // // map |
| 2561 | // if(verbose_opt[0]>1) |
| 2562 | // std::cout << "map" << std::endl; |
| 2563 | // for(size_t ifeature = 0; ifeature < ogrReader2.getFeatureCount(ilayer); ++ifeature) { |
| 2564 | // OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 2565 | // OGRFeature *thatFeature=ogrReader2.getFeatureRef(ifeature,ilayer); |
| 2566 | // if(!thatFeature){ |
| 2567 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2568 | // continue; |
| 2569 | // } |
| 2570 | // if(verbose_opt[0]>1) |
| 2571 | // std::cout << "set from ogrReader2" << std::endl; |
| 2572 | // writeFeature->SetFrom(thatFeature); |
| 2573 | // if(verbose_opt[0]>1) |
| 2574 | // std::cout << "map ifeature: " << ifeature << std::endl; |
| 2575 | // int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 2576 | // if(iField<0){ |
| 2577 | // std::ostringstream errorStream; |
| 2578 | // errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 2579 | // errorStream << "fields are: " << std::endl; |
| 2580 | // std::vector<OGRFieldDefn*> fields; |
| 2581 | // ogrReader2.getFields(fields,ilayer); |
| 2582 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2583 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2584 | // errorStream << std::endl; |
| 2585 | // throw(errorStream.str()); |
| 2586 | // } |
| 2587 | // auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 2588 | // for(auto it = range.first; it != range.second; ++it) { |
| 2589 | // if(verbose_opt[0]>1) |
| 2590 | // std::cout << "set from this" << std::endl; |
| 2591 | // writeFeature->SetFrom(ogrReader1.getFeatureRef(it->second,ilayer)); |
| 2592 | // } |
| 2593 | // if(verbose_opt[0]>1) |
| 2594 | // std::cout << "pushFeature" << std::endl; |
| 2595 | // ogrWriter.pushFeature(writeFeature); |
| 2596 | // } |
| 2597 | // break; |
| 2598 | // } |
| 2599 | // case(INNER):{ |
| 2600 | // if(verbose_opt[0]) |
| 2601 | // std::cout << "in INNER method" << std::endl; |
| 2602 | // std::unordered_multimap<std::string, size_t> hashmap; |
| 2603 | // // hash |
| 2604 | // for(size_t ifeature = 0; ifeature < ogrReader1.getFeatureCount(ilayer); ++ifeature) { |
| 2605 | // if(verbose_opt[0]>1) |
| 2606 | // std::cout << "hash ifeature: " << ifeature << std::endl; |
| 2607 | // OGRFeature *thisFeature=ogrReader1.getFeatureRef(ifeature,ilayer); |
| 2608 | // if(!thisFeature){ |
| 2609 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2610 | // continue; |
| 2611 | // } |
| 2612 | // int iField=thisFeature->GetFieldIndex(theKey1.c_str()); |
| 2613 | // if(iField<0){ |
| 2614 | // std::ostringstream errorStream; |
| 2615 | // errorStream << "Error: iField not found for " << theKey1 << std::endl; |
| 2616 | // errorStream << "fields are: " << std::endl; |
| 2617 | // std::vector<OGRFieldDefn*> fields; |
| 2618 | // ogrReader1.getFields(fields,ilayer); |
| 2619 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2620 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2621 | // errorStream << std::endl; |
| 2622 | // throw(errorStream.str()); |
| 2623 | // } |
| 2624 | // hashmap.insert(std::make_pair(thisFeature->GetFieldAsString(iField), ifeature)); |
| 2625 | // } |
| 2626 | // // map |
| 2627 | // if(verbose_opt[0]>1) |
| 2628 | // std::cout << "map" << std::endl; |
| 2629 | // for(size_t ifeature = 0; ifeature < ogrReader2.getFeatureCount(ilayer); ++ifeature) { |
| 2630 | // if(verbose_opt[0]>1) |
| 2631 | // std::cout << "map ifeature: " << ifeature << std::endl; |
| 2632 | // OGRFeature *thatFeature=ogrReader2.getFeatureRef(ifeature,ilayer); |
| 2633 | // //test |
| 2634 | // std::cout << "that feature: " << thatFeature << std::endl; |
| 2635 | // if(!thatFeature){ |
| 2636 | // std::cerr << "Warning: " << ifeature << " is NULL" << std::endl; |
| 2637 | // continue; |
| 2638 | // } |
| 2639 | // int iField=thatFeature->GetFieldIndex(theKey2.c_str()); |
| 2640 | // //test |
| 2641 | // std::cout << "iField: " << iField << std::endl; |
| 2642 | // if(iField<0){ |
| 2643 | // std::ostringstream errorStream; |
| 2644 | // errorStream << "Error: iField not found for " << theKey2 << std::endl; |
| 2645 | // errorStream << "fields are: " << std::endl; |
| 2646 | // std::vector<OGRFieldDefn*> fields; |
| 2647 | // ogrReader2.getFields(fields,ilayer); |
| 2648 | // for(unsigned int iField=0;iField<fields.size();++iField) |
| 2649 | // errorStream << " " << fields[iField]->GetNameRef(); |
| 2650 | // errorStream << std::endl; |
| 2651 | // throw(errorStream.str()); |
| 2652 | // } |
| 2653 | // //test |
| 2654 | // std::cout << "getting range" << std::endl; |
| 2655 | // auto range = hashmap.equal_range(thatFeature->GetFieldAsString(iField)); |
| 2656 | // for(auto it = range.first; it != range.second; ++it) { |
| 2657 | // //test |
| 2658 | // std::cout << "getLayer" << ilayer << std::endl; |
| 2659 | // OGRLayer *writeLayer=ogrWriter.getLayer(ilayer); |
| 2660 | // if(!writeLayer){ |
| 2661 | // std::ostringstream errorStream; |
| 2662 | // errorStream << "Error: could not get layer " << ilayer << std::endl; |
| 2663 | // throw(errorStream.str()); |
| 2664 | // } |
| 2665 | // //test |
| 2666 | // std::cout << "getLayerDefn" << std::endl; |
| 2667 | // OGRFeatureDefn *poFDefn = writeLayer->GetLayerDefn(); |
| 2668 | // if(!poFDefn){ |
| 2669 | // std::ostringstream errorStream; |
| 2670 | // errorStream << "Error: could not get layer definition" << std::endl; |
| 2671 | // throw(errorStream.str()); |
| 2672 | // } |
| 2673 | // OGRFeature *writeFeature=ogrWriter.createFeature(ilayer); |
| 2674 | // if(verbose_opt[0]>1) |
| 2675 | // std::cout << "set from this" << std::endl; |
| 2676 | // writeFeature->SetFrom(ogrReader1.getFeatureRef(it->second,ilayer)); |
| 2677 | // if(verbose_opt[0]>1) |
| 2678 | // std::cout << "set from ogrReader2" << std::endl; |
| 2679 | // writeFeature->SetFrom(thatFeature); |
| 2680 | // if(verbose_opt[0]>1) |
| 2681 | // std::cout << "pushFeature" << std::endl; |
| 2682 | // ogrWriter.pushFeature(writeFeature); |
| 2683 | // } |
| 2684 | // } |
| 2685 | // break; |
| 2686 | // } |
| 2687 | // default: |
| 2688 | // std::ostringstream errorStream; |
| 2689 | // errorStream << "Error: join method " << method_opt[0] << " not implemented " << std::endl; |
| 2690 | // throw(errorStream.str()); |
| 2691 | // break; |
| 2692 | // } |
| 2693 | // return(OGRERR_NONE); |
| 2694 | // } |
| 2695 | // } |
| 2696 | // catch(std::string errorString){ |
| 2697 | // std::cerr << errorString << std::endl; |
| 2698 | // return(OGRERR_FAILURE); |
| 2699 | // } |
| 2700 | // } |
| 2701 | |
| 2702 | std::ostream& operator<<(std::ostream& theOstream, VectorOgr& theVector){ |
| 2703 | for(size_t ilayer=0;ilayer<theVector.getLayerCount();++ilayer){ |
| 2704 | theOstream << "#" << theVector.getLayer(ilayer)->GetName() << std::endl; |
| 2705 | OGRFeatureDefn *poFDefn = theVector.getLayer(ilayer)->GetLayerDefn(); |
| 2706 | theOstream << "#"; |
| 2707 | for(int iField=0;iField<poFDefn->GetFieldCount();++iField){ |
| 2708 | OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField); |
| 2709 | std::string fieldname=poFieldDefn->GetNameRef(); |
| 2710 | theOstream << fieldname << " "; |
| 2711 | } |
| 2712 | theOstream << std::endl; |
| 2713 | |
| 2714 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 2715 | #pragma omp parallel for |
| 2716 | #else |
| 2717 | #endif |
| 2718 | for(size_t index=0;index<theVector.getFeatureCount(ilayer);++index){ |
| 2719 | OGRFeature *poFeature; |
| 2720 | poFeature=theVector.getFeatureRef(index,ilayer); |
| 2721 | for(int iField=0;iField<poFDefn->GetFieldCount();++iField) |
| 2722 | theOstream << poFeature->GetFieldAsString(iField) << " "; |
| 2723 | theOstream << std::endl; |
| 2724 | } |
| 2725 | theOstream << std::endl; |
| 2726 | } |
| 2727 | return(theOstream); |
| 2728 | } |