std::vector<OGRGeometry *> ogr_from_sfc(Rcpp::List sfc, OGRSpatialReference **sref) { Rcpp::List wkblst = CPL_write_wkb(sfc, false); std::vector<OGRGeometry *> g(sfc.length()); OGRSpatialReference *local_srs = NULL; Rcpp::List crs = sfc.attr("crs"); Rcpp::IntegerVector epsg(1); epsg[0] = crs["epsg"]; Rcpp::String p4s = crs["proj4string"]; if (p4s != NA_STRING) { Rcpp::CharacterVector cv = crs["proj4string"]; local_srs = new OGRSpatialReference; OGRErr err = local_srs->importFromProj4(cv[0]); if (err != 0) { local_srs->Release(); // #nocov handle_error(err); // #nocov } } for (int i = 0; i < wkblst.length(); i++) { Rcpp::RawVector r = wkblst[i]; OGRErr err = OGRGeometryFactory::createFromWkb(&(r[0]), local_srs, &(g[i]), r.length(), wkbVariantIso); if (err != 0) { if (local_srs != NULL) // #nocov local_srs->Release(); // #nocov handle_error(err); // #nocov } } if (sref != NULL) *sref = local_srs; // return and release later, or else if (local_srs != NULL) local_srs->Release(); // release now return g; }
SEXP assignRawSymmetricMatrixDiagonal(SEXP destination_, SEXP indices_, SEXP source_) { BEGIN_RCPP Rcpp::S4 destination = destination_; Rcpp::RawVector source = source_; Rcpp::RawVector destinationData = destination.slot("data"); Rcpp::IntegerVector indices = indices_; if(&(source(0)) == &(destinationData(0))) { throw std::runtime_error("Source and destination cannot be the same in assignRawSymmetricMatrixDiagonal"); } if((indices.size()*(indices.size()+(R_xlen_t)1))/(R_xlen_t)2 != source.size()) { throw std::runtime_error("Mismatch between index length and source object size"); } for(R_xlen_t column = 0; column < indices.size(); column++) { for(R_xlen_t row = 0; row <= column; row++) { R_xlen_t rowIndex = indices[row]; R_xlen_t columnIndex = indices[column]; if(rowIndex > columnIndex) { std::swap(rowIndex, columnIndex); } destinationData((columnIndex*(columnIndex-(R_xlen_t)1))/(R_xlen_t)2+rowIndex-(R_xlen_t)1) = source((column*(column+(R_xlen_t)1))/(R_xlen_t)2 + row); } } END_RCPP }
SEXP assignRawSymmetricMatrixFromEstimateRF(SEXP destination_, SEXP rowIndices_, SEXP columnIndices_, SEXP source_) { BEGIN_RCPP Rcpp::S4 destination = destination_; Rcpp::RawVector source = source_; Rcpp::RawVector destinationData = destination.slot("data"); Rcpp::IntegerVector rowIndices = rowIndices_; Rcpp::IntegerVector columnIndices = columnIndices_; if(&(source(0)) == &(destinationData(0))) { throw std::runtime_error("Source and destination cannot be the same in assignRawSymmetricMatrixDiagonal"); } std::vector<int> markerRows, markerColumns; markerRows = Rcpp::as<std::vector<int> >(rowIndices); markerColumns = Rcpp::as<std::vector<int> >(columnIndices); if(countValuesToEstimate(markerRows, markerColumns) != (unsigned long long)source.size()) { throw std::runtime_error("Mismatch between index length and source object size"); } triangularIterator iterator(markerRows, markerColumns); R_xlen_t counter = 0; for(; !iterator.isDone(); iterator.next()) { std::pair<int, int> markerPair = iterator.get(); R_xlen_t markerRow = markerPair.first, markerColumn = markerPair.second; destinationData((markerColumn*(markerColumn-(R_xlen_t)1))/(R_xlen_t)2 + (markerRow - (R_xlen_t)1)) = source(counter); counter++; } return R_NilValue; END_RCPP }
std::vector<GEOSGeom> geometries_from_sfc(GEOSContextHandle_t hGEOSCtxt, Rcpp::List sfc) { double precision = sfc.attr("precision"); Rcpp::List wkblst = CPL_write_wkb(sfc, false, native_endian(), "XY", precision); std::vector<GEOSGeom> g(sfc.size()); for (int i = 0; i < sfc.size(); i++) { Rcpp::RawVector r = wkblst[i]; g[i] = GEOSGeomFromWKB_buf_r(hGEOSCtxt, &(r[0]), r.size()); } return g; }
// [[Rcpp::export]] XPtrImage magick_image_read_list(Rcpp::List list){ XPtrImage image = create(); for(int i = 0; i < list.size(); i++) { if(TYPEOF(list[i]) != RAWSXP) throw std::runtime_error("magick_image_read_list can only read raw vectors"); Rcpp::RawVector x = list[i]; Magick::readImages(image.get(), Magick::Blob(x.begin(), x.length())); } return image; }
// [[Rcpp::export]] void sendWSMessage(std::string conn, bool binary, Rcpp::RObject message) { WebSocketConnection* wsc = internalize<WebSocketConnection>(conn); if (binary) { Rcpp::RawVector raw = Rcpp::as<Rcpp::RawVector>(message); wsc->sendWSMessage(Binary, reinterpret_cast<char*>(&raw[0]), raw.size()); } else { std::string str = Rcpp::as<std::string>(message); wsc->sendWSMessage(Text, str.c_str(), str.size()); } }
// the serialization function from SEXP to std::string inline std::string serializeToStr(SEXP object) { // using R's C API, all SEXP objects will be serialized into a raw vector Rcpp::RawVector val = serializeToRaw(object); // convert R raw vector into a std::string std::string res; for (size_t i = 0; i < val.size(); i++) { res = res + std::to_string(int(val[i])) + "\t"; } return res; }
// redis set -- serializes to R internal format std::string set(std::string key, SEXP s) { // if raw, use as is else serialize to raw Rcpp::RawVector x = (TYPEOF(s) == RAWSXP) ? s : serializeToRaw(s); // uses binary protocol, see hiredis doc at github redisReply *reply = static_cast<redisReply*>(redisCommand(prc_, "SET %s %b", key.c_str(), x.begin(), x.size())); std::string res(reply->str); freeReplyObject(reply); return(res); }
//Returns the value of any((object@data >= length(object@levels)) & object@data != as.raw(255)) SEXP checkRawSymmetricMatrix(SEXP rawSymmetric_) { BEGIN_RCPP Rcpp::S4 rawSymmetric = rawSymmetric_; Rcpp::NumericVector levels = Rcpp::as<Rcpp::NumericVector>(rawSymmetric.slot("levels")); Rcpp::RawVector data = Rcpp::as<Rcpp::RawVector>(rawSymmetric.slot("data")); R_xlen_t size = data.size(), levelsSize = levels.size(); for(R_xlen_t i = 0; i < size; i++) { if(data[i] >= levelsSize && data[i] != 0xff) return Rcpp::wrap(true); } return Rcpp::wrap(false); END_RCPP }
//[[Rcpp::export]] Rcpp::LogicalVector toLogical(Rcpp::RawVector bytes) { unsigned nBit = bytes.attr("bitlen"); unsigned nByte = bytes.size(); Rcpp::LogicalVector ans(nBit);//default are all 0s unsigned byteIndex, bitIndex; for(unsigned i =0 ; i < nBit; i++) { byteIndex = i / 8; bitIndex = i % 8; if(bytes[byteIndex] & 1 << bitIndex) ans(i) = 1; } return(ans); }
// [[Rcpp::export]] XPtrImage magick_image_readbin(Rcpp::RawVector x, Rcpp::CharacterVector density, Rcpp::IntegerVector depth, bool strip = false){ XPtrImage image = create(); #if MagickLibVersion >= 0x689 Magick::ReadOptions opts = Magick::ReadOptions(); #if MagickLibVersion >= 0x690 opts.quiet(1); #endif if(density.size()) opts.density(std::string(density.at(0)).c_str()); if(depth.size()) opts.depth(depth.at(0)); Magick::readImages(image.get(), Magick::Blob(x.begin(), x.length()), opts); #else Magick::readImages(image.get(), Magick::Blob(x.begin(), x.length())); #endif if(strip) for_each (image->begin(), image->end(), Magick::stripImage()); return image; }
// [[Rcpp::export]] bool context_assign_bin(std::string name, Rcpp::RawVector data, Rcpp::XPtr< v8::Persistent<v8::Context> > ctx) { // Test if context still exists if(!ctx) throw std::runtime_error("Context has been disposed."); // Create scope HandleScope handle_scope; Context::Scope context_scope(*ctx); v8::Handle<v8::Object> global = (*ctx)->Global(); // Currently converts raw vectors to strings. Better would be ArrayBuffer (Uint8Array specifically) Local<v8::String> mystring = v8::String::New((const char*) RAW(data), data.length()); global->Set(String::NewSymbol(name.c_str()), mystring); return true; }
uv_buf_t getData(size_t bytesDesired) { size_t bytes = _vector.size() - _pos; // Are we at the end? if (bytes == 0) return uv_buf_init(NULL, 0); if (bytesDesired < bytes) bytes = bytesDesired; char* buf = (char*)malloc(bytes); if (!buf) { throw Rcpp::exception("Couldn't allocate buffer"); } for (size_t i = 0; i < bytes; i++) { buf[i] = _vector[_pos + i]; } _pos += bytes; return uv_buf_init(buf, bytes); }
// [[Rcpp::export]] XPtrImage magick_image_readbitmap_raw(Rcpp::RawVector x){ Rcpp::IntegerVector dims(x.attr("dim")); return magick_image_bitmap(x.begin(), Magick::CharPixel, dims[0], dims[1], dims[2]); }
// [[Rcpp::export]] std::string base64encode(const Rcpp::RawVector& x) { return b64encode(x.begin(), x.end()); }
uint64_t size() const { return _vector.size(); }