| 1 | /********************************************************************** |
| 2 | jlextractogr_lib.cc: extract pixel values from raster image from a vector sample |
| 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 <assert.h> |
| 22 | #include <string> |
| 23 | #include <sstream> |
| 24 | #include <iostream> |
| 25 | #include <algorithm> |
| 26 | #include <ctime> |
| 27 | #include <vector> |
| 28 | #include <set> |
| 29 | #include <memory> |
| 30 | // #include <boost/filesystem.hpp> |
| 31 | #include <ogr_geometry.h> |
| 32 | #include "imageclasses/Jim.h" |
| 33 | #include "imageclasses/VectorOgr.h" |
| 34 | #include "base/Optionjl.h" |
| 35 | #include "algorithms/StatFactory.h" |
| 36 | #include "apps/AppFactory.h" |
| 37 | |
| 38 | using namespace std; |
| 39 | using namespace app; |
| 40 | |
| 41 | #ifndef PI |
| 42 | #define PI 3.1415926535897932384626433832795 |
| 43 | #endif |
| 44 | |
| 45 | namespace rule{ |
| 46 | enum RULE_TYPE {point=0, mean=1, proportion=2, custom=3, min=4, max=5, mode=6, centroid=7, sum=8, median=9, stdev=10, percentile=11, count=12, allpoints=13}; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * @param app application specific option arguments |
| 52 | * @return output Vector |
| 53 | **/ |
| 54 | // make sure to setSpatialFilterRect on vector before entering here |
| 55 | shared_ptr<VectorOgr> Jim::extractOgr(VectorOgr& sampleReader, AppFactory& app){ |
| 56 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 57 | if(extractOgr(sampleReader, *ogrWriter, app)!=OGRERR_NONE){ |
| 58 | std::cerr << "Failed to extract" << std::endl; |
| 59 | } |
| 60 | return(ogrWriter); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param app application specific option arguments |
| 65 | * @return output Vector |
| 66 | **/ |
| 67 | shared_ptr<VectorOgr> Jim::extractSample(AppFactory& app){ |
| 68 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 69 | if(extractSample(*ogrWriter, app)!=OGRERR_NONE){ |
| 70 | std::cerr << "Failed to extract" << std::endl; |
| 71 | } |
| 72 | return(ogrWriter); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param app application specific option arguments |
| 77 | * |
| 78 | * @return CE_None if success, CE_Failure if failure |
| 79 | */ |
| 80 | //todo: support multiple layers for writing |
| 81 | //output vector ogrWriter will take spatial reference system of input vector sampleReader |
| 82 | // make sure to setSpatialFilterRect on vector before entering here |
| 83 | |
| 84 | CPLErr Jim::extractOgr(VectorOgr& sampleReader, VectorOgr&ogrWriter, AppFactory& app){ |
| 85 | // Optionjl<string> image_opt("i", "input", "Raster input dataset containing band information"); |
| 86 | // Optionjl<string> sample_opt("s", "sample", "OGR vector dataset with features to be extracted from input data. Output will contain features with input band information included."); |
| 87 | // Optionjl<string> layer_opt("ln", "ln", "Layer name(s) in sample (leave empty to select all)"); |
| 88 | // Optionjl<unsigned int> random_opt("rand", "random", "Create simple random sample of points. Provide number of points to generate"); |
| 89 | // Optionjl<double> grid_opt("grid", "grid", "Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)"); |
| 90 | Optionjl<string> output_opt("o", "output", "Output sample dataset"); |
| 91 | Optionjl<int> label_opt("label", "label", "Create extra label field with value"); |
| 92 | Optionjl<std::string> fid_opt("fid", "fid", "Create extra field with field identifier (sequence in which the features have been read"); |
| 93 | // Optionjl<string> attribute_opt("attribute", "attribute", "use this field to rasterize when selecting extraction rule allpoints"); |
| 94 | Optionjl<string> copyFields_opt("copy", "copy", "Restrict these fields only to copy from input to output vector dataset (default is to copy all fields)"); |
| 95 | Optionjl<int> class_opt("c", "class", "Class(es) to take into account for the rules mode, proportion and count"); |
| 96 | Optionjl<float> threshold_opt("t", "threshold", "Probability threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). Use a single threshold per vector sample layer. Use value 100 to select all pixels for selected class(es)", 100); |
| 97 | Optionjl<double> percentile_opt("perc","perc","Percentile value(s) used for rule percentile",95); |
| 98 | Optionjl<string> ogrformat_opt("f", "oformat", "Output vector dataset format","SQLite"); |
| 99 | Optionjl<unsigned int> access_opt("access", "access", "Access (0: GDAL_OF_READ_ONLY, 1: GDAL_OF_UPDATE)",1); |
| 100 | Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 101 | Optionjl<string> ftype_opt("ft", "ftype", "Field type (only Real or Integer)", "Real"); |
| 102 | Optionjl<int> band_opt("b", "band", "Band index(es) to extract (0 based). Leave empty to use all bands"); |
| 103 | Optionjl<size_t> plane_opt("p", "plane", "Plane index(es) to extract (0 based). Leave empty to use all bands"); |
| 104 | Optionjl<std::string> bandNames_opt("bn", "bandname", "Band name(s) corresponding to band index(es)."); |
| 105 | Optionjl<std::string> planeNames_opt("bn", "planename", "Plane name(s) corresponding to plane index(es)."); |
| 106 | Optionjl<unsigned short> bstart_opt("sband", "startband", "Start band sequence number"); |
| 107 | Optionjl<unsigned short> bend_opt("eband", "endband", "End band sequence number"); |
| 108 | Optionjl<string> rule_opt("r", "rule", "Rule how to report image information per feature. point (single point within polygon), allpoints (all points within polygon), centroid, mean, stdev, median, proportion, count, min, max, mode, sum, percentile.","centroid"); |
| 109 | Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Invalid value(s) for input image"); |
| 110 | Optionjl<int> bndnodata_opt("bndnodata", "bndnodata", "Band(s) in input image to check if pixel is valid (used for srcnodata)", 0); |
| 111 | Optionjl<string> mask_opt("m", "mask", "Use the the specified file as a validity mask (0 is nodata)."); |
| 112 | Optionjl<double> msknodata_opt("msknodata", "msknodata", "Mask value not to extract.", 0); |
| 113 | Optionjl<unsigned int> mskband_opt("mskband", "mskband", "Mask band to read (0 indexed)", 0); |
| 114 | Optionjl<float> polythreshold_opt("tp", "thresholdPolygon", "(absolute) threshold for selecting samples in each polygon"); |
| 115 | Optionjl<float> buffer_opt("buf", "buffer", "Buffer for calculating statistics in geometric units of raster dataset"); |
| 116 | Optionjl<bool> disc_opt("circ", "circular", "Use a circular disc kernel buffer (for vector point sample datasets only, use in combination with buffer option)", false); |
| 117 | Optionjl<std::string> allCovered_opt("cover", "cover", "Which polygons to include based on coverage (ALL_TOUCHED, ALL_COVERED)", "ALL_TOUCHED"); |
| 118 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 119 | Optionjl<int> s_srs_opt("s_srs", "s_srs", "Spatial reference system of vector dataset (in EPSG)"); |
| 120 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 121 | |
| 122 | bstart_opt.setHide(1); |
| 123 | bend_opt.setHide(1); |
| 124 | bndnodata_opt.setHide(1); |
| 125 | srcnodata_opt.setHide(1); |
| 126 | mask_opt.setHide(1); |
| 127 | msknodata_opt.setHide(1); |
| 128 | mskband_opt.setHide(1); |
| 129 | polythreshold_opt.setHide(1); |
| 130 | percentile_opt.setHide(1); |
| 131 | buffer_opt.setHide(1); |
| 132 | disc_opt.setHide(1); |
| 133 | allCovered_opt.setHide(1); |
| 134 | option_opt.setHide(1); |
| 135 | memory_opt.setHide(1); |
| 136 | |
| 137 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 138 | try{ |
| 139 | // doProcess=sample_opt.retrieveOption(app); |
| 140 | // layer_opt.retrieveOption(app); |
| 141 | doProcess=output_opt.retrieveOption(app); |
| 142 | // random_opt.retrieveOption(app); |
| 143 | // grid_opt.retrieveOption(app); |
| 144 | class_opt.retrieveOption(app); |
| 145 | label_opt.retrieveOption(app); |
| 146 | fid_opt.retrieveOption(app); |
| 147 | copyFields_opt.retrieveOption(app); |
| 148 | // attribute_opt.retrieveOption(app); |
| 149 | threshold_opt.retrieveOption(app); |
| 150 | percentile_opt.retrieveOption(app); |
| 151 | ogrformat_opt.retrieveOption(app); |
| 152 | access_opt.retrieveOption(app); |
| 153 | option_opt.retrieveOption(app); |
| 154 | ftype_opt.retrieveOption(app); |
| 155 | band_opt.retrieveOption(app); |
| 156 | plane_opt.retrieveOption(app); |
| 157 | bandNames_opt.retrieveOption(app); |
| 158 | planeNames_opt.retrieveOption(app); |
| 159 | bstart_opt.retrieveOption(app); |
| 160 | bend_opt.retrieveOption(app); |
| 161 | rule_opt.retrieveOption(app); |
| 162 | bndnodata_opt.retrieveOption(app); |
| 163 | srcnodata_opt.retrieveOption(app); |
| 164 | mask_opt.retrieveOption(app); |
| 165 | msknodata_opt.retrieveOption(app); |
| 166 | mskband_opt.retrieveOption(app); |
| 167 | polythreshold_opt.retrieveOption(app); |
| 168 | buffer_opt.retrieveOption(app); |
| 169 | disc_opt.retrieveOption(app); |
| 170 | allCovered_opt.retrieveOption(app); |
| 171 | memory_opt.retrieveOption(app); |
| 172 | s_srs_opt.retrieveOption(app); |
| 173 | verbose_opt.retrieveOption(app); |
| 174 | if(!doProcess){ |
| 175 | cout << endl; |
| 176 | std::ostringstream helpStream; |
| 177 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 178 | throw(helpStream.str());//help was invoked, stop processing |
| 179 | } |
| 180 | |
| 181 | //initialize ruleMap |
| 182 | std::map<std::string, rule::RULE_TYPE> ruleMap; |
| 183 | ruleMap["point"]=rule::point; |
| 184 | ruleMap["centroid"]=rule::centroid; |
| 185 | ruleMap["mean"]=rule::mean; |
| 186 | ruleMap["stdev"]=rule::stdev; |
| 187 | ruleMap["median"]=rule::median; |
| 188 | ruleMap["proportion"]=rule::proportion; |
| 189 | ruleMap["count"]=rule::count; |
| 190 | ruleMap["min"]=rule::min; |
| 191 | ruleMap["max"]=rule::max; |
| 192 | ruleMap["custom"]=rule::custom; |
| 193 | ruleMap["mode"]=rule::mode; |
| 194 | ruleMap["sum"]=rule::sum; |
| 195 | ruleMap["percentile"]=rule::percentile; |
| 196 | ruleMap["allpoints"]=rule::allpoints; |
| 197 | |
| 198 | //initialize fieldMap |
| 199 | std::map<std::string, std::string> fieldMap; |
| 200 | fieldMap["point"]="point"; |
| 201 | fieldMap["centroid"]="cntrd"; |
| 202 | fieldMap["mean"]="mean"; |
| 203 | fieldMap["stdev"]="stdev"; |
| 204 | fieldMap["median"]="median"; |
| 205 | fieldMap["proportion"]="prop"; |
| 206 | fieldMap["count"]="count"; |
| 207 | fieldMap["min"]="min"; |
| 208 | fieldMap["max"]="max"; |
| 209 | fieldMap["custom"]="custom"; |
| 210 | fieldMap["mode"]="mode"; |
| 211 | fieldMap["sum"]="sum"; |
| 212 | fieldMap["percentile"]="perc"; |
| 213 | fieldMap["allpoints"]="allp"; |
| 214 | |
| 215 | statfactory::StatFactory stat; |
| 216 | if(srcnodata_opt.size()){ |
| 217 | while(srcnodata_opt.size()<bndnodata_opt.size()) |
| 218 | srcnodata_opt.push_back(srcnodata_opt[0]); |
| 219 | stat.setNoDataValues(srcnodata_opt); |
| 220 | } |
| 221 | Jim maskReader; |
| 222 | if(mask_opt.size()){ |
| 223 | try{ |
| 224 | //todo: open with resampling, resolution and projection according to input |
| 225 | maskReader.open(mask_opt[0]); |
| 226 | if(mskband_opt[0]>=maskReader.nrOfBand()){ |
| 227 | string errorString="Error: illegal mask band"; |
| 228 | throw(errorString); |
| 229 | } |
| 230 | } |
| 231 | catch(string error){ |
| 232 | cerr << error << std::endl; |
| 233 | throw; |
| 234 | } |
| 235 | } |
| 236 | Vector2d<unsigned int> posdata; |
| 237 | |
| 238 | if(output_opt.empty()){ |
| 239 | std::cerr << "No output dataset provided (use option -o). Use --help for help information"; |
| 240 | return(CE_Failure); |
| 241 | } |
| 242 | if(plane_opt.empty()){ |
| 243 | size_t iplane=0; |
| 244 | while(plane_opt.size()<nrOfPlane()) |
| 245 | plane_opt.push_back(iplane++); |
| 246 | } |
| 247 | int nplane=plane_opt.size(); |
| 248 | if(nplane>1){ |
| 249 | if(planeNames_opt.size()<nplane){ |
| 250 | planeNames_opt.clear(); |
| 251 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 252 | int thePlane=plane_opt[iplane]; |
| 253 | ostringstream planestream; |
| 254 | planestream << "t" << thePlane; |
| 255 | planeNames_opt.push_back(planestream.str()); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | char **papszOptions=NULL; |
| 261 | for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 262 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 263 | |
| 264 | bool initWriter=true; |
| 265 | try{ |
| 266 | if(find(rule_opt.begin(),rule_opt.end(),"allpoints")==rule_opt.end()){ |
| 267 | if(verbose_opt[0]) |
| 268 | std::cout << "Opening ogrWriter: " << output_opt[0] << " in format " << ogrformat_opt[0] << endl; |
| 269 | ogrWriter.open(output_opt[0],ogrformat_opt[0],access_opt[0]); |
| 270 | } |
| 271 | } |
| 272 | catch(std::string errorString){ |
| 273 | if(verbose_opt[0]) |
| 274 | std::cout << "initWriter is false" << endl; |
| 275 | initWriter=false; |
| 276 | } |
| 277 | |
| 278 | if(verbose_opt[0]) |
| 279 | std::cout << "number of layers: " << sampleReader.getLayerCount() << endl; |
| 280 | |
| 281 | for(int ilayer=0;ilayer<sampleReader.getLayerCount();++ilayer){ |
| 282 | if(verbose_opt[0]) |
| 283 | std::cout << "getLayer " << ilayer << std::endl; |
| 284 | OGRLayer *readLayer=sampleReader.getLayer(ilayer); |
| 285 | if(!readLayer){ |
| 286 | ostringstream ess; |
| 287 | ess << "Error: could not get layer of sampleReader" << endl; |
| 288 | throw(ess.str()); |
| 289 | } |
| 290 | |
| 291 | OGRSpatialReference thisSpatialRef=getSpatialRef(); |
| 292 | #if GDAL_VERSION_MAJOR > 2 |
| 293 | thisSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 294 | #endif |
| 295 | |
| 296 | OGRSpatialReference sampleSpatialRef(sampleReader.getSpatialRef()); |
| 297 | #if GDAL_VERSION_MAJOR > 2 |
| 298 | sampleSpatialRef.SetAxisMappingStrategy(OSRAxisMappingStrategy::OAMS_TRADITIONAL_GIS_ORDER); |
| 299 | #endif |
| 300 | // OGRCoordinateTransformation *sample2img = OGRCreateCoordinateTransformation(&sampleSpatialRef, &thisSpatialRef); |
| 301 | // OGRCoordinateTransformation *img2sample = OGRCreateCoordinateTransformation(&thisSpatialRef,&sampleSpatialRef); |
| 302 | if(verbose_opt[0]){ |
| 303 | std::cout << "spatialref of raster: " << this->getFileName() << std::endl; |
| 304 | thisSpatialRef.dumpReadable(); |
| 305 | std::cout << "spatialref of vector sample: " << std::endl; |
| 306 | sampleSpatialRef.dumpReadable(); |
| 307 | } |
| 308 | if(thisSpatialRef.IsSame(&sampleSpatialRef)){ |
| 309 | if(verbose_opt[0]) |
| 310 | std::cout << "spatial reference of vector sample is same as raster" << std::endl; |
| 311 | // sample2img=0; |
| 312 | // img2sample=0; |
| 313 | } |
| 314 | else{ |
| 315 | std::ostringstream errorStream; |
| 316 | errorStream << "Error: spatial reference of vector sample must be identical to raster" << std::endl; |
| 317 | throw(errorStream.str()); |
| 318 | // if(verbose_opt[0]) |
| 319 | // std::cout << "spatial reference of vector sample is different from raster, img2sample: " << img2sample << std::endl; |
| 320 | // if(!sample2img){ |
| 321 | // std::ostringstream errorStream; |
| 322 | // errorStream << "Error: cannot create OGRCoordinateTransformation sample to image" << std::endl; |
| 323 | // throw(errorStream.str()); |
| 324 | // } |
| 325 | // if(!img2sample){ |
| 326 | // std::ostringstream errorStream; |
| 327 | // errorStream << "Error: cannot create OGRCoordinateTransformation image to sample" << std::endl; |
| 328 | // throw(errorStream.str()); |
| 329 | // } |
| 330 | } |
| 331 | //image bounding box in SRS of the input sample vector sampleReader |
| 332 | double img_ulx,img_uly,img_lrx,img_lry; |
| 333 | // this->getBoundingBox(img_ulx,img_uly,img_lrx,img_lry,img2sample); |
| 334 | this->getBoundingBox(img_ulx,img_uly,img_lrx,img_lry); |
| 335 | //layer bounding box in SRS of this image raster |
| 336 | double layer_ulx; |
| 337 | double layer_uly; |
| 338 | double layer_lrx; |
| 339 | double layer_lry; |
| 340 | if(verbose_opt[0]) |
| 341 | std::cout << "getExtent " << std::endl; |
| 342 | // sampleReader.getExtent(layer_ulx,layer_uly,layer_lrx,layer_lry,ilayer,sample2img); |
| 343 | sampleReader.getExtent(layer_ulx,layer_uly,layer_lrx,layer_lry,ilayer); |
| 344 | |
| 345 | if(verbose_opt[0]) |
| 346 | std::cout<< std::setprecision(12) << "--ulx " << layer_ulx << " --uly " << layer_uly << " --lrx " << layer_lrx << " --lry " << layer_lry << std::endl; |
| 347 | if(verbose_opt[0]) |
| 348 | std::cout << "covered: " << allCovered_opt[0] << std::endl; |
| 349 | //check if rule contains allpoints |
| 350 | if(find(rule_opt.begin(),rule_opt.end(),"allpoints")!=rule_opt.end()){ |
| 351 | if(verbose_opt[0]>1) |
| 352 | std::cout << "we are in allpoints" << std::endl; |
| 353 | rule_opt.clear(); |
| 354 | rule_opt.push_back("allpoints"); |
| 355 | Jim sampleMask; |
| 356 | if(layer_ulx<this->getUlx()) |
| 357 | layer_ulx=this->getUlx(); |
| 358 | if(layer_uly>this->getUly()) |
| 359 | layer_uly=this->getUly(); |
| 360 | if(layer_lrx>this->getLrx()) |
| 361 | layer_lrx=this->getLrx(); |
| 362 | if(layer_lry<this->getLry()) |
| 363 | layer_lry=this->getLry(); |
| 364 | sampleMask.open(this->nrOfCol(),this->nrOfRow(),1,1,GDT_Byte); |
| 365 | double gt[6]; |
| 366 | this->getGeoTransform(gt); |
| 367 | sampleMask.setGeoTransform(gt); |
| 368 | sampleMask.setProjection(this->getProjection()); |
| 369 | double burnValue=1; |
| 370 | int startband=0; |
| 371 | int endband=nrOfBand()-1; |
| 372 | if(copyFields_opt.size()){ |
| 373 | if(verbose_opt[0]>1) |
| 374 | std::cout << "copy: " << copyFields_opt[0] << std::endl; |
| 375 | burnValue=0; |
| 376 | std::vector<std::string> eoption; |
| 377 | std::ostringstream eostream; |
| 378 | eostream << "ATTRIBUTE=" << copyFields_opt[0]; |
| 379 | eoption.push_back(eostream.str()); |
| 380 | sampleMask.d_rasterizeBuf(sampleReader,burnValue,eoption); |
| 381 | if(verbose_opt[0]>1) |
| 382 | std::cout << "sampleMask max: " << sampleMask.getMax() << std::endl << std::flush; |
| 383 | if(class_opt.empty()){ |
| 384 | //todo: get list of unique values in field copy |
| 385 | std::vector<OGRFieldDefn*> fields; |
| 386 | size_t fieldindex; |
| 387 | sampleReader.getFields(fields,ilayer); |
| 388 | for(unsigned int ifield=0;ifield<fields.size();++ifield){ |
| 389 | if(fields[ifield]->GetNameRef()==copyFields_opt[0]) |
| 390 | fieldindex=ifield; |
| 391 | } |
| 392 | std::set<int> copySet;//contains unique values in field copyFields_opt[0] |
| 393 | for(size_t ifeature = 0; ifeature < sampleReader.getFeatureCount(ilayer); ++ifeature) { |
| 394 | OGRFeature *thisFeature=sampleReader.getFeatureRef(ifeature,ilayer); |
| 395 | if(!thisFeature) |
| 396 | continue; |
| 397 | copySet.insert(thisFeature->GetFieldAsInteger(fieldindex)); |
| 398 | } |
| 399 | app.clearOption("class"); |
| 400 | std::set<int>::iterator setit; |
| 401 | for(setit=copySet.begin();setit!=copySet.end();++setit) |
| 402 | app.pushLongOption("class",*setit); |
| 403 | } |
| 404 | } |
| 405 | else{ |
| 406 | if(label_opt.size()) |
| 407 | burnValue=label_opt[0]; |
| 408 | sampleMask.d_rasterizeBuf(sampleReader,burnValue); |
| 409 | app.setLongOption("class",burnValue); |
| 410 | } |
| 411 | if(verbose_opt[0]>1) |
| 412 | std::cout << "calling extractImg" << std::endl; |
| 413 | // if(class_opt.empty()){ |
| 414 | // string errorstring="Error: classes to extract must be set"; |
| 415 | // throw(errorstring); |
| 416 | // } |
| 417 | extractImg(sampleMask,ogrWriter, app); |
| 418 | if(verbose_opt[0]>1) |
| 419 | std::cout << "closing mask" << std::endl; |
| 420 | sampleMask.close(); |
| 421 | if(verbose_opt[0]>1) |
| 422 | std::cout << "return from extractOgr" << std::endl; |
| 423 | return(CE_None); |
| 424 | } |
| 425 | |
| 426 | //convert start and end band options to vector of band indexes |
| 427 | if(bstart_opt.size()){ |
| 428 | if(bend_opt.size()!=bstart_opt.size()){ |
| 429 | string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; |
| 430 | throw(errorstring); |
| 431 | } |
| 432 | band_opt.clear(); |
| 433 | for(int ipair=0;ipair<bstart_opt.size();++ipair){ |
| 434 | if(bend_opt[ipair]<=bstart_opt[ipair]){ |
| 435 | string errorstring="Error: index for end band must be smaller then start band"; |
| 436 | throw(errorstring); |
| 437 | } |
| 438 | for(int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) |
| 439 | band_opt.push_back(iband); |
| 440 | } |
| 441 | } |
| 442 | else if(band_opt.empty()){ |
| 443 | size_t iband=0; |
| 444 | while(band_opt.size()<nrOfBand()) |
| 445 | band_opt.push_back(iband++); |
| 446 | } |
| 447 | int nband=(band_opt.size()) ? band_opt.size() : this->nrOfBand(); |
| 448 | if(class_opt.size()){ |
| 449 | if(nband>1){ |
| 450 | cerr << "Warning: using only first band of multiband image" << endl; |
| 451 | nband=1; |
| 452 | band_opt.clear(); |
| 453 | band_opt.push_back(0); |
| 454 | bandNames_opt.retrieveOption(app); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if(verbose_opt[0]>1) |
| 459 | std::cout << "Number of bands in input image: " << this->nrOfBand() << std::endl; |
| 460 | |
| 461 | OGRFieldType fieldType; |
| 462 | int ogr_typecount=11;//hard coded for now! |
| 463 | if(verbose_opt[0]>1) |
| 464 | std::cout << "field and label types can be: "; |
| 465 | for(int iType = 0; iType < ogr_typecount; ++iType){ |
| 466 | if(verbose_opt[0]>1) |
| 467 | std::cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType); |
| 468 | if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 469 | && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType), |
| 470 | ftype_opt[0].c_str())) |
| 471 | fieldType=(OGRFieldType) iType; |
| 472 | } |
| 473 | switch( fieldType ){ |
| 474 | case OFTInteger: |
| 475 | case OFTReal: |
| 476 | case OFTRealList: |
| 477 | case OFTString: |
| 478 | if(verbose_opt[0]>1) |
| 479 | std::cout << std::endl << "field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl; |
| 480 | break; |
| 481 | default: |
| 482 | cerr << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported" << std::endl; |
| 483 | return(CE_Failure); |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | const char* pszMessage; |
| 488 | srand(time(NULL)); |
| 489 | |
| 490 | bool sampleIsRaster=false; |
| 491 | |
| 492 | VectorOgr sampleWriterOgr; |
| 493 | |
| 494 | Vector2d<int> maskBuffer; |
| 495 | |
| 496 | double vectords_ulx; |
| 497 | double vectords_uly; |
| 498 | double vectords_lrx; |
| 499 | double vectords_lry; |
| 500 | bool calculateSpatialStatistics=false; |
| 501 | |
| 502 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 503 | switch(ruleMap[rule_opt[irule]]){ |
| 504 | case(rule::point): |
| 505 | case(rule::centroid): |
| 506 | case(rule::allpoints): |
| 507 | break; |
| 508 | case(rule::proportion): |
| 509 | case(rule::count): |
| 510 | case(rule::custom): |
| 511 | case(rule::mode):{ |
| 512 | if(class_opt.empty()){ |
| 513 | int theBand=0; |
| 514 | double minValue=0; |
| 515 | double maxValue=0; |
| 516 | if(band_opt.size()) |
| 517 | theBand=band_opt[0]; |
| 518 | this->getMinMax(minValue,maxValue,theBand); |
| 519 | int nclass=maxValue-minValue+1; |
| 520 | if(nclass<0&&nclass<256){ |
| 521 | string errorString="Error: Could not automatically define classes, please set class option"; |
| 522 | throw(errorString); |
| 523 | } |
| 524 | for(int iclass=minValue;iclass<=maxValue;++iclass) |
| 525 | class_opt.push_back(iclass); |
| 526 | } |
| 527 | }//deliberate fall through: calculate spatial statistics for all non-point like rules |
| 528 | default: |
| 529 | calculateSpatialStatistics=true; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | //support multiple layers |
| 535 | // int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount(); |
| 536 | // if(layer_opt.empty()) |
| 537 | // layer_opt.push_back(std::string()); |
| 538 | // int nlayerRead=layer_opt.size(); |
| 539 | unsigned long int ntotalvalid=0; |
| 540 | |
| 541 | // if(verbose_opt[0]) |
| 542 | // std::cout << "opening " << sample_opt[0] << " layer " << layer_opt[0] << " for reading input vector dataset" << std::endl; |
| 543 | // sampleReaderOgr.open(sample_opt[0],layer_opt[0]); |
| 544 | // sampleReaderOgr.readFeatures();//now already done when opening |
| 545 | |
| 546 | // OGRLayer *readLayer=sampleReaderOgr.getLayer(); |
| 547 | |
| 548 | // double layer_ulx; |
| 549 | // double layer_uly; |
| 550 | // double layer_lrx; |
| 551 | // double layer_lry; |
| 552 | // if(verbose_opt[0]) |
| 553 | // std::cout << "getExtent " << std::endl; |
| 554 | // sampleReader.getExtent(layer_ulx,layer_uly,layer_lrx,layer_lry,sample2img); |
| 555 | |
| 556 | if(verbose_opt[0]) |
| 557 | std::cout<< std::setprecision(12) << "--ulx " << layer_ulx << " --uly " << layer_uly << " --lrx " << layer_lrx << " --lry " << layer_lry << std::endl; |
| 558 | bool hasCoverage=((layer_ulx >= this->getUlx())&&(layer_lrx <= this->getLrx())&&(layer_uly <= this->getUly())&&(layer_lry >= this->getLry())); |
| 559 | if(!hasCoverage){ |
| 560 | std::cerr << "Warning: raster dataset does not fully cover vector layer " << ilayer << endl; |
| 561 | if(verbose_opt[0]) |
| 562 | std::cerr << std::setprecision(12) << "--ulx " << getUlx() << " --uly " << getUly() << " --lrx " << getLrx() << " --lry " << getLry() << std::endl; |
| 563 | } |
| 564 | if(layer_ulx>this->getLrx()||layer_lrx<this->getUlx()||layer_lry>this->getUly()||layer_uly<this->getLry()){ |
| 565 | std::cerr << "Warning: raster dataset does not fully coverage of vector layer " << ilayer << endl; |
| 566 | string errorString="Error: no coverage for layer in raster dataset"; |
| 567 | throw(errorString); |
| 568 | } |
| 569 | |
| 570 | //align bounding box to input image |
| 571 | layer_ulx-=fmod(layer_ulx-this->getUlx(),this->getDeltaX()); |
| 572 | layer_lrx+=fmod(this->getLrx()-layer_lrx,this->getDeltaX()); |
| 573 | layer_uly+=fmod(this->getUly()-layer_uly,this->getDeltaY()); |
| 574 | layer_lry-=fmod(layer_lry-this->getLry(),this->getDeltaY()); |
| 575 | |
| 576 | if(verbose_opt[0]) |
| 577 | std::cout << "aligned layer coordinates: " << std::setprecision(12) << "--ulx " << layer_ulx << " --uly " << layer_uly << " --lrx " << layer_lrx << " --lry " << layer_lry << std::endl; |
| 578 | |
| 579 | //do not read outside input image |
| 580 | if(layer_ulx<this->getUlx()) |
| 581 | layer_ulx=this->getUlx(); |
| 582 | if(layer_lrx>this->getLrx()) |
| 583 | layer_lrx=this->getLrx(); |
| 584 | if(layer_uly>this->getUly()) |
| 585 | layer_uly=this->getUly(); |
| 586 | if(layer_lry<this->getLry()) |
| 587 | layer_lry=this->getLry(); |
| 588 | |
| 589 | if(verbose_opt[0]) |
| 590 | std::cout << "constrained layer coordinates: " << std::setprecision(12) << "--ulx " << layer_ulx << " --uly " << layer_uly << " --lrx " << layer_lrx << " --lry " << layer_lry << std::endl; |
| 591 | |
| 592 | //read entire block for coverage in memory |
| 593 | //todo: use different data types |
| 594 | vector< vector< Vector2d<float> > > readValuesReal(nplane); |
| 595 | vector< vector< Vector2d<int> > > readValuesInt(nplane); |
| 596 | |
| 597 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 598 | switch( fieldType ){ |
| 599 | case OFTInteger: |
| 600 | readValuesInt[iplane].resize(nband); |
| 601 | break; |
| 602 | case OFTReal: |
| 603 | readValuesReal[iplane].resize(nband); |
| 604 | default: |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | double layer_uli; |
| 609 | double layer_ulj; |
| 610 | double layer_lri; |
| 611 | double layer_lrj; |
| 612 | this->geo2image(layer_ulx,layer_uly,layer_uli,layer_ulj); |
| 613 | this->geo2image(layer_lrx,layer_lry,layer_lri,layer_lrj); |
| 614 | |
| 615 | if(verbose_opt[0]){ |
| 616 | std::cout << "layer image coordinates: " << std::setprecision(12) << "--uli " << layer_uli << " --ulj " << layer_ulj << " --lri " << layer_lri << " --lrj " << layer_lrj << std::endl; |
| 617 | std::cout << "reading layer geometry" << std::endl; |
| 618 | } |
| 619 | OGRwkbGeometryType layerGeometry=readLayer->GetLayerDefn()->GetGeomType(); |
| 620 | if(verbose_opt[0]) |
| 621 | std::cout << "layer geometry: " << OGRGeometryTypeToName(layerGeometry) << std::endl; |
| 622 | |
| 623 | if(layerGeometry==wkbPoint){ |
| 624 | if(calculateSpatialStatistics){ |
| 625 | if(buffer_opt.size()){ |
| 626 | if(buffer_opt[0]<getDeltaX()) |
| 627 | buffer_opt[0]=getDeltaX(); |
| 628 | } |
| 629 | else |
| 630 | buffer_opt.push_back(getDeltaX()); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | //extend bounding box with buffer |
| 635 | //todo: check if safety margin is needed? |
| 636 | if(buffer_opt.size()){ |
| 637 | //in pixels: |
| 638 | // layer_uli-=buffer_opt[0]; |
| 639 | // layer_ulj-=buffer_opt[0]; |
| 640 | // layer_lri+=buffer_opt[0]; |
| 641 | // layer_lrj+=buffer_opt[0]; |
| 642 | //in geometric units of raster dataset: |
| 643 | double bufferI=buffer_opt[0]/getDeltaX(); |
| 644 | double bufferJ=buffer_opt[0]/getDeltaY(); |
| 645 | layer_uli-=bufferI; |
| 646 | layer_ulj-=bufferJ; |
| 647 | layer_lri+=bufferI; |
| 648 | layer_lrj+=bufferJ; |
| 649 | } |
| 650 | |
| 651 | //we already checked there is coverage |
| 652 | layer_uli=(layer_uli<0)? 0 : static_cast<int>(layer_uli); |
| 653 | layer_ulj=(layer_ulj<0)? 0 : static_cast<int>(layer_ulj); |
| 654 | layer_lri=(layer_lri>=this->nrOfCol())? this->nrOfCol()-1 : static_cast<int>(layer_lri); |
| 655 | layer_lrj=(layer_lrj>=this->nrOfRow())? this->nrOfRow()-1 : static_cast<int>(layer_lrj); |
| 656 | |
| 657 | if(verbose_opt[0]) |
| 658 | std::cout << "constrained layer image coordinates: " << std::setprecision(12) << "--uli " << layer_uli << " --ulj " << layer_ulj << " --lri " << layer_lri << " --lrj " << layer_lrj << std::endl; |
| 659 | //todo: separate between case when data has been read already or opened with noRead true; |
| 660 | // if(m_data.size()){ |
| 661 | bool layerRead=false; |
| 662 | size_t maxIndexJ=0; |
| 663 | size_t maxIndexI=0; |
| 664 | if(getBlockSize()>=layer_lrj-layer_ulj){ |
| 665 | if(verbose_opt[0]) |
| 666 | std::cout << "blockSize " << getBlockSize() << " >= " << layer_lrj-layer_ulj << std::endl; |
| 667 | for(int iplane=0;iplane<nplane;++iplane){ |
| 668 | int thePlane=plane_opt[iplane]; |
| 669 | for(int iband=0;iband<nband;++iband){ |
| 670 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 671 | if(theBand<0){ |
| 672 | string errorString="Error: illegal band (must be positive and starting from 0)"; |
| 673 | throw(errorString); |
| 674 | } |
| 675 | if(theBand>=this->nrOfBand()){ |
| 676 | string errorString="Error: illegal band (must be lower than number of bands in input raster dataset)"; |
| 677 | throw(errorString); |
| 678 | } |
| 679 | if(verbose_opt[0]) |
| 680 | cout << "reading image band " << theBand << " block rows " << layer_ulj << "-" << layer_lrj << ", cols " << layer_uli << "-" << layer_lri << endl; |
| 681 | switch( fieldType ){ |
| 682 | case OFTInteger: |
| 683 | this->readDataBlock3D(readValuesInt[iplane][iband],layer_uli,layer_lri,layer_ulj,layer_lrj,thePlane,theBand); |
| 684 | break; |
| 685 | case OFTReal: |
| 686 | default: |
| 687 | this->readDataBlock3D(readValuesReal[iplane][iband],layer_uli,layer_lri,layer_ulj,layer_lrj,thePlane,theBand); |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | layerRead=true; |
| 692 | } |
| 693 | } |
| 694 | else{ |
| 695 | if(verbose_opt[0]) |
| 696 | std::cout << "blockSize " << getBlockSize() << " < " << layer_lrj-layer_ulj << std::endl; |
| 697 | } |
| 698 | if(maskReader.isInit()){//mask already read when random is set |
| 699 | if(maskReader.covers(layer_ulx,layer_uly,layer_lrx,layer_lry,"ALL_COVERED")) |
| 700 | maskReader.readDataBlock(maskBuffer,layer_uli,layer_lri,layer_ulj,layer_lrj,mskband_opt[0]); |
| 701 | else{ |
| 702 | string errorString="Error: mask does not entirely cover the geographical layer boundaries"; |
| 703 | throw(errorString); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | // float theThreshold=(threshold_opt.size()==layer_opt.size())? threshold_opt[layerIndex]: threshold_opt[0]; |
| 709 | float theThreshold=threshold_opt[0]; |
| 710 | |
| 711 | bool createPolygon=true; |
| 712 | if(find(rule_opt.begin(),rule_opt.end(),"allpoints")!=rule_opt.end()) |
| 713 | createPolygon=false; |
| 714 | |
| 715 | // if(!ogrWriter.getDataset()){ |
| 716 | // OGRLayer *writeLayer; |
| 717 | try{ |
| 718 | if(initWriter){ |
| 719 | if(verbose_opt[0]) |
| 720 | std::cout << "initWriter is true" << std::endl; |
| 721 | if(createPolygon){ |
| 722 | //create polygon |
| 723 | if(verbose_opt[0]) |
| 724 | std::cout << "create polygons" << std::endl; |
| 725 | if(verbose_opt[0]) |
| 726 | std::cout << "open ogrWriter for polygons (1)" << std::endl; |
| 727 | ogrWriter.pushLayer(readLayer->GetName(),readLayer->GetSpatialRef(),wkbPolygon,papszOptions); |
| 728 | // ostringstream fs; |
| 729 | // fs << "push layer to ogrWriter with polygons failed "; |
| 730 | // fs << "layer name: "<< readLayer->GetName() << std::endl; |
| 731 | // throw(fs.str()); |
| 732 | if(verbose_opt[0]) |
| 733 | std::cout << "pushed layer " << readLayer->GetName() << std::endl; |
| 734 | } |
| 735 | else{ |
| 736 | if(verbose_opt[0]) |
| 737 | std::cout << "create points in layer " << readLayer->GetName() << std::endl; |
| 738 | if(verbose_opt[0]) |
| 739 | std::cout << "open ogrWriter for points (1)" << std::endl; |
| 740 | ogrWriter.pushLayer(readLayer->GetName(),readLayer->GetSpatialRef(),wkbPoint,papszOptions); |
| 741 | // ostringstream fs; |
| 742 | // fs << "push layer to ogrWriter with points failed "; |
| 743 | // fs << "layer name: "<< readLayer->GetName() << std::endl; |
| 744 | // throw(fs.str()); |
| 745 | } |
| 746 | if(verbose_opt[0]){ |
| 747 | std::cout << "ogrWriter opened" << std::endl; |
| 748 | // writeLayer=ogrWriter.createLayer(readLayer->GetName(), this->getProjection(), wkbPoint, papszOptions); |
| 749 | } |
| 750 | if(verbose_opt[0]){ |
| 751 | std::cout << "copy fields" << std::endl; |
| 752 | for(size_t ifield=0;ifield<copyFields_opt.size();++ifield) |
| 753 | std::cout << "field " << ifield << ": " << copyFields_opt[ifield] << std::endl; |
| 754 | } |
| 755 | ogrWriter.copyFields(sampleReader,copyFields_opt,ilayer,ilayer); |
| 756 | |
| 757 | if(verbose_opt[0]) |
| 758 | std::cout << "create new fields" << std::endl; |
| 759 | if(label_opt.size()){ |
| 760 | if(verbose_opt[0]) |
| 761 | std::cout << "create label" << std::endl; |
| 762 | ogrWriter.createField("label",OFTInteger,ilayer); |
| 763 | } |
| 764 | if(fid_opt.size()){ |
| 765 | if(verbose_opt[0]) |
| 766 | std::cout << "create fid" << std::endl; |
| 767 | if(ogrWriter.createField(fid_opt[0],OFTInteger64,ilayer)!=OGRERR_NONE){ |
| 768 | std::string errorString="Error: could not create fid"; |
| 769 | throw(errorString); |
| 770 | } |
| 771 | if(verbose_opt[0]) |
| 772 | std::cout << "fid has been created" << std::endl; |
| 773 | } |
| 774 | |
| 775 | if(verbose_opt[0]) |
| 776 | std::cout << "checking rules" << std::endl; |
| 777 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 778 | for(int iplane=0;iplane<nplane;++iplane){ |
| 779 | int thePlane=plane_opt[iplane]; |
| 780 | for(int iband=0;iband<nband;++iband){ |
| 781 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 782 | ostringstream fs; |
| 783 | if(bandNames_opt.size()){ |
| 784 | if(rule_opt.size()>1) |
| 785 | fs << fieldMap[rule_opt[irule]]; |
| 786 | if(planeNames_opt.size()) |
| 787 | fs << planeNames_opt[iplane]; |
| 788 | fs << bandNames_opt[iband]; |
| 789 | } |
| 790 | else{ |
| 791 | if(rule_opt.size()>1||nband==1) |
| 792 | fs << fieldMap[rule_opt[irule]]; |
| 793 | if(planeNames_opt.size()) |
| 794 | fs << planeNames_opt[iplane]; |
| 795 | if(nband>1) |
| 796 | fs << "b" << theBand; |
| 797 | } |
| 798 | switch(ruleMap[rule_opt[irule]]){ |
| 799 | case(rule::proportion): |
| 800 | case(rule::count):{//count for each class |
| 801 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 802 | ostringstream fsclass; |
| 803 | fsclass << fs.str() << "class" << class_opt[iclass]; |
| 804 | ogrWriter.createField(fsclass.str(),fieldType,ilayer); |
| 805 | } |
| 806 | break; |
| 807 | } |
| 808 | case(rule::percentile):{//for each percentile |
| 809 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 810 | ostringstream fsperc; |
| 811 | fsperc << fs.str() << percentile_opt[iperc]; |
| 812 | ogrWriter.createField(fsperc.str(),fieldType,ilayer); |
| 813 | } |
| 814 | break; |
| 815 | } |
| 816 | default: |
| 817 | ogrWriter.createField(fs.str(),fieldType,ilayer); |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | if(verbose_opt[0]){ |
| 824 | std::vector<std::string> fieldnames; |
| 825 | ogrWriter.getFieldNames(fieldnames,ilayer); |
| 826 | std::cout << "field names of ogrWriter" << std::endl; |
| 827 | for(size_t ifield=0;ifield<fieldnames.size();++ifield) |
| 828 | std::cout << fieldnames[ifield] << std::endl; |
| 829 | std::cout << endl; |
| 830 | std::cout << "end of initWriter" << std::endl; |
| 831 | } |
| 832 | } |
| 833 | if(verbose_opt[0]) |
| 834 | std::cout << "after initWriter" << std::endl; |
| 835 | } |
| 836 | catch(std::string errorString){ |
| 837 | std::cerr << errorString << "failed to initWriter" << std::endl; |
| 838 | throw; |
| 839 | } |
| 840 | // OGRFeature *readFeature; |
| 841 | // unsigned long int ifeature=0; |
| 842 | // unsigned long int nfeatureLayer=sampleReaderOgr.getFeatureCount(); |
| 843 | unsigned long int nfeatureLayer=sampleReader.getFeatureCount(ilayer); |
| 844 | if(verbose_opt[0]) |
| 845 | std::cout << "nfeatureLayer: " << nfeatureLayer << std::endl; |
| 846 | unsigned long int ntotalvalidLayer=0; |
| 847 | |
| 848 | ogrWriter.resize(sampleReader.getFeatureCount(ilayer),ilayer); |
| 849 | // if(verbose_opt[0]) |
| 850 | // std::cout << "start processing " << sampleReader.getFeatureCount(ilayer) << " features" << std::endl; |
| 851 | // progress=0; |
| 852 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 853 | // readLayer->ResetReading(); |
| 854 | // while( (readFeature = readLayer->GetNextFeature()) != NULL ){ |
| 855 | |
| 856 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 857 | if(verbose_opt[0]) |
| 858 | std::cout << "process in parallel" << std::endl; |
| 859 | #pragma omp parallel for ordered shared(ntotalvalidLayer, ntotalvalid, sampleReader, ogrWriter) |
| 860 | #else |
| 861 | #endif |
| 862 | for(unsigned long int ifeature=0;ifeature<sampleReader.getFeatureCount(ilayer);++ifeature){ |
| 863 | // OGRFeature *readFeature=sampleReaderOgr.getFeatureRef(ifeature); |
| 864 | OGRFeature *readFeature=sampleReader.cloneFeature(ifeature,ilayer); |
| 865 | bool validFeature=false; |
| 866 | if(verbose_opt[0]>2) |
| 867 | std::cout << "reading feature " << readFeature->GetFID() << std::endl; |
| 868 | if(theThreshold>0){//percentual value |
| 869 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 870 | p*=100.0; |
| 871 | if(p>theThreshold){ |
| 872 | continue;//do not select for now, go to next feature |
| 873 | } |
| 874 | } |
| 875 | else{//absolute value |
| 876 | if(threshold_opt.size()==sampleReader.getLayerCount()){ |
| 877 | if(ntotalvalidLayer>=-theThreshold){ |
| 878 | continue;//do not select any more pixels, go to next column feature |
| 879 | } |
| 880 | } |
| 881 | else{ |
| 882 | if(ntotalvalid>=-theThreshold){ |
| 883 | continue;//do not select any more pixels, go to next column feature |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | if(verbose_opt[0]>2) |
| 888 | std::cout << "processing feature " << readFeature->GetFID() << std::endl; |
| 889 | //get x and y from readFeature |
| 890 | // double x,y; |
| 891 | OGRGeometry *readGeometry; |
| 892 | readGeometry = readFeature->GetGeometryRef(); |
| 893 | if(!readGeometry){ |
| 894 | std::string errorString="Error: geometry is empty"; |
| 895 | throw(errorString); |
| 896 | } |
| 897 | //create buffer |
| 898 | OGRGeometry* poGeometry=readGeometry->clone(); |
| 899 | // if(!VectorOgr::transform(poGeometry,sample2img)){ |
| 900 | // std::string errorString="Error: coordinate transform not successful"; |
| 901 | // throw(errorString); |
| 902 | // } |
| 903 | if(buffer_opt.size()){ |
| 904 | if(verbose_opt[0]>2){ |
| 905 | std::cout << "before buffer: " << std::endl; |
| 906 | std::cout << poGeometry->exportToJson() << std::endl; |
| 907 | } |
| 908 | poGeometry=poGeometry->Buffer(buffer_opt[0]); |
| 909 | if(verbose_opt[0]>2){ |
| 910 | std::cout << "after buffer: " << std::endl; |
| 911 | std::cout << poGeometry->exportToJson() << std::endl; |
| 912 | } |
| 913 | } |
| 914 | if(!poGeometry){ |
| 915 | std::string errorString="Error: po geometry is empty"; |
| 916 | throw(errorString); |
| 917 | } |
| 918 | try{ |
| 919 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){ |
| 920 | if(verbose_opt[0]>2) |
| 921 | std::cout << "poGeometry is wkbPoint" << std::endl; |
| 922 | //todo: handle case if m_data is empty |
| 923 | OGRPoint readPoint = *((OGRPoint *) poGeometry);//readPoint is in SRS of raster |
| 924 | |
| 925 | double i_centre,j_centre; |
| 926 | this->geo2image(readPoint.getX(),readPoint.getY(),i_centre,j_centre); |
| 927 | //nearest neighbour |
| 928 | j_centre=static_cast<int>(j_centre); |
| 929 | i_centre=static_cast<int>(i_centre); |
| 930 | |
| 931 | double uli=i_centre;//-buffer_opt[0]; |
| 932 | double ulj=j_centre;//-buffer_opt[0]; |
| 933 | double lri=i_centre;//+buffer_opt[0]; |
| 934 | double lrj=j_centre;//+buffer_opt[0]; |
| 935 | |
| 936 | //nearest neighbour |
| 937 | ulj=static_cast<int>(ulj); |
| 938 | uli=static_cast<int>(uli); |
| 939 | lrj=static_cast<int>(lrj); |
| 940 | lri=static_cast<int>(lri); |
| 941 | |
| 942 | //check if j is out of bounds |
| 943 | // if(static_cast<int>(ulj)<0||static_cast<int>(ulj)>=this->nrOfRow()) |
| 944 | if(static_cast<int>(ulj)<0||static_cast<int>(ulj)>=layer_lrj) |
| 945 | continue; |
| 946 | //check if j is out of bounds |
| 947 | // if(static_cast<int>(uli)<0||static_cast<int>(lri)>=this->nrOfCol()) |
| 948 | if(static_cast<int>(uli)<0||static_cast<int>(lri)>=layer_lri) |
| 949 | continue; |
| 950 | |
| 951 | OGRPoint ulPoint,urPoint,llPoint,lrPoint; |
| 952 | OGRPolygon writePolygon; |
| 953 | OGRPoint writePoint; |
| 954 | OGRLinearRing writeRing; |
| 955 | OGRFeature *writePolygonFeature; |
| 956 | |
| 957 | int nPointPolygon=0; |
| 958 | if(createPolygon){ |
| 959 | if(disc_opt[0]&&buffer_opt.size()){ |
| 960 | //todo: check if this still makes sense with new meaning of buffer |
| 961 | double radius=buffer_opt[0]*sqrt(this->getDeltaX()*this->getDeltaY()); |
| 962 | unsigned short nstep = 25; |
| 963 | for(int i=0;i<nstep;++i){ |
| 964 | OGRPoint aPoint(readPoint); |
| 965 | // VectorOgr::transform(&aPoint,sample2img); |
| 966 | aPoint.setX(aPoint.getX()+this->getDeltaX()/2.0+radius*cos(2*PI*i/nstep)); |
| 967 | aPoint.setY(aPoint.getY()-this->getDeltaY()/2.0+radius*sin(2*PI*i/nstep)); |
| 968 | writeRing.addPoint(&aPoint); |
| 969 | } |
| 970 | writePolygon.addRing(&writeRing); |
| 971 | writePolygon.closeRings(); |
| 972 | } |
| 973 | else{ |
| 974 | double ulx,uly,lrx,lry; |
| 975 | this->image2geo(uli,ulj,ulx,uly); |
| 976 | this->image2geo(lri,lrj,lrx,lry); |
| 977 | ulPoint.setX(ulx-this->getDeltaX()/2.0); |
| 978 | ulPoint.setY(uly+this->getDeltaY()/2.0); |
| 979 | lrPoint.setX(lrx+this->getDeltaX()/2.0); |
| 980 | lrPoint.setY(lry-this->getDeltaY()/2.0); |
| 981 | urPoint.setX(lrx+this->getDeltaX()/2.0); |
| 982 | urPoint.setY(uly+this->getDeltaY()/2.0); |
| 983 | llPoint.setX(ulx-this->getDeltaX()/2.0); |
| 984 | llPoint.setY(lry-this->getDeltaY()/2.0); |
| 985 | |
| 986 | writeRing.addPoint(&ulPoint); |
| 987 | writeRing.addPoint(&urPoint); |
| 988 | writeRing.addPoint(&lrPoint); |
| 989 | writeRing.addPoint(&llPoint); |
| 990 | writePolygon.addRing(&writeRing); |
| 991 | writePolygon.closeRings(); |
| 992 | } |
| 993 | //coordinate transform |
| 994 | // if(!VectorOgr::transform(&writePolygon,img2sample)){ |
| 995 | // std::string errorString="Error: coordinate transform img2sample not successful"; |
| 996 | // throw(errorString); |
| 997 | // } |
| 998 | writePolygonFeature = ogrWriter.createFeature(ilayer); |
| 999 | if(verbose_opt[0]>2) |
| 1000 | std::cout << "geometry of feature (1): " << writePolygonFeature->GetGeometryRef()->getGeometryName() << std::endl; |
| 1001 | // writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 1002 | if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 1003 | cerr << "writing feature failed" << std::endl; |
| 1004 | writePolygonFeature->SetGeometry(&writePolygon); |
| 1005 | if(verbose_opt[0]>2) |
| 1006 | std::cout << "geometry of feature (2): " << writePolygonFeature->GetGeometryRef()->getGeometryName() << std::endl; |
| 1007 | if(verbose_opt[0]>1) |
| 1008 | std::cout << "copying new fields write polygon " << std::endl; |
| 1009 | if(verbose_opt[0]>1) |
| 1010 | std::cout << "write feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl; |
| 1011 | if(verbose_opt[0]>2) |
| 1012 | std::cout << "geometry of feature (3): " << writePolygonFeature->GetGeometryRef()->getGeometryName() << std::endl; |
| 1013 | |
| 1014 | OGRPoint readPoint;//this readPoint is in SRS of vector layer |
| 1015 | if(find(rule_opt.begin(),rule_opt.end(),"centroid")!=rule_opt.end()){ |
| 1016 | if(verbose_opt[0]>1) |
| 1017 | std::cout << "get centroid" << std::endl; |
| 1018 | writePolygon.Centroid(&readPoint); |
| 1019 | double i,j; |
| 1020 | // this->geo2image(readPoint.getX(),readPoint.getY(),i,j,sample2img); |
| 1021 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 1022 | if(verbose_opt[0]>1) |
| 1023 | std::cout << "centroid in vector SRS: " << readPoint.getX() << ", " << readPoint.getY() << std::endl; |
| 1024 | int indexJ=static_cast<int>(j-layer_ulj); |
| 1025 | int indexI=static_cast<int>(i-layer_uli); |
| 1026 | bool valid=true; |
| 1027 | valid=valid&&(indexJ>=0); |
| 1028 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 1029 | valid=valid&&(indexI>=0); |
| 1030 | // valid=valid&&(indexI<this->nrOfCol()); |
| 1031 | valid=valid&&(j<=layer_lrj); |
| 1032 | valid=valid&&(i<=layer_lri); |
| 1033 | |
| 1034 | if(valid){ |
| 1035 | if(maskReader.isInit()){ |
| 1036 | double maskI,maskJ; |
| 1037 | // maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ,sample2img); |
| 1038 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 1039 | maskI=static_cast<unsigned int>(maskI); |
| 1040 | maskJ=static_cast<unsigned int>(maskJ); |
| 1041 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 1042 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 1043 | if(maskBuffer[maskJ][maskI]==msknodata_opt[ivalue]){ |
| 1044 | valid=false; |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1052 | if(valid){ |
| 1053 | if(srcnodata_opt.empty()) |
| 1054 | validFeature=true; |
| 1055 | else{ |
| 1056 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 1057 | switch( fieldType ){ |
| 1058 | case OFTInteger:{ |
| 1059 | int value; |
| 1060 | value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 1061 | if(value==srcnodata_opt[vband]) |
| 1062 | valid=false; |
| 1063 | break; |
| 1064 | } |
| 1065 | case OFTReal:{ |
| 1066 | double value; |
| 1067 | value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 1068 | if(value==srcnodata_opt[vband]) |
| 1069 | valid=false; |
| 1070 | break; |
| 1071 | } |
| 1072 | } |
| 1073 | if(!valid) |
| 1074 | continue; |
| 1075 | else |
| 1076 | validFeature=true; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | // if(valid){ |
| 1081 | if(validFeature){//replace valid with validFeature! |
| 1082 | if(label_opt.size()) |
| 1083 | writePolygonFeature->SetField("label",label_opt[0]); |
| 1084 | if(fid_opt.size()) |
| 1085 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1086 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1087 | for(int iband=0;iband<nband;++iband){ |
| 1088 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1089 | //write fields for point on surface and centroid |
| 1090 | string fieldname; |
| 1091 | ostringstream fs; |
| 1092 | if(bandNames_opt.size()){ |
| 1093 | if(rule_opt.size()>1) |
| 1094 | fs << fieldMap["centroid"]; |
| 1095 | if(planeNames_opt.size()) |
| 1096 | fs << planeNames_opt[iplane]; |
| 1097 | fs << bandNames_opt[iband]; |
| 1098 | } |
| 1099 | else{ |
| 1100 | if(rule_opt.size()>1||nband==1) |
| 1101 | fs << fieldMap["centroid"]; |
| 1102 | if(planeNames_opt.size()) |
| 1103 | fs << planeNames_opt[iplane]; |
| 1104 | if(nband>1) |
| 1105 | fs << "b" << theBand; |
| 1106 | } |
| 1107 | fieldname=fs.str(); |
| 1108 | switch( fieldType ){ |
| 1109 | case OFTInteger: |
| 1110 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iplane][iband])[indexJ])[indexI])); |
| 1111 | break; |
| 1112 | case OFTReal: |
| 1113 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iplane][iband])[indexJ])[indexI]); |
| 1114 | break; |
| 1115 | default://not supported |
| 1116 | std::string errorString="field type not supported"; |
| 1117 | throw(errorString); |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | }//if centroid |
| 1125 | if(find(rule_opt.begin(),rule_opt.end(),"point")!=rule_opt.end()){ |
| 1126 | if(writePolygon.PointOnSurface(&readPoint)!=OGRERR_NONE) |
| 1127 | writePolygon.Centroid(&readPoint); |
| 1128 | double i,j; |
| 1129 | // this->geo2image(readPoint.getX(),readPoint.getY(),i,j,sample2img); |
| 1130 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 1131 | int indexJ=static_cast<int>(j-layer_ulj); |
| 1132 | int indexI=static_cast<int>(i-layer_uli); |
| 1133 | bool valid=true; |
| 1134 | valid=valid&&(indexJ>=0); |
| 1135 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 1136 | valid=valid&&(indexI>=0); |
| 1137 | // valid=valid&&(indexI<this->nrOfCol()); |
| 1138 | valid=valid&&(j<=layer_lrj); |
| 1139 | valid=valid&&(i<=layer_lri); |
| 1140 | |
| 1141 | if(valid){ |
| 1142 | if(maskReader.isInit()){ |
| 1143 | double maskI,maskJ; |
| 1144 | // maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ,sample2img); |
| 1145 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 1146 | maskI=static_cast<unsigned int>(maskI); |
| 1147 | maskJ=static_cast<unsigned int>(maskJ); |
| 1148 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 1149 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 1150 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 1151 | valid=false; |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | if(valid){ |
| 1159 | if(srcnodata_opt.empty()) |
| 1160 | validFeature=true; |
| 1161 | else{ |
| 1162 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1163 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 1164 | switch( fieldType ){ |
| 1165 | case OFTInteger:{ |
| 1166 | int value; |
| 1167 | value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 1168 | if(value==srcnodata_opt[vband]) |
| 1169 | valid=false; |
| 1170 | break; |
| 1171 | } |
| 1172 | case OFTReal:{ |
| 1173 | double value; |
| 1174 | value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 1175 | if(value==srcnodata_opt[vband]) |
| 1176 | valid=false; |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | if(!valid) |
| 1181 | continue; |
| 1182 | else |
| 1183 | validFeature=true; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | // if(valid){ |
| 1190 | if(validFeature){//replaced valid with validFeature! |
| 1191 | if(label_opt.size()) |
| 1192 | writePolygonFeature->SetField("label",label_opt[0]); |
| 1193 | if(fid_opt.size()) |
| 1194 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1195 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1196 | for(int iband=0;iband<nband;++iband){ |
| 1197 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1198 | //write fields for point on surface and centroid |
| 1199 | string fieldname; |
| 1200 | ostringstream fs; |
| 1201 | if(bandNames_opt.size()){ |
| 1202 | if(rule_opt.size()>1) |
| 1203 | fs << fieldMap["point"]; |
| 1204 | if(planeNames_opt.size()) |
| 1205 | fs << planeNames_opt[iplane]; |
| 1206 | fs << bandNames_opt[iband]; |
| 1207 | } |
| 1208 | else{ |
| 1209 | if(rule_opt.size()>1||nband==1) |
| 1210 | fs << fieldMap["point"]; |
| 1211 | if(planeNames_opt.size()) |
| 1212 | fs << planeNames_opt[iplane]; |
| 1213 | if(nband>1) |
| 1214 | fs << "b" << theBand; |
| 1215 | } |
| 1216 | fieldname=fs.str(); |
| 1217 | switch( fieldType ){ |
| 1218 | case OFTInteger: |
| 1219 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iplane][iband])[indexJ])[indexI])); |
| 1220 | break; |
| 1221 | case OFTReal: |
| 1222 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iplane][iband])[indexJ])[indexI]); |
| 1223 | break; |
| 1224 | default://not supported |
| 1225 | std::string errorString="field type not supported"; |
| 1226 | throw(errorString); |
| 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | }//if point |
| 1233 | }//if createPolygon |
| 1234 | if(calculateSpatialStatistics||!createPolygon){ |
| 1235 | vector< Vector2d<double> > polyValues(nrOfPlane()); |
| 1236 | vector< vector<double> > polyClassValues(nrOfPlane()); |
| 1237 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1238 | if(class_opt.size()){ |
| 1239 | polyClassValues[iplane].resize(class_opt.size()); |
| 1240 | //initialize |
| 1241 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 1242 | polyClassValues[iplane][iclass]=0; |
| 1243 | } |
| 1244 | else |
| 1245 | polyValues[iplane].resize(nband); |
| 1246 | } |
| 1247 | |
| 1248 | OGRPoint thePoint; |
| 1249 | for(int j=ulj;j<=lrj;++j){ |
| 1250 | for(int i=uli;i<=lri;++i){ |
| 1251 | // //check if within raster image |
| 1252 | // if(i<0||i>=this->nrOfCol()) |
| 1253 | // continue; |
| 1254 | // if(j<0||j>=this->nrOfRow()) |
| 1255 | // continue; |
| 1256 | if(j<0){ |
| 1257 | // std::cerr << "Warning: j is " << j << ", setting to 0" << std::endl; |
| 1258 | j=0; |
| 1259 | } |
| 1260 | if(j>=layer_lrj){ |
| 1261 | // std::cerr << "Warning: j is " << j << " and out of reading block, skipping" << std::endl; |
| 1262 | continue; |
| 1263 | } |
| 1264 | if(i<0){ |
| 1265 | // std::cerr << "Warning: i is " << i << ", setting to 0" << std::endl; |
| 1266 | i=0; |
| 1267 | } |
| 1268 | if(i>=layer_lri){ |
| 1269 | // std::cerr << "Warning: i is " << i << " and out of reading block, skipping" << std::endl; |
| 1270 | continue; |
| 1271 | } |
| 1272 | int indexJ=j-layer_ulj; |
| 1273 | int indexI=i-layer_uli; |
| 1274 | if(indexJ<0) |
| 1275 | continue; |
| 1276 | if(indexI<0) |
| 1277 | continue; |
| 1278 | if(indexJ>=this->nrOfRow()) |
| 1279 | continue; |
| 1280 | if(indexI>=this->nrOfCol()) |
| 1281 | continue; |
| 1282 | |
| 1283 | double theX=0; |
| 1284 | double theY=0; |
| 1285 | this->image2geo(i,j,theX,theY); |
| 1286 | thePoint.setX(theX); |
| 1287 | thePoint.setY(theY); |
| 1288 | //todo: check if this still makes sense with new meaning of buffer |
| 1289 | if(disc_opt[0]&&buffer_opt.size()){ |
| 1290 | if(buffer_opt[0]>0){ |
| 1291 | double radius=buffer_opt[0]*sqrt(this->getDeltaX()*this->getDeltaY()); |
| 1292 | if((theX-readPoint.getX())*(theX-readPoint.getX())+(theY-readPoint.getY())*(theY-readPoint.getY())>radius*radius) |
| 1293 | continue; |
| 1294 | } |
| 1295 | } |
| 1296 | bool valid=true; |
| 1297 | |
| 1298 | if(maskReader.isInit()){ |
| 1299 | double maskI,maskJ; |
| 1300 | // maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ,sample2img); |
| 1301 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 1302 | maskI=static_cast<unsigned int>(maskI); |
| 1303 | maskJ=static_cast<unsigned int>(maskJ); |
| 1304 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 1305 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 1306 | // if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 1307 | if(maskBuffer[maskJ][maskI]==msknodata_opt[ivalue]){ |
| 1308 | valid=false; |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1315 | if(srcnodata_opt.size()){ |
| 1316 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 1317 | switch( fieldType ){ |
| 1318 | case OFTInteger:{ |
| 1319 | int value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 1320 | if(value==srcnodata_opt[vband]){ |
| 1321 | valid=false; |
| 1322 | } |
| 1323 | break; |
| 1324 | } |
| 1325 | default:{ |
| 1326 | float value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 1327 | if(value==srcnodata_opt[vband]){ |
| 1328 | valid=false; |
| 1329 | } |
| 1330 | break; |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | if(!valid){ |
| 1336 | continue; |
| 1337 | } |
| 1338 | else |
| 1339 | validFeature=true; |
| 1340 | } |
| 1341 | |
| 1342 | ++nPointPolygon; |
| 1343 | OGRFeature *writePointFeature; |
| 1344 | // if(valid&&!createPolygon){//write all points |
| 1345 | if(validFeature&&!createPolygon){//write all points: replaced valid with validFeature! |
| 1346 | if(polythreshold_opt.size()){ |
| 1347 | if(polythreshold_opt[0]>0){ |
| 1348 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 1349 | p*=100.0; |
| 1350 | if(p>polythreshold_opt[0]) |
| 1351 | continue;//do not select for now, go to next feature |
| 1352 | } |
| 1353 | else if(nPointPolygon>-polythreshold_opt[0]) |
| 1354 | continue; |
| 1355 | } |
| 1356 | //create feature |
| 1357 | writePointFeature = OGRFeature::CreateFeature(ogrWriter.getLayer(ilayer)->GetLayerDefn()); |
| 1358 | // writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 1359 | if(verbose_opt[0]>1) |
| 1360 | std::cout << "copying fields from point feature " << std::endl; |
| 1361 | if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 1362 | cerr << "writing feature failed" << std::endl; |
| 1363 | if(verbose_opt[0]>1) |
| 1364 | std::cout << "set geometry as point " << std::endl; |
| 1365 | //coordinate transform |
| 1366 | // if(!VectorOgr::transform(&thePoint,img2sample)){ |
| 1367 | // std::string errorString="Error: coordinate transform img2sample not successful"; |
| 1368 | // throw(errorString); |
| 1369 | // } |
| 1370 | writePointFeature->SetGeometry(&thePoint); |
| 1371 | assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint); |
| 1372 | if(verbose_opt[0]>1){ |
| 1373 | std::cout << "write feature has " << writePointFeature->GetFieldCount() << " fields:" << std::endl; |
| 1374 | for(int iField=0;iField<writePointFeature->GetFieldCount();++iField){ |
| 1375 | std::string fieldname=ogrWriter.getLayer(ilayer)->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 1376 | // std::string fieldname=writeLayer->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 1377 | cout << fieldname << endl; |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | // if(valid&&class_opt.size()){ |
| 1382 | // short value=0; |
| 1383 | // switch( fieldType ){ |
| 1384 | // case OFTInteger: |
| 1385 | // value=((readValuesInt[0])[indexJ])[indexI]; |
| 1386 | // break; |
| 1387 | // case OFTReal: |
| 1388 | // value=((readValuesReal[0])[indexJ])[indexI]; |
| 1389 | // break; |
| 1390 | // } |
| 1391 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 1392 | // if(value==class_opt[iclass]) |
| 1393 | // polyClassValues[iclass]+=1; |
| 1394 | // } |
| 1395 | // } |
| 1396 | // if(valid){ |
| 1397 | if(validFeature){//replaced valid with validFeature... |
| 1398 | if(!createPolygon&&label_opt.size()) |
| 1399 | writePointFeature->SetField("label",label_opt[0]); |
| 1400 | if(!createPolygon&&fid_opt.size()) |
| 1401 | writePointFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1402 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1403 | for(int iband=0;iband<nband;++iband){ |
| 1404 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1405 | double value=0; |
| 1406 | switch( fieldType ){ |
| 1407 | case OFTInteger: |
| 1408 | value=((readValuesInt[iplane][iband])[indexJ])[indexI]; |
| 1409 | break; |
| 1410 | case OFTReal: |
| 1411 | value=((readValuesReal[iplane][iband])[indexJ])[indexI]; |
| 1412 | break; |
| 1413 | } |
| 1414 | if(!iband&&class_opt.size()){ |
| 1415 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 1416 | if(value==class_opt[iclass]) |
| 1417 | polyClassValues[iplane][iclass]+=1; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if(verbose_opt[0]>1) |
| 1422 | std::cout << ": " << value << std::endl; |
| 1423 | if(!createPolygon){//write all points within polygon |
| 1424 | string fieldname; |
| 1425 | ostringstream fs; |
| 1426 | if(bandNames_opt.size()){ |
| 1427 | if(rule_opt.size()>1) |
| 1428 | fs << fieldMap["allpoints"]; |
| 1429 | if(planeNames_opt.size()) |
| 1430 | fs << planeNames_opt[iplane]; |
| 1431 | fs << bandNames_opt[iband]; |
| 1432 | } |
| 1433 | else{ |
| 1434 | if(rule_opt.size()>1||nband==1) |
| 1435 | fs << fieldMap["allpoints"]; |
| 1436 | if(planeNames_opt.size()) |
| 1437 | fs << planeNames_opt[iplane]; |
| 1438 | if(nband>1) |
| 1439 | fs << "b" << theBand; |
| 1440 | } |
| 1441 | fieldname=fs.str(); |
| 1442 | int fieldIndex=writePointFeature->GetFieldIndex(fieldname.c_str()); |
| 1443 | if(fieldIndex<0){ |
| 1444 | ostringstream ess; |
| 1445 | ess << "field " << fieldname << " was not found" << endl; |
| 1446 | throw(ess.str()); |
| 1447 | // return(CE_Failure); |
| 1448 | } |
| 1449 | if(verbose_opt[0]>1) |
| 1450 | std::cout << "set field " << fieldname << " to " << value << std::endl; |
| 1451 | switch( fieldType ){ |
| 1452 | case OFTInteger: |
| 1453 | case OFTReal: |
| 1454 | writePointFeature->SetField(fieldname.c_str(),value); |
| 1455 | break; |
| 1456 | default://not supported |
| 1457 | assert(0); |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | else{ |
| 1462 | if(class_opt.empty()) |
| 1463 | polyValues[iplane][iband].push_back(value); |
| 1464 | } |
| 1465 | }//iband |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | // if(valid&&!createPolygon){ |
| 1470 | if(validFeature&&!createPolygon){//replaced valid with validFeature! |
| 1471 | //write feature |
| 1472 | if(verbose_opt[0]>1) |
| 1473 | std::cout << "creating point feature" << std::endl; |
| 1474 | // if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 1475 | ogrWriter.getLayer(ilayer)->CreateFeature( writePointFeature ); |
| 1476 | //destroy feature |
| 1477 | // OGRFeature::DestroyFeature( writePointFeature ); |
| 1478 | #pragma omp atomic |
| 1479 | ++ntotalvalid; |
| 1480 | #pragma omp atomic |
| 1481 | ++ntotalvalidLayer; |
| 1482 | } |
| 1483 | }//for in i |
| 1484 | }//for int j |
| 1485 | |
| 1486 | if(createPolygon){ |
| 1487 | //do not create if no points found within polygon |
| 1488 | if(!nPointPolygon){ |
| 1489 | if(verbose_opt[0]) |
| 1490 | cout << "no points found in polygon, continuing" << endl; |
| 1491 | continue; |
| 1492 | } |
| 1493 | //write field attributes to polygon feature |
| 1494 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 1495 | //skip centroid and point |
| 1496 | if(ruleMap[rule_opt[irule]]==rule::centroid||ruleMap[rule_opt[irule]]==rule::point) |
| 1497 | continue; |
| 1498 | if(!irule&&label_opt.size()) |
| 1499 | writePolygonFeature->SetField("label",label_opt[0]); |
| 1500 | if(!irule&&fid_opt.size()) |
| 1501 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1502 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1503 | for(int iband=0;iband<nband;++iband){ |
| 1504 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1505 | vector< vector<double> > theValue(nrOfPlane()); |
| 1506 | vector< vector<string> > fieldname(nrOfPlane()); |
| 1507 | ostringstream fs; |
| 1508 | if(bandNames_opt.size()){ |
| 1509 | if(rule_opt.size()>1) |
| 1510 | fs << fieldMap[rule_opt[irule]]; |
| 1511 | if(planeNames_opt.size()) |
| 1512 | fs << planeNames_opt[iplane]; |
| 1513 | fs << bandNames_opt[iband]; |
| 1514 | } |
| 1515 | else{ |
| 1516 | if(rule_opt.size()>1||nband==1) |
| 1517 | fs << fieldMap[rule_opt[irule]]; |
| 1518 | if(planeNames_opt.size()) |
| 1519 | fs << planeNames_opt[iplane]; |
| 1520 | if(nband>1) |
| 1521 | fs << "b" << theBand; |
| 1522 | } |
| 1523 | switch(ruleMap[rule_opt[irule]]){ |
| 1524 | case(rule::proportion)://deliberate fall through |
| 1525 | stat.normalize_pct(polyClassValues[iplane]); |
| 1526 | case(rule::count):{//count for each class |
| 1527 | for(int index=0;index<polyClassValues[iplane].size();++index){ |
| 1528 | theValue[iplane].push_back(polyClassValues[iplane][index]); |
| 1529 | ostringstream fsclass; |
| 1530 | fsclass << fs.str() << "class" << class_opt[index]; |
| 1531 | fieldname[iplane].push_back(fsclass.str()); |
| 1532 | } |
| 1533 | break; |
| 1534 | } |
| 1535 | case(rule::mode):{ |
| 1536 | //maximum votes in polygon |
| 1537 | if(verbose_opt[0]) |
| 1538 | std::cout << "number of points in polygon: " << nPointPolygon << std::endl; |
| 1539 | //search for class with maximum votes |
| 1540 | int maxClass=stat.mymin(class_opt); |
| 1541 | vector<double>::iterator maxit; |
| 1542 | maxit=stat.mymax(polyClassValues[iplane],polyClassValues[iplane].begin(),polyClassValues[iplane].end()); |
| 1543 | int maxIndex=distance(polyClassValues[iplane].begin(),maxit); |
| 1544 | maxClass=class_opt[maxIndex]; |
| 1545 | if(verbose_opt[0]>0) |
| 1546 | std::cout << "maxClass: " << maxClass << std::endl; |
| 1547 | theValue[iplane].push_back(maxClass); |
| 1548 | fieldname[iplane].push_back(fs.str()); |
| 1549 | break; |
| 1550 | } |
| 1551 | case(rule::mean): |
| 1552 | theValue[iplane].push_back(stat.mean(polyValues[iplane][iband])); |
| 1553 | fieldname[iplane].push_back(fs.str()); |
| 1554 | break; |
| 1555 | case(rule::median): |
| 1556 | theValue[iplane].push_back(stat.median(polyValues[iplane][iband])); |
| 1557 | fieldname[iplane].push_back(fs.str()); |
| 1558 | break; |
| 1559 | case(rule::stdev): |
| 1560 | theValue[iplane].push_back(sqrt(stat.var(polyValues[iplane][iband]))); |
| 1561 | fieldname[iplane].push_back(fs.str()); |
| 1562 | break; |
| 1563 | case(rule::percentile):{ |
| 1564 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 1565 | theValue[iplane].push_back(stat.percentile(polyValues[iplane][iband],polyValues[iplane][iband].begin(),polyValues[iplane][iband].end(),percentile_opt[iperc])); |
| 1566 | ostringstream fsperc; |
| 1567 | fsperc << fs.str() << percentile_opt[iperc]; |
| 1568 | fieldname[iplane].push_back(fsperc.str()); |
| 1569 | } |
| 1570 | break; |
| 1571 | } |
| 1572 | case(rule::sum): |
| 1573 | theValue[iplane].push_back(stat.sum(polyValues[iplane][iband])); |
| 1574 | fieldname[iplane].push_back(fs.str()); |
| 1575 | break; |
| 1576 | case(rule::max): |
| 1577 | theValue[iplane].push_back(stat.mymax(polyValues[iplane][iband])); |
| 1578 | fieldname[iplane].push_back(fs.str()); |
| 1579 | break; |
| 1580 | case(rule::min): |
| 1581 | theValue[iplane].push_back(stat.mymin(polyValues[iplane][iband])); |
| 1582 | fieldname[iplane].push_back(fs.str()); |
| 1583 | break; |
| 1584 | case(rule::point): |
| 1585 | case(rule::centroid): |
| 1586 | theValue[iplane].push_back(polyValues[iplane][iband].back()); |
| 1587 | fieldname[iplane].push_back(fs.str()); |
| 1588 | break; |
| 1589 | default://not supported |
| 1590 | break; |
| 1591 | } |
| 1592 | for(int ivalue=0;ivalue<theValue[iplane].size();++ivalue){ |
| 1593 | switch( fieldType ){ |
| 1594 | case OFTInteger: |
| 1595 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),static_cast<int>(theValue[iplane][ivalue])); |
| 1596 | break; |
| 1597 | case OFTReal: |
| 1598 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),theValue[iplane][ivalue]); |
| 1599 | break; |
| 1600 | case OFTString: |
| 1601 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),type2string<double>(theValue[iplane][ivalue]).c_str()); |
| 1602 | break; |
| 1603 | default://not supported |
| 1604 | std::string errorString="field type not supported"; |
| 1605 | throw(errorString); |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | if(createPolygon&&validFeature){ |
| 1615 | // if(createPolygon){ |
| 1616 | //write polygon feature |
| 1617 | //todo: create only in case of valid feature |
| 1618 | if(verbose_opt[0]>1) |
| 1619 | std::cout << "creating polygon feature (1)" << std::endl; |
| 1620 | // if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){ |
| 1621 | // std::string errorString="Failed to create polygon feature in ogr vector dataset"; |
| 1622 | // throw(errorString); |
| 1623 | // } |
| 1624 | //test: no need to destroy anymore? |
| 1625 | // OGRFeature::DestroyFeature( writePolygonFeature ); |
| 1626 | //make sure to use setFeature instead of pushFeature when in processing in parallel!!! |
| 1627 | if(!writePolygonFeature) |
| 1628 | std::cerr << "Warning: NULL feature" << ifeature << std::endl; |
| 1629 | ogrWriter.setFeature(ifeature,writePolygonFeature,ilayer); |
| 1630 | #pragma omp atomic |
| 1631 | ++ntotalvalid; |
| 1632 | #pragma omp atomic |
| 1633 | ++ntotalvalidLayer; |
| 1634 | } |
| 1635 | }//for points |
| 1636 | else{//(multi-)polygons |
| 1637 | if(verbose_opt[0]>2) |
| 1638 | std::cout << "poGeometry is (multi-)polygon" << std::endl; |
| 1639 | OGRPolygon readPolygon;//readPolygon is in SRS of raster dataset |
| 1640 | OGRMultiPolygon readMultiPolygon;//readMultiPolygon is in SRS of raster dataset |
| 1641 | |
| 1642 | //get envelope |
| 1643 | OGREnvelope* psEnvelope=new OGREnvelope(); |
| 1644 | |
| 1645 | |
| 1646 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 1647 | if(verbose_opt[0]>2) |
| 1648 | std::cout << "get psEnvelope from readPolygon" << std::endl; |
| 1649 | readPolygon = *((OGRPolygon *) poGeometry); |
| 1650 | readPolygon.closeRings(); |
| 1651 | readPolygon.getEnvelope(psEnvelope); |
| 1652 | } |
| 1653 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 1654 | if(verbose_opt[0]>2) |
| 1655 | std::cout << "get psEnvelope from readMultiPolygon" << std::endl; |
| 1656 | readMultiPolygon = *((OGRMultiPolygon *) poGeometry); |
| 1657 | readMultiPolygon.closeRings(); |
| 1658 | readMultiPolygon.getEnvelope(psEnvelope); |
| 1659 | } |
| 1660 | else{ |
| 1661 | ostringstream oss; |
| 1662 | oss << "geometry " << static_cast<std::string>(poGeometry->getGeometryName()) << " not supported"; |
| 1663 | throw(oss.str()); |
| 1664 | } |
| 1665 | |
| 1666 | double ulx,uly,lrx,lry; |
| 1667 | double uli,ulj,lri,lrj; |
| 1668 | if(verbose_opt[0]>2) |
| 1669 | std::cout << "calculating ulx, uly, lrx, lry from psEnvelope" << std::endl; |
| 1670 | |
| 1671 | ulx=psEnvelope->MinX; |
| 1672 | uly=psEnvelope->MaxY; |
| 1673 | lrx=psEnvelope->MaxX; |
| 1674 | lry=psEnvelope->MinY; |
| 1675 | delete psEnvelope; |
| 1676 | |
| 1677 | //check if feature is covered by input raster dataset |
| 1678 | if(!this->covers(ulx,uly,lrx,lry,allCovered_opt[0])){ |
| 1679 | if(verbose_opt[0]>2){ |
| 1680 | std::cerr << "Warning: raster does not cover polygon: " << readFeature->GetFID() << std::endl; |
| 1681 | std::cerr << "Envelope polygon in SRS of raster:" << std::endl; |
| 1682 | std::cout << std::setprecision(12) << "--ulx " << ulx << " --uly " << uly << " --lrx " << lrx << " --lry " << lry << std::endl; |
| 1683 | std::cerr << "Bounding box of raster:" << std::endl; |
| 1684 | std::cerr << std::setprecision(12) << "--ulx " << getUlx() << " --uly " << getUly() << " --lrx " << getLrx() << " --lry " << getLry() << std::endl; |
| 1685 | } |
| 1686 | continue; |
| 1687 | } |
| 1688 | else if(verbose_opt[0]>2){ |
| 1689 | std::cout << "raster does cover polygon: " << readFeature->GetFID() << std::endl; |
| 1690 | std::cout << "bounding box of polygon in SRS of raster: " << readFeature->GetFID() << std::endl; |
| 1691 | std::cout<< std::setprecision(12) << "--ulx " << ulx << " --uly " << uly<< " --lrx " << lrx << " --lry " << lry << std::endl; |
| 1692 | } |
| 1693 | // if(!this->covers(ulx,uly,lrx,lry,allCovered_opt[0])){ |
| 1694 | // if(verbose_opt[0]>1) |
| 1695 | // std::cout << "polygon not covered, skipping" << std::endl; |
| 1696 | // continue; |
| 1697 | // } |
| 1698 | |
| 1699 | if(!layerRead){//todo (implementation not finished) |
| 1700 | ostringstream oss; |
| 1701 | oss << "implementation not finished"; |
| 1702 | throw(oss.str()); |
| 1703 | |
| 1704 | if(verbose_opt[0]>1) |
| 1705 | std::cout << "read data within polygon" << std::endl; |
| 1706 | Jim blockRaster; |
| 1707 | AppFactory anApp; |
| 1708 | anApp.pushLongOption("ulx",ulx); |
| 1709 | anApp.pushLongOption("uly",uly); |
| 1710 | anApp.pushLongOption("lrx",lrx); |
| 1711 | anApp.pushLongOption("lry",lry); |
| 1712 | cropDS(blockRaster,anApp); |
| 1713 | switch( fieldType ){ |
| 1714 | case OFTInteger: |
| 1715 | anApp.pushLongOption("otype","Int32"); |
| 1716 | cropDS(blockRaster,anApp); |
| 1717 | // for(int iband=0;iband<nband;++iband){ |
| 1718 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1719 | // blockRaster.readDataBlock(readValuesInt[iband],theBand); |
| 1720 | // } |
| 1721 | break; |
| 1722 | case OFTReal: |
| 1723 | default: |
| 1724 | anApp.pushLongOption("otype","Float32"); |
| 1725 | cropDS(blockRaster,anApp); |
| 1726 | // for(int iband=0;iband<nband;++iband){ |
| 1727 | // int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1728 | // blockRaster.readDataBlock(readValuesReal[iband],theBand); |
| 1729 | // } |
| 1730 | break; |
| 1731 | } |
| 1732 | } |
| 1733 | else{ |
| 1734 | if(verbose_opt[0]>1) |
| 1735 | std::cout << "layer has been read already" << std::endl; |
| 1736 | } |
| 1737 | OGRFeature *writePolygonFeature; |
| 1738 | int nPointPolygon=0; |
| 1739 | if(createPolygon){ |
| 1740 | if(verbose_opt[0]>2) |
| 1741 | std::cout << "writePolygonFeature in ogrWriter for layer (!)" << ilayer << std::endl; |
| 1742 | // writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 1743 | writePolygonFeature = ogrWriter.createFeature(ilayer); |
| 1744 | //coordinate transform |
| 1745 | // if(!VectorOgr::transform(poGeometry,img2sample)){ |
| 1746 | // std::string errorString="Error: coordinate transform img2sample not successful"; |
| 1747 | // throw(errorString); |
| 1748 | // } |
| 1749 | //writePolygonFeature and readFeature are both of type wkbPolygon |
| 1750 | if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 1751 | cerr << "writing feature failed" << std::endl; |
| 1752 | if(verbose_opt[0]>2) |
| 1753 | std::cout << "geometry of feature (3): " << writePolygonFeature->GetGeometryRef()->getGeometryName() << std::endl; |
| 1754 | //uncomment if we want to get buffered geometry |
| 1755 | if(verbose_opt[0]>2) |
| 1756 | std::cout << "geometry of poGeometry (3): " << poGeometry->getGeometryName() << std::endl; |
| 1757 | writePolygonFeature->SetGeometry(poGeometry); |
| 1758 | if(verbose_opt[0]>2) |
| 1759 | std::cout << "geometry of feature (4): " << writePolygonFeature->GetGeometryRef()->getGeometryName() << std::endl; |
| 1760 | if(verbose_opt[0]>1) |
| 1761 | std::cout << "copying new fields write polygon " << std::endl; |
| 1762 | if(verbose_opt[0]>1) |
| 1763 | std::cout << "write polygon feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl; |
| 1764 | } |
| 1765 | |
| 1766 | OGRPoint readPoint;//readPoint is in SRS of raster dataset |
| 1767 | if(find(rule_opt.begin(),rule_opt.end(),"centroid")!=rule_opt.end()){ |
| 1768 | if(verbose_opt[0]>1) |
| 1769 | std::cout << "get centroid" << std::endl; |
| 1770 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon) |
| 1771 | readPolygon.Centroid(&readPoint); |
| 1772 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon) |
| 1773 | readMultiPolygon.Centroid(&readPoint); |
| 1774 | |
| 1775 | double i,j; |
| 1776 | // blockRaster.geo2image(readPoint.getX(),readPoint.getY(),i,j,sample2img) |
| 1777 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 1778 | if(verbose_opt[0]>1) |
| 1779 | std::cout << "centroid in raster SRS: " << readPoint.getX() << ", " << readPoint.getY() << std::endl; |
| 1780 | int indexJ=static_cast<int>(j-layer_ulj); |
| 1781 | int indexI=static_cast<int>(i-layer_uli); |
| 1782 | // if(m_data.empty()){ |
| 1783 | // //todo: check |
| 1784 | // int indexJ-=polygon_ulj; |
| 1785 | // int indexI-=polygon_uli; |
| 1786 | // } |
| 1787 | bool valid=true; |
| 1788 | |
| 1789 | valid=valid&&(indexJ>=0); |
| 1790 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 1791 | valid=valid&&(indexI>=0); |
| 1792 | // valid=valid&&(indexI<this->nrOfCol()); |
| 1793 | valid=valid&&(j<=layer_lrj); |
| 1794 | valid=valid&&(i<=layer_lri); |
| 1795 | if(valid){ |
| 1796 | if(maskReader.isInit()){ |
| 1797 | double maskI,maskJ; |
| 1798 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 1799 | maskI=static_cast<unsigned int>(maskI); |
| 1800 | maskJ=static_cast<unsigned int>(maskJ); |
| 1801 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 1802 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 1803 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 1804 | valid=false; |
| 1805 | break; |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | } |
| 1811 | if(valid){ |
| 1812 | if(srcnodata_opt.empty()) |
| 1813 | validFeature=true; |
| 1814 | else{ |
| 1815 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1816 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 1817 | switch( fieldType ){ |
| 1818 | case OFTInteger:{ |
| 1819 | int value; |
| 1820 | value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 1821 | if(value==srcnodata_opt[vband]) |
| 1822 | valid=false; |
| 1823 | break; |
| 1824 | } |
| 1825 | case OFTReal:{ |
| 1826 | double value; |
| 1827 | value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 1828 | if(value==srcnodata_opt[vband]) |
| 1829 | valid=false; |
| 1830 | break; |
| 1831 | } |
| 1832 | } |
| 1833 | if(!valid) |
| 1834 | continue; |
| 1835 | else |
| 1836 | validFeature=true; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | // if(valid){ |
| 1842 | if(validFeature){//replace valid with validFeature! |
| 1843 | if(label_opt.size()) |
| 1844 | writePolygonFeature->SetField("label",label_opt[0]); |
| 1845 | if(fid_opt.size()) |
| 1846 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1847 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1848 | for(int iband=0;iband<nband;++iband){ |
| 1849 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1850 | //write fields for point on surface and centroid |
| 1851 | string fieldname; |
| 1852 | ostringstream fs; |
| 1853 | if(bandNames_opt.size()){ |
| 1854 | if(rule_opt.size()>1) |
| 1855 | fs << fieldMap["centroid"]; |
| 1856 | if(planeNames_opt.size()) |
| 1857 | fs << planeNames_opt[iplane]; |
| 1858 | fs << bandNames_opt[iband]; |
| 1859 | } |
| 1860 | else{ |
| 1861 | if(rule_opt.size()>1||nband==1) |
| 1862 | fs << fieldMap["centroid"]; |
| 1863 | if(planeNames_opt.size()) |
| 1864 | fs << planeNames_opt[iplane]; |
| 1865 | if(nband>1) |
| 1866 | fs << "b" << theBand; |
| 1867 | } |
| 1868 | fieldname=fs.str(); |
| 1869 | switch( fieldType ){ |
| 1870 | case OFTInteger: |
| 1871 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iplane][iband])[indexJ])[indexI])); |
| 1872 | break; |
| 1873 | case OFTReal: |
| 1874 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iplane][iband])[indexJ])[indexI]); |
| 1875 | break; |
| 1876 | default://not supported |
| 1877 | std::string errorString="field type not supported"; |
| 1878 | throw(errorString); |
| 1879 | break; |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | if(find(rule_opt.begin(),rule_opt.end(),"point")!=rule_opt.end()){ |
| 1886 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 1887 | if(readPolygon.PointOnSurface(&readPoint)!=OGRERR_NONE){ |
| 1888 | if(verbose_opt[0]>1) |
| 1889 | std::cout << "get centroid from readPolygon" << std::endl << std::flush; |
| 1890 | readPolygon.Centroid(&readPoint); |
| 1891 | } |
| 1892 | } |
| 1893 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 1894 | // if(readMultiPolygon.PointOnSurface(&readPoint)!=OGRERR_NONE) |
| 1895 | readMultiPolygon.Centroid(&readPoint); |
| 1896 | } |
| 1897 | double i,j; |
| 1898 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 1899 | int indexJ=static_cast<int>(j-layer_ulj); |
| 1900 | int indexI=static_cast<int>(i-layer_uli); |
| 1901 | bool valid=true; |
| 1902 | valid=valid&&(indexJ>=0); |
| 1903 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 1904 | valid=valid&&(indexI>=0); |
| 1905 | // valid=valid&&(indexI<this->nrOfCol()); |
| 1906 | valid=valid&&(j<=layer_lrj); |
| 1907 | valid=valid&&(i<=layer_lri); |
| 1908 | if(valid){ |
| 1909 | if(maskReader.isInit()){ |
| 1910 | double maskI,maskJ; |
| 1911 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 1912 | maskI=static_cast<unsigned int>(maskI); |
| 1913 | maskJ=static_cast<unsigned int>(maskJ); |
| 1914 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 1915 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 1916 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 1917 | valid=false; |
| 1918 | break; |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1925 | if(valid){ |
| 1926 | if(srcnodata_opt.empty()) |
| 1927 | validFeature=true; |
| 1928 | else{ |
| 1929 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 1930 | switch( fieldType ){ |
| 1931 | case OFTInteger:{ |
| 1932 | int value; |
| 1933 | value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 1934 | if(value==srcnodata_opt[vband]) |
| 1935 | valid=false; |
| 1936 | break; |
| 1937 | } |
| 1938 | case OFTReal:{ |
| 1939 | double value; |
| 1940 | value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 1941 | if(value==srcnodata_opt[vband]) |
| 1942 | valid=false; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | if(!valid) |
| 1947 | continue; |
| 1948 | else |
| 1949 | validFeature=true; |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | // if(valid){ |
| 1955 | if(validFeature){//replace valid with validFeature! |
| 1956 | if(label_opt.size()){ |
| 1957 | writePolygonFeature->SetField("label",label_opt[0]); |
| 1958 | } |
| 1959 | if(fid_opt.size()){ |
| 1960 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 1961 | } |
| 1962 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 1963 | for(int iband=0;iband<nband;++iband){ |
| 1964 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 1965 | //write fields for point on surface and centroid |
| 1966 | string fieldname; |
| 1967 | ostringstream fs; |
| 1968 | if(bandNames_opt.size()){ |
| 1969 | if(rule_opt.size()>1) |
| 1970 | fs << fieldMap["point"]; |
| 1971 | if(planeNames_opt.size()) |
| 1972 | fs << planeNames_opt[iplane]; |
| 1973 | fs << bandNames_opt[iband]; |
| 1974 | } |
| 1975 | else{ |
| 1976 | if(rule_opt.size()>1||nband==1) |
| 1977 | fs << fieldMap["point"]; |
| 1978 | if(planeNames_opt.size()) |
| 1979 | fs << planeNames_opt[iplane]; |
| 1980 | if(nband>1) |
| 1981 | fs << "b" << theBand; |
| 1982 | } |
| 1983 | fieldname=fs.str(); |
| 1984 | switch( fieldType ){ |
| 1985 | case OFTInteger: |
| 1986 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iplane][iband])[indexJ])[indexI])); |
| 1987 | break; |
| 1988 | case OFTReal: |
| 1989 | writePolygonFeature->SetField(fieldname.c_str(), ((readValuesReal[iplane][iband])[indexJ])[indexI]); |
| 1990 | break; |
| 1991 | default://not supported |
| 1992 | std::string errorString="field type not supported"; |
| 1993 | throw(errorString); |
| 1994 | break; |
| 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | if(calculateSpatialStatistics||ruleMap[rule_opt[0]]==rule::allpoints){ |
| 2001 | this->geo2image(ulx,uly,uli,ulj); |
| 2002 | this->geo2image(lrx,lry,lri,lrj); |
| 2003 | if(verbose_opt[0]>1){ |
| 2004 | std::cout << "calculated uli=" << uli << "and ulj" << ulj << " from ulx=" << ulx << "and uly=" << uly << std::endl; |
| 2005 | std::cout << "calculated lri=" << lri << "and lrj" << lrj << " from lrx=" << lrx << "and lry=" << lry << std::endl; |
| 2006 | std::cout << "bounding box for polygon feature " << ifeature << ": " << uli << " " << ulj << " " << lri << " " << lrj << std::endl; |
| 2007 | } |
| 2008 | //nearest neighbour |
| 2009 | ulj=static_cast<int>(ulj); |
| 2010 | uli=static_cast<int>(uli); |
| 2011 | lrj=static_cast<int>(lrj); |
| 2012 | lri=static_cast<int>(lri); |
| 2013 | //iterate through all pixels |
| 2014 | if(verbose_opt[0]>1) |
| 2015 | std::cout << "bounding box for polygon feature " << ifeature << ": " << uli << " " << ulj << " " << lri << " " << lrj << std::endl; |
| 2016 | |
| 2017 | if(uli<0) |
| 2018 | uli=0; |
| 2019 | if(lri<0) |
| 2020 | lri=0; |
| 2021 | // if(uli>=this->nrOfCol()) |
| 2022 | // uli=this->nrOfCol()-1; |
| 2023 | if(uli>=layer_lri) |
| 2024 | uli=layer_lri-1; |
| 2025 | // if(lri>=this->nrOfCol()) |
| 2026 | // lri=this->nrOfCol()-1; |
| 2027 | if(lri>=layer_lri) |
| 2028 | lri=layer_lri-1; |
| 2029 | if(ulj<0) |
| 2030 | ulj=0; |
| 2031 | if(lrj<0) |
| 2032 | lrj=0; |
| 2033 | // if(ulj>=this->nrOfRow()) |
| 2034 | // ulj=this->nrOfRow()-1; |
| 2035 | if(ulj>=layer_lrj) |
| 2036 | ulj=layer_lrj-1; |
| 2037 | // if(lrj>=this->nrOfRow()) |
| 2038 | // lrj=this->nrOfRow()-1; |
| 2039 | if(lrj>=layer_lrj) |
| 2040 | lrj=layer_lrj-1; |
| 2041 | |
| 2042 | if(verbose_opt[0]>1) |
| 2043 | std::cout << "bounding box for polygon feature after check " << ifeature << ": " << uli << " " << ulj << " " << lri << " " << lrj << std::endl; |
| 2044 | vector< Vector2d<double> > polyValues(nrOfPlane()); |
| 2045 | vector< vector<double> > polyClassValues(nrOfPlane()); |
| 2046 | |
| 2047 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 2048 | if(class_opt.size()){ |
| 2049 | polyClassValues[iplane].resize(class_opt.size()); |
| 2050 | //initialize |
| 2051 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 2052 | polyClassValues[iplane][iclass]=0; |
| 2053 | } |
| 2054 | else |
| 2055 | polyValues[iplane].resize(nband); |
| 2056 | } |
| 2057 | |
| 2058 | OGRPoint thePoint;//in SRS of raster dataset |
| 2059 | for(int j=ulj;j<=lrj;++j){ |
| 2060 | for(int i=uli;i<=lri;++i){ |
| 2061 | // //check if within raster image |
| 2062 | // if(i<0||i>=this->nrOfCol()) |
| 2063 | // continue; |
| 2064 | // if(j<0||j>=this->nrOfRow()) |
| 2065 | // continue; |
| 2066 | //check if within read block of raster image |
| 2067 | if(j<0){ |
| 2068 | std::cerr << "Warning: j is " << j << ", setting to 0" << std::endl; |
| 2069 | j=0; |
| 2070 | } |
| 2071 | if(j>=layer_lrj){ |
| 2072 | std::cerr << "Warning: j is " << j << " and out of reading block, skipping" << std::endl; |
| 2073 | continue; |
| 2074 | } |
| 2075 | if(i<0){ |
| 2076 | std::cerr << "Warning: i is " << i << ", setting to 0" << std::endl; |
| 2077 | i=0; |
| 2078 | } |
| 2079 | if(i>=layer_lri){ |
| 2080 | std::cerr << "Warning: i is " << i << " and out of reading block, skipping" << std::endl; |
| 2081 | continue; |
| 2082 | } |
| 2083 | int indexJ=j-layer_ulj; |
| 2084 | int indexI=i-layer_uli; |
| 2085 | if(indexJ<0||indexJ>=this->nrOfRow()||indexI<0||indexI>=this->nrOfCol()) |
| 2086 | continue; |
| 2087 | double theX=0; |
| 2088 | double theY=0; |
| 2089 | this->image2geo(i,j,theX,theY); |
| 2090 | thePoint.setX(theX); |
| 2091 | thePoint.setY(theY); |
| 2092 | //check if point is on surface |
| 2093 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 2094 | if(!readPolygon.Contains(&thePoint))//readPolygon is in SRS of raster dataset |
| 2095 | continue; |
| 2096 | } |
| 2097 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 2098 | if(!readMultiPolygon.Contains(&thePoint))//readMultiPolygon is in SRS of raster dataset |
| 2099 | continue; |
| 2100 | } |
| 2101 | |
| 2102 | bool valid=true; |
| 2103 | double maskI,maskJ; |
| 2104 | if(maskReader.isInit()){ |
| 2105 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 2106 | maskI=static_cast<unsigned int>(maskI); |
| 2107 | maskJ=static_cast<unsigned int>(maskJ); |
| 2108 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 2109 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 2110 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 2111 | valid=false; |
| 2112 | break; |
| 2113 | } |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 2118 | if(srcnodata_opt.size()){ |
| 2119 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 2120 | switch( fieldType ){ |
| 2121 | case OFTInteger:{ |
| 2122 | int value=((readValuesInt[iplane][vband])[indexJ])[indexI]; |
| 2123 | if(value==srcnodata_opt[vband]){ |
| 2124 | valid=false; |
| 2125 | } |
| 2126 | break; |
| 2127 | } |
| 2128 | default:{ |
| 2129 | float value=((readValuesReal[iplane][vband])[indexJ])[indexI]; |
| 2130 | if(value==srcnodata_opt[vband]){ |
| 2131 | valid=false; |
| 2132 | } |
| 2133 | break; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | if(!valid) |
| 2139 | continue; |
| 2140 | else |
| 2141 | validFeature=true; |
| 2142 | } |
| 2143 | |
| 2144 | if(verbose_opt[0]>1) |
| 2145 | std::cout << "point is on surface: " << thePoint.getX() << "," << thePoint.getY() << std::endl; |
| 2146 | ++nPointPolygon; |
| 2147 | |
| 2148 | OGRFeature *writePointFeature; |
| 2149 | if(!createPolygon){//write all points within polygon |
| 2150 | if(verbose_opt[0]>1) |
| 2151 | std::cout << "do not create polygon" << std::endl; |
| 2152 | if(polythreshold_opt.size()){ |
| 2153 | if(polythreshold_opt[0]>0){ |
| 2154 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 2155 | p*=100.0; |
| 2156 | if(p>polythreshold_opt[0]) |
| 2157 | continue;//do not select for now, go to next feature |
| 2158 | } |
| 2159 | else if(nPointPolygon>-polythreshold_opt[0]) |
| 2160 | continue; |
| 2161 | } |
| 2162 | // if(polythreshold_opt.size()) |
| 2163 | // if(nPointPolygon>=polythreshold_opt[0]) |
| 2164 | // continue; |
| 2165 | // if(threshold_opt.size()){ |
| 2166 | // if(threshold_opt[0]<=0){ |
| 2167 | // if(ntotalvalid>=-threshold_opt[0]) |
| 2168 | // continue; |
| 2169 | // } |
| 2170 | // else if(threshold_opt[0]<100){ |
| 2171 | // double p=static_cast<double>(rand())/(RAND_MAX); |
| 2172 | // p*=100.0; |
| 2173 | // if(p>threshold_opt[0]){ |
| 2174 | // continue;//do not select for now, go to next feature |
| 2175 | // } |
| 2176 | // } |
| 2177 | // } |
| 2178 | //create feature |
| 2179 | writePointFeature = OGRFeature::CreateFeature(ogrWriter.getLayer(ilayer)->GetLayerDefn()); |
| 2180 | // writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 2181 | if(verbose_opt[0]>1) |
| 2182 | std::cout << "copying fields from polygons " << std::endl; |
| 2183 | if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 2184 | cerr << "writing feature failed" << std::endl; |
| 2185 | if(verbose_opt[0]>1) |
| 2186 | std::cout << "set geometry as point in SRS of vector layer" << std::endl; |
| 2187 | // VectorOgr::transform(&thePoint,img2sample); |
| 2188 | writePointFeature->SetGeometry(&thePoint); |
| 2189 | assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint); |
| 2190 | if(verbose_opt[0]>1){ |
| 2191 | std::cout << "write point feature has " << writePointFeature->GetFieldCount() << " fields:" << std::endl; |
| 2192 | for(int iField=0;iField<writePointFeature->GetFieldCount();++iField){ |
| 2193 | std::string fieldname=ogrWriter.getLayer(ilayer)->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 2194 | // std::string fieldname=writeLayer->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 2195 | cout << fieldname << endl; |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | // if(class_opt.size()){ |
| 2200 | // short value=0; |
| 2201 | // switch( fieldType ){ |
| 2202 | // case OFTInteger: |
| 2203 | // value=((readValuesInt[0])[indexJ])[indexI]; |
| 2204 | // break; |
| 2205 | // case OFTReal: |
| 2206 | // value=((readValuesReal[0])[indexJ])[indexI]; |
| 2207 | // break; |
| 2208 | // } |
| 2209 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 2210 | // if(value==class_opt[iclass]) |
| 2211 | // polyClassValues[iclass]+=1; |
| 2212 | // } |
| 2213 | // } |
| 2214 | // else{ |
| 2215 | |
| 2216 | if(!createPolygon&&label_opt.size()){ |
| 2217 | if(verbose_opt[0]>1) |
| 2218 | std::cout << "set field label" << std::endl; |
| 2219 | writePointFeature->SetField("label",label_opt[0]); |
| 2220 | } |
| 2221 | |
| 2222 | if(!createPolygon&&fid_opt.size()) |
| 2223 | writePointFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 2224 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 2225 | for(int iband=0;iband<nband;++iband){ |
| 2226 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 2227 | double value=0; |
| 2228 | switch( fieldType ){ |
| 2229 | case OFTInteger: |
| 2230 | value=((readValuesInt[iplane][iband])[indexJ])[indexI]; |
| 2231 | break; |
| 2232 | case OFTReal: |
| 2233 | value=((readValuesReal[iplane][iband])[indexJ])[indexI]; |
| 2234 | break; |
| 2235 | } |
| 2236 | if(!iband&&class_opt.size()){ |
| 2237 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 2238 | if(value==class_opt[iclass]) |
| 2239 | polyClassValues[iplane][iclass]+=1; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | if(verbose_opt[0]>1) |
| 2244 | std::cout << ": " << value << std::endl; |
| 2245 | if(!createPolygon){//write all points within polygon |
| 2246 | string fieldname; |
| 2247 | ostringstream fs; |
| 2248 | if(bandNames_opt.size()){ |
| 2249 | if(rule_opt.size()>1) |
| 2250 | fs << fieldMap["allpoints"]; |
| 2251 | if(planeNames_opt.size()) |
| 2252 | fs << planeNames_opt[iplane]; |
| 2253 | fs << bandNames_opt[iband]; |
| 2254 | } |
| 2255 | else{ |
| 2256 | if(rule_opt.size()>1||nband==1) |
| 2257 | fs << fieldMap["allpoints"]; |
| 2258 | if(planeNames_opt.size()) |
| 2259 | fs << planeNames_opt[iplane]; |
| 2260 | if(nband>1) |
| 2261 | fs << "b" << theBand; |
| 2262 | } |
| 2263 | fieldname=fs.str(); |
| 2264 | int fieldIndex=writePointFeature->GetFieldIndex(fieldname.c_str()); |
| 2265 | if(fieldIndex<0){ |
| 2266 | ostringstream ess; |
| 2267 | ess << "field " << fieldname << " was not found" << endl; |
| 2268 | throw(ess.str()); |
| 2269 | // cerr << "field " << fieldname << " was not found" << endl; |
| 2270 | // return(CE_Failure); |
| 2271 | } |
| 2272 | if(verbose_opt[0]>1) |
| 2273 | std::cout << "set field " << fieldname << " to " << value << std::endl; |
| 2274 | switch( fieldType ){ |
| 2275 | case OFTInteger: |
| 2276 | case OFTReal: |
| 2277 | writePointFeature->SetField(fieldname.c_str(),value); |
| 2278 | break; |
| 2279 | default://not supported |
| 2280 | assert(0); |
| 2281 | break; |
| 2282 | } |
| 2283 | } |
| 2284 | else{ |
| 2285 | if(class_opt.empty()) |
| 2286 | polyValues[iplane][iband].push_back(value); |
| 2287 | } |
| 2288 | }//iband |
| 2289 | }//iplane |
| 2290 | |
| 2291 | if(!createPolygon){ |
| 2292 | //todo: only if valid feature? |
| 2293 | //write feature |
| 2294 | if(verbose_opt[0]) |
| 2295 | std::cout << "creating point feature " << ntotalvalidLayer << std::endl; |
| 2296 | // if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 2297 | ogrWriter.getLayer(ilayer)->CreateFeature( writePointFeature ); |
| 2298 | //destroy feature |
| 2299 | // OGRFeature::DestroyFeature( writePointFeature ); |
| 2300 | #pragma omp atomic |
| 2301 | ++ntotalvalid; |
| 2302 | #pragma omp atomic |
| 2303 | ++ntotalvalidLayer; |
| 2304 | } |
| 2305 | }//for in i |
| 2306 | }//for int j |
| 2307 | if(createPolygon){ |
| 2308 | //do not create if no points found within polygon |
| 2309 | if(!nPointPolygon){ |
| 2310 | if(verbose_opt[0]) |
| 2311 | cout << "no points found in polygon, continuing" << endl; |
| 2312 | continue; |
| 2313 | } |
| 2314 | //write field attributes to polygon feature |
| 2315 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 2316 | //skip centroid and point |
| 2317 | if(ruleMap[rule_opt[irule]]==rule::centroid||ruleMap[rule_opt[irule]]==rule::point) |
| 2318 | continue; |
| 2319 | if(!irule&&label_opt.size()) |
| 2320 | writePolygonFeature->SetField("label",label_opt[0]); |
| 2321 | if(!irule&&fid_opt.size()) |
| 2322 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 2323 | for(size_t iplane=0;iplane<nplane;++iplane){ |
| 2324 | for(int iband=0;iband<nband;++iband){ |
| 2325 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 2326 | vector< vector<double> > theValue(nrOfPlane()); |
| 2327 | vector< vector<string> > fieldname(nrOfPlane()); |
| 2328 | ostringstream fs; |
| 2329 | if(bandNames_opt.size()){ |
| 2330 | if(rule_opt.size()>1) |
| 2331 | fs << fieldMap[rule_opt[irule]]; |
| 2332 | if(planeNames_opt.size()) |
| 2333 | fs << planeNames_opt[iplane]; |
| 2334 | fs << bandNames_opt[iband]; |
| 2335 | } |
| 2336 | else{ |
| 2337 | if(rule_opt.size()>1||nband==1) |
| 2338 | fs << fieldMap[rule_opt[irule]]; |
| 2339 | if(planeNames_opt.size()) |
| 2340 | fs << planeNames_opt[iplane]; |
| 2341 | if(nband>1) |
| 2342 | fs << "b" << theBand; |
| 2343 | } |
| 2344 | switch(ruleMap[rule_opt[irule]]){ |
| 2345 | case(rule::proportion): |
| 2346 | stat.normalize_pct(polyClassValues[iplane]); |
| 2347 | case(rule::count):{//count for each class |
| 2348 | for(int index=0;index<polyClassValues[iplane].size();++index){ |
| 2349 | theValue[iplane].push_back(polyClassValues[iplane][index]); |
| 2350 | ostringstream fsclass; |
| 2351 | fsclass << fs.str() << "class" << class_opt[index]; |
| 2352 | fieldname[iplane].push_back(fsclass.str()); |
| 2353 | } |
| 2354 | break; |
| 2355 | } |
| 2356 | case(rule::mode):{ |
| 2357 | //maximum votes in polygon |
| 2358 | if(verbose_opt[0]) |
| 2359 | std::cout << "number of points in polygon: " << nPointPolygon << std::endl; |
| 2360 | //search for class with maximum votes |
| 2361 | int maxClass=stat.mymin(class_opt); |
| 2362 | vector<double>::iterator maxit; |
| 2363 | maxit=stat.mymax(polyClassValues[iplane],polyClassValues[iplane].begin(),polyClassValues[iplane].end()); |
| 2364 | int maxIndex=distance(polyClassValues[iplane].begin(),maxit); |
| 2365 | maxClass=class_opt[maxIndex]; |
| 2366 | if(verbose_opt[0]>0) |
| 2367 | std::cout << "maxClass: " << maxClass << std::endl; |
| 2368 | theValue[iplane].push_back(maxClass); |
| 2369 | fieldname[iplane].push_back(fs.str()); |
| 2370 | break; |
| 2371 | } |
| 2372 | case(rule::mean): |
| 2373 | theValue[iplane].push_back(stat.mean(polyValues[iplane][iband])); |
| 2374 | fieldname[iplane].push_back(fs.str()); |
| 2375 | break; |
| 2376 | case(rule::median): |
| 2377 | theValue[iplane].push_back(stat.median(polyValues[iplane][iband])); |
| 2378 | fieldname[iplane].push_back(fs.str()); |
| 2379 | break; |
| 2380 | case(rule::stdev): |
| 2381 | theValue[iplane].push_back(sqrt(stat.var(polyValues[iplane][iband]))); |
| 2382 | fieldname[iplane].push_back(fs.str()); |
| 2383 | break; |
| 2384 | case(rule::percentile):{ |
| 2385 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 2386 | theValue[iplane].push_back(stat.percentile(polyValues[iplane][iband],polyValues[iplane][iband].begin(),polyValues[iplane][iband].end(),percentile_opt[iperc])); |
| 2387 | ostringstream fsperc; |
| 2388 | fsperc << fs.str() << percentile_opt[iperc]; |
| 2389 | fieldname[iplane].push_back(fsperc.str()); |
| 2390 | } |
| 2391 | break; |
| 2392 | } |
| 2393 | case(rule::sum): |
| 2394 | theValue[iplane].push_back(stat.sum(polyValues[iplane][iband])); |
| 2395 | fieldname[iplane].push_back(fs.str()); |
| 2396 | break; |
| 2397 | case(rule::max): |
| 2398 | theValue[iplane].push_back(stat.mymax(polyValues[iplane][iband])); |
| 2399 | fieldname[iplane].push_back(fs.str()); |
| 2400 | break; |
| 2401 | case(rule::min): |
| 2402 | theValue[iplane].push_back(stat.mymin(polyValues[iplane][iband])); |
| 2403 | fieldname[iplane].push_back(fs.str()); |
| 2404 | break; |
| 2405 | case(rule::centroid): |
| 2406 | case(rule::point): |
| 2407 | theValue[iplane].push_back(polyValues[iplane][iband].back()); |
| 2408 | fieldname[iplane].push_back(fs.str()); |
| 2409 | break; |
| 2410 | default://not supported |
| 2411 | break; |
| 2412 | } |
| 2413 | for(int ivalue=0;ivalue<theValue[iplane].size();++ivalue){ |
| 2414 | switch( fieldType ){ |
| 2415 | case OFTInteger: |
| 2416 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),static_cast<int>(theValue[iplane][ivalue])); |
| 2417 | break; |
| 2418 | case OFTReal: |
| 2419 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),theValue[iplane][ivalue]); |
| 2420 | break; |
| 2421 | case OFTString: |
| 2422 | writePolygonFeature->SetField(fieldname[iplane][ivalue].c_str(),type2string<double>(theValue[iplane][ivalue]).c_str()); |
| 2423 | break; |
| 2424 | default://not supported |
| 2425 | std::string errorString="field type not supported"; |
| 2426 | throw(errorString); |
| 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | if(createPolygon&&validFeature){ |
| 2436 | //todo: only create if valid feature? |
| 2437 | //write polygon feature |
| 2438 | if(verbose_opt[0]>1) |
| 2439 | std::cout << "creating polygon feature (2)" << std::endl; |
| 2440 | // if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){ |
| 2441 | // std::string errorString="Failed to create polygon feature in ogr vector dataset"; |
| 2442 | // throw(errorString); |
| 2443 | // } |
| 2444 | //test: no need to destroy anymore? |
| 2445 | // OGRFeature::DestroyFeature( writePolygonFeature ); |
| 2446 | //make sure to use setFeature instead of pushFeature when in processing in parallel!!! |
| 2447 | if(verbose_opt[0]){ |
| 2448 | std::cout << "set feature " << ifeature << " with address " << writePolygonFeature << " from " << ogrWriter.getFeatureCount() << "in layer " << ilayer << std::endl; |
| 2449 | std::cout << "writePolygonFeature: " << writePolygonFeature << " (" << ifeature << " from " << sampleReader.getFeatureCount(ilayer) << ")" << std::endl; |
| 2450 | } |
| 2451 | if(!writePolygonFeature) |
| 2452 | std::cerr << "Warning: NULL feature" << ifeature << std::endl; |
| 2453 | ogrWriter.setFeature(ifeature,writePolygonFeature,ilayer); |
| 2454 | #pragma omp atomic |
| 2455 | ++ntotalvalid; |
| 2456 | #pragma omp atomic |
| 2457 | ++ntotalvalidLayer; |
| 2458 | if(verbose_opt[0]) |
| 2459 | std::cout << "ntotalvalidLayer: " << ntotalvalidLayer << std::endl; |
| 2460 | } |
| 2461 | } |
| 2462 | // ++ifeature; |
| 2463 | // if(theThreshold>0){ |
| 2464 | // if(threshold_opt.size()==sampleReader.getLayerCount()) |
| 2465 | // progress=(100.0/theThreshold)*static_cast<float>(ntotalvalidLayer)/nfeatureLayer; |
| 2466 | // else |
| 2467 | // progress=static_cast<float>(ntotalvalidLayer)/nfeatureLayer; |
| 2468 | // } |
| 2469 | // else |
| 2470 | // progress=static_cast<float>(ifeature+1)/(-theThreshold); |
| 2471 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 2472 | } |
| 2473 | catch(std::string e){ |
| 2474 | std::cout << e << std::endl; |
| 2475 | continue; |
| 2476 | } |
| 2477 | catch(int npoint){ |
| 2478 | if(verbose_opt[0]) |
| 2479 | std::cout << "number of points read in polygon: " << npoint << std::endl; |
| 2480 | continue; |
| 2481 | } |
| 2482 | catch(...){ |
| 2483 | std::cout << "Error: something went wrong in extractOgr" << std::endl; |
| 2484 | } |
| 2485 | } |
| 2486 | // if(rbox_opt[0]>0||cbox_opt[0]>0) |
| 2487 | // boxWriter.close(); |
| 2488 | // progress=1.0; |
| 2489 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 2490 | if(verbose_opt[0]) |
| 2491 | std::cout << "number of valid points in layer: " << ntotalvalidLayer << std::endl; |
| 2492 | if(verbose_opt[0]) |
| 2493 | std::cout << "number of valid points in all layers: " << ntotalvalid<< std::endl; |
| 2494 | }//for int ilayer |
| 2495 | |
| 2496 | if(papszOptions) |
| 2497 | CSLDestroy(papszOptions); |
| 2498 | |
| 2499 | if(maskReader.isInit()){ |
| 2500 | maskReader.close(); |
| 2501 | } |
| 2502 | return(CE_None); |
| 2503 | } |
| 2504 | catch(string predefinedString){ |
| 2505 | std::cerr << predefinedString << std::endl; |
| 2506 | throw; |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | /** |
| 2511 | * @param app application specific option arguments |
| 2512 | * |
| 2513 | * @return CE_None if success, CE_Failure if failure |
| 2514 | */ |
| 2515 | //todo: support multiple layers for writing |
| 2516 | CPLErr Jim::extractSample(VectorOgr& ogrWriter, AppFactory& app){ |
| 2517 | // Optionjl<string> image_opt("i", "input", "Raster input dataset containing band information"); |
| 2518 | // Optionjl<string> sample_opt("s", "sample", "OGR vector dataset with features to be extracted from input data. Output will contain features with input band information included."); |
| 2519 | Optionjl<string> layer_opt("ln", "ln", "Layer name of output vector dataset"); |
| 2520 | Optionjl<unsigned int> random_opt("rand", "random", "Create simple random sample of points. Provide number of points to generate"); |
| 2521 | Optionjl<double> grid_opt("grid", "grid", "Create systematic grid of points. Provide cell grid size (in projected units, e.g,. m)"); |
| 2522 | Optionjl<string> output_opt("o", "output", "Output sample dataset"); |
| 2523 | Optionjl<int> label_opt("label", "label", "Create extra label field with this value"); |
| 2524 | Optionjl<std::string> fid_opt("fid", "fid", "Create extra field with field identifier (sequence in which the features have been read"); |
| 2525 | Optionjl<int> class_opt("c", "class", "Class(es) to extract from input sample image. Leave empty to extract all valid data pixels from sample dataset. Make sure to set classes if rule is set to mode, proportion or count"); |
| 2526 | Optionjl<float> threshold_opt("t", "threshold", "Probability threshold for selecting samples (randomly). Provide probability in percentage (>0) or absolute (<0). Use a single threshold per vector sample layer. Use value 100 to select all pixels for selected class(es)", 100); |
| 2527 | Optionjl<double> percentile_opt("perc","perc","Percentile value(s) used for rule percentile",95); |
| 2528 | Optionjl<string> ogrformat_opt("f", "oformat", "Output sample dataset format","SQLite"); |
| 2529 | Optionjl<std::string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified."); |
| 2530 | Optionjl<string> ftype_opt("ft", "ftype", "Field type (only Real or Integer)", "Real"); |
| 2531 | Optionjl<int> band_opt("b", "band", "Band index(es) to extract (0 based). Leave empty to use all bands"); |
| 2532 | Optionjl<std::string> bandNames_opt("bn", "bandname", "Band name(s) corresponding to band index(es). Leave empty to use all bands"); |
| 2533 | Optionjl<unsigned short> bstart_opt("sband", "startband", "Start band sequence number"); |
| 2534 | Optionjl<unsigned short> bend_opt("eband", "endband", "End band sequence number"); |
| 2535 | Optionjl<string> rule_opt("r", "rule", "Rule how to report image information per feature. point (single point within polygon), allpoints (all points within polygon), centroid, mean, stdev, median, proportion, count, min, max, mode, sum, percentile.","centroid"); |
| 2536 | Optionjl<double> srcnodata_opt("srcnodata", "srcnodata", "Invalid value(s) for input image"); |
| 2537 | Optionjl<int> bndnodata_opt("bndnodata", "bndnodata", "Band(s) in input image to check if pixel is valid (used for srcnodata)", 0); |
| 2538 | Optionjl<string> mask_opt("m", "mask", "Use the the specified file as a validity mask (0 is nodata)."); |
| 2539 | Optionjl<double> msknodata_opt("msknodata", "msknodata", "Mask value not to consider for crop.", 0); |
| 2540 | Optionjl<unsigned int> mskband_opt("mskband", "mskband", "Mask band to read (0 indexed)", 0); |
| 2541 | Optionjl<float> polythreshold_opt("tp", "thresholdPolygon", "(absolute) threshold for selecting samples in each polygon"); |
| 2542 | Optionjl<float> buffer_opt("buf", "buffer", "Buffer for calculating statistics for point features (in geometric units of raster dataset) "); |
| 2543 | Optionjl<bool> disc_opt("circ", "circular", "Use a circular disc kernel buffer (for vector point sample datasets only, use in combination with buffer option)", false); |
| 2544 | Optionjl<std::string> allCovered_opt("cover", "cover", "Which polygons to include based on coverage (ALL_TOUCHED, ALL_COVERED)", "ALL_TOUCHED"); |
| 2545 | Optionjl<unsigned long int> memory_opt("mem", "mem", "Buffer size (in MB) to read image data blocks in memory",0,1); |
| 2546 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 2547 | |
| 2548 | bstart_opt.setHide(1); |
| 2549 | bend_opt.setHide(1); |
| 2550 | bndnodata_opt.setHide(1); |
| 2551 | srcnodata_opt.setHide(1); |
| 2552 | mask_opt.setHide(1); |
| 2553 | msknodata_opt.setHide(1); |
| 2554 | mskband_opt.setHide(1); |
| 2555 | polythreshold_opt.setHide(1); |
| 2556 | percentile_opt.setHide(1); |
| 2557 | buffer_opt.setHide(1); |
| 2558 | disc_opt.setHide(1); |
| 2559 | allCovered_opt.setHide(1); |
| 2560 | option_opt.setHide(1); |
| 2561 | memory_opt.setHide(1); |
| 2562 | |
| 2563 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 2564 | try{ |
| 2565 | // doProcess=sample_opt.retrieveOption(app); |
| 2566 | layer_opt.retrieveOption(app); |
| 2567 | doProcess=output_opt.retrieveOption(app); |
| 2568 | random_opt.retrieveOption(app); |
| 2569 | grid_opt.retrieveOption(app); |
| 2570 | class_opt.retrieveOption(app); |
| 2571 | label_opt.retrieveOption(app); |
| 2572 | fid_opt.retrieveOption(app); |
| 2573 | threshold_opt.retrieveOption(app); |
| 2574 | percentile_opt.retrieveOption(app); |
| 2575 | ogrformat_opt.retrieveOption(app); |
| 2576 | option_opt.retrieveOption(app); |
| 2577 | ftype_opt.retrieveOption(app); |
| 2578 | band_opt.retrieveOption(app); |
| 2579 | bandNames_opt.retrieveOption(app); |
| 2580 | bstart_opt.retrieveOption(app); |
| 2581 | bend_opt.retrieveOption(app); |
| 2582 | rule_opt.retrieveOption(app); |
| 2583 | bndnodata_opt.retrieveOption(app); |
| 2584 | srcnodata_opt.retrieveOption(app); |
| 2585 | mask_opt.retrieveOption(app); |
| 2586 | msknodata_opt.retrieveOption(app); |
| 2587 | mskband_opt.retrieveOption(app); |
| 2588 | polythreshold_opt.retrieveOption(app); |
| 2589 | buffer_opt.retrieveOption(app); |
| 2590 | disc_opt.retrieveOption(app); |
| 2591 | allCovered_opt.retrieveOption(app); |
| 2592 | memory_opt.retrieveOption(app); |
| 2593 | verbose_opt.retrieveOption(app); |
| 2594 | if(!doProcess){ |
| 2595 | cout << endl; |
| 2596 | std::ostringstream helpStream; |
| 2597 | helpStream << "short option -h shows basic options only, use long option --help to show all options" << std::endl; |
| 2598 | throw(helpStream.str());//help was invoked, stop processing |
| 2599 | } |
| 2600 | |
| 2601 | // std::vector<std::string> badKeys; |
| 2602 | // app.badKeys(badKeys); |
| 2603 | // if(badKeys.size()){ |
| 2604 | // std::ostringstream errorStream; |
| 2605 | // if(badKeys.size()>1) |
| 2606 | // errorStream << "Error: unknown keys: "; |
| 2607 | // else |
| 2608 | // errorStream << "Error: unknown key: "; |
| 2609 | // for(int ikey=0;ikey<badKeys.size();++ikey){ |
| 2610 | // errorStream << badKeys[ikey] << " "; |
| 2611 | // } |
| 2612 | // errorStream << std::endl; |
| 2613 | // throw(errorStream.str()); |
| 2614 | // } |
| 2615 | |
| 2616 | //initialize ruleMap |
| 2617 | std::map<std::string, rule::RULE_TYPE> ruleMap; |
| 2618 | ruleMap["point"]=rule::point; |
| 2619 | ruleMap["centroid"]=rule::centroid; |
| 2620 | ruleMap["mean"]=rule::mean; |
| 2621 | ruleMap["stdev"]=rule::stdev; |
| 2622 | ruleMap["median"]=rule::median; |
| 2623 | ruleMap["proportion"]=rule::proportion; |
| 2624 | ruleMap["count"]=rule::count; |
| 2625 | ruleMap["min"]=rule::min; |
| 2626 | ruleMap["max"]=rule::max; |
| 2627 | ruleMap["custom"]=rule::custom; |
| 2628 | ruleMap["mode"]=rule::mode; |
| 2629 | ruleMap["sum"]=rule::sum; |
| 2630 | ruleMap["percentile"]=rule::percentile; |
| 2631 | ruleMap["allpoints"]=rule::allpoints; |
| 2632 | |
| 2633 | //initialize fieldMap |
| 2634 | std::map<std::string, std::string> fieldMap; |
| 2635 | fieldMap["point"]="point"; |
| 2636 | fieldMap["centroid"]="cntrd"; |
| 2637 | fieldMap["mean"]="mean"; |
| 2638 | fieldMap["stdev"]="stdev"; |
| 2639 | fieldMap["median"]="median"; |
| 2640 | fieldMap["proportion"]="prop"; |
| 2641 | fieldMap["count"]="count"; |
| 2642 | fieldMap["min"]="min"; |
| 2643 | fieldMap["max"]="max"; |
| 2644 | fieldMap["custom"]="custom"; |
| 2645 | fieldMap["mode"]="mode"; |
| 2646 | fieldMap["sum"]="sum"; |
| 2647 | fieldMap["percentile"]="perc"; |
| 2648 | fieldMap["allpoints"]="allp"; |
| 2649 | |
| 2650 | statfactory::StatFactory stat; |
| 2651 | if(srcnodata_opt.size()){ |
| 2652 | while(srcnodata_opt.size()<bndnodata_opt.size()) |
| 2653 | srcnodata_opt.push_back(srcnodata_opt[0]); |
| 2654 | stat.setNoDataValues(srcnodata_opt); |
| 2655 | } |
| 2656 | Jim maskReader; |
| 2657 | if(mask_opt.size()){ |
| 2658 | try{ |
| 2659 | //todo: open with resampling, resolution and projection according to input |
| 2660 | maskReader.open(mask_opt[0]); |
| 2661 | if(mskband_opt[0]>=maskReader.nrOfBand()){ |
| 2662 | string errorString="Error: illegal mask band"; |
| 2663 | throw(errorString); |
| 2664 | } |
| 2665 | } |
| 2666 | catch(string error){ |
| 2667 | cerr << error << std::endl; |
| 2668 | throw; |
| 2669 | } |
| 2670 | } |
| 2671 | Vector2d<unsigned int> posdata; |
| 2672 | |
| 2673 | if(output_opt.empty()){ |
| 2674 | std::cerr << "No output dataset provided (use option -o). Use --help for help information"; |
| 2675 | return(CE_Failure); |
| 2676 | } |
| 2677 | |
| 2678 | //check if rule contains allpoints |
| 2679 | if(find(rule_opt.begin(),rule_opt.end(),"allpoints")!=rule_opt.end()){ |
| 2680 | string errorstring="Error: allpoints not supported for random and grid sampling"; |
| 2681 | throw(errorstring); |
| 2682 | } |
| 2683 | //convert start and end band options to vector of band indexes |
| 2684 | if(bstart_opt.size()){ |
| 2685 | if(bend_opt.size()!=bstart_opt.size()){ |
| 2686 | string errorstring="Error: options for start and end band indexes must be provided as pairs, missing end band"; |
| 2687 | throw(errorstring); |
| 2688 | } |
| 2689 | band_opt.clear(); |
| 2690 | for(int ipair=0;ipair<bstart_opt.size();++ipair){ |
| 2691 | if(bend_opt[ipair]<=bstart_opt[ipair]){ |
| 2692 | string errorstring="Error: index for end band must be smaller then start band"; |
| 2693 | throw(errorstring); |
| 2694 | } |
| 2695 | for(int iband=bstart_opt[ipair];iband<=bend_opt[ipair];++iband) |
| 2696 | band_opt.push_back(iband); |
| 2697 | } |
| 2698 | } |
| 2699 | else if(band_opt.empty()){ |
| 2700 | size_t iband=0; |
| 2701 | while(band_opt.size()<nrOfBand()) |
| 2702 | band_opt.push_back(iband++); |
| 2703 | } |
| 2704 | int nband=(band_opt.size()) ? band_opt.size() : this->nrOfBand(); |
| 2705 | if(class_opt.size()){ |
| 2706 | if(nband>1){ |
| 2707 | cerr << "Warning: using only first band of multiband image" << endl; |
| 2708 | nband=1; |
| 2709 | band_opt.clear(); |
| 2710 | band_opt.push_back(0); |
| 2711 | } |
| 2712 | } |
| 2713 | |
| 2714 | if(verbose_opt[0]>1) |
| 2715 | std::cout << "Number of bands in input image: " << this->nrOfBand() << std::endl; |
| 2716 | |
| 2717 | OGRFieldType fieldType; |
| 2718 | int ogr_typecount=11;//hard coded for now! |
| 2719 | if(verbose_opt[0]>1) |
| 2720 | std::cout << "field and label types can be: "; |
| 2721 | for(int iType = 0; iType < ogr_typecount; ++iType){ |
| 2722 | if(verbose_opt[0]>1) |
| 2723 | std::cout << " " << OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType); |
| 2724 | if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 2725 | && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType), |
| 2726 | ftype_opt[0].c_str())) |
| 2727 | fieldType=(OGRFieldType) iType; |
| 2728 | } |
| 2729 | switch( fieldType ){ |
| 2730 | case OFTInteger: |
| 2731 | case OFTReal: |
| 2732 | case OFTRealList: |
| 2733 | case OFTString: |
| 2734 | if(verbose_opt[0]>1) |
| 2735 | std::cout << std::endl << "field type is: " << OGRFieldDefn::GetFieldTypeName(fieldType) << std::endl; |
| 2736 | break; |
| 2737 | default: |
| 2738 | cerr << "field type " << OGRFieldDefn::GetFieldTypeName(fieldType) << " not supported" << std::endl; |
| 2739 | return(CE_Failure); |
| 2740 | break; |
| 2741 | } |
| 2742 | |
| 2743 | const char* pszMessage; |
| 2744 | // void* pProgressArg=NULL; |
| 2745 | // GDALProgressFunc pfnProgress=GDALTermProgress; |
| 2746 | // double progress=0; |
| 2747 | srand(time(NULL)); |
| 2748 | |
| 2749 | bool sampleIsRaster=false; |
| 2750 | |
| 2751 | VectorOgr sampleReaderOgr; |
| 2752 | // ImgWriterOgr sampleWriterOgr; |
| 2753 | VectorOgr sampleWriterOgr; |
| 2754 | |
| 2755 | Vector2d<int> maskBuffer; |
| 2756 | // sampleWriterOgr.open("/vsimem/virtual",ogrformat_opt[0]); |
| 2757 | |
| 2758 | char **papszOptions=NULL; |
| 2759 | for(std::vector<std::string>::const_iterator optionIt=option_opt.begin();optionIt!=option_opt.end();++optionIt) |
| 2760 | papszOptions=CSLAddString(papszOptions,optionIt->c_str()); |
| 2761 | |
| 2762 | char **vsiOptions=NULL; |
| 2763 | std::string overwriteOption="OVERWRITE=YES"; |
| 2764 | vsiOptions=CSLAddString(vsiOptions,overwriteOption.c_str()); |
| 2765 | std::string vsifn; |
| 2766 | if(random_opt.size()){ |
| 2767 | //create simple random sampling within boundary |
| 2768 | double ulx,uly,lrx,lry; |
| 2769 | this->getBoundingBox(ulx,uly,lrx,lry); |
| 2770 | vsifn="/vsimem/random.sqlite"; |
| 2771 | if(random_opt[0]>0){ |
| 2772 | if(verbose_opt[0]) |
| 2773 | std::cout << "Opening " << vsifn << endl; |
| 2774 | try{ |
| 2775 | sampleWriterOgr.open(vsifn,"SQLite"); |
| 2776 | } |
| 2777 | catch(std::string errorString){ |
| 2778 | std::cerr << errorString << endl; |
| 2779 | std::cerr << "Warning: could not open sampleWriterOgr" << endl; |
| 2780 | throw; |
| 2781 | //todo: check if error handling is needed |
| 2782 | } |
| 2783 | if(verbose_opt[0]) |
| 2784 | std::cout << "Pushing layer random in " << vsifn << endl; |
| 2785 | sampleWriterOgr.pushLayer("random",this->getProjectionRef(),wkbPoint,vsiOptions); |
| 2786 | if(verbose_opt[0]) |
| 2787 | std::cout << "Pushed layer random in " << vsifn << endl; |
| 2788 | // sampleWriterOgr.open(vsifn,"random","ESRI Shapefile", "wkbPoint"); |
| 2789 | } |
| 2790 | if(maskReader.isInit()){ |
| 2791 | if(verbose_opt[0]) |
| 2792 | std::cout << "read data block from maskReader" << std::endl; |
| 2793 | double maskULI,maskULJ,maskLRI,maskLRJ; |
| 2794 | maskReader.geo2image(ulx,uly,maskULI,maskULJ); |
| 2795 | maskReader.geo2image(lrx,lry,maskLRI,maskLRJ); |
| 2796 | maskULI=(maskULI>0)? maskULI : 0; |
| 2797 | maskULJ=(maskULJ>0)? maskULJ : 0; |
| 2798 | maskLRI=(maskLRI<maskReader.nrOfCol())? maskLRI : maskReader.nrOfCol()-1; |
| 2799 | maskLRJ=(maskLRJ<maskReader.nrOfRow())? maskLRJ : maskReader.nrOfRow()-1; |
| 2800 | maskReader.readDataBlock(maskBuffer,maskULI,maskLRI,maskULJ,maskLRJ,mskband_opt[0]); |
| 2801 | } |
| 2802 | OGRPoint pt; |
| 2803 | unsigned int ipoint=0; |
| 2804 | unsigned int trials=0; |
| 2805 | double maxTrials=nrOfCol()*nrOfRow(); |
| 2806 | size_t outOfRegion=0; |
| 2807 | while(ipoint<random_opt[0]&&trials<=maxTrials){ |
| 2808 | ++trials; |
| 2809 | double theX=ulx+static_cast<double>(rand())/(RAND_MAX)*(lrx-ulx); |
| 2810 | double theY=uly-static_cast<double>(rand())/(RAND_MAX)*(uly-lry); |
| 2811 | //check if point is valid |
| 2812 | bool valid=true; |
| 2813 | //test |
| 2814 | double maskI,maskJ; |
| 2815 | if(maskReader.isInit()){ |
| 2816 | maskReader.geo2image(theX,theY,maskI,maskJ); |
| 2817 | maskI=static_cast<unsigned int>(maskI); |
| 2818 | maskJ=static_cast<unsigned int>(maskJ); |
| 2819 | if(maskI<0||maskI>=maskBuffer.nrOfCol()){ |
| 2820 | ++outOfRegion; |
| 2821 | continue; |
| 2822 | } |
| 2823 | if(maskJ<0||maskJ>=maskBuffer.nrOfRow()){ |
| 2824 | ++outOfRegion; |
| 2825 | continue; |
| 2826 | } |
| 2827 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 2828 | if(maskBuffer[maskJ][maskI]==msknodata_opt[ivalue]){ |
| 2829 | valid=false; |
| 2830 | break; |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | if(!valid) |
| 2835 | continue; |
| 2836 | else |
| 2837 | ++ipoint; |
| 2838 | pt.setX(theX); |
| 2839 | pt.setY(theY); |
| 2840 | // OGRFeature *pointFeature; |
| 2841 | OGRFeature *pointFeature=sampleWriterOgr.createFeature(); |
| 2842 | // pointFeature=sampleWriterOgr.createFeature(); |
| 2843 | pointFeature->SetGeometry( &pt ); |
| 2844 | sampleWriterOgr.pushFeature(pointFeature); |
| 2845 | // if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){ |
| 2846 | // string errorString="Error: failed to create feature in vector dataset"; |
| 2847 | // throw(errorString); |
| 2848 | // } |
| 2849 | // OGRFeature::DestroyFeature(pointFeature); |
| 2850 | } |
| 2851 | if(verbose_opt[0]) |
| 2852 | std::cout << "out of region: " << outOfRegion << " from " << trials << " trials"<< std::endl; |
| 2853 | if(!ipoint){ |
| 2854 | ostringstream ess; |
| 2855 | ess << "Error: no random point created after " << trials << " trials" << endl; |
| 2856 | throw(ess.str()); |
| 2857 | } |
| 2858 | else if(trials>=maxTrials) |
| 2859 | std::cerr << "Warning: maximum number of trials reached (" << maxTrials << ")" << std::endl; |
| 2860 | else if(verbose_opt[0]) |
| 2861 | std::cout << "number of points created from " << trials << " trials: " << ipoint << std::endl; |
| 2862 | sampleWriterOgr.write(); |
| 2863 | sampleWriterOgr.close(); |
| 2864 | // sample_opt.push_back(vsifn); |
| 2865 | } |
| 2866 | else if(grid_opt.size()){ |
| 2867 | bool initSample=true; |
| 2868 | //create systematic grid of points |
| 2869 | double ulx,uly,lrx,lry; |
| 2870 | this->getBoundingBox(ulx,uly,lrx,lry); |
| 2871 | vsifn="/vsimem/grid.sqlite"; |
| 2872 | if(uly-grid_opt[0]/2<lry&&ulx+grid_opt[0]/2>lrx){ |
| 2873 | string errorString="Error: grid distance too large"; |
| 2874 | throw(errorString); |
| 2875 | } |
| 2876 | else if(grid_opt[0]>0){ |
| 2877 | if(verbose_opt[0]) |
| 2878 | std::cout << "Opening " << vsifn << endl; |
| 2879 | try{ |
| 2880 | sampleWriterOgr.open(vsifn,"SQLite"); |
| 2881 | } |
| 2882 | catch(std::string errorString){ |
| 2883 | initSample=false; |
| 2884 | std::cerr << errorString << endl; |
| 2885 | std::cerr << "Warning: could not open sampleWriterOgr" << endl; |
| 2886 | //todo: check if error handling is needed |
| 2887 | } |
| 2888 | if(verbose_opt[0]) |
| 2889 | std::cout << "Pushing layer grid in " << vsifn << endl; |
| 2890 | if(initSample){ |
| 2891 | sampleWriterOgr.pushLayer("grid",this->getProjectionRef(),wkbPoint,vsiOptions); |
| 2892 | } |
| 2893 | if(verbose_opt[0]) |
| 2894 | std::cout << "Pushed layer grid in " << vsifn << endl; |
| 2895 | // sampleWriterOgr.open(vsifn,"grid","ESRI Shapefile", "wkbPoint"); |
| 2896 | // sampleWriterOgr.createLayer("points", this->getProjection(), wkbPoint, papszOptions); |
| 2897 | } |
| 2898 | else{ |
| 2899 | string errorString="Error: grid distance must be strictly positive number"; |
| 2900 | throw(errorString); |
| 2901 | } |
| 2902 | OGRPoint pt; |
| 2903 | unsigned int ipoint=0; |
| 2904 | for(double theY=uly-grid_opt[0]/2;theY>lry;theY-=grid_opt[0]){ |
| 2905 | for(double theX=ulx+grid_opt[0]/2;theX<lrx;theX+=grid_opt[0]){ |
| 2906 | OGRFeature *pointFeature; |
| 2907 | pointFeature=sampleWriterOgr.createFeature(); |
| 2908 | pt.setX(theX); |
| 2909 | pt.setY(theY); |
| 2910 | pointFeature->SetGeometry( &pt ); |
| 2911 | sampleWriterOgr.pushFeature(pointFeature); |
| 2912 | // if(sampleWriterOgr.createFeature(pointFeature) != OGRERR_NONE ){ |
| 2913 | // string errorString="Failed to create feature in vector dataset"; |
| 2914 | // throw(errorString); |
| 2915 | // } |
| 2916 | ++ipoint; |
| 2917 | // OGRFeature::DestroyFeature(pointFeature); |
| 2918 | } |
| 2919 | } |
| 2920 | if(!ipoint){ |
| 2921 | string errorString="Error: no points created in grid"; |
| 2922 | throw(errorString); |
| 2923 | } |
| 2924 | sampleWriterOgr.write(); |
| 2925 | sampleWriterOgr.close(); |
| 2926 | // sample_opt.push_back(vsifn); |
| 2927 | } |
| 2928 | else{ |
| 2929 | string errorString="Error: no random nor grid option provided. Use --help for help information"; |
| 2930 | throw(errorString); |
| 2931 | } |
| 2932 | // if(verbose_opt[0]) |
| 2933 | // std::cout << "number of features in sample: " << sampleReaderOgr.getFeatureCount() << std::endl; |
| 2934 | |
| 2935 | |
| 2936 | // ImgWriterOgr ogrWriter; |
| 2937 | // VectorOgr ogrWriter; |
| 2938 | double vectords_ulx; |
| 2939 | double vectords_uly; |
| 2940 | double vectords_lrx; |
| 2941 | double vectords_lry; |
| 2942 | bool calculateSpatialStatistics=false; |
| 2943 | |
| 2944 | // if(verbose_opt[0]) |
| 2945 | // std::cout << "opening " << output_opt[0] << " for writing output vector dataset" << std::endl; |
| 2946 | // ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 2947 | //if class_opt not set, get number of classes from input image for these rules |
| 2948 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 2949 | switch(ruleMap[rule_opt[irule]]){ |
| 2950 | case(rule::point): |
| 2951 | case(rule::centroid): |
| 2952 | case(rule::allpoints): |
| 2953 | break; |
| 2954 | case(rule::proportion): |
| 2955 | case(rule::count): |
| 2956 | case(rule::custom): |
| 2957 | case(rule::mode):{ |
| 2958 | if(class_opt.empty()){ |
| 2959 | int theBand=0; |
| 2960 | double minValue=0; |
| 2961 | double maxValue=0; |
| 2962 | if(band_opt.size()) |
| 2963 | theBand=band_opt[0]; |
| 2964 | this->getMinMax(minValue,maxValue,theBand); |
| 2965 | int nclass=maxValue-minValue+1; |
| 2966 | if(nclass<0&&nclass<256){ |
| 2967 | string errorString="Error: Could not automatically define classes, please set class option"; |
| 2968 | throw(errorString); |
| 2969 | } |
| 2970 | for(int iclass=minValue;iclass<=maxValue;++iclass) |
| 2971 | class_opt.push_back(iclass); |
| 2972 | } |
| 2973 | }//deliberate fall through: calculate spatial statistics for all non-point like rules |
| 2974 | default: |
| 2975 | calculateSpatialStatistics=true; |
| 2976 | break; |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | //support multiple layers |
| 2981 | // int nlayerRead=sampleReaderOgr.getDataSource()->GetLayerCount(); |
| 2982 | // if(layer_opt.empty()) |
| 2983 | // layer_opt.push_back(std::string()); |
| 2984 | int nlayerRead=1; |
| 2985 | unsigned long int ntotalvalid=0; |
| 2986 | |
| 2987 | if(verbose_opt[0]) |
| 2988 | std::cout << "opening " << vsifn << " for reading input vector dataset" << std::endl; |
| 2989 | sampleReaderOgr.open(vsifn); |
| 2990 | // sampleReaderOgr.readFeatures();//now already done when opening |
| 2991 | |
| 2992 | OGRLayer *readLayer=sampleReaderOgr.getLayer(); |
| 2993 | |
| 2994 | double layer_ulx; |
| 2995 | double layer_uly; |
| 2996 | double layer_lrx; |
| 2997 | double layer_lry; |
| 2998 | sampleReaderOgr.getExtent(layer_ulx,layer_uly,layer_lrx,layer_lry); |
| 2999 | if(verbose_opt[0]) |
| 3000 | std::cout << "--ulx " << layer_ulx<< " --uly " << layer_uly<< " --lrx " << layer_lrx << " --lry " << layer_lry << std::endl; |
| 3001 | // bool hasCoverage=((layer_ulx >= this->getLrx())&&(layer_lrx <= this->getUlx())&&(layer_lry >= this->getUly())&&(layer_uly <= this->getLry())); |
| 3002 | bool hasCoverage=this->covers(layer_ulx,layer_uly,layer_lrx,layer_lry); |
| 3003 | if(!hasCoverage) |
| 3004 | std::cerr << "Warning: No coverage for layer in " << vsifn << endl; |
| 3005 | |
| 3006 | //align bounding box to input image |
| 3007 | layer_ulx-=fmod(layer_ulx-this->getUlx(),this->getDeltaX()); |
| 3008 | layer_lrx+=fmod(this->getLrx()-layer_lrx,this->getDeltaX()); |
| 3009 | layer_uly+=fmod(this->getUly()-layer_uly,this->getDeltaY()); |
| 3010 | layer_lry-=fmod(layer_lry-this->getLry(),this->getDeltaY()); |
| 3011 | |
| 3012 | //do not read outside input image |
| 3013 | if(layer_ulx<this->getUlx()) |
| 3014 | layer_ulx=this->getUlx(); |
| 3015 | if(layer_lrx>this->getLrx()) |
| 3016 | layer_lrx=this->getLrx(); |
| 3017 | if(layer_uly>this->getUly()) |
| 3018 | layer_uly=this->getUly(); |
| 3019 | if(layer_lry<this->getLry()) |
| 3020 | layer_lry=this->getLry(); |
| 3021 | |
| 3022 | //read entire block for coverage in memory |
| 3023 | //todo: use different data types |
| 3024 | vector< Vector2d<float> > readValuesReal(nband); |
| 3025 | vector< Vector2d<int> > readValuesInt(nband); |
| 3026 | |
| 3027 | double layer_uli; |
| 3028 | double layer_ulj; |
| 3029 | double layer_lri; |
| 3030 | double layer_lrj; |
| 3031 | this->geo2image(layer_ulx,layer_uly,layer_uli,layer_ulj); |
| 3032 | this->geo2image(layer_lrx,layer_lry,layer_lri,layer_lrj); |
| 3033 | |
| 3034 | if(verbose_opt[0]) |
| 3035 | std::cout << "reading layer geometry" << std::endl; |
| 3036 | OGRwkbGeometryType layerGeometry=readLayer->GetLayerDefn()->GetGeomType(); |
| 3037 | if(verbose_opt[0]) |
| 3038 | std::cout << "layer geometry: " << OGRGeometryTypeToName(layerGeometry) << std::endl; |
| 3039 | |
| 3040 | if(layerGeometry==wkbPoint){ |
| 3041 | if(verbose_opt[0]) |
| 3042 | std::cout << "layerGeometry is wkbPoint" << std::endl; |
| 3043 | if(calculateSpatialStatistics){ |
| 3044 | if(verbose_opt[0]) |
| 3045 | std::cout << "calculateSpatialStatistics is true" << std::endl; |
| 3046 | // if(buffer_opt.size()){ |
| 3047 | // if(buffer_opt[0]<getDeltaX()) |
| 3048 | // buffer_opt[0]=getDeltaX(); |
| 3049 | // } |
| 3050 | // else |
| 3051 | // buffer_opt.push_back(getDeltaX()); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | //extend bounding box with buffer |
| 3056 | if(buffer_opt.size()){ |
| 3057 | layer_uli-=buffer_opt[0]/getDeltaX(); |
| 3058 | layer_ulj-=buffer_opt[0]/getDeltaY(); |
| 3059 | layer_lri+=buffer_opt[0]/getDeltaX(); |
| 3060 | layer_lrj+=buffer_opt[0]/getDeltaY(); |
| 3061 | } |
| 3062 | |
| 3063 | //we already checked there is coverage |
| 3064 | layer_uli=(layer_uli<0)? 0 : static_cast<int>(layer_uli); |
| 3065 | layer_ulj=(layer_ulj<0)? 0 : static_cast<int>(layer_ulj); |
| 3066 | layer_lri=(layer_lri>=this->nrOfCol())? this->nrOfCol()-1 : static_cast<int>(layer_lri); |
| 3067 | layer_lrj=(layer_lrj>=this->nrOfRow())? this->nrOfRow()-1 : static_cast<int>(layer_lrj); |
| 3068 | |
| 3069 | for(int iband=0;iband<nband;++iband){ |
| 3070 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3071 | if(theBand<0){ |
| 3072 | ostringstream ess; |
| 3073 | ess << "Error: illegal band " << theBand << " (must be positive and starting from 0)" << endl; |
| 3074 | throw(ess.str()); |
| 3075 | } |
| 3076 | if(theBand>=this->nrOfBand()){ |
| 3077 | ostringstream ess; |
| 3078 | ess << "Error: illegal band " << theBand << " (must be lower than number of bands in input raster dataset)" << endl; |
| 3079 | throw(ess.str()); |
| 3080 | } |
| 3081 | switch( fieldType ){ |
| 3082 | case OFTInteger: |
| 3083 | this->readDataBlock(readValuesInt[iband],layer_uli,layer_lri,layer_ulj,layer_lrj,theBand); |
| 3084 | break; |
| 3085 | case OFTReal: |
| 3086 | default: |
| 3087 | this->readDataBlock(readValuesReal[iband],layer_uli,layer_lri,layer_ulj,layer_lrj,theBand); |
| 3088 | break; |
| 3089 | } |
| 3090 | } |
| 3091 | if(maskReader.isInit()&&random_opt.empty()){//mask already read when random is set |
| 3092 | if(maskReader.covers(layer_ulx,layer_uly,layer_lrx,layer_lry,"ALL_COVERED")) |
| 3093 | maskReader.readDataBlock(maskBuffer,layer_uli,layer_lri,layer_ulj,layer_lrj,mskband_opt[0]); |
| 3094 | else{ |
| 3095 | string errorString="Error: mask does not entirely cover the geographical layer boundaries"; |
| 3096 | throw(errorString); |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | |
| 3101 | // float theThreshold=(threshold_opt.size()==layer_opt.size())? threshold_opt[layerIndex]: threshold_opt[0]; |
| 3102 | float theThreshold=threshold_opt[0]; |
| 3103 | |
| 3104 | bool createPolygon=true; |
| 3105 | if(find(rule_opt.begin(),rule_opt.end(),"allpoints")!=rule_opt.end()) |
| 3106 | createPolygon=false; |
| 3107 | |
| 3108 | // OGRLayer *writeLayer; |
| 3109 | std::string layername=(layer_opt.size())? layer_opt[0] : readLayer->GetName(); |
| 3110 | bool initWriter=true; |
| 3111 | if(createPolygon){ |
| 3112 | //create polygon |
| 3113 | if(verbose_opt[0]) |
| 3114 | std::cout << "create polygons" << std::endl; |
| 3115 | if(verbose_opt[0]) |
| 3116 | std::cout << "open ogrWriter for polygons (2)" << std::endl; |
| 3117 | |
| 3118 | try{ |
| 3119 | ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 3120 | } |
| 3121 | catch(std::string errorString){ |
| 3122 | initWriter=false; |
| 3123 | std::cerr << errorString << endl; |
| 3124 | std::cerr << "Warning: could not open " << output_opt[0] << endl; |
| 3125 | } |
| 3126 | if(initWriter){ |
| 3127 | ogrWriter.pushLayer(layername,this->getProjectionRef(),wkbPolygon,papszOptions); |
| 3128 | } |
| 3129 | } |
| 3130 | else{ |
| 3131 | if(verbose_opt[0]) |
| 3132 | std::cout << "create points in layer " << readLayer->GetName() << std::endl; |
| 3133 | if(verbose_opt[0]) |
| 3134 | std::cout << "open ogrWriter for points (2)" << std::endl; |
| 3135 | try{ |
| 3136 | ogrWriter.open(output_opt[0],ogrformat_opt[0]); |
| 3137 | } |
| 3138 | catch(std::string errorString){ |
| 3139 | initWriter=false; |
| 3140 | std::cerr << errorString << endl; |
| 3141 | std::cerr << "Warning: could not open " << output_opt[0] << endl; |
| 3142 | } |
| 3143 | if(initWriter){ |
| 3144 | ogrWriter.pushLayer(layername,this->getProjectionRef(),wkbPoint,papszOptions); |
| 3145 | } |
| 3146 | } |
| 3147 | if(verbose_opt[0]){ |
| 3148 | std::cout << "ogrWriter opened" << std::endl; |
| 3149 | // writeLayer=ogrWriter.createLayer(readLayer->GetName(), this->getProjection(), wkbPoint, papszOptions); |
| 3150 | } |
| 3151 | if(verbose_opt[0]) |
| 3152 | std::cout << "copy fields" << std::endl; |
| 3153 | ogrWriter.copyFields(sampleReaderOgr); |
| 3154 | |
| 3155 | if(verbose_opt[0]) |
| 3156 | std::cout << "create new fields" << std::endl; |
| 3157 | if(label_opt.size()) |
| 3158 | ogrWriter.createField("label",OFTInteger); |
| 3159 | if(fid_opt.size()) |
| 3160 | ogrWriter.createField(fid_opt[0],OFTInteger64,0); |
| 3161 | |
| 3162 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 3163 | for(int iband=0;iband<nband;++iband){ |
| 3164 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3165 | ostringstream fs; |
| 3166 | if(bandNames_opt.size()){ |
| 3167 | if(rule_opt.size()>1) |
| 3168 | fs << fieldMap[rule_opt[irule]]; |
| 3169 | fs << bandNames_opt[iband]; |
| 3170 | } |
| 3171 | else{ |
| 3172 | if(rule_opt.size()>1||nband==1) |
| 3173 | fs << fieldMap[rule_opt[irule]]; |
| 3174 | if(nband>1) |
| 3175 | fs << "b" << theBand; |
| 3176 | } |
| 3177 | switch(ruleMap[rule_opt[irule]]){ |
| 3178 | case(rule::proportion): |
| 3179 | case(rule::count):{//count for each class |
| 3180 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 3181 | ostringstream fsclass; |
| 3182 | fsclass << fs.str() << "class" << class_opt[iclass]; |
| 3183 | ogrWriter.createField(fsclass.str(),fieldType); |
| 3184 | } |
| 3185 | break; |
| 3186 | } |
| 3187 | case(rule::percentile):{//for each percentile |
| 3188 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 3189 | ostringstream fsperc; |
| 3190 | fsperc << fs.str() << percentile_opt[iperc]; |
| 3191 | ogrWriter.createField(fsperc.str(),fieldType); |
| 3192 | } |
| 3193 | break; |
| 3194 | } |
| 3195 | default: |
| 3196 | ogrWriter.createField(fs.str(),fieldType); |
| 3197 | break; |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | // OGRFeature *readFeature; |
| 3202 | unsigned long int nfeatureLayer=sampleReaderOgr.getFeatureCount(); |
| 3203 | unsigned long int ntotalvalidLayer=0; |
| 3204 | |
| 3205 | |
| 3206 | ogrWriter.resize(sampleReaderOgr.getFeatureCount()); |
| 3207 | if(verbose_opt[0]) |
| 3208 | std::cout << "start processing " << sampleReaderOgr.getFeatureCount() << " features" << std::endl; |
| 3209 | // progress=0; |
| 3210 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 3211 | // readLayer->ResetReading(); |
| 3212 | // while( (readFeature = readLayer->GetNextFeature()) != NULL ){ |
| 3213 | |
| 3214 | #if JIPLIB_PROCESS_IN_PARALLEL == 1 |
| 3215 | #pragma omp parallel for ordered shared(ntotalvalidLayer, ntotalvalid, ogrWriter) |
| 3216 | #else |
| 3217 | #endif |
| 3218 | for(unsigned long int ifeature=0;ifeature<sampleReaderOgr.getFeatureCount();++ifeature){ |
| 3219 | // OGRFeature *readFeature=sampleReaderOgr.getFeatureRef(ifeature); |
| 3220 | OGRFeature *readFeature=sampleReaderOgr.cloneFeature(ifeature); |
| 3221 | bool validFeature=false; |
| 3222 | if(verbose_opt[0]>2) |
| 3223 | std::cout << "reading feature " << readFeature->GetFID() << std::endl; |
| 3224 | if(theThreshold>0){//percentual value |
| 3225 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 3226 | p*=100.0; |
| 3227 | if(p>theThreshold){ |
| 3228 | continue;//do not select for now, go to next feature |
| 3229 | } |
| 3230 | } |
| 3231 | else{//absolute value |
| 3232 | if(threshold_opt.size()==sampleReaderOgr.getLayerCount()){ |
| 3233 | if(ntotalvalidLayer>=-theThreshold){ |
| 3234 | continue;//do not select any more pixels, go to next column feature |
| 3235 | } |
| 3236 | } |
| 3237 | else{ |
| 3238 | if(ntotalvalid>=-theThreshold){ |
| 3239 | continue;//do not select any more pixels, go to next column feature |
| 3240 | } |
| 3241 | } |
| 3242 | } |
| 3243 | if(verbose_opt[0]>2) |
| 3244 | std::cout << "processing feature " << readFeature->GetFID() << std::endl; |
| 3245 | //get x and y from readFeature |
| 3246 | // double x,y; |
| 3247 | OGRGeometry *poGeometry; |
| 3248 | poGeometry = readFeature->GetGeometryRef(); |
| 3249 | assert(poGeometry!=NULL); |
| 3250 | try{ |
| 3251 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint ){ |
| 3252 | OGRPoint readPoint = *((OGRPoint *) poGeometry); |
| 3253 | double i_centre,j_centre; |
| 3254 | this->geo2image(readPoint.getX(),readPoint.getY(),i_centre,j_centre); |
| 3255 | //nearest neighbour |
| 3256 | j_centre=static_cast<int>(j_centre); |
| 3257 | i_centre=static_cast<int>(i_centre); |
| 3258 | |
| 3259 | double bufferI=buffer_opt.size() ? buffer_opt[0]/getDeltaX() : 1; |
| 3260 | double bufferJ=buffer_opt.size() ? buffer_opt[0]/getDeltaY() : 1; |
| 3261 | double uli=i_centre-bufferI; |
| 3262 | double ulj=j_centre-bufferJ; |
| 3263 | double lri=i_centre+bufferI; |
| 3264 | double lrj=j_centre+bufferJ; |
| 3265 | // double uli=i_centre-buffer_opt[0]; |
| 3266 | // double ulj=j_centre-buffer_opt[0]; |
| 3267 | // double lri=i_centre+buffer_opt[0]; |
| 3268 | // double lrj=j_centre+buffer_opt[0]; |
| 3269 | |
| 3270 | //nearest neighbour |
| 3271 | ulj=static_cast<int>(ulj); |
| 3272 | uli=static_cast<int>(uli); |
| 3273 | lrj=static_cast<int>(lrj); |
| 3274 | lri=static_cast<int>(lri); |
| 3275 | |
| 3276 | //check if j is out of bounds |
| 3277 | if(static_cast<int>(ulj)<0||static_cast<int>(ulj)>=this->nrOfRow()) |
| 3278 | continue; |
| 3279 | //check if j is out of bounds |
| 3280 | if(static_cast<int>(uli)<0||static_cast<int>(lri)>=this->nrOfCol()) |
| 3281 | continue; |
| 3282 | |
| 3283 | OGRPoint ulPoint,urPoint,llPoint,lrPoint; |
| 3284 | OGRPolygon writePolygon; |
| 3285 | OGRPoint writePoint; |
| 3286 | OGRLinearRing writeRing; |
| 3287 | OGRFeature *writePolygonFeature; |
| 3288 | |
| 3289 | int nPointPolygon=0; |
| 3290 | if(createPolygon){ |
| 3291 | if(disc_opt[0]){ |
| 3292 | double radius=buffer_opt.size() ? buffer_opt[0] : sqrt(this->getDeltaX()*this->getDeltaY()); |
| 3293 | unsigned short nstep = 25; |
| 3294 | for(int i=0;i<nstep;++i){ |
| 3295 | OGRPoint aPoint; |
| 3296 | aPoint.setX(readPoint.getX()+this->getDeltaX()/2.0+radius*cos(2*PI*i/nstep)); |
| 3297 | aPoint.setY(readPoint.getY()-this->getDeltaY()/2.0+radius*sin(2*PI*i/nstep)); |
| 3298 | writeRing.addPoint(&aPoint); |
| 3299 | } |
| 3300 | writePolygon.addRing(&writeRing); |
| 3301 | writePolygon.closeRings(); |
| 3302 | } |
| 3303 | else{ |
| 3304 | double ulx,uly,lrx,lry; |
| 3305 | this->image2geo(uli,ulj,ulx,uly); |
| 3306 | this->image2geo(lri,lrj,lrx,lry); |
| 3307 | ulPoint.setX(ulx-this->getDeltaX()/2.0); |
| 3308 | ulPoint.setY(uly+this->getDeltaY()/2.0); |
| 3309 | lrPoint.setX(lrx+this->getDeltaX()/2.0); |
| 3310 | lrPoint.setY(lry-this->getDeltaY()/2.0); |
| 3311 | urPoint.setX(lrx+this->getDeltaX()/2.0); |
| 3312 | urPoint.setY(uly+this->getDeltaY()/2.0); |
| 3313 | llPoint.setX(ulx-this->getDeltaX()/2.0); |
| 3314 | llPoint.setY(lry-this->getDeltaY()/2.0); |
| 3315 | |
| 3316 | writeRing.addPoint(&ulPoint); |
| 3317 | writeRing.addPoint(&urPoint); |
| 3318 | writeRing.addPoint(&lrPoint); |
| 3319 | writeRing.addPoint(&llPoint); |
| 3320 | writePolygon.addRing(&writeRing); |
| 3321 | writePolygon.closeRings(); |
| 3322 | } |
| 3323 | writePolygonFeature = ogrWriter.createFeature(); |
| 3324 | // writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 3325 | if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 3326 | cerr << "writing feature failed" << std::endl; |
| 3327 | writePolygonFeature->SetGeometry(&writePolygon); |
| 3328 | if(verbose_opt[0]>1) |
| 3329 | std::cout << "copying new fields write polygon " << std::endl; |
| 3330 | if(verbose_opt[0]>1) |
| 3331 | std::cout << "write feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl; |
| 3332 | |
| 3333 | OGRPoint readPoint; |
| 3334 | if(find(rule_opt.begin(),rule_opt.end(),"centroid")!=rule_opt.end()){ |
| 3335 | if(verbose_opt[0]>1) |
| 3336 | std::cout << "get centroid" << std::endl; |
| 3337 | writePolygon.Centroid(&readPoint); |
| 3338 | double i,j; |
| 3339 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 3340 | int indexJ=static_cast<int>(j-layer_ulj); |
| 3341 | int indexI=static_cast<int>(i-layer_uli); |
| 3342 | bool valid=true; |
| 3343 | valid=valid&&(indexJ>=0); |
| 3344 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 3345 | valid=valid&&(indexI>=0); |
| 3346 | // valid=valid&&(indexI<this->nrOfCol()); |
| 3347 | valid=valid&&(j<=layer_lrj); |
| 3348 | valid=valid&&(i<=layer_lri); |
| 3349 | |
| 3350 | if(valid){ |
| 3351 | if(maskReader.isInit()){ |
| 3352 | double maskI,maskJ; |
| 3353 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 3354 | maskI=static_cast<unsigned int>(maskI); |
| 3355 | maskJ=static_cast<unsigned int>(maskJ); |
| 3356 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 3357 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 3358 | if(maskBuffer[maskJ][maskI]==msknodata_opt[ivalue]){ |
| 3359 | valid=false; |
| 3360 | break; |
| 3361 | } |
| 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | if(valid){ |
| 3367 | if(srcnodata_opt.empty()) |
| 3368 | validFeature=true; |
| 3369 | else{ |
| 3370 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 3371 | switch( fieldType ){ |
| 3372 | case OFTInteger:{ |
| 3373 | int value; |
| 3374 | value=((readValuesInt[vband])[indexJ])[indexI]; |
| 3375 | if(value==srcnodata_opt[vband]) |
| 3376 | valid=false; |
| 3377 | break; |
| 3378 | } |
| 3379 | case OFTReal:{ |
| 3380 | double value; |
| 3381 | value=((readValuesReal[vband])[indexJ])[indexI]; |
| 3382 | if(value==srcnodata_opt[vband]) |
| 3383 | valid=false; |
| 3384 | break; |
| 3385 | } |
| 3386 | } |
| 3387 | if(!valid) |
| 3388 | continue; |
| 3389 | else |
| 3390 | validFeature=true; |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | if(valid){ |
| 3395 | if(label_opt.size()) |
| 3396 | writePolygonFeature->SetField("label",label_opt[0]); |
| 3397 | if(fid_opt.size()) |
| 3398 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 3399 | for(int iband=0;iband<nband;++iband){ |
| 3400 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3401 | //write fields for point on surface and centroid |
| 3402 | string fieldname; |
| 3403 | ostringstream fs; |
| 3404 | if(bandNames_opt.size()){ |
| 3405 | if(rule_opt.size()>1) |
| 3406 | fs << fieldMap["centroid"]; |
| 3407 | fs << bandNames_opt[iband]; |
| 3408 | } |
| 3409 | else{ |
| 3410 | if(rule_opt.size()>1||nband==1) |
| 3411 | fs << fieldMap["centroid"]; |
| 3412 | if(nband>1) |
| 3413 | fs << "b" << theBand; |
| 3414 | } |
| 3415 | fieldname=fs.str(); |
| 3416 | switch( fieldType ){ |
| 3417 | case OFTInteger: |
| 3418 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iband])[indexJ])[indexI])); |
| 3419 | break; |
| 3420 | case OFTReal: |
| 3421 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iband])[indexJ])[indexI]); |
| 3422 | break; |
| 3423 | default://not supported |
| 3424 | std::string errorString="field type not supported"; |
| 3425 | throw(errorString); |
| 3426 | break; |
| 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | }//if centroid |
| 3431 | if(find(rule_opt.begin(),rule_opt.end(),"point")!=rule_opt.end()){ |
| 3432 | if(verbose_opt[0]>1) |
| 3433 | std::cout << "get point on surface" << std::endl; |
| 3434 | if(writePolygon.PointOnSurface(&readPoint)!=OGRERR_NONE) |
| 3435 | writePolygon.Centroid(&readPoint); |
| 3436 | double i,j; |
| 3437 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 3438 | int indexJ=static_cast<int>(j-layer_ulj); |
| 3439 | int indexI=static_cast<int>(i-layer_uli); |
| 3440 | bool valid=true; |
| 3441 | valid=valid&&(indexJ>=0); |
| 3442 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 3443 | valid=valid&&(indexI>=0); |
| 3444 | // valid=valid&&(indexI<this->nrOfCol()); |
| 3445 | valid=valid&&(j<=layer_lrj); |
| 3446 | valid=valid&&(i<=layer_lri); |
| 3447 | |
| 3448 | if(valid){ |
| 3449 | if(maskReader.isInit()){ |
| 3450 | double maskI,maskJ; |
| 3451 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 3452 | maskI=static_cast<unsigned int>(maskI); |
| 3453 | maskJ=static_cast<unsigned int>(maskJ); |
| 3454 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 3455 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 3456 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 3457 | valid=false; |
| 3458 | break; |
| 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | } |
| 3463 | } |
| 3464 | if(valid){ |
| 3465 | if(srcnodata_opt.empty()) |
| 3466 | validFeature=true; |
| 3467 | else{ |
| 3468 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 3469 | switch( fieldType ){ |
| 3470 | case OFTInteger:{ |
| 3471 | int value; |
| 3472 | value=((readValuesInt[vband])[indexJ])[indexI]; |
| 3473 | if(value==srcnodata_opt[vband]) |
| 3474 | valid=false; |
| 3475 | break; |
| 3476 | } |
| 3477 | case OFTReal:{ |
| 3478 | double value; |
| 3479 | value=((readValuesReal[vband])[indexJ])[indexI]; |
| 3480 | if(value==srcnodata_opt[vband]) |
| 3481 | valid=false; |
| 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | if(!valid) |
| 3486 | continue; |
| 3487 | else |
| 3488 | validFeature=true; |
| 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | if(valid){ |
| 3493 | if(label_opt.size()) |
| 3494 | writePolygonFeature->SetField("label",label_opt[0]); |
| 3495 | if(fid_opt.size()) |
| 3496 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 3497 | for(int iband=0;iband<nband;++iband){ |
| 3498 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3499 | //write fields for point on surface and centroid |
| 3500 | string fieldname; |
| 3501 | ostringstream fs; |
| 3502 | if(bandNames_opt.size()){ |
| 3503 | if(rule_opt.size()>1) |
| 3504 | fs << fieldMap["point"]; |
| 3505 | fs << bandNames_opt[iband]; |
| 3506 | } |
| 3507 | else{ |
| 3508 | if(rule_opt.size()>1||nband==1) |
| 3509 | fs << fieldMap["point"]; |
| 3510 | if(nband>1) |
| 3511 | fs << "b" << theBand; |
| 3512 | } |
| 3513 | fieldname=fs.str(); |
| 3514 | switch( fieldType ){ |
| 3515 | case OFTInteger: |
| 3516 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iband])[indexJ])[indexI])); |
| 3517 | break; |
| 3518 | case OFTReal: |
| 3519 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iband])[indexJ])[indexI]); |
| 3520 | break; |
| 3521 | default://not supported |
| 3522 | std::string errorString="field type not supported"; |
| 3523 | throw(errorString); |
| 3524 | break; |
| 3525 | } |
| 3526 | } |
| 3527 | } |
| 3528 | }//if point |
| 3529 | }//if createPolygon |
| 3530 | |
| 3531 | if(calculateSpatialStatistics||!createPolygon){ |
| 3532 | Vector2d<double> polyValues; |
| 3533 | vector<double> polyClassValues; |
| 3534 | |
| 3535 | if(class_opt.size()){ |
| 3536 | polyClassValues.resize(class_opt.size()); |
| 3537 | //initialize |
| 3538 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 3539 | polyClassValues[iclass]=0; |
| 3540 | } |
| 3541 | else |
| 3542 | polyValues.resize(nband); |
| 3543 | |
| 3544 | OGRPoint thePoint; |
| 3545 | for(int j=ulj;j<=lrj;++j){ |
| 3546 | for(int i=uli;i<=lri;++i){ |
| 3547 | //check if within raster image |
| 3548 | if(i<0||i>=this->nrOfCol()) |
| 3549 | continue; |
| 3550 | if(j<0||j>=this->nrOfRow()) |
| 3551 | continue; |
| 3552 | int indexJ=j-layer_ulj; |
| 3553 | int indexI=i-layer_uli; |
| 3554 | if(indexJ<0) |
| 3555 | continue; |
| 3556 | if(indexI<0) |
| 3557 | continue; |
| 3558 | // if(indexJ>=this->nrOfRow()) |
| 3559 | // indexJ=this->nrOfRow()-1; |
| 3560 | // if(indexI>=this->nrOfCol()) |
| 3561 | // indexI=this->nrOfCol()-1; |
| 3562 | if(indexJ>=this->nrOfRow()) |
| 3563 | continue; |
| 3564 | if(indexI>=this->nrOfCol()) |
| 3565 | continue; |
| 3566 | |
| 3567 | double theX=0; |
| 3568 | double theY=0; |
| 3569 | this->image2geo(i,j,theX,theY); |
| 3570 | thePoint.setX(theX); |
| 3571 | thePoint.setY(theY); |
| 3572 | if(disc_opt[0]&&buffer_opt.size()){ |
| 3573 | double radius=buffer_opt.size() ? buffer_opt[0] : sqrt(this->getDeltaX()*this->getDeltaY()); |
| 3574 | if((theX-readPoint.getX())*(theX-readPoint.getX())+(theY-readPoint.getY())*(theY-readPoint.getY())>radius*radius) |
| 3575 | continue; |
| 3576 | } |
| 3577 | bool valid=true; |
| 3578 | |
| 3579 | if(maskReader.isInit()){ |
| 3580 | double maskI,maskJ; |
| 3581 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 3582 | maskI=static_cast<unsigned int>(maskI); |
| 3583 | maskJ=static_cast<unsigned int>(maskJ); |
| 3584 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 3585 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 3586 | // if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 3587 | if(maskBuffer[maskJ][maskI]==msknodata_opt[ivalue]){ |
| 3588 | valid=false; |
| 3589 | break; |
| 3590 | } |
| 3591 | } |
| 3592 | } |
| 3593 | } |
| 3594 | if(srcnodata_opt.size()){ |
| 3595 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 3596 | switch( fieldType ){ |
| 3597 | case OFTInteger:{ |
| 3598 | int value=((readValuesInt[vband])[indexJ])[indexI]; |
| 3599 | if(value==srcnodata_opt[vband]){ |
| 3600 | valid=false; |
| 3601 | } |
| 3602 | break; |
| 3603 | } |
| 3604 | default:{ |
| 3605 | float value=((readValuesReal[vband])[indexJ])[indexI]; |
| 3606 | if(value==srcnodata_opt[vband]){ |
| 3607 | valid=false; |
| 3608 | } |
| 3609 | break; |
| 3610 | } |
| 3611 | } |
| 3612 | } |
| 3613 | } |
| 3614 | if(!valid) |
| 3615 | continue; |
| 3616 | else |
| 3617 | validFeature=true; |
| 3618 | |
| 3619 | ++nPointPolygon; |
| 3620 | OGRFeature *writePointFeature; |
| 3621 | if(valid&&!createPolygon){//write all points |
| 3622 | if(polythreshold_opt.size()){ |
| 3623 | if(polythreshold_opt[0]>0){ |
| 3624 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 3625 | p*=100.0; |
| 3626 | if(p>polythreshold_opt[0]) |
| 3627 | continue;//do not select for now, go to next feature |
| 3628 | } |
| 3629 | else if(nPointPolygon>-polythreshold_opt[0]) |
| 3630 | continue; |
| 3631 | } |
| 3632 | //create feature |
| 3633 | writePointFeature = OGRFeature::CreateFeature(ogrWriter.getLayer()->GetLayerDefn()); |
| 3634 | // writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 3635 | if(verbose_opt[0]>1) |
| 3636 | std::cout << "copying fields from point feature " << std::endl; |
| 3637 | if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 3638 | cerr << "writing feature failed" << std::endl; |
| 3639 | if(verbose_opt[0]>1) |
| 3640 | std::cout << "set geometry as point " << std::endl; |
| 3641 | writePointFeature->SetGeometry(&thePoint); |
| 3642 | assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint); |
| 3643 | if(verbose_opt[0]>1){ |
| 3644 | std::cout << "write feature has " << writePointFeature->GetFieldCount() << " fields:" << std::endl; |
| 3645 | for(int iField=0;iField<writePointFeature->GetFieldCount();++iField){ |
| 3646 | std::string fieldname=ogrWriter.getLayer()->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 3647 | // std::string fieldname=writeLayer->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 3648 | cout << fieldname << endl; |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | // if(valid&&class_opt.size()){ |
| 3653 | // short value=0; |
| 3654 | // switch( fieldType ){ |
| 3655 | // case OFTInteger: |
| 3656 | // value=((readValuesInt[0])[indexJ])[indexI]; |
| 3657 | // break; |
| 3658 | // case OFTReal: |
| 3659 | // value=((readValuesReal[0])[indexJ])[indexI]; |
| 3660 | // break; |
| 3661 | // } |
| 3662 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 3663 | // if(value==class_opt[iclass]) |
| 3664 | // polyClassValues[iclass]+=1; |
| 3665 | // } |
| 3666 | // } |
| 3667 | if(valid){ |
| 3668 | if(!createPolygon&&label_opt.size()) |
| 3669 | writePointFeature->SetField("label",label_opt[0]); |
| 3670 | if(!createPolygon&&fid_opt.size()) |
| 3671 | writePointFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 3672 | for(int iband=0;iband<nband;++iband){ |
| 3673 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3674 | double value=0; |
| 3675 | switch( fieldType ){ |
| 3676 | case OFTInteger: |
| 3677 | value=((readValuesInt[iband])[indexJ])[indexI]; |
| 3678 | break; |
| 3679 | case OFTReal: |
| 3680 | value=((readValuesReal[iband])[indexJ])[indexI]; |
| 3681 | break; |
| 3682 | } |
| 3683 | if(!iband&&class_opt.size()){ |
| 3684 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 3685 | if(value==class_opt[iclass]) |
| 3686 | polyClassValues[iclass]+=1; |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | if(verbose_opt[0]>1) |
| 3691 | std::cout << ": " << value << std::endl; |
| 3692 | if(!createPolygon){//write all points within polygon |
| 3693 | string fieldname; |
| 3694 | ostringstream fs; |
| 3695 | if(bandNames_opt.size()){ |
| 3696 | if(rule_opt.size()>1) |
| 3697 | fs << fieldMap["allpoints"]; |
| 3698 | fs << bandNames_opt[iband]; |
| 3699 | } |
| 3700 | else{ |
| 3701 | if(rule_opt.size()>1||nband==1) |
| 3702 | fs << fieldMap["allpoints"]; |
| 3703 | if(nband>1) |
| 3704 | fs << "b" << theBand; |
| 3705 | } |
| 3706 | fieldname=fs.str(); |
| 3707 | int fieldIndex=writePointFeature->GetFieldIndex(fieldname.c_str()); |
| 3708 | if(fieldIndex<0){ |
| 3709 | ostringstream ess; |
| 3710 | ess << "field " << fieldname << " was not found" << endl; |
| 3711 | throw(ess.str()); |
| 3712 | // return(CE_Failure); |
| 3713 | } |
| 3714 | if(verbose_opt[0]>1) |
| 3715 | std::cout << "set field " << fieldname << " to " << value << std::endl; |
| 3716 | switch( fieldType ){ |
| 3717 | case OFTInteger: |
| 3718 | case OFTReal: |
| 3719 | writePointFeature->SetField(fieldname.c_str(),value); |
| 3720 | break; |
| 3721 | default://not supported |
| 3722 | assert(0); |
| 3723 | break; |
| 3724 | } |
| 3725 | } |
| 3726 | else{ |
| 3727 | polyValues[iband].push_back(value); |
| 3728 | } |
| 3729 | }//iband |
| 3730 | } |
| 3731 | if(valid&&!createPolygon){ |
| 3732 | //write feature |
| 3733 | if(verbose_opt[0]>1) |
| 3734 | std::cout << "creating point feature" << std::endl; |
| 3735 | // if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 3736 | if(ogrWriter.getLayer()->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 3737 | std::string errorString="Failed to create feature in ogr vector dataset"; |
| 3738 | throw(errorString); |
| 3739 | } |
| 3740 | //destroy feature |
| 3741 | // OGRFeature::DestroyFeature( writePointFeature ); |
| 3742 | #pragma omp atomic |
| 3743 | ++ntotalvalid; |
| 3744 | #pragma omp atomic |
| 3745 | ++ntotalvalidLayer; |
| 3746 | } |
| 3747 | }//for in i |
| 3748 | }//for int j |
| 3749 | |
| 3750 | if(createPolygon){ |
| 3751 | //do not create if no points found within polygon |
| 3752 | if(!nPointPolygon){ |
| 3753 | if(verbose_opt[0]) |
| 3754 | cout << "no points found in polygon, continuing" << endl; |
| 3755 | continue; |
| 3756 | } |
| 3757 | //write field attributes to polygon feature |
| 3758 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 3759 | //skip centroid and point |
| 3760 | if(ruleMap[rule_opt[irule]]==rule::centroid||ruleMap[rule_opt[irule]]==rule::point) |
| 3761 | continue; |
| 3762 | if(!irule&&label_opt.size()) |
| 3763 | writePolygonFeature->SetField("label",label_opt[0]); |
| 3764 | if(!irule&&fid_opt.size()) |
| 3765 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 3766 | for(int iband=0;iband<nband;++iband){ |
| 3767 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 3768 | vector<double> theValue; |
| 3769 | vector<string> fieldname; |
| 3770 | ostringstream fs; |
| 3771 | if(bandNames_opt.size()){ |
| 3772 | if(rule_opt.size()>1) |
| 3773 | fs << fieldMap[rule_opt[irule]]; |
| 3774 | fs << bandNames_opt[iband]; |
| 3775 | } |
| 3776 | else{ |
| 3777 | if(rule_opt.size()>1||nband==1) |
| 3778 | fs << fieldMap[rule_opt[irule]]; |
| 3779 | if(nband>1) |
| 3780 | fs << "b" << theBand; |
| 3781 | } |
| 3782 | switch(ruleMap[rule_opt[irule]]){ |
| 3783 | case(rule::proportion)://deliberate fall through |
| 3784 | stat.normalize_pct(polyClassValues); |
| 3785 | case(rule::count):{//count for each class |
| 3786 | for(int index=0;index<polyClassValues.size();++index){ |
| 3787 | theValue.push_back(polyClassValues[index]); |
| 3788 | ostringstream fsclass; |
| 3789 | fsclass << fs.str() << "class" << class_opt[index]; |
| 3790 | fieldname.push_back(fsclass.str()); |
| 3791 | } |
| 3792 | break; |
| 3793 | } |
| 3794 | case(rule::mode):{ |
| 3795 | //maximum votes in polygon |
| 3796 | if(verbose_opt[0]) |
| 3797 | std::cout << "number of points in polygon: " << nPointPolygon << std::endl; |
| 3798 | //search for class with maximum votes |
| 3799 | int maxClass=stat.mymin(class_opt); |
| 3800 | vector<double>::iterator maxit; |
| 3801 | maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end()); |
| 3802 | int maxIndex=distance(polyClassValues.begin(),maxit); |
| 3803 | maxClass=class_opt[maxIndex]; |
| 3804 | if(verbose_opt[0]>0) |
| 3805 | std::cout << "maxClass: " << maxClass << std::endl; |
| 3806 | theValue.push_back(maxClass); |
| 3807 | fieldname.push_back(fs.str()); |
| 3808 | break; |
| 3809 | } |
| 3810 | case(rule::mean): |
| 3811 | theValue.push_back(stat.mean(polyValues[iband])); |
| 3812 | fieldname.push_back(fs.str()); |
| 3813 | break; |
| 3814 | case(rule::median): |
| 3815 | theValue.push_back(stat.median(polyValues[iband])); |
| 3816 | fieldname.push_back(fs.str()); |
| 3817 | break; |
| 3818 | case(rule::stdev): |
| 3819 | theValue.push_back(sqrt(stat.var(polyValues[iband]))); |
| 3820 | fieldname.push_back(fs.str()); |
| 3821 | break; |
| 3822 | case(rule::percentile):{ |
| 3823 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 3824 | theValue.push_back(stat.percentile(polyValues[iband],polyValues[iband].begin(),polyValues[iband].end(),percentile_opt[iperc])); |
| 3825 | ostringstream fsperc; |
| 3826 | fsperc << fs.str() << percentile_opt[iperc]; |
| 3827 | fieldname.push_back(fsperc.str()); |
| 3828 | } |
| 3829 | break; |
| 3830 | } |
| 3831 | case(rule::sum): |
| 3832 | theValue.push_back(stat.sum(polyValues[iband])); |
| 3833 | fieldname.push_back(fs.str()); |
| 3834 | break; |
| 3835 | case(rule::max): |
| 3836 | theValue.push_back(stat.mymax(polyValues[iband])); |
| 3837 | fieldname.push_back(fs.str()); |
| 3838 | break; |
| 3839 | case(rule::min): |
| 3840 | theValue.push_back(stat.mymin(polyValues[iband])); |
| 3841 | fieldname.push_back(fs.str()); |
| 3842 | break; |
| 3843 | case(rule::point): |
| 3844 | case(rule::centroid): |
| 3845 | theValue.push_back(polyValues[iband].back()); |
| 3846 | fieldname.push_back(fs.str()); |
| 3847 | break; |
| 3848 | default://not supported |
| 3849 | break; |
| 3850 | } |
| 3851 | for(int ivalue=0;ivalue<theValue.size();++ivalue){ |
| 3852 | switch( fieldType ){ |
| 3853 | case OFTInteger: |
| 3854 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),static_cast<int>(theValue[ivalue])); |
| 3855 | break; |
| 3856 | case OFTReal: |
| 3857 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),theValue[ivalue]); |
| 3858 | break; |
| 3859 | case OFTString: |
| 3860 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),type2string<double>(theValue[ivalue]).c_str()); |
| 3861 | break; |
| 3862 | default://not supported |
| 3863 | std::string errorString="field type not supported"; |
| 3864 | throw(errorString); |
| 3865 | break; |
| 3866 | } |
| 3867 | } |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | } |
| 3872 | if(createPolygon&&validFeature){ |
| 3873 | // if(createPolygon){ |
| 3874 | //write polygon feature |
| 3875 | //todo: create only in case of valid feature |
| 3876 | if(verbose_opt[0]>1) |
| 3877 | std::cout << "creating polygon feature (1)" << std::endl; |
| 3878 | // if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){ |
| 3879 | // std::string errorString="Failed to create polygon feature in ogr vector dataset"; |
| 3880 | // throw(errorString); |
| 3881 | // } |
| 3882 | //test: no need to destroy anymore? |
| 3883 | // OGRFeature::DestroyFeature( writePolygonFeature ); |
| 3884 | //make sure to use setFeature instead of pushFeature when in processing in parallel!!! |
| 3885 | if(!writePolygonFeature) |
| 3886 | std::cerr << "Warning: NULL feature" << ifeature << std::endl; |
| 3887 | ogrWriter.setFeature(ifeature,writePolygonFeature); |
| 3888 | #pragma omp atomic |
| 3889 | ++ntotalvalid; |
| 3890 | #pragma omp atomic |
| 3891 | ++ntotalvalidLayer; |
| 3892 | } |
| 3893 | } |
| 3894 | else{ |
| 3895 | OGRPolygon readPolygon; |
| 3896 | OGRMultiPolygon readMultiPolygon; |
| 3897 | |
| 3898 | //get envelope |
| 3899 | OGREnvelope* psEnvelope=new OGREnvelope(); |
| 3900 | |
| 3901 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 3902 | readPolygon = *((OGRPolygon *) poGeometry); |
| 3903 | readPolygon.closeRings(); |
| 3904 | readPolygon.getEnvelope(psEnvelope); |
| 3905 | } |
| 3906 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 3907 | readMultiPolygon = *((OGRMultiPolygon *) poGeometry); |
| 3908 | readMultiPolygon.closeRings(); |
| 3909 | readMultiPolygon.getEnvelope(psEnvelope); |
| 3910 | } |
| 3911 | else{ |
| 3912 | std::string test; |
| 3913 | test=poGeometry->getGeometryName(); |
| 3914 | ostringstream oss; |
| 3915 | oss << "geometry " << test << " not supported"; |
| 3916 | throw(oss.str()); |
| 3917 | } |
| 3918 | |
| 3919 | double ulx,uly,lrx,lry; |
| 3920 | double uli,ulj,lri,lrj; |
| 3921 | ulx=psEnvelope->MinX; |
| 3922 | uly=psEnvelope->MaxY; |
| 3923 | lrx=psEnvelope->MaxX; |
| 3924 | lry=psEnvelope->MinY; |
| 3925 | delete psEnvelope; |
| 3926 | |
| 3927 | //check if feature is covered by input raster dataset |
| 3928 | if(!this->covers(ulx,uly,lrx,lry,allCovered_opt[0])){ |
| 3929 | if(verbose_opt[0]>2){ |
| 3930 | std::cout << "Warning: raster does not cover polygon: " << readFeature->GetFID() << std::endl; |
| 3931 | std::cout<< std::setprecision(12) << "--ulx " << ulx << " --uly " << uly << " --lrx " << lrx << " --lry " << lry << std::endl; |
| 3932 | } |
| 3933 | continue; |
| 3934 | } |
| 3935 | else if(verbose_opt[0]>2){ |
| 3936 | std::cout << "raster does cover polygon: " << readFeature->GetFID() << std::endl; |
| 3937 | std::cout<< std::setprecision(12) << "--ulx " << ulx << " --uly " << uly << " --lrx " << lrx << " --lry " << lry << std::endl; |
| 3938 | } |
| 3939 | OGRFeature *writePolygonFeature; |
| 3940 | int nPointPolygon=0; |
| 3941 | if(createPolygon){ |
| 3942 | // writePolygonFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 3943 | writePolygonFeature = ogrWriter.createFeature(); |
| 3944 | writePolygonFeature->SetGeometry(poGeometry); |
| 3945 | //writePolygonFeature and readFeature are both of type wkbPolygon |
| 3946 | if(writePolygonFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 3947 | cerr << "writing feature failed" << std::endl; |
| 3948 | if(verbose_opt[0]>1) |
| 3949 | std::cout << "copying new fields write polygon " << std::endl; |
| 3950 | if(verbose_opt[0]>1) |
| 3951 | std::cout << "write polygon feature has " << writePolygonFeature->GetFieldCount() << " fields" << std::endl; |
| 3952 | } |
| 3953 | |
| 3954 | OGRPoint readPoint; |
| 3955 | if(find(rule_opt.begin(),rule_opt.end(),"centroid")!=rule_opt.end()){ |
| 3956 | if(verbose_opt[0]>1) |
| 3957 | std::cout << "get centroid" << std::endl; |
| 3958 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon) |
| 3959 | readPolygon.Centroid(&readPoint); |
| 3960 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon) |
| 3961 | readMultiPolygon.Centroid(&readPoint); |
| 3962 | |
| 3963 | double i,j; |
| 3964 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 3965 | int indexJ=static_cast<int>(j-layer_ulj); |
| 3966 | int indexI=static_cast<int>(i-layer_uli); |
| 3967 | bool valid=true; |
| 3968 | valid=valid&&(indexJ>=0); |
| 3969 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 3970 | valid=valid&&(indexI>=0); |
| 3971 | // valid=valid&&(indexI<this->nrOfCol()); |
| 3972 | valid=valid&&(j<=layer_lrj); |
| 3973 | valid=valid&&(i<=layer_lri); |
| 3974 | if(valid){ |
| 3975 | if(maskReader.isInit()){ |
| 3976 | double maskI,maskJ; |
| 3977 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 3978 | maskI=static_cast<unsigned int>(maskI); |
| 3979 | maskJ=static_cast<unsigned int>(maskJ); |
| 3980 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 3981 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 3982 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 3983 | valid=false; |
| 3984 | break; |
| 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | if(valid){ |
| 3991 | if(srcnodata_opt.empty()) |
| 3992 | validFeature=true; |
| 3993 | else{ |
| 3994 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 3995 | switch( fieldType ){ |
| 3996 | case OFTInteger:{ |
| 3997 | int value; |
| 3998 | value=((readValuesInt[vband])[indexJ])[indexI]; |
| 3999 | if(value==srcnodata_opt[vband]) |
| 4000 | valid=false; |
| 4001 | break; |
| 4002 | } |
| 4003 | case OFTReal:{ |
| 4004 | double value; |
| 4005 | value=((readValuesReal[vband])[indexJ])[indexI]; |
| 4006 | if(value==srcnodata_opt[vband]) |
| 4007 | valid=false; |
| 4008 | break; |
| 4009 | } |
| 4010 | } |
| 4011 | if(!valid) |
| 4012 | continue; |
| 4013 | else |
| 4014 | validFeature=true; |
| 4015 | } |
| 4016 | } |
| 4017 | } |
| 4018 | if(valid){ |
| 4019 | if(label_opt.size()) |
| 4020 | writePolygonFeature->SetField("label",label_opt[0]); |
| 4021 | if(fid_opt.size()) |
| 4022 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 4023 | for(int iband=0;iband<nband;++iband){ |
| 4024 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 4025 | //write fields for point on surface and centroid |
| 4026 | string fieldname; |
| 4027 | ostringstream fs; |
| 4028 | if(bandNames_opt.size()){ |
| 4029 | if(rule_opt.size()>1) |
| 4030 | fs << fieldMap["centroid"]; |
| 4031 | fs << bandNames_opt[iband]; |
| 4032 | } |
| 4033 | else{ |
| 4034 | if(rule_opt.size()>1||nband==1) |
| 4035 | fs << fieldMap["centroid"]; |
| 4036 | if(nband>1) |
| 4037 | fs << "b" << theBand; |
| 4038 | } |
| 4039 | fieldname=fs.str(); |
| 4040 | switch( fieldType ){ |
| 4041 | case OFTInteger: |
| 4042 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iband])[indexJ])[indexI])); |
| 4043 | break; |
| 4044 | case OFTReal: |
| 4045 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iband])[indexJ])[indexI]); |
| 4046 | break; |
| 4047 | default://not supported |
| 4048 | std::string errorString="field type not supported"; |
| 4049 | throw(errorString); |
| 4050 | break; |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | } |
| 4055 | if(find(rule_opt.begin(),rule_opt.end(),"point")!=rule_opt.end()){ |
| 4056 | if(verbose_opt[0]>1) |
| 4057 | std::cout << "get point on surface" << std::endl; |
| 4058 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 4059 | if(readPolygon.PointOnSurface(&readPoint)!=OGRERR_NONE) |
| 4060 | readPolygon.Centroid(&readPoint); |
| 4061 | } |
| 4062 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 4063 | // if(readMultiPolygon.PointOnSurface(&readPoint)!=OGRERR_NONE) |
| 4064 | readMultiPolygon.Centroid(&readPoint); |
| 4065 | } |
| 4066 | double i,j; |
| 4067 | this->geo2image(readPoint.getX(),readPoint.getY(),i,j); |
| 4068 | int indexJ=static_cast<int>(j-layer_ulj); |
| 4069 | int indexI=static_cast<int>(i-layer_uli); |
| 4070 | bool valid=true; |
| 4071 | valid=valid&&(indexJ>=0); |
| 4072 | // valid=valid&&(indexJ<this->nrOfRow()); |
| 4073 | valid=valid&&(indexI>=0); |
| 4074 | // valid=valid&&(indexI<this->nrOfCol()); |
| 4075 | valid=valid&&(j<=layer_lrj); |
| 4076 | valid=valid&&(i<=layer_lri); |
| 4077 | if(valid){ |
| 4078 | if(maskReader.isInit()){ |
| 4079 | double maskI,maskJ; |
| 4080 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 4081 | maskI=static_cast<unsigned int>(maskI); |
| 4082 | maskJ=static_cast<unsigned int>(maskJ); |
| 4083 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 4084 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 4085 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 4086 | valid=false; |
| 4087 | break; |
| 4088 | } |
| 4089 | } |
| 4090 | } |
| 4091 | } |
| 4092 | } |
| 4093 | if(valid){ |
| 4094 | if(srcnodata_opt.empty()) |
| 4095 | validFeature=true; |
| 4096 | else{ |
| 4097 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 4098 | switch( fieldType ){ |
| 4099 | case OFTInteger:{ |
| 4100 | int value; |
| 4101 | value=((readValuesInt[vband])[indexJ])[indexI]; |
| 4102 | if(value==srcnodata_opt[vband]) |
| 4103 | valid=false; |
| 4104 | break; |
| 4105 | } |
| 4106 | case OFTReal:{ |
| 4107 | double value; |
| 4108 | value=((readValuesReal[vband])[indexJ])[indexI]; |
| 4109 | if(value==srcnodata_opt[vband]) |
| 4110 | valid=false; |
| 4111 | break; |
| 4112 | } |
| 4113 | } |
| 4114 | if(!valid) |
| 4115 | continue; |
| 4116 | else |
| 4117 | validFeature=true; |
| 4118 | } |
| 4119 | } |
| 4120 | } |
| 4121 | if(valid){ |
| 4122 | if(label_opt.size()) |
| 4123 | writePolygonFeature->SetField("label",label_opt[0]); |
| 4124 | if(fid_opt.size()) |
| 4125 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 4126 | for(int iband=0;iband<nband;++iband){ |
| 4127 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 4128 | //write fields for point on surface and centroid |
| 4129 | string fieldname; |
| 4130 | ostringstream fs; |
| 4131 | if(bandNames_opt.size()){ |
| 4132 | if(rule_opt.size()>1) |
| 4133 | fs << fieldMap["point"]; |
| 4134 | fs << bandNames_opt[iband]; |
| 4135 | } |
| 4136 | else{ |
| 4137 | if(rule_opt.size()>1||nband==1) |
| 4138 | fs << fieldMap["point"]; |
| 4139 | if(nband>1) |
| 4140 | fs << "b" << theBand; |
| 4141 | } |
| 4142 | fieldname=fs.str(); |
| 4143 | switch( fieldType ){ |
| 4144 | case OFTInteger: |
| 4145 | writePolygonFeature->SetField(fieldname.c_str(),static_cast<int>(((readValuesInt[iband])[indexJ])[indexI])); |
| 4146 | break; |
| 4147 | case OFTReal: |
| 4148 | writePolygonFeature->SetField(fieldname.c_str(),((readValuesReal[iband])[indexJ])[indexI]); |
| 4149 | break; |
| 4150 | default://not supported |
| 4151 | std::string errorString="field type not supported"; |
| 4152 | throw(errorString); |
| 4153 | break; |
| 4154 | } |
| 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | if(calculateSpatialStatistics||ruleMap[rule_opt[0]]==rule::allpoints){ |
| 4159 | this->geo2image(ulx,uly,uli,ulj); |
| 4160 | this->geo2image(lrx,lry,lri,lrj); |
| 4161 | //nearest neighbour |
| 4162 | ulj=static_cast<int>(ulj); |
| 4163 | uli=static_cast<int>(uli); |
| 4164 | lrj=static_cast<int>(lrj); |
| 4165 | lri=static_cast<int>(lri); |
| 4166 | //iterate through all pixels |
| 4167 | if(verbose_opt[0]>1) |
| 4168 | std::cout << "bounding box for polygon feature " << ifeature << ": " << uli << " " << ulj << " " << lri << " " << lrj << std::endl; |
| 4169 | |
| 4170 | if(uli<0) |
| 4171 | uli=0; |
| 4172 | if(lri<0) |
| 4173 | lri=0; |
| 4174 | // if(uli>=this->nrOfCol()) |
| 4175 | // uli=this->nrOfCol()-1; |
| 4176 | if(uli>=layer_lri) |
| 4177 | uli=layer_lri-1; |
| 4178 | // if(lri>=this->nrOfCol()) |
| 4179 | // lri=this->nrOfCol()-1; |
| 4180 | if(lri>=layer_lri) |
| 4181 | lri=layer_lri-1; |
| 4182 | if(ulj<0) |
| 4183 | ulj=0; |
| 4184 | if(lrj<0) |
| 4185 | lrj=0; |
| 4186 | // if(ulj>=this->nrOfRow()) |
| 4187 | // ulj=this->nrOfRow()-1; |
| 4188 | if(ulj>=layer_lrj) |
| 4189 | ulj=layer_lrj-1; |
| 4190 | // if(lrj>=this->nrOfRow()) |
| 4191 | // lrj=this->nrOfRow()-1; |
| 4192 | if(lrj>=layer_lrj) |
| 4193 | lrj=layer_lrj-1; |
| 4194 | |
| 4195 | if(verbose_opt[0]>1) |
| 4196 | std::cout << "bounding box for polygon feature after check " << ifeature << ": " << uli << " " << ulj << " " << lri << " " << lrj << std::endl; |
| 4197 | Vector2d<double> polyValues; |
| 4198 | vector<double> polyClassValues; |
| 4199 | |
| 4200 | if(class_opt.size()){ |
| 4201 | polyClassValues.resize(class_opt.size()); |
| 4202 | //initialize |
| 4203 | for(int iclass=0;iclass<class_opt.size();++iclass) |
| 4204 | polyClassValues[iclass]=0; |
| 4205 | } |
| 4206 | else |
| 4207 | polyValues.resize(nband); |
| 4208 | |
| 4209 | OGRPoint thePoint; |
| 4210 | for(int j=ulj;j<=lrj;++j){ |
| 4211 | for(int i=uli;i<=lri;++i){ |
| 4212 | //check if within raster image |
| 4213 | // if(i<0||i>=this->nrOfCol()) |
| 4214 | if(i<0||i>=layer_lri) |
| 4215 | continue; |
| 4216 | // if(j<0||j>=this->nrOfRow()) |
| 4217 | // continue; |
| 4218 | if(j<0||j>=layer_lrj) |
| 4219 | continue; |
| 4220 | int indexJ=j-layer_ulj; |
| 4221 | int indexI=i-layer_uli; |
| 4222 | //test |
| 4223 | if(indexJ<0||indexJ>=this->nrOfRow()||indexI<0||indexI>=this->nrOfCol()) |
| 4224 | continue; |
| 4225 | |
| 4226 | double theX=0; |
| 4227 | double theY=0; |
| 4228 | this->image2geo(i,j,theX,theY); |
| 4229 | thePoint.setX(theX); |
| 4230 | thePoint.setY(theY); |
| 4231 | //check if point is on surface |
| 4232 | if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){ |
| 4233 | if(!readPolygon.Contains(&thePoint)) |
| 4234 | continue; |
| 4235 | } |
| 4236 | else if(wkbFlatten(poGeometry->getGeometryType()) == wkbMultiPolygon){ |
| 4237 | if(!readMultiPolygon.Contains(&thePoint)) |
| 4238 | continue; |
| 4239 | } |
| 4240 | |
| 4241 | bool valid=true; |
| 4242 | double maskI,maskJ; |
| 4243 | if(maskReader.isInit()){ |
| 4244 | maskReader.geo2image(readPoint.getX(),readPoint.getY(),maskI,maskJ); |
| 4245 | maskI=static_cast<unsigned int>(maskI); |
| 4246 | maskJ=static_cast<unsigned int>(maskJ); |
| 4247 | if(maskI>0&&maskI<maskBuffer.nrOfCol()&&maskJ>0&&maskJ<maskBuffer.nrOfRow()){ |
| 4248 | for(int ivalue=0;ivalue<msknodata_opt.size();++ivalue){ |
| 4249 | if(maskBuffer[indexJ][indexI]==msknodata_opt[ivalue]){ |
| 4250 | valid=false; |
| 4251 | break; |
| 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | if(srcnodata_opt.size()){ |
| 4257 | for(int vband=0;vband<bndnodata_opt.size();++vband){ |
| 4258 | switch( fieldType ){ |
| 4259 | case OFTInteger:{ |
| 4260 | int value=((readValuesInt[vband])[indexJ])[indexI]; |
| 4261 | if(value==srcnodata_opt[vband]){ |
| 4262 | valid=false; |
| 4263 | } |
| 4264 | break; |
| 4265 | } |
| 4266 | default:{ |
| 4267 | float value=((readValuesReal[vband])[indexJ])[indexI]; |
| 4268 | if(value==srcnodata_opt[vband]){ |
| 4269 | valid=false; |
| 4270 | } |
| 4271 | break; |
| 4272 | } |
| 4273 | } |
| 4274 | } |
| 4275 | } |
| 4276 | if(!valid) |
| 4277 | continue; |
| 4278 | else |
| 4279 | validFeature=true; |
| 4280 | |
| 4281 | if(verbose_opt[0]>1) |
| 4282 | std::cout << "point is on surface: " << thePoint.getX() << "," << thePoint.getY() << std::endl; |
| 4283 | ++nPointPolygon; |
| 4284 | |
| 4285 | OGRFeature *writePointFeature; |
| 4286 | if(!createPolygon){//write all points within polygon |
| 4287 | if(polythreshold_opt.size()){ |
| 4288 | if(polythreshold_opt[0]>0){ |
| 4289 | double p=static_cast<double>(rand())/(RAND_MAX); |
| 4290 | p*=100.0; |
| 4291 | if(p>polythreshold_opt[0]) |
| 4292 | continue;//do not select for now, go to next feature |
| 4293 | } |
| 4294 | else if(nPointPolygon>-polythreshold_opt[0]) |
| 4295 | continue; |
| 4296 | } |
| 4297 | // if(polythreshold_opt.size()) |
| 4298 | // if(nPointPolygon>=polythreshold_opt[0]) |
| 4299 | // continue; |
| 4300 | // if(threshold_opt.size()){ |
| 4301 | // if(threshold_opt[0]<=0){ |
| 4302 | // if(ntotalvalid>=-threshold_opt[0]) |
| 4303 | // continue; |
| 4304 | // } |
| 4305 | // else if(threshold_opt[0]<100){ |
| 4306 | // double p=static_cast<double>(rand())/(RAND_MAX); |
| 4307 | // p*=100.0; |
| 4308 | // if(p>threshold_opt[0]){ |
| 4309 | // continue;//do not select for now, go to next feature |
| 4310 | // } |
| 4311 | // } |
| 4312 | // } |
| 4313 | //create feature |
| 4314 | writePointFeature = OGRFeature::CreateFeature(ogrWriter.getLayer()->GetLayerDefn()); |
| 4315 | // writePointFeature = OGRFeature::CreateFeature(writeLayer->GetLayerDefn()); |
| 4316 | if(verbose_opt[0]>1) |
| 4317 | std::cout << "copying fields from polygons " << std::endl; |
| 4318 | if(writePointFeature->SetFrom(readFeature)!= OGRERR_NONE) |
| 4319 | cerr << "writing feature failed" << std::endl; |
| 4320 | if(verbose_opt[0]>1) |
| 4321 | std::cout << "set geometry as point " << std::endl; |
| 4322 | writePointFeature->SetGeometry(&thePoint); |
| 4323 | assert(wkbFlatten(writePointFeature->GetGeometryRef()->getGeometryType()) == wkbPoint); |
| 4324 | if(verbose_opt[0]>1){ |
| 4325 | std::cout << "write point feature has " << writePointFeature->GetFieldCount() << " fields:" << std::endl; |
| 4326 | for(int iField=0;iField<writePointFeature->GetFieldCount();++iField){ |
| 4327 | std::string fieldname=ogrWriter.getLayer()->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 4328 | // std::string fieldname=writeLayer->GetLayerDefn()->GetFieldDefn(iField)->GetNameRef(); |
| 4329 | cout << fieldname << endl; |
| 4330 | } |
| 4331 | } |
| 4332 | } |
| 4333 | // if(class_opt.size()){ |
| 4334 | // short value=0; |
| 4335 | // switch( fieldType ){ |
| 4336 | // case OFTInteger: |
| 4337 | // value=((readValuesInt[0])[indexJ])[indexI]; |
| 4338 | // break; |
| 4339 | // case OFTReal: |
| 4340 | // value=((readValuesReal[0])[indexJ])[indexI]; |
| 4341 | // break; |
| 4342 | // } |
| 4343 | // for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 4344 | // if(value==class_opt[iclass]) |
| 4345 | // polyClassValues[iclass]+=1; |
| 4346 | // } |
| 4347 | // } |
| 4348 | // else{ |
| 4349 | if(!createPolygon&&label_opt.size()) |
| 4350 | writePointFeature->SetField("label",label_opt[0]); |
| 4351 | if(!createPolygon&&fid_opt.size()) |
| 4352 | writePointFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 4353 | for(int iband=0;iband<nband;++iband){ |
| 4354 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 4355 | double value=0; |
| 4356 | switch( fieldType ){ |
| 4357 | case OFTInteger: |
| 4358 | value=((readValuesInt[iband])[indexJ])[indexI]; |
| 4359 | break; |
| 4360 | case OFTReal: |
| 4361 | value=((readValuesReal[iband])[indexJ])[indexI]; |
| 4362 | break; |
| 4363 | } |
| 4364 | |
| 4365 | if(!iband&&class_opt.size()){ |
| 4366 | for(int iclass=0;iclass<class_opt.size();++iclass){ |
| 4367 | if(value==class_opt[iclass]) |
| 4368 | polyClassValues[iclass]+=1; |
| 4369 | } |
| 4370 | } |
| 4371 | if(verbose_opt[0]>1) |
| 4372 | std::cout << ": " << value << std::endl; |
| 4373 | if(!createPolygon){//write all points within polygon |
| 4374 | string fieldname; |
| 4375 | ostringstream fs; |
| 4376 | if(bandNames_opt.size()){ |
| 4377 | if(rule_opt.size()>1) |
| 4378 | fs << fieldMap["allpoints"]; |
| 4379 | fs << bandNames_opt[iband]; |
| 4380 | } |
| 4381 | else{ |
| 4382 | if(rule_opt.size()>1||nband==1) |
| 4383 | fs << fieldMap["allpoints"]; |
| 4384 | if(nband>1) |
| 4385 | fs << "b" << theBand; |
| 4386 | } |
| 4387 | fieldname=fs.str(); |
| 4388 | int fieldIndex=writePointFeature->GetFieldIndex(fieldname.c_str()); |
| 4389 | if(fieldIndex<0){ |
| 4390 | ostringstream ess; |
| 4391 | ess << "field " << fieldname << " was not found" << endl; |
| 4392 | throw(ess.str()); |
| 4393 | // cerr << "field " << fieldname << " was not found" << endl; |
| 4394 | // return(CE_Failure); |
| 4395 | } |
| 4396 | if(verbose_opt[0]>1) |
| 4397 | std::cout << "set field " << fieldname << " to " << value << std::endl; |
| 4398 | switch( fieldType ){ |
| 4399 | case OFTInteger: |
| 4400 | case OFTReal: |
| 4401 | writePointFeature->SetField(fieldname.c_str(),value); |
| 4402 | break; |
| 4403 | default://not supported |
| 4404 | assert(0); |
| 4405 | break; |
| 4406 | } |
| 4407 | } |
| 4408 | else{ |
| 4409 | polyValues[iband].push_back(value); |
| 4410 | } |
| 4411 | }//iband |
| 4412 | if(!createPolygon){ |
| 4413 | //todo: only if valid feature? |
| 4414 | //write feature |
| 4415 | if(verbose_opt[0]) |
| 4416 | std::cout << "creating point feature " << ntotalvalidLayer << std::endl; |
| 4417 | // if(writeLayer->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 4418 | if(ogrWriter.getLayer()->CreateFeature( writePointFeature ) != OGRERR_NONE ){ |
| 4419 | std::string errorString="Failed to create feature in ogr vector dataset"; |
| 4420 | throw(errorString); |
| 4421 | } |
| 4422 | //destroy feature |
| 4423 | // OGRFeature::DestroyFeature( writePointFeature ); |
| 4424 | #pragma omp atomic |
| 4425 | ++ntotalvalid; |
| 4426 | #pragma omp atomic |
| 4427 | ++ntotalvalidLayer; |
| 4428 | } |
| 4429 | }//for in i |
| 4430 | }//for int j |
| 4431 | if(createPolygon){ |
| 4432 | //do not create if no points found within polygon |
| 4433 | if(!nPointPolygon){ |
| 4434 | if(verbose_opt[0]) |
| 4435 | cout << "no points found in polygon, continuing" << endl; |
| 4436 | continue; |
| 4437 | } |
| 4438 | //write field attributes to polygon feature |
| 4439 | for(int irule=0;irule<rule_opt.size();++irule){ |
| 4440 | //skip centroid and point |
| 4441 | if(ruleMap[rule_opt[irule]]==rule::centroid||ruleMap[rule_opt[irule]]==rule::point) |
| 4442 | continue; |
| 4443 | if(!irule&&label_opt.size()) |
| 4444 | writePolygonFeature->SetField("label",label_opt[0]); |
| 4445 | if(!irule&&fid_opt.size()) |
| 4446 | writePolygonFeature->SetField(fid_opt[0].c_str(),static_cast<GIntBig>(ifeature)); |
| 4447 | for(int iband=0;iband<nband;++iband){ |
| 4448 | int theBand=(band_opt.size()) ? band_opt[iband] : iband; |
| 4449 | vector<double> theValue; |
| 4450 | vector<string> fieldname; |
| 4451 | ostringstream fs; |
| 4452 | if(bandNames_opt.size()){ |
| 4453 | if(rule_opt.size()>1) |
| 4454 | fs << fieldMap[rule_opt[irule]]; |
| 4455 | fs << bandNames_opt[iband]; |
| 4456 | } |
| 4457 | else{ |
| 4458 | if(rule_opt.size()>1||nband==1) |
| 4459 | fs << fieldMap[rule_opt[irule]]; |
| 4460 | if(nband>1) |
| 4461 | fs << "b" << theBand; |
| 4462 | } |
| 4463 | switch(ruleMap[rule_opt[irule]]){ |
| 4464 | case(rule::proportion): |
| 4465 | stat.normalize_pct(polyClassValues); |
| 4466 | case(rule::count):{//count for each class |
| 4467 | for(int index=0;index<polyClassValues.size();++index){ |
| 4468 | theValue.push_back(polyClassValues[index]); |
| 4469 | ostringstream fsclass; |
| 4470 | fsclass << fs.str() << "class" << class_opt[index]; |
| 4471 | fieldname.push_back(fsclass.str()); |
| 4472 | } |
| 4473 | break; |
| 4474 | } |
| 4475 | case(rule::mode):{ |
| 4476 | //maximum votes in polygon |
| 4477 | if(verbose_opt[0]) |
| 4478 | std::cout << "number of points in polygon: " << nPointPolygon << std::endl; |
| 4479 | //search for class with maximum votes |
| 4480 | int maxClass=stat.mymin(class_opt); |
| 4481 | vector<double>::iterator maxit; |
| 4482 | maxit=stat.mymax(polyClassValues,polyClassValues.begin(),polyClassValues.end()); |
| 4483 | int maxIndex=distance(polyClassValues.begin(),maxit); |
| 4484 | maxClass=class_opt[maxIndex]; |
| 4485 | if(verbose_opt[0]>0) |
| 4486 | std::cout << "maxClass: " << maxClass << std::endl; |
| 4487 | theValue.push_back(maxClass); |
| 4488 | fieldname.push_back(fs.str()); |
| 4489 | break; |
| 4490 | } |
| 4491 | case(rule::mean): |
| 4492 | theValue.push_back(stat.mean(polyValues[iband])); |
| 4493 | fieldname.push_back(fs.str()); |
| 4494 | break; |
| 4495 | case(rule::median): |
| 4496 | theValue.push_back(stat.median(polyValues[iband])); |
| 4497 | fieldname.push_back(fs.str()); |
| 4498 | break; |
| 4499 | case(rule::stdev): |
| 4500 | theValue.push_back(sqrt(stat.var(polyValues[iband]))); |
| 4501 | fieldname.push_back(fs.str()); |
| 4502 | break; |
| 4503 | case(rule::percentile):{ |
| 4504 | for(int iperc=0;iperc<percentile_opt.size();++iperc){ |
| 4505 | theValue.push_back(stat.percentile(polyValues[iband],polyValues[iband].begin(),polyValues[iband].end(),percentile_opt[iperc])); |
| 4506 | ostringstream fsperc; |
| 4507 | fsperc << fs.str() << percentile_opt[iperc]; |
| 4508 | fieldname.push_back(fsperc.str()); |
| 4509 | } |
| 4510 | break; |
| 4511 | } |
| 4512 | case(rule::sum): |
| 4513 | theValue.push_back(stat.sum(polyValues[iband])); |
| 4514 | fieldname.push_back(fs.str()); |
| 4515 | break; |
| 4516 | case(rule::max): |
| 4517 | theValue.push_back(stat.mymax(polyValues[iband])); |
| 4518 | fieldname.push_back(fs.str()); |
| 4519 | break; |
| 4520 | case(rule::min): |
| 4521 | theValue.push_back(stat.mymin(polyValues[iband])); |
| 4522 | fieldname.push_back(fs.str()); |
| 4523 | break; |
| 4524 | case(rule::centroid): |
| 4525 | case(rule::point): |
| 4526 | theValue.push_back(polyValues[iband].back()); |
| 4527 | fieldname.push_back(fs.str()); |
| 4528 | break; |
| 4529 | default://not supported |
| 4530 | break; |
| 4531 | } |
| 4532 | for(int ivalue=0;ivalue<theValue.size();++ivalue){ |
| 4533 | switch( fieldType ){ |
| 4534 | case OFTInteger: |
| 4535 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),static_cast<int>(theValue[ivalue])); |
| 4536 | break; |
| 4537 | case OFTReal: |
| 4538 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),theValue[ivalue]); |
| 4539 | break; |
| 4540 | case OFTString: |
| 4541 | writePolygonFeature->SetField(fieldname[ivalue].c_str(),type2string<double>(theValue[ivalue]).c_str()); |
| 4542 | break; |
| 4543 | default://not supported |
| 4544 | std::string errorString="field type not supported"; |
| 4545 | throw(errorString); |
| 4546 | break; |
| 4547 | } |
| 4548 | } |
| 4549 | } |
| 4550 | } |
| 4551 | } |
| 4552 | } |
| 4553 | if(createPolygon&&validFeature){ |
| 4554 | //todo: only create if valid feature? |
| 4555 | //write polygon feature |
| 4556 | if(verbose_opt[0]>1) |
| 4557 | std::cout << "creating polygon feature (2)" << std::endl; |
| 4558 | // if(writeLayer->CreateFeature( writePolygonFeature ) != OGRERR_NONE ){ |
| 4559 | // std::string errorString="Failed to create polygon feature in ogr vector dataset"; |
| 4560 | // throw(errorString); |
| 4561 | // } |
| 4562 | //test: no need to destroy anymore? |
| 4563 | // OGRFeature::DestroyFeature( writePolygonFeature ); |
| 4564 | //make sure to use setFeature instead of pushFeature when in processing in parallel!!! |
| 4565 | if(verbose_opt[0]) |
| 4566 | std::cout << "set feature " << ifeature << std::endl; |
| 4567 | if(!writePolygonFeature) |
| 4568 | std::cerr << "Warning: NULL feature" << ifeature << std::endl; |
| 4569 | ogrWriter.setFeature(ifeature,writePolygonFeature); |
| 4570 | #pragma omp atomic |
| 4571 | ++ntotalvalid; |
| 4572 | #pragma omp atomic |
| 4573 | ++ntotalvalidLayer; |
| 4574 | } |
| 4575 | } |
| 4576 | // ++ifeature; |
| 4577 | // if(theThreshold>0){ |
| 4578 | // if(threshold_opt.size()==sampleReaderOgr.getLayerCount()) |
| 4579 | // progress=(100.0/theThreshold)*static_cast<float>(ntotalvalidLayer)/nfeatureLayer; |
| 4580 | // else |
| 4581 | // progress=static_cast<float>(ntotalvalidLayer)/nfeatureLayer; |
| 4582 | // } |
| 4583 | // else |
| 4584 | // progress=static_cast<float>(ifeature+1)/(-theThreshold); |
| 4585 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 4586 | } |
| 4587 | catch(std::string e){ |
| 4588 | std::cout << e << std::endl; |
| 4589 | continue; |
| 4590 | } |
| 4591 | catch(int npoint){ |
| 4592 | if(verbose_opt[0]) |
| 4593 | std::cout << "number of points read in polygon: " << npoint << std::endl; |
| 4594 | continue; |
| 4595 | } |
| 4596 | } |
| 4597 | // if(rbox_opt[0]>0||cbox_opt[0]>0) |
| 4598 | // boxWriter.close(); |
| 4599 | // progress=1.0; |
| 4600 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 4601 | if(verbose_opt[0]) |
| 4602 | std::cout << "number of valid points in layer: " << ntotalvalidLayer << std::endl; |
| 4603 | if(verbose_opt[0]) |
| 4604 | std::cout << "number of valid points in all layers: " << ntotalvalid<< std::endl; |
| 4605 | sampleReaderOgr.close(); |
| 4606 | |
| 4607 | if(papszOptions) |
| 4608 | CSLDestroy(papszOptions); |
| 4609 | // ogrWriter.write(); |
| 4610 | // ogrWriter.close(); |
| 4611 | // progress=1.0; |
| 4612 | // MyProgressFunc(progress,pszMessage,pProgressArg); |
| 4613 | // this->close(); |
| 4614 | if(maskReader.isInit()) |
| 4615 | maskReader.close(); |
| 4616 | return(CE_None); |
| 4617 | } |
| 4618 | catch(string predefinedString){ |
| 4619 | std::cerr << predefinedString << std::endl; |
| 4620 | throw; |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | /** |
| 4625 | * @param app application specific option arguments |
| 4626 | * |
| 4627 | * @return CE_None if success, CE_Failure if failure |
| 4628 | */ |
| 4629 | //todo: support multiple layers for writing |
| 4630 | //output vector ogrWriter will take spatial reference system of input vector sampleReader |
| 4631 | // make sure to setSpatialFilterRect on vector before entering here |
| 4632 | //todo: extract each Jim individually and store in tmpWriter and then join the tmpWriter in ogrWriter if different number of bands (or this is behavior of ImgMultiList?) |
| 4633 | CPLErr JimList::extractOgr(VectorOgr& sampleReader, VectorOgr& ogrWriter, AppFactory& app){ |
| 4634 | |
| 4635 | Optionjl<short> verbose_opt("v", "verbose", "Verbose mode if > 0", 0,2); |
| 4636 | Optionjl<std::string> combine_opt("combine", "combine", "Combine results of extract by append or join (default is append)","append"); |
| 4637 | Optionjl<std::string> bandNames_opt("bn", "bandname", "Band name(s) corresponding to band index(es)."); |
| 4638 | |
| 4639 | try{ |
| 4640 | bool doProcess;//stop process when program was invoked with help option (-h --help) |
| 4641 | try{ |
| 4642 | doProcess=combine_opt.retrieveOption(app); |
| 4643 | bandNames_opt.retrieveOption(app); |
| 4644 | verbose_opt.retrieveOption(app); |
| 4645 | } |
| 4646 | catch(string predefinedString){ |
| 4647 | string errorstring="Error: could not get combine rule"; |
| 4648 | throw(errorstring); |
| 4649 | } |
| 4650 | bool append=true; |
| 4651 | if(combine_opt[0]!="append") |
| 4652 | append=false; |
| 4653 | app.clearOption("combine"); |
| 4654 | app::AppFactory extractApp(app); |
| 4655 | std::list<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 4656 | CPLErr result=CE_None; |
| 4657 | size_t iband=0; |
| 4658 | |
| 4659 | std::string ogrFilename; |
| 4660 | if(!append){//join |
| 4661 | if(verbose_opt[0]) |
| 4662 | std::cout << "We are in join, first image" << std::endl; |
| 4663 | extractApp.setLongOption("fid","fid"); |
| 4664 | extractApp.setLongOption("co","OVERWRITE=YES"); |
| 4665 | extractApp.clearOption("bandname"); |
| 4666 | if(bandNames_opt.size()) |
| 4667 | extractApp.setLongOption("bandname",bandNames_opt[iband]); |
| 4668 | |
| 4669 | if(!((*imit)->isGeoRef())){ |
| 4670 | std::cerr << "Warning: input image is not georeferenced" << std::endl; |
| 4671 | // string errorstring="Warning: input image is not georeferenced"; |
| 4672 | // throw(errorstring); |
| 4673 | } |
| 4674 | if((*imit)->extractOgr(sampleReader,ogrWriter,extractApp)!=CE_None){ |
| 4675 | string errorstring="Error: could not extractOgr"; |
| 4676 | throw(errorstring); |
| 4677 | } |
| 4678 | ogrFilename=ogrWriter.getFileName(); |
| 4679 | if(verbose_opt[0]) |
| 4680 | std::cout << "Writing vector dataset " << ogrFilename << std::endl; |
| 4681 | ogrWriter.write(); |
| 4682 | ogrWriter.close(); |
| 4683 | ++imit; |
| 4684 | ++iband; |
| 4685 | } |
| 4686 | else if(verbose_opt[0]) |
| 4687 | std::cout << "We are in append" << std::endl; |
| 4688 | while(imit!=end()){ |
| 4689 | try{ |
| 4690 | if(!((*imit)->isGeoRef())){ |
| 4691 | std::cerr << "Warning: input image is not georeferenced" << std::endl; |
| 4692 | // string errorstring="Warning: input image is not georeferenced"; |
| 4693 | // throw(errorstring); |
| 4694 | } |
| 4695 | if(append){ |
| 4696 | if(verbose_opt[0]) |
| 4697 | std::cout << "We are in append"<< std::endl; |
| 4698 | if(verbose_opt[0]){ |
| 4699 | std::cout << "bbox of sampleReader: " << " -ulx " << sampleReader.getUlx()<< " -uly " << sampleReader.getUly()<< " -lrx " << sampleReader.getLrx() << " -lry " << sampleReader.getLry() << std::endl; |
| 4700 | std::cout << "bbox of raster image: " << " -ulx " << (*imit)->getUlx()<< " -uly " << (*imit)->getUly()<< " -lrx " << (*imit)->getLrx() << " -lry " << (*imit)->getLry() << std::endl; |
| 4701 | } |
| 4702 | (*imit)->extractOgr(sampleReader,ogrWriter,extractApp); |
| 4703 | } |
| 4704 | else{//join |
| 4705 | if(verbose_opt[0]) |
| 4706 | std::cout << "We are in join, band " << iband << std::endl; |
| 4707 | |
| 4708 | VectorOgr v1=VectorOgr(ogrFilename); |
| 4709 | VectorOgr v2; |
| 4710 | extractApp.clearOption("bandname"); |
| 4711 | if(bandNames_opt.size()) |
| 4712 | extractApp.setLongOption("bandname",bandNames_opt[iband]); |
| 4713 | extractApp.setLongOption("output","/vsimem/v2.sqlite"); |
| 4714 | //*imit is ImageProcess |
| 4715 | (*imit)->extractOgr(sampleReader, v2, extractApp); |
| 4716 | v2.write(); |
| 4717 | app::AppFactory joinApp(extractApp); |
| 4718 | joinApp.setLongOption("output",ogrFilename); |
| 4719 | joinApp.setLongOption("key","fid"); |
| 4720 | VectorOgr nextWriter; |
| 4721 | if(verbose_opt[0]) |
| 4722 | std::cout << "joining vectors" << std::endl; |
| 4723 | v1.join(v2,nextWriter,joinApp); |
| 4724 | v1.close(); |
| 4725 | v2.close(); |
| 4726 | if(verbose_opt[0]) |
| 4727 | std::cout << "Writing vector nextWriter " << nextWriter.getFileName() << std::endl; |
| 4728 | nextWriter.write(); |
| 4729 | nextWriter.close(); |
| 4730 | ++iband; |
| 4731 | } |
| 4732 | } |
| 4733 | catch(string errorString){ |
| 4734 | std::cerr << errorString << ", continuing with next image"<< std::endl; |
| 4735 | ++imit; |
| 4736 | continue; |
| 4737 | } |
| 4738 | ++imit; |
| 4739 | } |
| 4740 | return(CE_None); |
| 4741 | } |
| 4742 | catch(string errorString){ |
| 4743 | std::cerr << errorString << std::endl; |
| 4744 | throw; |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | /** |
| 4749 | * @param app application specific option arguments |
| 4750 | * @return output Vector |
| 4751 | **/ |
| 4752 | // make sure to setSpatialFilterRect on vector before entering here |
| 4753 | shared_ptr<VectorOgr> JimList::extractOgr(VectorOgr& sampleReader, AppFactory& app){ |
| 4754 | shared_ptr<VectorOgr> ogrWriter=VectorOgr::createVector(); |
| 4755 | if(extractOgr(sampleReader, *ogrWriter, app)!=OGRERR_NONE){ |
| 4756 | std::cerr << "Failed to extract" << std::endl; |
| 4757 | } |
| 4758 | return(ogrWriter); |
| 4759 | } |
| 4760 | |
| 4761 | size_t JimList::extractOgrMem(VectorOgr& sampleReader, vector<unsigned char> &vbytes, AppFactory& app){ |
| 4762 | try{ |
| 4763 | size_t filesize=0; |
| 4764 | Optionjl<string> output_opt("o", "output", "Output sample dataset","/vsimem/extractmem"); |
| 4765 | Optionjl<string> ogrformat_opt("f", "oformat", "Output sample dataset format","SQLite"); |
| 4766 | output_opt.clear(); |
| 4767 | ogrformat_opt.clear(); |
| 4768 | output_opt.retrieveOption(app); |
| 4769 | ogrformat_opt.retrieveOption(app); |
| 4770 | |
| 4771 | VectorOgr ogrWriter; |
| 4772 | std::list<std::shared_ptr<Jim> >::const_iterator imit=begin(); |
| 4773 | for(imit=begin();imit!=end();++imit){ |
| 4774 | if(!((*imit)->isGeoRef())){ |
| 4775 | std::cerr << "Warning: input image is not georeferenced" << std::endl; |
| 4776 | // string errorstring="Error: input image is not georeferenced"; |
| 4777 | // throw(errorstring); |
| 4778 | } |
| 4779 | if((*imit)->extractOgr(sampleReader,ogrWriter,app)!=CE_None){ |
| 4780 | string errorstring="Error: extractOgr failed"; |
| 4781 | throw(errorstring); |
| 4782 | } |
| 4783 | } |
| 4784 | ogrWriter.write(); |
| 4785 | ogrWriter.close(); |
| 4786 | return(ogrWriter.serialize(vbytes)); |
| 4787 | } |
| 4788 | catch(string errorString){ |
| 4789 | std::cerr << errorString << std::endl; |
| 4790 | throw; |
| 4791 | } |
| 4792 | } |