Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 9, 2023
1 parent 0263117 commit 926e85e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
useDynLib(terra, .registration=TRUE)
import(methods, Rcpp)
exportClasses(SpatExtent, SpatRaster, SpatRasterDataset, SpatRasterCollection, SpatVector, SpatVectorProxy, SpatVectorCollection)
exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, area, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin,readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize)
exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, area, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, extractRange, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin,readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize)

S3method(cbind, SpatVector)
S3method(rbind, SpatVector)
Expand Down
8 changes: 4 additions & 4 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,13 @@ setMethod("extractRange", signature(x="SpatRaster", y="ANY"),

first <- getLyrNrs(first, names(x), nrow(y)) + 1
last <- getLyrNrs(last, names(x), nrow(y)) + 1
e <- extract(x, y, space_fun, ID=TRUE, na.rm=na.rm, ...)
e <- extract(x, y, geom_fun, ID=TRUE, na.rm=na.rm, ...)
if (nrow(e) != nrow(y)) {
error("range_extract", "space_fun must return a single value for each geometry/layer")
error("range_extract", "geom_fun must return a single value for each geometry/layer")
}
a <- lapply(1:nrow(e), function(i) e[i, c(first[i]:last[i])])
if (!is.null(time_fun)) {
a <- sapply(a, time_fun, na.rm=na.rm)
if (!is.null(lyr_fun)) {
a <- sapply(a, lyr_fun, na.rm=na.rm)
}
if (ID) {
if (is.vector(a)) {
Expand Down
58 changes: 41 additions & 17 deletions src/geos_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,8 @@ SpatVector lonlat_buf(SpatVector x, double dist, unsigned quadsegs, bool ispol,
x = x.disaggregate(false);
SpatVector tmp;
tmp.reserve(x.size());
for (size_t i =0; i<x.geoms.size(); i++) {
Rcpp::Rcout << x.geoms.size() << std::endl;
for (size_t i=0; i<x.geoms.size(); i++) {
SpatVector p(x.geoms[i]);
p.srs = x.srs;
p = p.as_points(false, true);
Expand Down Expand Up @@ -1137,7 +1138,6 @@ SpatVector lonlat_buf(SpatVector x, double dist, unsigned quadsegs, bool ispol,
tmp = ishole ? tmp.get_holes() : tmp.remove_holes();
}
}

return tmp;
}

Expand Down Expand Up @@ -1169,31 +1169,55 @@ SpatVector SpatVector::buffer(std::vector<double> d, unsigned quadsegs, std::str
recycle(d, size());

if (islonlat) {
std::vector<unsigned> keep;
keep.reserve(size());
if (vt == "points") {
return point_buffer(d, quadsegs, false, true);
} else {
SpatVector p;
bool ispol = vt == "polygons";
for (size_t i =0; i<size(); i++) {
p = subset_rows(i);
if (ispol) {
SpatVector h = p.get_holes();
p = p.remove_holes();
p = lonlat_buf(p, d[i], quadsegs, true, false);
if (!h.empty()) {
h = lonlat_buf(h, d[i], quadsegs, true, true);
if (!h.empty()) {
for (size_t j=0; j<h.geoms[0].parts.size(); j++) {
p.geoms[0].parts[0].addHole(h.geoms[0].parts[j].x, h.geoms[0].parts[j].y);
if (vt == "polygons") {
for (size_t i =0; i<size(); i++) {
p = subset_rows(i).disaggregate(false);
SpatVector tmp;
for (size_t j =0; j<p.size(); j++) {
SpatVector pp = p.subset_rows(j);
SpatVector h = pp.get_holes();
pp = pp.remove_holes();
pp = lonlat_buf(pp, d[i], quadsegs, true, false);
if (!(pp.empty() || h.empty())) {
h = lonlat_buf(h, d[i], quadsegs, true, true);
if (!h.empty()) {
if (d[i] < 0) {
pp = pp.erase(h);
if (!pp.empty()) {
h = h.crop(pp);
}
if (h.empty()) continue;
}
for (size_t k=0; k<h.geoms[0].parts.size(); k++) {
pp.geoms[0].parts[0].addHole(h.geoms[0].parts[k].x, h.geoms[0].parts[k].y);
}
}
}
tmp = tmp.append(pp, true);
}
} else {
if (!tmp.empty()) {
keep.push_back(i);
out = out.append(tmp, true);
}
}
} else {
for (size_t i =0; i<size(); i++) {
p = subset_rows(i);
p = lonlat_buf(p, d[i], quadsegs, false, false);
out = out.append(p, true);
}
out = out.append(p, true);
}
out.df = df;
if (keep.size() < size()) {
out.df = df.subset_rows(keep);
} else {
out.df = df;
}
out.srs = srs;
return out;
}
Expand Down

0 comments on commit 926e85e

Please sign in to comment.