I'm trying to access some Landsat data from S3 without making local copies of the files. As a test I wanted to run a simple GetRasterBand on the file but I'm not sure how to go about treating a VSILFILE as a GDALDataset without downloading the file.
GDAL API guide states VSILFILE "cannot be used with any functions other than the "VSI*L" family of functions. They aren't "real" FILE objects."
Snip of my code :
VSILFILE *poVs3Dataset;
//GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
GDALAllRegister();
VSIInstallS3FileHandler();
CPLSetConfigOption( "AWS_ACCESS_KEY_ID", "XXX" );
CPLSetConfigOption( "AWS_SECRET_ACCESS_KEY", "XXX" );
poVs3Dataset = VSIFOpenL(path, "r");
poBand = poVs3Dataset->GetRasterBand( 1 );
Which ultimately and understandably fails
g++ -g -L/usr/local/lib -lgdal stats.cpp
error: ‘VSILFILE’ has no member named ‘GetRasterBand’
Are there any good C++ examples out there I could work through?
Thanks!
Setting my environment variables prior to calling the executable seemed to help:
$> env AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx ./a.out /vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF
This is now my test code to prove to myself I can access with both GDALOpenEx & VSIFOpenExL which works in case it helps someone else:
VSILFILE *fpL;
GDALDataset *poDataset;
GDALRasterBand *poBand;
char * path = (char *)"/vsis3/landsat-pds/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/LC08_L1TP_139045_20170304_20170316_01_T1_B1.TIF";
VSIStatBufL sStat;
const char* const apszAllowedDrivers[] = { "GTiff", NULL };
GDALAllRegister();
poDataset = reinterpret_cast<GDALDataset*>(GDALOpenEx(path, GDAL_OF_READONLY | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL, NULL, NULL));
if( poDataset == NULL )
{
std::cout << "Couldn't open " << std::endl;
}
poBand = poDataset->GetRasterBand( 1 );
int nXSize = poBand->GetXSize();
int nYSize = poBand->GetYSize();
std::cout << "nXSize : " << nXSize << std::endl;
std::cout << "nYSize : " << nYSize << std::endl;
fpL = VSIFOpenExL(path, "rb", 1);
if( fpL != NULL )
{
....
works!!!
Related
Consider the CUDA graphs API function cuFindNodeInClone(). The documentation says, that it:
Returns:
CUDA_SUCCESS, CUDA_ERROR_INVALID_VALUE
This seems problematic to me. How can I tell whether the search failed (e.g. because there is no copy of the passed node in the graph), or whether the node or graph are simply invalid (e.g. nullptr)? Does the second error value signify both? Can I get a third error value which is just not mentioned?
When using the runtime API, the returned node is nullptr if the original node does not exist in the cloned graph. For nullptr original node or nullptr cloned graph, the output node is left unmodified.
#include <iostream>
#include <cassert>
int main(){
cudaError_t status;
cudaGraph_t graph;
status = cudaGraphCreate(&graph, 0);
assert(status == cudaSuccess);
cudaGraphNode_t originalNode;
status = cudaGraphAddEmptyNode(&originalNode, graph, nullptr, 0);
assert(status == cudaSuccess);
cudaGraph_t graphclone;
status = cudaGraphClone(&graphclone, graph);
assert(status == cudaSuccess);
cudaGraphNode_t anotherNode;
status = cudaGraphAddEmptyNode(&anotherNode, graph, nullptr, 0);
assert(status == cudaSuccess);
cudaGraphNode_t nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, originalNode, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, nullptr, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, originalNode, nullptr);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
nodeInClone = (cudaGraphNode_t)7;
status = cudaGraphNodeFindInClone(&nodeInClone, anotherNode, graphclone);
std::cout << cudaGetErrorString(status) << " " << (void*)nodeInClone << "\n";
}
On my machine with CUDA 11.8, this prints
no error 0x555e3cf287c0
invalid argument 0x7
invalid argument 0x7
invalid argument 0
this is my first stackoverflow question, so I hope the following text meets the question requirements. If not, please tell me what needs to be changed so I can adapt the question.
I'm new to CGAL and C++ in general. I would like to use CGAL 5.0.2 on a Macbook Pro early 2015 with macOS Catalina Version 10.15.4.
So to begin with, I followed the instruction steps given by the CGAL documentation using the package manager Homebrew. Since CGAL is a header-only library I configured it using CMake, as is recommended by the documentation.
It all worked out fine, so I went on trying the recommended examples given in the file CGAL-5.0.2.tar.xz, which is provided here. I'm particularly interested in the example Voronoi_Diagram_2.
Using the Terminal I executed the command -DCGAL_DIR=$HOME/CGAL-5.0.2 -DCMAKE_BUILD_TYPE=Release . in the example folder called Voronoi_Diagram_2. Then I executed the command make. All went well, no error messages were prompted. But executing the resulting exec file didn't produce any results.
After some research I managed to modify the code in a way that it prints the values of some variables. Problem seems to be that the input file which contains the line segments for which the voronoi diagramm shall be calculated is not correctly read.
The while loop which I highlighted in the code below by inserting //// signs seems not to be entered. That's why I assume that the variable ifs is empty, even though the input file "data1.svd.cin", which can be found in the folder "data" of the example, wasn't.
Does anyone have an idea for the reasons of this behaviour? Any help is appreciated.
This is the vd_2_point_location_sdg_linf.cpp file included in the example, which I modified:
// standard includes
#include <iostream>
#include <fstream>
#include <cassert>
// includes for defining the Voronoi diagram adaptor
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Segment_Delaunay_graph_Linf_filtered_traits_2.h>
#include <CGAL/Segment_Delaunay_graph_Linf_2.h>
#include <CGAL/Voronoi_diagram_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_traits_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_policies_2.h>
// typedefs for defining the adaptor
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Segment_Delaunay_graph_Linf_filtered_traits_2<K> Gt;
typedef CGAL::Segment_Delaunay_graph_Linf_2<Gt> DT;
typedef CGAL::Segment_Delaunay_graph_adaptation_traits_2<DT> AT;
typedef CGAL::Segment_Delaunay_graph_degeneracy_removal_policy_2<DT> AP;
typedef CGAL::Voronoi_diagram_2<DT,AT,AP> VD;
// typedef for the result type of the point location
typedef AT::Site_2 Site_2;
typedef AT::Point_2 Point_2;
typedef VD::Locate_result Locate_result;
typedef VD::Vertex_handle Vertex_handle;
typedef VD::Face_handle Face_handle;
typedef VD::Halfedge_handle Halfedge_handle;
typedef VD::Ccb_halfedge_circulator Ccb_halfedge_circulator;
void print_endpoint(Halfedge_handle e, bool is_src) {
std::cout << "\t";
if ( is_src ) {
if ( e->has_source() ) std::cout << e->source()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
} else {
if ( e->has_target() ) std::cout << e->target()->point() << std::endl;
else std::cout << "point at infinity" << std::endl;
}
}
int main()
{
std::ifstream ifs("data/data1.svd.cin");
assert( ifs );
VD vd;
Site_2 t;
// /////////// Inserted Comment ////////////////////////////////
std::cout << "In the following the insertion from ifs should take place" << std::flush;
// ///////////////// while loop which doesn't seem to be active //////////////////
while ( ifs >> t ) {
// Existing Code to insert the points in the voronoi structure
vd.insert(t);
// Inserted Code to check if while loop is entered
std::cout << "Entered while loop" << std::flush;
}
// ///////////////////////////////////////////////////////////////////////////////
ifs.close();
assert( vd.is_valid() );
std::ifstream ifq("data/queries1.svd.cin");
assert( ifq );
Point_2 p;
while ( ifq >> p ) {
std::cout << "Query point (" << p.x() << "," << p.y()
<< ") lies on a Voronoi " << std::flush;
Locate_result lr = vd.locate(p);
if ( Vertex_handle* v = boost::get<Vertex_handle>(&lr) ) {
std::cout << "vertex." << std::endl;
std::cout << "The Voronoi vertex is:" << std::endl;
std::cout << "\t" << (*v)->point() << std::endl;
} else if ( Halfedge_handle* e = boost::get<Halfedge_handle>(&lr) ) {
std::cout << "edge." << std::endl;
std::cout << "The source and target vertices "
<< "of the Voronoi edge are:" << std::endl;
print_endpoint(*e, true);
print_endpoint(*e, false);
} else if ( Face_handle* f = boost::get<Face_handle>(&lr) ) {
std::cout << "face." << std::endl;
std::cout << "The vertices of the Voronoi face are"
<< " (in counterclockwise order):" << std::endl;
Ccb_halfedge_circulator ec_start = (*f)->ccb();
Ccb_halfedge_circulator ec = ec_start;
do {
print_endpoint(ec, false);
} while ( ++ec != ec_start );
}
std::cout << std::endl;
}
ifq.close();
return 0;
}
The following parser does not compile when I use std::optional<std::string> for values of a -lexeme[+alpha] rule: something breaks in the attribute management.
The grammar works fine if instead of std::string I use int as a base type, it also works if I use twice std::string (i.e., no std::optional). However it does not work with boost::optional either. In the real grammar there is a difference between the absence of the string, and an empty string: I want to use std::optional (or its Boost predecessor).
The full example is available on Coliru, but here are the relevant bits:
struct pair_t
{
std::string first;
std::optional<std::string> second;
};
BOOST_FUSION_ADAPT_STRUCT(pair_t, first, second)
const auto pair_rule = lexeme[+alpha] >> -lexeme[+alpha] >> eoi;
for (std::string i: {"ab", "ab cd"})
{
auto res = pair_t{};
auto first = i.cbegin();
auto last = i.cend();
auto r = x3::phrase_parse(first, last, pair_rule, space, res);
if (r && first == last)
std::cout << i << ": " << res << '\n';
else
std::cout << i << ": failed\n";
}
My compiler reports:
clang++-mp-5.0 -std=c++17 -isystem /opt/local/include/ x3-optional.cc && ./a.out
In file included from x3-optional.cc:8:
In file included from /opt/local/include/boost/spirit/home/x3.hpp:14:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary.hpp:11:
In file included from /opt/local/include/boost/spirit/home/x3/auxiliary/any_parser.hpp:17:
/opt/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:180:9: error: no matching function
for call to 'move_to'
detail::move_to(std::move(src), dest
^~~~~~~~~~~~~~~
/opt/local/include/boost/spirit/home/x3/char/char_parser.hpp:31:29: note: in instantiation of
function template specialization 'boost::spirit::x3::traits::move_to<const char &,
std::__1::basic_string<char> >' requested here
x3::traits::move_to(*first, attr);
^
I am using QtCreator on windows, and I would like to know how to make my compiler optimize the output.
My understanding is that MinGW is a port of GCC. So, I should be able to use arguments such as -O2. However, in the "Projects" bar, the only things I see are :
Build step for qmake (probably not here, qmake is about the .pro files / MOC / Qt stuff ...)
Build step for mingw32-make (probably)
Clean steps (probably not)
So, I tried to add -O2 in the "Make arguments" box, but unfortunately, I get an error "invalid option --O"
For anyone interested, I was trying to make an implementation of the Ackermann function because I read that :
The Ackermann function, due to its definition in terms of extremely
deep recursion, can be used as a benchmark of a compiler's ability to
optimize recursion
The code (which doesn't really use Qt) :
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <ctime>
using namespace std;
int nbRecursion;
int nbRecursions9;
int Ackermann(int m, int n){
nbRecursion++;
if(nbRecursion % 1000000 == 0){
qDebug() << nbRecursions9 << nbRecursion;
}
if(nbRecursion == 1000000000){
nbRecursion = 0;
nbRecursions9++;
}
if(m==0){
return n+1;
}
if(m>0 && n>0){
return Ackermann(m-1,Ackermann(m, n-1));
}
if(m>0 && n==0){
return Ackermann(m-1,1);
}
qDebug() << "Bug at " << m << ", " << n;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
nbRecursion = 0;
nbRecursions9 = 0;
int m = 3;
int n = 13;
clock_t begin = clock();
Ackermann(m,n);
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
qDebug() << "There are " << CLOCKS_PER_SEC << " CLOCKS_PER_SEC";
qDebug() << "There were " << nbRecursions9 << nbRecursion << " recursions in " << elapsed_secs << " seconds";
double timeX = 1000000000.0*((elapsed_secs)/(double)nbRecursion);
if(nbRecursions9>0){
timeX += elapsed_secs/(double)nbRecursions9;
}
qDebug() << "Time for a function call : " << timeX << " nanoseconds";
return a.exec();
}
-O2 is used by default when you do a release build. Only debug builds don't use optimization. Regardless, if you want to use specific compiler options, you do so in the project file (*.pro) itself, by appending your options to the QMAKE_CFLAGS_RELEASE (for C files) and QMAKE_CXXFLAGS_RELEASE (for C++ files). For example:
QMAKE_CFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer
QMAKE_CXXFLAGS_RELEASE += -O3 -march=i686 -mtune=generic -fomit-frame-pointer
If you really want to use some specific options always, regardless of whether it's a debug or release build, then append to QMAKE_CFLAGS and QMAKE_CXXFLAGS instead. But usually, you'll only want optimization options in your release builds, not the debug ones.
HI all,
I am trying to modify the code provided by MS try to access the the Network Adapter Configuration
I am getting null pointer exception in it when i try to access the Mac Address or IPAddress property im using VC++ 2005. check for the // exception here: vtProp is returned as NULL line where am getting the exception.
#define _WIN32_DCOM
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib")
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for NIC(s) name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
//hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0);
// wcout << " Caption : " << vtProp.bstrVal << endl;
// VariantClear(&vtProp);
// pclsObj->Release();
hr = pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);
// exception here: vtProp is returned as NULL
wcout << " MACAddress : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
pclsObj->Release();
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
// pclsObj->Release();
CoUninitialize();
return 0; // Program successfully completed.
}
what is wrong in the code??
Abdul khaliq
This is old but I had the same problem and I didn't see a solution anywhere. I solved it by putting a check for VT_NULL.
if( vtProp.vt != VT_NULL )
wcout << " MACAddress : " << vtProp.bstrVal << endl;
I don't understand why some of the results are VT_NULL but I suspect it could be avoided by adding a "where" clause to the "select" statement.
Thanks to finnw for the tip!
I'd start by initializing the vtProp variable -- it shouldn't matter, but sometimes COM servers make assumptions about out params;
VariantInit(&vtProp);
Then you can inspect vtProp after it's been returned and see what the actual type is (.vt member) -- maybe it's not a string, for some reason?
Could you post back with the type (you can cross-reference with the VARTYPE definition from oaidl.h to see what the friendly name(s) are)?
The list of network adapters usually contains a few "virtual" adapters, and they don't all have MAC addresses. Some (e.g. "Packet Scheduler Miniport') duplicate the MAC addresses of physical adapters. You just need to check the vt field (it may be VT_EMPTY) and remove duplicates from the resulting list.