Skip to content

Commit c0e56b4

Browse files
committed
Merge branch 'main' into pointx
2 parents 250cc44 + e91f5e3 commit c0e56b4

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ CPL_proj_info <- function(type) {
333333
.Call(`_sf_CPL_proj_info`, type)
334334
}
335335

336-
CPL_xy2sfc <- function(cc, dim, to_points, which) {
337-
.Call(`_sf_CPL_xy2sfc`, cc, dim, to_points, which)
336+
CPL_xy2sfc <- function(cc, dim, to_points, which, cc_has_NAs) {
337+
.Call(`_sf_CPL_xy2sfc`, cc, dim, to_points, which, cc_has_NAs)
338338
}
339339

340340
sfc_is_null <- function(sfc) {

R/gdal_utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ gdal_utils = function(util = "info", source, destination, options = character(0)
114114
}, # nocov end
115115
translate = CPL_gdaltranslate(source, destination, options, oo, config_options, quiet),
116116
vectortranslate = CPL_gdalvectortranslate(source, destination, options, oo, doo, config_options, quiet),
117-
buildvrt = CPL_gdalbuildvrt(source, destination, options, oo, config_options, quiet),
117+
buildvrt = CPL_gdalbuildvrt(if (missing(source)) character(0) else source, destination, options, oo, config_options, quiet),
118118
demprocessing = CPL_gdaldemprocessing(source, destination, options, processing, colorfilename, oo, config_options, quiet),
119119
nearblack = CPL_gdalnearblack(source, destination, options, oo, config_options, doo, quiet),
120120
grid = CPL_gdalgrid(source, destination, options, oo, config_options, quiet),

R/stars.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,14 @@ st_as_sfc.dimensions = function(x, ..., as_points = NA, use_cpp = TRUE, which =
192192
}
193193
}
194194
dims = dim(x) + !as_points
195-
if (use_cpp)
196-
structure(CPL_xy2sfc(cc, as.integer(dims), as_points, as.integer(which)),
197-
crs = st_crs(xd$refsys), n_empty = 0L, bbox = bbox.Mtrx(cc))
198-
else
195+
if (use_cpp) {
196+
bb = if (cc_has_NAs <- anyNA(cc))
197+
bbox.Mtrx(na.omit(cc))
198+
else
199+
bbox.Mtrx(cc)
200+
structure(CPL_xy2sfc(cc, as.integer(dims), as_points, as.integer(which), cc_has_NAs),
201+
crs = st_crs(xd$refsys), n_empty = 0L, bbox = bb)
202+
} else
199203
st_sfc(xy2sfc(cc, dims, as_points), crs = xd$refsys)
200204
}
201205

