1 // Copyright (c) 2018-2020  Robert J. Hijmans
2 //
3 // This file is part of the "spat" library.
4 //
5 // spat is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // spat is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with spat. If not, see <http://www.gnu.org/licenses/>.
17 
18 #include <fstream>
19 #include <numeric>
20 #include "spatVector.h"
21 
22 #ifdef useGDAL
23 #include "gdal_priv.h"
24 #endif
25 
26 #ifdef useRcpp
27 #include <Rcpp.h>
28 // Rcpp::depends(RcppProgress)
29 #include "progress.hpp"
30 #include "progress_bar.hpp"
31 #endif
32 
33 typedef long long int_64;
34 
35 
36 class SpatCategories {
37 	public:
38 		SpatDataFrame d;
39 		unsigned index = 0;
40 		bool vat = false;
41 };
42 
43 
44 class SpatWindow {
45 	public:
46 		SpatExtent full_extent;
47 		size_t full_ncol, full_nrow, off_row, off_col;
48 		bool expanded = false;
49 		std::vector<size_t> expand;
50 };
51 
52 
53 
54 
55 class SpatRasterSource {
56     private:
57 //		std::ofstream ofs;
58 	public:
59 #ifdef useGDAL
60 		GDALDataset* gdalconnection;
61 #if GDAL_VERSION_MAJOR >= 3 && GDAL_VERSION_MINOR >= 1
62 		GDALMDArrayH gdalmdarray;
63 #endif
64 #endif
65 		bool open_read=false;
66 		bool open_write=false;
67 
68 		SpatRasterSource();
69 
70 //		void fsopen(std::string filename);
71 //		bool fswrite(std::vector<double> &v);
72 //		void fsclose();
73 
74 		size_t ncol, nrow;
75 		unsigned nlyr;
76 		unsigned nlyrfile = 0;
77 		SpatExtent extent;
78 		bool extset=false;
79 		bool rotated=false;
80 		bool flipped=false;
81 		bool hasWindow=false;
82 		SpatWindow window;
83 
84 		bool multidim = false;
85 		size_t m_ndims;
86 		std::vector<size_t> m_dims;
87 		std::vector<std::string> m_dimnames;
88 //		std::vector<double> m_dimstart;
89 //		std::vector<double> m_dimend;
90 		std::vector<size_t> m_counts;
91 		std::vector<size_t> m_order;
92 		std::vector<size_t> m_subset;
93 		bool m_hasNA = false;
94 		double m_missing_value;
95 
96 
97 		//std::vector<std::string> crs = std::vector<std::string>(2, "");
98 		SpatSRS srs;
99 		std::vector<unsigned> layers;
100 		// layer names
101 		std::vector<std::string> names;
102 		// data source (sds) has one "variable name" / long_name
103 		std::string source_name;
104 		std::string source_name_long;
105 
106 		std::vector<int_64> time;
107 		std::string timestep = "seconds";
108 		bool hasTime = false;
109 		std::vector<double> depth;
110 		std::vector<std::string> unit;
111 
112 		//std::vector< std::vector<double> values;
113         std::vector<double> values;
114         //std::vector<int64_t> ivalues;
115         //std::vector<bool> bvalues;
116 
117 //		unsigned char datatype;
118 
119 		std::vector<int> blockrows;
120 		std::vector<int> blockcols;
121 		std::vector<bool> hasRange;
122 		std::vector<double> range_min;
123 		std::vector<double> range_max;
124 //		std::vector<bool> hasAttributes;
125 //		std::vector<SpatDataFrame> atts;
126 //		std::vector<int> attsIndex;
127 		std::vector<bool> hasCategories;
128 		std::vector<SpatCategories> cats;
129 
130 
131 		std::vector<bool> hasColors;
132 		std::vector<SpatDataFrame> cols;
133 
134 		bool memory=true;
135 		bool hasValues=false;
136 		std::string filename;
137 		std::string driver;
138 		std::string datatype;
139 		std::vector<std::string> open_ops;
140 
141 		// user set for reading:
142 		bool hasNAflag = false;
143 		double NAflag = NAN;
144 
145 		std::vector<bool> has_scale_offset;
146 		std::vector<double> scale;
147 		std::vector<double> offset;
148 
149 //		std::vector<SpatRasterSource> subset(std::vector<unsigned> lyrs);
150 		SpatRasterSource subset(std::vector<unsigned> lyrs);
151 //		void getValues(std::vector<double> &v, unsigned lyr, SpatOptions &opt);
152 		void appendValues(std::vector<double> &v, unsigned lyr);
153 
154 		void setRange();
155 		void resize(unsigned n);
156 		bool in_order();
157 		bool combine_sources(const SpatRasterSource &x);
158 		bool combine(SpatRasterSource &x);
159 
160 
161 		bool parameters_changed = false;
162 
163 		void set_names_time_ncdf(std::vector<std::string> metadata, std::vector<std::vector<std::string>> bandmeta, std::string &msg);
164 
165 };
166 
167 
168 class BlockSize {
169 	public:
170 		std::vector<size_t> row;
171 		std::vector<size_t> nrows;
172 		unsigned n;
173 };
174 
175 class SpatRaster {
176 
177     private:
178 		std::string copy_driver = "";
179 		std::string copy_filename = "";
180 		std::vector<std::string> gdal_options;
181 		bool compute_stats = true;
182 		bool gdal_stats = false;
183 		bool gdal_approx = true;
184 		bool gdal_minmax = true;
185 
186 	protected:
187 		SpatExtent window;
188 
189 	public:
190 
191 #ifdef useRcpp
192 		Progress* pbar;
193 		bool progressbar = false;
194 #endif
195 
196 ////////////////////////////////////////////////////
197 // properties and property-like methods for entire object
198 ////////////////////////////////////////////////////
199 
200 		std::vector<SpatRasterSource> source;
201 
202 		BlockSize bs;
203 		//BlockSize getBlockSize(unsigned n, double frac, unsigned steps=0);
204 		BlockSize getBlockSize(SpatOptions &opt);
205 		std::vector<double> mem_needs(SpatOptions &opt);
206 
207 		SpatMessages msg;
setError(std::string s)208 		void setError(std::string s) { msg.setError(s); }
addWarning(std::string s)209 		void addWarning(std::string s) { msg.addWarning(s); }
setMessage(std::string s)210 		void setMessage(std::string s) { msg.setMessage(s); }
hasError()211 		bool hasError() { return msg.has_error; }
hasWarning()212 		bool hasWarning() { return msg.has_warning; }
getWarnings()213 		std::string getWarnings() { return msg.getWarnings();}
getError()214 		std::string getError() { return msg.getError();}
getMessage()215 		std::string getMessage() { return msg.getMessage();}
216 
217 		//double NA = std::numeric_limits<double>::quiet_NaN();
218 
219 		size_t ncol();
220 		size_t nrow();
221 		SpatExtent getExtent();
222 		void setExtent(SpatExtent e);
223 		void setExtent(SpatExtent ext, bool keepRes=false, std::string snap="");  // also set it for sources?
224 		SpatVector dense_extent();
225 
226 		//std::vector<std::string> getCRS();
227 		//void setCRS(std::vector<std::string> _crs);
228 
229 		std::string getSRS(std::string x);
230 		bool setSRS(std::string crs);
231 
232 
233 		bool rgb=false;
234 		std::vector<int> rgblyrs;
235 		bool setRGB(int r, int g, int b, int alpha);
236 		std::vector<int> getRGB();
237 		void removeRGB();
238 
239 
240 /*
241 #ifdef useGDAL
242 		bool setSRS(OGRSpatialReference *poSRS, std::string &msg) {
243 #endif
244 */
245 
246 		bool is_lonlat();
247 		bool could_be_lonlat();
248 		bool is_global_lonlat();
249 
250 		std::vector<double> resolution();
251 		SpatRaster setResolution(double xres, double yres);
ncell()252 		double ncell() { return nrow() * ncol(); }
size()253 		double size() { return ncol() * nrow() * nlyr() ; }
254 
255 		double xres();
256 		double yres();
257 		std::vector<double> origin();
258 		unsigned nlyr();
259 
260 		// only no values allowed with a single SpatRasterSource
hasValues()261 		bool hasValues() { return source[0].hasValues ; };
262 		std::vector<double> getValues(long lyr, SpatOptions &opt);
263 
264 		bool getValuesSource(size_t src, std::vector<double> &out);
265 		bool setValues(std::vector<double> &v, SpatOptions &opt);
266 		bool replaceCellValues(std::vector<double> cells, std::vector<double> _values, int ncols);
267 		void setRange(SpatOptions &opt);
268 
269 
270 
271 ////////////////////////////////////////////////////
272 // property like methods for RasterSources
273 ////////////////////////////////////////////////////
274 		std::vector<std::string> filenames();
275 		bool isSource(std::string filename);
276 		std::vector<bool> inMemory();
277 
278 
279 ////////////////////////////////////////////////////
280 // property like methods for layers
281 ////////////////////////////////////////////////////
282 
283 		std::vector<bool> hasRange();
284 		std::vector<double> range_min();
285 		std::vector<double> range_max();
286 
287 		std::vector<std::string> getNames();
288 		bool setNames(std::vector<std::string> names, bool make_valid=false);
289 
290 		std::vector<std::string> getSourceNames();
291 		bool setSourceNames(std::vector<std::string>);
292 		std::vector<std::string> getLongSourceNames();
293 		bool setLongSourceNames(std::vector<std::string>);
294 
295 		bool hasTime();
296 		std::vector<int_64> getTime();
297 		std::string getTimeStep();
298 
299 		std::vector<std::string> getTimeStr();
300 
301 		bool setTime(std::vector<int_64> time, std::string step);
302 		std::vector<double> getDepth();
303 		bool setDepth(std::vector<double> depths);
304 		std::vector<std::string> getUnit();
305 		bool setUnit(std::vector<std::string> units);
306 
307 		bool setNAflag(std::vector<double> flag);
308 		std::vector<double> getNAflag();
309 
310 
311 ////////////////////////////////////////////////////
312 // constructors
313 ////////////////////////////////////////////////////
314 
315 		SpatRaster();
316 		SpatRaster(unsigned nr, unsigned nc, unsigned nl, SpatExtent ext, std::string crs);
317 		SpatRaster(std::vector<unsigned> rcl, std::vector<double> ext, std::string crs);
318 		SpatRaster(std::vector<std::string> fname, std::vector<int> subds, std::vector<std::string> subdsname, bool multi, std::vector<std::string> options, std::vector<size_t> x);
319 		SpatRaster(std::string fname, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> options);
320 		SpatRaster(SpatRasterSource s);
321 		void setSource(SpatRasterSource s);
322 		void setSources(std::vector<SpatRasterSource> s);
323 		//SpatRaster(const SpatRaster& x);
324 
325         SpatRaster deepCopy();
326 		SpatRaster hardCopy(SpatOptions &opt);
327         SpatRaster geometry(long nlyrs=-1, bool properties=false, bool time=true);
328 
329 		bool constructFromFile(std::string fname, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> options);
330 		bool constructFromFileMulti(std::string fname, std::string subdsname, std::vector<size_t> xyz);
331 		bool constructFromSDS(std::string filename, std::vector<std::string> meta, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> options, bool ncdf);
332 
333 		SpatRaster fromFiles(std::vector<std::string> fname, std::vector<int> subds, std::vector<std::string> subdsname, std::vector<std::string> options);
334 
335 //		bool constructFromNCDFsds(std::string filename, std::vector<std::string> meta, std::vector<int> subds, std::vector<std::string> subdsname);
336 
337 
~SpatRaster()338 		virtual ~SpatRaster(){}
339 
340 
341 		void addSource(SpatRaster x);
342 		SpatRaster combineSources(SpatRaster x);
343 		void combine(SpatRaster x);
344 
345 		SpatRaster subset(std::vector<unsigned> lyrs, SpatOptions &opt);
346 		SpatRaster replace(SpatRaster x, unsigned layer, SpatOptions &opt);
347 ////////////////////////////////////////////////////
348 // helper methods
349 ////////////////////////////////////////////////////
350 
351 		void gdalogrproj_init(std::string path);
352 
353 		bool compare_geom(SpatRaster x, bool lyrs, bool crs, double tol, bool warncrs=false, bool ext=true, bool rowcol=true, bool res=false);
354 		bool compare_origin(std::vector<double> x, double tol);
355 		bool shared_basegeom(SpatRaster &x, double tol, bool test_overlap);
356 
357 		std::vector<double> cellFromXY (std::vector<double> x, std::vector<double> y);
358 		double cellFromXY(double x, double y);
359 		std::vector<double> cellFromRowCol(std::vector<int_64> row, std::vector<int_64> col);
360 		double cellFromRowCol(int_64 row, int_64 col);
361 		std::vector<double> cellFromRowColCombine(std::vector<int_64> row, std::vector<int_64> col);
362 		double cellFromRowColCombine(int_64 row, int_64 col);
363 		std::vector<double> yFromRow(const std::vector<int_64> &row);
364 		double yFromRow(int_64 row);
365 		std::vector<double> xFromCol(const std::vector<int_64> &col);
366 		double xFromCol(int_64 col);
367 
368 		std::vector<int_64> colFromX(const std::vector<double> &x);
369 		int_64 colFromX(double x);
370 		std::vector<int_64> rowFromY(const std::vector<double> &y);
371 		int_64 rowFromY(double y);
372 		std::vector<std::vector<double>> xyFromCell( std::vector<double> &cell);
373 		std::vector<std::vector<double>> xyFromCell( double cell);
374 		std::vector<std::vector<int_64>> rowColFromCell(std::vector<double> &cell);
375 		std::vector<int_64> rowColFromY(std::vector<double> &y);
376 		std::vector<std::vector<int_64>> rowColFromExtent(SpatExtent e);
377 
378 
379         std::vector<unsigned> sourcesFromLyrs(std::vector<unsigned> lyrs);
380 		int sourceFromLyr(unsigned lyr);
381 		std::vector<unsigned> findLyr(unsigned lyr);
382 		std::vector<unsigned> getBands();
383 
384         std::vector<unsigned> nlyrBySource();
385         std::vector<unsigned> lyrsBySource();
386         unsigned nsrc();
387 
388 		SpatRaster makeCategorical(unsigned layer, SpatOptions &opt);
389 		bool createCategories(unsigned layer, SpatOptions &opt);
390 		std::vector<bool> hasCategories();
391 		bool isRat();
392 		bool setCategories(unsigned layer, SpatDataFrame d, unsigned index, bool is_vat);
393 		bool removeCategories(unsigned layer);
394 		std::vector<SpatCategories> getCategories();
395 		SpatCategories getLayerCategories(unsigned layer);
396 		std::vector<std::string> getLabels(unsigned layer);
397 		bool setLabels(unsigned layer, std::vector<std::string> labels);
398 		int getCatIndex(unsigned layer);
399 		bool setCatIndex(unsigned layer, unsigned index);
400 
401 
402 
403 		//bool setAttrIndex(size_t layer, int i);
404 		//std::vector<int> getAttrIndex();
405 		//void createAttributes(unsigned layer);
406 		//std::vector<bool> hasAttributes();
407 		//void setAttributes(unsigned layer, SpatDataFrame df);
408 		//std::vector<SpatDataFrame> getAttributes();
409 		//SpatDataFrame getLayerAttributes(unsigned layer);
410 
411 		std::vector<bool> hasColors();
412 		std::vector<SpatDataFrame> getColors();
413 		bool setColors(size_t layer, SpatDataFrame cols);
414 		bool removeColors(size_t layer);
415 
416 		double valuesCell(double);
417 		double valuesCell(int, int);
418 		std::vector<double> valuesCell(std::vector<double>);
419 		std::vector<double> valuesRow(int);
420 
421 ////////////////////////////////////////////////////
422 // read and write
423 ////////////////////////////////////////////////////
424 
425 		bool valid_sources(bool files=true, bool rotated=true);
426 		bool readStart();
427 		std::vector<double> readValues(size_t row, size_t nrows, size_t col, size_t ncols);
428 		void readChunkMEM(std::vector<double> &out, size_t src, size_t row, size_t nrows, size_t col, size_t ncols);
429 
430 		std::vector<double> readBlock(BlockSize bs, unsigned i);
431 		std::vector<std::vector<double>> readBlock2(BlockSize bs, unsigned i);
432 		std::vector<double> readBlockIP(BlockSize bs, unsigned i);
433 		std::vector<double> readExtent(SpatExtent e);
434 		bool readStop();
435 
436 		bool readAll();
437 
438 		bool writeStart(SpatOptions &opt);
439 		bool writeValues(std::vector<double> &vals, size_t startrow, size_t nrows, size_t startcol, size_t ncols);
440 		bool writeValues2(std::vector<std::vector<double>> &vals, size_t startrow, size_t nrows, size_t startcol, size_t ncols);
441 		bool writeStop();
442 		bool writeHDR(std::string filename);
443 		SpatRaster make_vrt(std::vector<std::string> filenames, SpatOptions &opt);
444 
445 		//bool writeStartGDAL(std::string filename, std::string driver, std::string datatype, bool overwrite, SpatOptions &opt);
446 		bool writeStartGDAL(SpatOptions &opt);
447 		bool fillValuesGDAL(double fillvalue);
448 		bool writeValuesGDAL(std::vector<double> &vals, size_t startrow, size_t nrows, size_t startcol, size_t ncols);
449 		bool writeStopGDAL();
450 
451 
452 		bool readStartMulti(unsigned src);
453 		bool readStopMulti(unsigned src);
454 		bool readValuesMulti(std::vector<double> &data, size_t src, size_t row, size_t nrows, size_t col, size_t ncols);
455 
456 
457 
458 		//bool writeStartBinary(std::string filename, std::string datatype, std::string bandorder, bool overwrite);
459 		//bool writeValuesBinary(std::vector<double> &vals, unsigned startrow, unsigned nrows, unsigned startcol, unsigned ncols);
460 
461 		bool writeValuesMem(std::vector<double> &vals, size_t startrow, size_t nrows, size_t startcol, size_t ncols);
462 
463 		// binary (flat) source
464 		//std::vector<double> readValuesBinary(unsigned src, unsigned row, unsigned nrows, unsigned col, unsigned ncols);
465 		//std::vector<double> readSampleBinary(unsigned src, unsigned srows, unsigned scols);
466 		//std::vector<std::vector<double>> readCellsBinary(unsigned src, std::vector<double> cells);
467 
468 		// gdal source
469 		std::vector<double> readValuesGDAL(unsigned src, size_t row, size_t nrows, size_t col, size_t ncols, int lyr = -1);
470 		std::vector<double> readGDALsample(unsigned src, size_t srows, size_t scols);
471 		std::vector<std::vector<double>> readRowColGDAL(unsigned src, std::vector<int_64> &rows, const std::vector<int_64> &cols);
472 		std::vector<double> readRowColGDALFlat(unsigned src, std::vector<int_64> &rows, const std::vector<int_64> &cols);
473 
474 		bool readStartGDAL(unsigned src);
475 		bool readStopGDAL(unsigned src);
476 		void readChunkGDAL(std::vector<double> &data, unsigned src, size_t row, unsigned nrows, size_t col, unsigned ncols);
477 
478 		bool setWindow(SpatExtent x);
479 		bool removeWindow();
480 		std::vector<bool> hasWindow();
481 
482 		void openFS(std::string const &filename);
483 
484 		SpatRaster writeRaster(SpatOptions &opt);
485 		//SpatRaster writeRasterGDAL(std::string filename, std::string format, std::string datatype, bool overwrite, SpatOptions &opt);
486 		//SpatRaster writeRasterBinary(std::string filename, std::string datatype, std::string bandorder, bool overwrite);
487 		//bool checkFormatRequirements(const std::string &driver, std::string &filename);
488 
489 		bool canProcessInMemory(SpatOptions &opt);
490 		size_t chunkSize(SpatOptions &opt);
491 
492 		void fill(double x);
493 
494 		SpatRaster sources_to_disk(std::vector<std::string> &tmpfs, bool unique, SpatOptions &opt);
495 		bool sources_from_file();
496 
497 		bool differentFilenames(std::vector<std::string> outf, bool &duplicates, bool &empty);
498 
499 		std::vector<int> getFileBlocksize();
500 
501 ////////////////////////////////////////////////////
502 // main methods
503 ////////////////////////////////////////////////////
504 
505 		SpatRaster collapse_sources();
506 		void collapse();
507 
508 		SpatRaster rectify(std::string method, SpatRaster aoi, unsigned useaoi, bool snap, SpatOptions &opt);
509 
510         std::vector<double> adjacent(std::vector<double> cells, std::string directions, bool include);
511         std::vector<double> adjacentMat(std::vector<double> cells, std::vector<bool> mat, std::vector<unsigned> dim, bool include);
512  		SpatRaster aggregate(std::vector<unsigned> fact, std::string fun, bool narm, SpatOptions &opt);
513 		SpatExtent align(SpatExtent e, std::string snap);
514 		SpatRaster rst_area(bool mask, std::string unit, bool transform, SpatOptions &opt);
515 		std::vector<double> sum_area(std::string unit, bool transform, SpatOptions &opt);
516 		std::vector<std::vector<double>> area_by_value(SpatOptions &opt);
517 
518 		SpatRaster arith(SpatRaster x, std::string oper, SpatOptions &opt);
519 		SpatRaster arith(double x, std::string oper, bool reverse, SpatOptions &opt);
520 		SpatRaster arith(std::vector<double> x, std::string oper, bool reverse, SpatOptions &opt);
521 		SpatRaster apply(std::vector<unsigned> ind, std::string fun, bool narm, std::vector<std::string> nms, SpatOptions &opt);
522 		SpatRaster rapply(SpatRaster x, double first, double last, std::string fun, bool clamp, bool narm, SpatOptions &opt);
523 		std::vector<std::vector<double>> rappvals(SpatRaster x, double first, double last, bool clamp, bool all, double fill, size_t startrow, size_t nrows);
524 
525 		SpatVector as_polygons(bool trunc, bool dissolve, bool values, bool narm, SpatOptions &opt);
526 		SpatVector polygonize(bool trunc, bool values, bool narm, bool aggregate, SpatOptions &opt);
527 		SpatVector as_lines(SpatOptions &opt);
528 		SpatVector as_points(bool values, bool narm, SpatOptions &opt);
529 		SpatRaster atan_2(SpatRaster x, SpatOptions &opt);
530 
531 		std::vector<std::vector<double>> bilinearValues(const std::vector<double> &x, const std::vector<double> &y);
532 		std::vector<double> bilinearCells(const std::vector<double> &x, const std::vector<double> &y);
533 		std::vector<double> fourCellsFromXY(const std::vector<double> &x, const std::vector<double> &y);
534 
535 
536 		SpatRaster buffer(double d, SpatOptions &opt);
537 		SpatRaster clamp(double low, double high, bool usevalue, SpatOptions &opt);
538 		SpatRaster cover(SpatRaster x, std::vector<double> value, SpatOptions &opt);
539 
540 		SpatRaster crop(SpatExtent e, std::string snap, SpatOptions &opt);
541 		SpatRaster cum(std::string fun, bool narm, SpatOptions &opt);
542         SpatRaster disaggregate(std::vector<unsigned> fact, SpatOptions &opt);
543 		SpatRaster distance(SpatOptions &opt);
544 
545 		SpatRaster distance_vector_rasterize(SpatVector p, bool align_points, SpatOptions &opt);
546 		SpatRaster distance_vector(SpatVector p, SpatOptions &opt);
547 
548 		SpatRaster clumps(int directions, bool zeroAsNA, SpatOptions &opt);
549 
550 		SpatRaster edges(bool classes, std::string type, unsigned directions, double falseval, SpatOptions &opt);
551 		SpatRaster extend(SpatExtent e, SpatOptions &opt);
552 		std::vector<std::vector<std::vector<double>>> extractVector(SpatVector v, bool touches, std::string method, bool cells, bool xy, bool weights, bool exact, SpatOptions &opt);
553 		std::vector<double> extractVectorFlat(SpatVector v, bool touches, std::string method, bool cells, bool xy, bool weights, bool exact, SpatOptions &opt);
554 
555 
556 		std::vector<double> vectCells(SpatVector v, bool touches, std::string method, bool weights, bool exact, SpatOptions &opt);
557 		std::vector<double> extCells(SpatExtent ext);
558 
559 		std::vector<std::vector<double>> extractCell(std::vector<double> &cell);
560 		std::vector<double> extractCellFlat(std::vector<double> &cell);
561 
562         //std::vector<std::vector<double>> extractXY(std::vector<double> &x, std::vector<double> &y, std::string method, bool cells=false);
563 
564 		std::vector<std::vector<double>> extractXY(const std::vector<double> &x, const std::vector<double> &y, const std::string & method, const bool &cells);
565 		std::vector<double> extractXYFlat(const std::vector<double> &x, const std::vector<double> &y, const std::string & method, const bool &cells);
566 
567 		SpatRaster flip(bool vertical, SpatOptions &opt);
568 		SpatRaster filler(SpatRaster x, SpatOptions &opt);
569 
570 //		SpatRaster focal1(std::vector<unsigned> w, std::vector<double> m, double fillvalue, bool narm, bool naonly, std::string fun, SpatOptions &opt);
571 //		SpatRaster focal2(std::vector<unsigned> w, std::vector<double> m, double fillvalue, bool narm, bool naonly, std::string fun, SpatOptions &opt);
572 		SpatRaster focal3(std::vector<unsigned> w, std::vector<double> m, double fillvalue, bool narm, bool naonly, std::string fun, bool expand, SpatOptions &opt);
573 
574 		std::vector<double> focal_values(std::vector<unsigned> w, double fillvalue, int row, int nrows);
575 		std::vector<std::vector<double>> freq(bool bylayer, bool round, int digits, SpatOptions &opt);
576 		std::vector<size_t> count(double value, bool bylayer, bool round, int digits, SpatOptions &opt);
577 
578 		bool get_aggregate_dims(std::vector<unsigned> &fact, std::string &message);
579 		std::vector<unsigned> get_aggregate_dims2(std::vector<unsigned> fact);
580 		std::vector<std::vector<double> > get_aggregates(std::vector<double> &in, size_t nr, std::vector<unsigned> dim);
581 //		std::vector<double> compute_aggregates(std::vector<double> &in, size_t nr, std::vector<unsigned> dim, std::function<double(std::vector<double>&, bool)> fun, bool narm);
582 		SpatDataFrame global(std::string fun, bool narm, SpatOptions &opt);
583 		SpatDataFrame global_weighted_mean(SpatRaster &weights, std::string fun, bool narm, SpatOptions &opt);
584 
585 		SpatRaster gridDistance(SpatOptions &opt);
586 		SpatRaster gridCostDistance(SpatRaster cost, SpatOptions &opt);
587 
588 		SpatRaster init(std::string value, bool plusone, SpatOptions &opt);
589 		SpatRaster init(std::vector<double> values, SpatOptions &opt);
590 
591 		SpatRaster is_in(std::vector<double> m, SpatOptions &opt);
592 		std::vector<std::vector<double>> is_in_cells(std::vector<double> m, SpatOptions &opt);
593 
594 		SpatRaster isnot(SpatOptions &opt);
595 		SpatRaster isnan(SpatOptions &opt);
596 		SpatRaster isnotnan(SpatOptions &opt);
597 		SpatRaster isfinite(SpatOptions &opt);
598 		SpatRaster isinfinite(SpatOptions &opt);
599 
600 		std::vector<double> line_cells(SpatGeom& g);
601 		SpatRaster logic(SpatRaster x, std::string oper, SpatOptions &opt);
602 		SpatRaster logic(bool x, std::string oper, SpatOptions &opt);
603 		SpatRaster mask(SpatRaster x, bool inverse, double maskvalue, double updatevalue, SpatOptions &opt);
604 		SpatRaster mask(SpatRaster x, bool inverse, std::vector<double> maskvalues, double updatevalue, SpatOptions &opt);
605 
606 
607 		SpatRaster mask(SpatVector x, bool inverse, double updatevalue, bool touches, SpatOptions &opt);
608 		SpatRaster math(std::string fun, SpatOptions &opt);
609 		SpatRaster math2(std::string fun, unsigned digits, SpatOptions &opt);
610 
611 
612 		SpatRaster separate(std::vector<double> classes, double keepvalue, double othervalue, SpatOptions &opt);
613 
614 		SpatRaster modal(std::vector<double> add, std::string ties, bool narm, SpatOptions &opt);
615 
616         std::vector<double> polygon_cells(SpatGeom& g);
617 		SpatRaster quantile(std::vector<double> probs, bool narm, SpatOptions &opt);
618 		SpatRaster stretch(std::vector<double> minv, std::vector<double> maxv, std::vector<double> minq, std::vector<double> maxq, std::vector<double> smin, std::vector<double> smax, SpatOptions &opt);
619 		SpatRaster reverse(SpatOptions &opt);
620 
621 		SpatRaster range(std::vector<double> add, bool narm, SpatOptions &opt);
622 
623 		SpatRaster rasterizeLyr(SpatVector x, double value, double background, bool touches, bool update, SpatOptions &opt);
624 
625 		SpatRaster rasterize(SpatVector x, std::string field, std::vector<double> values, double background, bool touches, bool add, bool weights, bool update, bool minmax, SpatOptions &opt);
626 		std::vector<double> rasterizeCells(SpatVector &v, bool touches, SpatOptions &opt);
627 		//std::vector<std::vector<double>> rasterizeCellsWeights(SpatVector &v, bool touches);
628 
629 		void rasterizeCellsWeights(std::vector<double> &cells, std::vector<double> &weights, SpatVector &v, SpatOptions &opt);
630 		void rasterizeCellsExact(std::vector<double> &cells, std::vector<double> &weights, SpatVector &v, SpatOptions &opt);
631 
632 		SpatRaster replaceValues(std::vector<double> from, std::vector<double> to, long nl, SpatOptions &opt);
633 		SpatRaster reclassify(std::vector<std::vector<double>> rcl, unsigned right, bool lowest, bool othersNA, bool bylayer, SpatOptions &opt);
634 		SpatRaster reclassify(std::vector<double> rcl, unsigned nc, unsigned right, bool lowest, bool othersNA, bool bylayer, SpatOptions &opt);
635 		//SpatRaster classify_layers(std::vector<std::vector<double>> groups, std::vector<double> id, SpatOptions &opt);
636 		//SpatRaster classify_layers(std::vector<double> groups, unsigned nc, std::vector<double> id, SpatOptions &opt);
637 
638 		std::vector<double> readSample(unsigned src, size_t srows, size_t scols);
639 		SpatRaster rotate(bool left, SpatOptions &opt);
640 
641 		std::vector<size_t> sampleCells(unsigned size, std::string method, bool replace, unsigned seed);
642 		SpatRaster sampleRegularRaster(unsigned size);
643 		SpatRaster sampleRowColRaster(size_t nr, size_t nc);
644 		SpatRaster sampleRandomRaster(unsigned size, bool replace, unsigned seed);
645 		std::vector<std::vector<double>> sampleRegularValues(unsigned size, SpatOptions &opt);
646 		std::vector<std::vector<double>> sampleRowColValues(size_t nr, size_t nc, SpatOptions &opt);
647 
648 		std::vector<std::vector<double>> sampleRandomValues(unsigned size, bool replace, unsigned seed);
649 
650 		SpatRaster scale(std::vector<double> center, bool docenter, std::vector<double> scale, bool doscale, SpatOptions &opt);
651 		SpatRaster terrain(std::vector<std::string> v, unsigned neighbors, bool degrees, unsigned seed, SpatOptions &opt);
652 
653 		SpatRaster selRange(SpatRaster x, int z, int recycleby, SpatOptions &opt);
654 		SpatRaster selectHighest(size_t n, bool low, SpatOptions &opt);
655 
656 		SpatRaster shift(double x, double y, SpatOptions &opt);
657 		SpatRaster summary(std::string fun, bool narm, SpatOptions &opt);
658 		SpatRaster summary_numb(std::string fun, std::vector<double> add, bool narm, SpatOptions &opt);
659 
660 		SpatRaster transpose(SpatOptions &opt);
661 		SpatRaster trig(std::string fun, SpatOptions &opt);
662 		SpatRaster trim(double value, unsigned padding, SpatOptions &opt);
663 		std::vector<std::vector<double>> unique(bool bylayer, SpatOptions &opt);
664 		SpatRaster project1(std::string newcrs, std::string method, SpatOptions &opt);
665 		SpatRaster project2(SpatRaster &x, std::string method, SpatOptions &opt);
666 		void project3(SpatRaster &out, std::string method, SpatOptions &opt);
667 
668 //		SpatRaster resample1(SpatRaster &x, const std::string &method, SpatOptions &opt);
669 //		void resample2(SpatRaster &out, const std::string &method, SpatOptions &opt);
670 
671 #ifdef useGDAL
672 		bool getDSh(GDALDatasetH &rstDS, SpatRaster &out, std::string &filename, std::string &driver, double &naval, bool update, double background, SpatOptions &opt);
673 		bool open_gdal(GDALDatasetH &hDS, int src, bool update, SpatOptions &opt);
674 		bool create_gdalDS(GDALDatasetH &hDS, std::string filename, std::string driver, bool fill, double fillvalue, std::vector<bool> has_so, std::vector<double> scale, std::vector<double> offset, SpatOptions& opt);
675 		bool from_gdalMEM(GDALDatasetH hDS, bool set_geometry, bool get_values);
676 
677 		bool as_gdalvrt(GDALDatasetH &hVRT, SpatOptions &opt);
678 		//bool as_gdalmem(GDALDatasetH &hVRT);
679 
680 #endif
681 
682 		SpatRaster to_memory_copy(SpatOptions &opt);
683 		bool to_memory(SpatOptions &opt);
684 
685 		SpatRaster weighted_mean(SpatRaster w, bool narm, SpatOptions &opt);
686 		SpatRaster weighted_mean(std::vector<double> w, bool narm, SpatOptions &opt);
687 
688 		SpatRaster warp(SpatRaster x, const std::string &method, SpatOptions &opt);
689 		SpatRaster warpcrs(std::string x, const std::string &method, SpatOptions &opt);
690 
691 		SpatRaster warper(SpatRaster x, std::string crs, std::string method, bool mask, bool align, SpatOptions &opt);
692 		SpatRaster old_warper(SpatRaster x, std::string crs, std::string method, bool mask, SpatOptions &opt);
693 		//SpatRaster tester(bool geom);
694 		SpatRaster applyGCP(std::vector<double> fx, std::vector<double> fy, std::vector<double> tx, std::vector<double> ty, SpatOptions &opt);
695 
696 		//SpatRaster warp_gdal(SpatRaster x, const std::string &method, SpatOptions &opt);
697 		//SpatRaster warp_gdal_crs(std::string x, const std::string &method, SpatOptions &opt);
698 		SpatDataFrame zonal(SpatRaster x, std::string fun, bool narm, SpatOptions &opt);
699 		SpatRaster rgb2col(size_t r,  size_t g, size_t b, SpatOptions &opt);
700 		SpatRaster which(SpatOptions &opt);
701 		SpatRaster is_true(SpatOptions &opt);
702 
703 		SpatRaster sievefilter(int threshold, int connections, SpatOptions &opt);
704 
705 };
706 
707