这个例子是调用gdal_polygonize.py将Tif格式的文件转换为shape格式的文件。
#include
#include
#include
using namespace std;
PROCESS_INFORMATION* _handle;
/* exec由绝对路径和参数构成 */
int RunPythonScript(string& exec)
{
STARTUPINFOA si;
PROCESS_INFORMATION* pi = new PROCESS_INFORMATION;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(pi, sizeof(PROCESS_INFORMATION));
// Start the child process.
if (!CreateProcessA(NULL, // No module name (use command line)
(LPSTR)exec.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
(LPSTARTUPINFOA)&si, // Pointer to STARTUPINFO structure
pi) // Pointer to PROCESS_INFORMATION structure
)
{
return 1;
}
_handle = pi;
return 0;
}
int main()
{
string pyexe = "D:\\Python27\\python.exe";
string pyscript = "D:\\bin2\\gdal_polygonize.py";
string tiffile = "D:\\120104.tif";
string param = "-f";
string drive = "ESRI SHAPEFILE";
string shapefile = "D:\\1.shp";
string commond = pyexe + " " + "\"" + pyscript + "\"" + " " + "\"" + tiffile + "\"" + " " + param + " " + "\"" + drive + "\"" + " " + "\"" + shapefile + "\"";
//string exe = "D:\\Python27\\python.exe \"D:\\bin2\\gdal_polygonize.py\" \"D:\\120104.tif\" -f \"ESRI SHAPEFILE\" \"D:\\1.shp\"";
if (RunPythonScript(commond) != 0)
{
cout << GetLastError() << endl;
}
else
{
cout << "succeed convert Tif file to shape file." << endl;
}
getchar();
return 0;
}
运行截图:
shape file: