SEXP mongo_gridfile_get_metadata(SEXP gfile) { gridfile* _gfile = _checkGridfile(gfile); bson meta = gridfile_get_metadata(_gfile); if (bson_size(&meta) <= 5) return R_NilValue; SEXP ret = _mongo_bson_create(&meta); UNPROTECT(3); return ret; }
int mongo_gridfile_get_metadata(struct gridfile_* gf, struct bson_** out) { bson meta, *b; gridfile_get_metadata((gridfile*)gf, &meta); if (bson_size(&meta) <= 5) return 0; b = (bson*)malloc(sizeof(bson)); bson_copy(b, &meta); *out = (struct bson_*) b; return 1; }
//! Read raster data from MongoDB int clsRasterData::ReadFromMongoDB(gridfs *gfs, const char* remoteFilename) { gridfile gfile[1]; bson b[1]; bson_init(b); bson_append_string(b, "filename", remoteFilename); bson_finish(b); int flag = gridfs_find_query(gfs, b, gfile); if(0 != flag) { throw ModelException("clsRasterData", "ReadFromMongoDB", "The file " + string(remoteFilename) + " does not exist."); } size_t length = (size_t)gridfile_get_contentlength(gfile); char* buf = (char*)malloc(length); gridfile_read (gfile, length, buf); float *data = (float*)buf; bson bmeta[1]; gridfile_get_metadata(gfile, bmeta); bson_iterator iterator[1]; if ( bson_find( iterator, bmeta, "NCOLS" )) m_headers["NCOLS"] = (float)bson_iterator_int(iterator); if ( bson_find( iterator, bmeta, "NROWS" )) m_headers["NROWS"] = (float)bson_iterator_int(iterator); if ( bson_find( iterator, bmeta, "NODATA_VALUE" )) m_headers["NODATA_VALUE"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "XLLCENTER" )) m_headers["XLLCENTER"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "YLLCENTER" )) m_headers["YLLCENTER"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "CELLSIZE" )) { m_headers["CELLSIZE"] = (float)bson_iterator_double(iterator); //m_headers["DY"] = m_headers["DX"]; } int nRows = (int)m_headers["NROWS"]; int nCols = (int)m_headers["NCOLS"]; vector<float> values; vector<int> positionRows; vector<int> positionCols; //get all valid values float nodataFloat = m_headers["NODATA_VALUE"]; for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { int index = i*nCols + j; float value = data[index]; if(FloatEqual(nodataFloat, value)) continue; values.push_back(value); positionRows.push_back(i); positionCols.push_back(j); } } //create float array m_nRows = values.size(); m_rasterData = new float[m_nRows]; m_rasterPositionData = new float*[m_nRows]; for (int i = 0; i < m_nRows; ++i) { m_rasterData[i] = values.at(i); m_rasterPositionData[i] = new float[2]; m_rasterPositionData[i][0] = float(positionRows.at(i)); m_rasterPositionData[i][1] = float(positionCols.at(i)); } bson_destroy(b); gridfile_destroy(gfile); free(buf); return 0; }