-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathpolygonize.cpp
187 lines (165 loc) · 6.41 KB
/
polygonize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <gdal.h>
#include <gdal_alg.h>
#include <gdal_priv.h> // GDALDriver
#include <ogr_api.h>
#include <ogr_geometry.h>
#include <ogr_srs_api.h>
#include <ogr_spatialref.h>
#include <cpl_conv.h>
#include <cpl_string.h>
#include <stdlib.h> // atoi
#include <string.h>
#include <ogrsf_frmts.h>
#include <Rcpp.h>
#include "gdal.h"
#include "gdal_read.h"
#include "gdal_sf_pkg.h"
// # nocov start
// [[Rcpp::export(rng=false)]]
Rcpp::List CPL_polygonize(Rcpp::CharacterVector raster, Rcpp::CharacterVector mask_name,
Rcpp::CharacterVector raster_driver,
Rcpp::CharacterVector vector_driver, Rcpp::CharacterVector vector_dsn,
Rcpp::CharacterVector options, Rcpp::IntegerVector iPixValField,
Rcpp::CharacterVector contour_options, bool use_contours = false,
bool use_integer = true) {
GDALDataset *poDataset = (GDALDataset *) GDALOpenEx(raster[0], GA_ReadOnly,
raster_driver.size() ? create_options(raster_driver).data() : NULL,
// options.size() ? create_options(options).data() : NULL,
NULL, NULL);
if (poDataset == NULL) {
Rcpp::Rcout << "trying to read file: " << raster[0] << std::endl; // #nocov
Rcpp::stop("file not found"); // #nocov
}
const char *wkt = poDataset->GetProjectionRef();
GDALRasterBand *poBand = NULL;
if (poDataset->GetRasterCount() > 0)
poBand = poDataset->GetRasterBand( 1 );
else
Rcpp::Rcout << "No bands in raster file." << std::endl; // #nocov
// mask:
GDALDataset *maskDataset = NULL;
GDALRasterBand *maskBand = NULL;
if (mask_name.size()) {
maskDataset = (GDALDataset *) GDALOpenEx(mask_name[0], GA_ReadOnly,
raster_driver.size() ? create_options(raster_driver).data() : NULL,
// options.size() ? create_options(options).data() : NULL,
NULL, NULL);
if (maskDataset == NULL) {
Rcpp::Rcout << "trying to read file: " << mask_name[0] << std::endl; // #nocov
Rcpp::stop("file not found"); // #nocov
}
if (maskDataset->GetRasterCount() > 0)
maskBand = maskDataset->GetRasterBand( 1 );
else
Rcpp::Rcout << "No bands in mask file." << std::endl; // #nocov
}
// output: vector layer
GDALDriver *poDriver = GetGDALDriverManager()->GetDriverByName(vector_driver[0]);
if (poDriver == NULL) {
Rcpp::Rcout << "driver `" << vector_driver[0] << "' not available." << std::endl; // #nocov
Rcpp::stop("Driver not available.\n"); // #nocov
}
GDALDataset *poDS;
if ((poDS = poDriver->Create(vector_dsn[0], 0, 0, 0, GDT_Unknown, NULL)) == NULL) {
Rcpp::Rcout << "Creating dataset " << vector_dsn[0] << " failed." << std::endl; // #nocov
Rcpp::stop("Creation failed.\n"); // #nocov
}
OGRSpatialReference *sr = NULL;
if (wkt != NULL && *wkt != '\0') {
sr = new OGRSpatialReference;
sr = handle_axis_order(sr);
char **ppt = (char **) &wkt;
#if GDAL_VERSION_NUM < 2030000
sr->importFromWkt(ppt);
#else
sr->importFromWkt( (const char**) ppt);
#endif
}
OGRLayer *poLayer = poDS->CreateLayer("raster", sr, wkbMultiPolygon, NULL);
delete sr;
if (use_integer) {
// create field:
OGRFieldDefn oField("Value", OFTInteger);
if (poLayer->CreateField(&oField) != OGRERR_NONE)
Rcpp::stop("Creating attribute field failed.\n"); // #nocov
if (GDALPolygonize((GDALRasterBandH) poBand, maskBand,
(OGRLayerH) poLayer,
iPixValField[0],
NULL, // create_options(options, true),
NULL, NULL) != OGRERR_NONE)
Rcpp::Rcout << "GDALPolygonize returned an error" << std::endl; // #nocov
} else {
OGRFieldDefn oField("Value", OFTReal);
if (poLayer->CreateField(&oField) != OGRERR_NONE)
Rcpp::stop("Creating attribute field failed.\n"); // #nocov
OGRFieldDefn minField("Min", OFTReal);
if (poLayer->CreateField(&minField) != OGRERR_NONE)
Rcpp::stop("Creating attribute field failed.\n"); // #nocov
OGRFieldDefn maxField("Max", OFTReal);
if (poLayer->CreateField(&maxField) != OGRERR_NONE)
Rcpp::stop("Creating attribute field failed.\n"); // #nocov
if (!use_contours) {
if (GDALFPolygonize((GDALRasterBandH) poBand, maskBand,
(OGRLayerH) poLayer,
iPixValField[0],
create_options(options, true).data(),
NULL, NULL) != OGRERR_NONE)
Rcpp::Rcout << "GDALFPolygonize returned an error" << std::endl; // #nocov
} else {
#if GDAL_VERSION_NUM >= 2040000
if (GDALContourGenerateEx((GDALRasterBandH) poBand, (void *) poLayer,
create_options(contour_options).data(), NULL, NULL) != OGRERR_NONE)
Rcpp::stop("GDALContourGenerateEx returned an error");
#else
Rcpp::stop("contour requires GDAL >= 2.4.0");
#endif
}
}
Rcpp::NumericVector type(1);
type[0] = 0;
Rcpp::CharacterVector fid_column; // empty
Rcpp::List lst = sf_from_ogrlayer(poLayer, false, true, type, fid_column, true, -1);
GDALClose(poDataset); // raster
GDALClose(poDS); // vector
if (maskDataset != NULL)
GDALClose(maskDataset); // mask
return lst;
}
// # nocov end
// [[Rcpp::export(rng=false)]]
Rcpp::List CPL_rasterize(Rcpp::CharacterVector raster, Rcpp::CharacterVector raster_driver,
Rcpp::List sfc, Rcpp::NumericVector values,
Rcpp::CharacterVector options,
Rcpp::NumericVector NA_value) {
GDALDataset *poDataset = (GDALDataset *) GDALOpenEx(raster[0], GDAL_OF_UPDATE,
raster_driver.size() ? create_options(raster_driver).data() : NULL,
// options.size() ? create_options(options).data() : NULL,
NULL, NULL);
if (poDataset == NULL) {
Rcpp::Rcout << "trying to read file: " << raster[0] << std::endl; // #nocov
Rcpp::stop("file not found"); // #nocov
}
std::vector<OGRGeometry *> geoms = ogr_from_sfc(sfc, NULL);
// int bandlist = 1;
std::vector<int> bandlist(poDataset->GetRasterCount());
for (size_t i = 0; i < bandlist.size(); i++)
bandlist[i] = (int) i+1; // 1-based
CPLErr err = GDALRasterizeGeometries((GDALDatasetH) poDataset, // hDS,
poDataset->GetRasterCount(), // int nBandCount,
bandlist.data(), // int * panBandList,
geoms.size(), // int nGeomCount,
(OGRGeometryH *) geoms.data(), // OGRGeometryH * pahGeometries,
NULL, // GDALTransformerFunc pfnTransformer,
NULL, // void * pTransformArg,
(double *) &(values[0]), // double * padfGeomBurnValue,
options.size() ? create_options(options).data() : NULL, // char ** papszOptions,
NULL, // GDALProgressFunc pfnProgress,
NULL //void * pProgressArg
);
for (size_t i = 0; i < geoms.size(); i++)
OGRGeometryFactory::destroyGeometry(geoms[i]);
if (err != OGRERR_NONE)
Rcpp::Rcout << "GDALRasterizeGeometries returned an error" << std::endl; // #nocov
GDALClose(poDataset); // raster
return Rcpp::List::create();
}