1 2 3 4setMethod("geomtype", signature(x="SpatVector"), 5 function(x){ 6 x@ptr$type() 7 } 8) 9 10setMethod("datatype", signature(x="SpatVector"), 11 function(x){ 12 x@ptr$df$get_datatypes() 13 } 14) 15 16 17setMethod("is.lines", signature(x="SpatVector"), 18 function(x) { 19 geomtype(x) == "lines" 20 } 21) 22 23setMethod("is.polygons", signature(x="SpatVector"), 24 function(x) { 25 geomtype(x) == "polygons" 26 } 27) 28setMethod("is.points", signature(x="SpatVector"), 29 function(x) { 30 grepl("points", geomtype(x)) 31 } 32) 33 34 35setMethod("geomtype", signature(x="Spatial"), 36 function(x){ 37 type <- sub("spatial", "", as.vector(tolower(class(x)))) 38 type <- sub("dataframe", "", type) 39 if (type %in% c("grid", "pixels")) type <- "raster" 40 type 41 } 42) 43 44setMethod("geom", signature(x="SpatVector"), 45 function(x, wkt=FALSE, hex=FALSE, df=FALSE){ 46 if (hex) { 47 x@ptr$hex() 48 } else if (wkt) { 49 x@ptr$getGeometryWKT() 50 # or via geos with 51 # x@ptr$wkt() 52 } else { 53 g <- x@ptr$get_geometry() 54 g <- do.call(cbind, g) 55 colnames(g) <- c("geom", "part", "x", "y", "hole")[1:ncol(g)] 56 if (df) { 57 data.frame(g) 58 } else { 59 g 60 } 61 } 62 } 63) 64 65setMethod("crds", signature(x="SpatVector"), 66 function(x, df=FALSE){ 67 g <- x@ptr$coordinates() 68 g <- do.call(cbind, g) 69 colnames(g) <- c("x", "y") 70 if (df) { 71 data.frame(g) 72 } else { 73 g 74 } 75 } 76) 77 78setMethod("crds", signature(x="SpatRaster"), 79 function(x, df=FALSE, na.rm=TRUE){ 80 x <- as.points(x, na.rm=na.rm) 81 crds(x, df=df) 82 } 83) 84 85 86setMethod("dim", signature(x="SpatVector"), 87 function(x){ 88 c(nrow(x), ncol(x)) 89 } 90) 91 92setMethod("as.data.frame", signature(x="SpatVector"), 93 function(x, geom=NULL) { 94 d <- .getSpatDF(x@ptr$df) 95 # fix empty names 96 colnames(d) <- x@ptr$names 97 if (!is.null(geom)) { 98 geom <- match.arg(toupper(geom), c("WKT", "HEX")) 99 g <- geom(x, wkt=geom=="WKT", hex=geom=="HEX") 100 if (nrow(d) > 0) { 101 d$geometry <- g 102 } else { 103 d <- data.frame(geometry=g, stringsAsFactors=FALSE) 104 } 105 } 106 d 107 } 108) 109 110setMethod("as.list", signature(x="SpatVector"), 111 function(x, geom=NULL) { 112 as.list(as.data.frame(x, geom=geom)) 113 } 114) 115 116 117 118setMethod ("expanse", "SpatVector", 119 function(x, unit="m", transform=TRUE) { 120 a <- x@ptr$area(unit, transform, double()); 121 x <- messages(x, "expanse"); 122 return(a) 123 } 124) 125 126 127setMethod("perim", signature(x="SpatVector"), 128 function(x) { 129 p <- x@ptr$length(); 130 x <- messages(x, "length"); 131 return(p) 132 } 133) 134 135setMethod("length", signature(x="SpatVector"), 136 function(x) { 137 x@ptr$size() 138 } 139) 140 141 142setMethod("fillHoles", signature(x="SpatVector"), 143 function(x, inverse=FALSE) { 144 if (inverse) { 145 x@ptr <- x@ptr$get_holes() 146 } else { 147 x@ptr <- x@ptr$remove_holes() 148 } 149 messages(x) 150 } 151) 152 153 154setMethod("centroids", signature(x="SpatVector"), 155 function(x) { 156 x@ptr <- x@ptr$centroid() 157 messages(x) 158 } 159) 160 161