| 1 | /********************************************************************** |
| 2 | jlpolygonize_lib.cc: program to make vector file from raster image |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2022 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 "cpl_string.h" |
| 22 | #include "gdal_priv.h" |
| 23 | #include "gdal.h" |
| 24 | #include "ogrsf_frmts.h" |
| 25 | #include "gdal_alg.h" |
| 26 | #include "ogr_api.h" |
| 27 | #include <config_jiplib.h> |
| 28 | #include "base/Optionjl.h" |
| 29 | #include "Jim.h" |
| 30 | #include "VectorOgr.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | |
| 34 | shared_ptr<VectorOgr> Jim::polygonize(app::AppFactory& app, std::shared_ptr<Jim> mask){ |
| 35 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 36 | polygonize(*ogrWriter, app, mask); |
| 37 | return(ogrWriter); |
| 38 | } |
| 39 | |
| 40 | void Jim::polygonize(VectorOgr&ogrWriter, app::AppFactory &theApp, std::shared_ptr<Jim> mask){ |
| 41 | Optionjl<string> output_opt("o", "output", "Output vector file"); |
| 42 | Optionjl<string> layername_opt("ln", "ln", "Output layer name","polygonize"); |
| 43 | Optionjl<string> ogrformat_opt("f", "oformat", "Output OGR file format","SQLite"); |
| 44 | Optionjl<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 45 | Optionjl<string> fname_opt("n", "name", "the field name of the output layer", "DN"); |
| 46 | Optionjl<double> nodata_opt("nodata", "nodata", "Disgard this nodata value when creating polygons."); |
| 47 | Optionjl<short> verbose_opt("verbose", "verbose", "verbose output",0,2); |
| 48 | |
| 49 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 50 | try{ |
| 51 | doProcess=output_opt.retrieveOption(theApp); |
| 52 | layername_opt.retrieveOption(theApp); |
| 53 | ogrformat_opt.retrieveOption(theApp); |
| 54 | option_opt.retrieveOption(theApp); |
| 55 | fname_opt.retrieveOption(theApp); |
| 56 | nodata_opt.retrieveOption(theApp); |
| 57 | verbose_opt.retrieveOption(theApp); |
| 58 | } |
| 59 | catch(std::string predefinedString){ |
| 60 | std::cout << predefinedString << std::endl; |
| 61 | } |
| 62 | if(!doProcess){ |
| 63 | std::cout << std::endl; |
| 64 | std::ostringstream helpStream; |
| 65 | helpStream << "exception thrown due to help info"; |
| 66 | throw(helpStream.str());//help was invoked, stop processing |
| 67 | } |
| 68 | |
| 69 | std::vector<std::string> badKeys; |
| 70 | theApp.badKeys(badKeys); |
| 71 | if(badKeys.size()){ |
| 72 | std::ostringstream errorStream; |
| 73 | if(badKeys.size()>1) |
| 74 | errorStream << "Error: unknown keys: "; |
| 75 | else |
| 76 | errorStream << "Error: unknown key: "; |
| 77 | for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 78 | errorStream << badKeys[ikey] << " "; |
| 79 | } |
| 80 | errorStream << std::endl; |
| 81 | throw(errorStream.str()); |
| 82 | } |
| 83 | |
| 84 | const char *pszFormat = "MEM"; |
| 85 | GDALDriver *poDriverMem = GetGDALDriverManager()->GetDriverByName(pszFormat); |
| 86 | |
| 87 | GDALDataset *poDatasetMask = NULL; |
| 88 | GDALRasterBand *poMaskBandMem = NULL; |
| 89 | GDALDataset *poDatasetMem = NULL; |
| 90 | GDALRasterBand *poBandMem = NULL; |
| 91 | try{ |
| 92 | if( poDriverMem ){ |
| 93 | if(mask){ |
| 94 | if(mask->nrOfCol()!=nrOfCol()){ |
| 95 | std::string errorString="Error: number of cols in mask does not match input raster"; |
| 96 | throw(errorString); |
| 97 | } |
| 98 | if(mask->nrOfRow()!=nrOfRow()){ |
| 99 | std::string errorString="Error: number of rows in mask does not match input raster"; |
| 100 | throw(errorString); |
| 101 | } |
| 102 | poDatasetMask = (GDALDataset *)poDriverMem->Create("memmask",mask->nrOfCol(),mask->nrOfRow(),1,mask->getGDALDataType(),NULL); |
| 103 | if(poDatasetMask){ |
| 104 | if(verbose_opt[0]) |
| 105 | cout << "get raster band from mask" << endl; |
| 106 | std::vector<double> maskGT(6); |
| 107 | getGeoTransform(maskGT); |
| 108 | poDatasetMask->SetGeoTransform(&maskGT[0]); |
| 109 | poMaskBandMem = poDatasetMask->GetRasterBand(1); |
| 110 | if( poMaskBandMem->RasterIO(GF_Write, 0, 0, mask->nrOfCol(), mask->nrOfRow(), mask->getDataPointer(0), mask->nrOfCol(), mask->nrOfRow(), mask->getGDALDataType(), 0, 0, NULL) != CE_None ){ |
| 111 | cerr << CPLGetLastErrorMsg() << endl; |
| 112 | } |
| 113 | } |
| 114 | else{ |
| 115 | std::string errorString="Error: could not create memory dataset for mask dataset"; |
| 116 | throw(errorString); |
| 117 | } |
| 118 | } |
| 119 | poDatasetMem = (GDALDataset *)poDriverMem->Create("memband",nrOfCol(),nrOfRow(),1,getGDALDataType(),NULL); |
| 120 | if(poDatasetMem){ |
| 121 | //printf("Copy MemoryBand to memband\n"); fflush(stdout); |
| 122 | std::vector<double> sourceGT(6); |
| 123 | getGeoTransform(sourceGT); |
| 124 | poDatasetMem->SetGeoTransform(&sourceGT[0]); |
| 125 | poBandMem = poDatasetMem->GetRasterBand(1); |
| 126 | if( poBandMem->RasterIO(GF_Write, 0, 0, nrOfCol(), nrOfRow(), m_data[0], nrOfCol(), nrOfRow(), getGDALDataType(), 0, 0, NULL) == CE_None ){ |
| 127 | // Quality parameters for warping operation |
| 128 | if(nodata_opt.size()) |
| 129 | poBandMem->SetNoDataValue(nodata_opt[0]); |
| 130 | |
| 131 | char **papszOptions=NULL; |
| 132 | for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 133 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 134 | |
| 135 | if(verbose_opt[0]) |
| 136 | std::cout << "Opening ogrWriter: " << output_opt[0] << " in format " << ogrformat_opt[0] << endl; |
| 137 | ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 138 | ogrWriter.pushLayer(layername_opt[0],getProjection(),wkbUnknown,papszOptions); |
| 139 | |
| 140 | if(verbose_opt[0]) |
| 141 | cout << "projection: " << getProjection() << endl; |
| 142 | ogrWriter.createField(fname_opt[0],OFTInteger); |
| 143 | |
| 144 | if(verbose_opt[0]) |
| 145 | cout << "GDALPolygonize started..." << endl; |
| 146 | |
| 147 | int index=ogrWriter.getLayer()->GetLayerDefn()->GetFieldIndex(fname_opt[0].c_str()); |
| 148 | double dfComplete=0.0; |
| 149 | const char* pszMessage; |
| 150 | // void* pProgressArg=NULL; |
| 151 | // GDALProgressFunc pfnProgress=NULL; |
| 152 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 153 | // pfnProgress(dfComplete,pszMessage,pProgressArg); |
| 154 | // if(GDALPolygonize((GDALRasterBandH)poBandMem, (GDALRasterBandH)poMaskBandMem, (OGRLayerH)ogrWriter.getLayer(),index,NULL,pfnProgress,pProgressArg)!=CE_None){ |
| 155 | if(GDALPolygonize((GDALRasterBandH)poBandMem, (GDALRasterBandH)poMaskBandMem, (OGRLayerH)ogrWriter.getLayer(),index,NULL,NULL,NULL)!=CE_None){ |
| 156 | std::string errorString=CPLGetLastErrorMsg(); |
| 157 | throw(errorString); |
| 158 | } |
| 159 | else{ |
| 160 | dfComplete=1.0; |
| 161 | // pfnProgress(dfComplete,pszMessage,pProgressArg); |
| 162 | } |
| 163 | for(size_t ilayer=0;ilayer<ogrWriter.getLayerCount();++ilayer) |
| 164 | ogrWriter.readFeatures(ilayer); |
| 165 | } |
| 166 | } |
| 167 | else{ |
| 168 | std::string errorString="Error: could not create memory dataset for input dataset"; |
| 169 | throw(errorString); |
| 170 | } |
| 171 | } |
| 172 | else{ |
| 173 | std::string errorString="Error: could not create memory driver"; |
| 174 | throw(errorString); |
| 175 | } |
| 176 | } |
| 177 | catch(std::string errorString){ |
| 178 | std::cerr << errorString << std::endl; |
| 179 | throw; |
| 180 | } |
| 181 | catch(...){ |
| 182 | std::cerr << "Error: could not polygonize" << std::endl; |
| 183 | throw; |
| 184 | } |
| 185 | } |