src/RcppExports.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,16 +1126,17 @@ BEGIN_RCPP
11261126
END_RCPP
11271127
}
11281128
// CPL_xy2sfc
1129-
List CPL_xy2sfc(NumericMatrix cc, IntegerVector dim, bool to_points, IntegerVector which);
1130-
RcppExport SEXP _sf_CPL_xy2sfc(SEXP ccSEXP, SEXP dimSEXP, SEXP to_pointsSEXP, SEXP whichSEXP) {
1129+
List CPL_xy2sfc(NumericMatrix cc, IntegerVector dim, bool to_points, IntegerVector which, bool cc_has_NAs);
1130+
RcppExport SEXP _sf_CPL_xy2sfc(SEXP ccSEXP, SEXP dimSEXP, SEXP to_pointsSEXP, SEXP whichSEXP, SEXP cc_has_NAsSEXP) {
11311131
BEGIN_RCPP
11321132
Rcpp::RObject rcpp_result_gen;
11331133
Rcpp::RNGScope rcpp_rngScope_gen;
11341134
Rcpp::traits::input_parameter< NumericMatrix >::type cc(ccSEXP);
11351135
Rcpp::traits::input_parameter< IntegerVector >::type dim(dimSEXP);
11361136
Rcpp::traits::input_parameter< bool >::type to_points(to_pointsSEXP);
11371137
Rcpp::traits::input_parameter< IntegerVector >::type which(whichSEXP);
1138-
rcpp_result_gen = Rcpp::wrap(CPL_xy2sfc(cc, dim, to_points, which));
1138+
Rcpp::traits::input_parameter< bool >::type cc_has_NAs(cc_has_NAsSEXP);
1139+
rcpp_result_gen = Rcpp::wrap(CPL_xy2sfc(cc, dim, to_points, which, cc_has_NAs));
11391140
return rcpp_result_gen;
11401141
END_RCPP
11411142
}
@@ -1496,7 +1497,7 @@ static const R_CallMethodDef CallEntries[] = {
14961497
{"_sf_CPL_have_datum_files", (DL_FUNC) &_sf_CPL_have_datum_files, 1},
14971498
{"_sf_CPL_proj_direct", (DL_FUNC) &_sf_CPL_proj_direct, 5},
14981499
{"_sf_CPL_proj_info", (DL_FUNC) &_sf_CPL_proj_info, 1},
1499-
{"_sf_CPL_xy2sfc", (DL_FUNC) &_sf_CPL_xy2sfc, 4},
1500+
{"_sf_CPL_xy2sfc", (DL_FUNC) &_sf_CPL_xy2sfc, 5},
15001501
{"_sf_sfc_is_null", (DL_FUNC) &_sf_sfc_is_null, 1},
15011502
{"_sf_sfc_unique_sfg_dims_and_types", (DL_FUNC) &_sf_sfc_unique_sfg_dims_and_types, 1},
15021503
{"_sf_sfc_is_empty", (DL_FUNC) &_sf_sfc_is_empty, 1},

src/raster2sf.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
using namespace Rcpp;
44

55
// [[Rcpp::export]]
6-
List CPL_xy2sfc(NumericMatrix cc, IntegerVector dim, bool to_points, IntegerVector which) {
6+
List CPL_xy2sfc(NumericMatrix cc, IntegerVector dim, bool to_points, IntegerVector which, bool cc_has_NAs) {
77
if (cc.nrow() != dim[0] * dim[1])
88
stop("xy2sfc: wrong dimensions"); // #nocov
9+
List sfc(which.length());
910
if (to_points) {
10-
List ret(which.length());
1111
NumericVector point(2);
1212
point.attr("class") = CharacterVector::create("XY", "POINT", "sfg");
1313
for (int i = 0; i < which.length(); i++) {
1414
int ix = which[i] - 1;
1515
point(0) = cc(ix, 0);
1616
point(1) = cc(ix, 1);
17-
ret(i) = clone(point);
17+
sfc(i) = clone(point);
1818
}
19-
ret.attr("class") = CharacterVector::create("sfc_POINT", "sfc");
20-
ret.attr("precision") = NumericVector::create(0.0);
21-
return(ret);
19+
sfc.attr("class") = CharacterVector::create("sfc_POINT", "sfc");
2220
} else {
23-
// List ret((dim[0] - 1) * (dim[1] - 1));
24-
List ret(which.length());
2521
for (int i = 0; i < which.length(); i++) {
2622
int ix = which[i] - 1; // from R, 1-based
2723
size_t y = ix / (dim[0] - 1); // row index
@@ -38,13 +34,34 @@ List CPL_xy2sfc(NumericMatrix cc, IntegerVector dim, bool to_points, IntegerVect
3834
points(3,1) = cc((y + 1) * (dim[0]) + x , 1); // bottom left
3935
points(4,0) = cc(y * (dim[0]) + x , 0); // top left
4036
points(4,1) = cc(y * (dim[0]) + x , 1); // top left
41-
List polygon(1);
42-
polygon.attr("class") = CharacterVector::create("XY", "POLYGON", "sfg");
43-
polygon(0) = points;
44-
ret(i) = polygon;
37+
bool empty = false;
38+
if (cc_has_NAs) {
39+
NumericVector xy(8);
40+
xy(0) = points(0,0);
41+
xy(1) = points(0,1);
42+
xy(2) = points(1,0);
43+
xy(3) = points(1,1);
44+
xy(4) = points(2,0);
45+
xy(5) = points(2,1);
46+
xy(6) = points(3,0);
47+
xy(7) = points(3,1);
48+
LogicalVector b = any(is_na(xy));
49+
if (b[0]) {
50+
List polygon;
51+
polygon.attr("class") = CharacterVector::create("XY", "POLYGON", "sfg");
52+
sfc(i) = polygon; // empty polygon
53+
empty = true;
54+
}
55+
}
56+
if (!empty) {
57+
List polygon(1);
58+
polygon.attr("class") = CharacterVector::create("XY", "POLYGON", "sfg");
59+
polygon(0) = points;
60+
sfc(i) = polygon;
61+
}
4562
}
46-
ret.attr("class") = CharacterVector::create("sfc_POLYGON", "sfc");
47-
ret.attr("precision") = NumericVector::create(0.0);
48-
return(ret);
63+
sfc.attr("class") = CharacterVector::create("sfc_POLYGON", "sfc");
4964
}
65+
sfc.attr("precision") = NumericVector::create(0.0);
66+
return(sfc);
5067
}

0 commit comments

Comments
 (0)