GDALDriver *OGRSFDriverRegistrar::GetDriverByName( const char * pszName )

{
    GDALDriverManager* poDriverManager = GetGDALDriverManager();
    GDALDriver* poGDALDriver =
        poDriverManager->GetDriverByName(CPLSPrintf("OGR_%s", pszName));
    if( poGDALDriver == NULL )
        poGDALDriver = poDriverManager->GetDriverByName(pszName);
    if( poGDALDriver == NULL ||
        poGDALDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == NULL )
        return NULL;
    return poGDALDriver;
}
GDALDataset* CreateGDALRaster(TeRasterParams& params)
{
	// Gets the appropriate GDAL driver
	std::string path = params.fileName_;
	if(path.empty())
		return 0;
	
	std::string extension = TeGetExtension(path.c_str());
	std::string driverName = TeGDALDecoder::getGDALDriverName(extension);
	if(driverName.empty())
		return 0;

	GDALDriverManager* driverManager = GetGDALDriverManager();
	GDALDriver* driver = driverManager->GetDriverByName(driverName.c_str());

	if(driver == 0)
		return 0;

	// Converts the raster data type
	GDALDataType gDataType = Convert2GDAL(params.dataType_[0]);
	// Creates the raster GDAL
	GDALDataset* ds = driver->Create(path.c_str(), params.ncols_, params.nlines_, params.nBands(), gDataType, 0);

	if(ds == 0)
		return 0;

	// Sets the geometric transformations
	double gt[6];
	Convert2GDAL(gt, params);
	ds->SetGeoTransform(gt);

	// Sets the raster projection
	TeProjection* proj = params.projection();
	if(proj)
	{
		int epsg = proj->epsgCode();
		
		OGRSpatialReference oSRS;
		oSRS.importFromEPSG(epsg);
		
		char* projWKT = 0;
		if(oSRS.exportToWkt(&projWKT) == OGRERR_NONE)
		{
			ds->SetProjection(projWKT);
			OGRFree(projWKT);
		}
	}

	return ds;
}
GDALDataset* CreateOutputDataset(char* fileName, int width, int height, int bands)
{
	GDALDriverManager *gdm = GetGDALDriverManager();
	if(gdm == NULL)
		error("GetGDALDriverManager() failed!");

	GDALDriver *gd = gdm->GetDriverByName("GTiff");
	if(gd == NULL)
		error("Get GTiff Driver failed!");

	char* options[2];
	options[0] = "INTERLEAVE=BAND";
	options[1] = NULL;
	
	GDALDataset *dstDS = gd->Create(fileName, width, height, bands, GDT_Byte, options);
	
	return dstDS;
}
Exemple #4
0
MAIN_START(argc, argv)
{
    /* Check strict compilation and runtime library version as we use C++ API */
    if (! GDAL_CHECK_VERSION(argv[0]))
        exit(1);

    EarlySetConfigOptions(argc, argv);

/* -------------------------------------------------------------------- */
/*      Generic arg processing.                                         */
/* -------------------------------------------------------------------- */
    GDALAllRegister();
    argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
    if( argc < 1 )
        exit( -argc );

    for( int i = 0; i < argc; i++ )
    {
        if( EQUAL(argv[i], "--utility_version") )
        {
            printf("%s was compiled against GDAL %s and "
                   "is running against GDAL %s\n",
                   argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
            CSLDestroy(argv);
            return 0;
        }
        else if( EQUAL(argv[i], "--help") )
        {
            Usage();
        }
    }

    GDALRasterizeOptionsForBinary* psOptionsForBinary =
        GDALRasterizeOptionsForBinaryNew();
    // coverity[tainted_data]
    GDALRasterizeOptions *psOptions =
        GDALRasterizeOptionsNew(argv + 1, psOptionsForBinary);
    CSLDestroy(argv);

    if( psOptions == nullptr )
    {
        Usage();
    }

    if( !(psOptionsForBinary->bQuiet) )
    {
        GDALRasterizeOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
    }

    if( psOptionsForBinary->pszSource == nullptr )
        Usage("No input file specified.");

    if( psOptionsForBinary->pszDest == nullptr )
        Usage("No output file specified.");

/* -------------------------------------------------------------------- */
/*      Open input file.                                                */
/* -------------------------------------------------------------------- */
    GDALDatasetH hInDS = GDALOpenEx(
        psOptionsForBinary->pszSource, GDAL_OF_VECTOR | GDAL_OF_VERBOSE_ERROR,
        nullptr, nullptr, nullptr);

    if( hInDS == nullptr )
        exit(1);

/* -------------------------------------------------------------------- */
/*      Open output file if it exists.                                  */
/* -------------------------------------------------------------------- */
    GDALDatasetH hDstDS = nullptr;
    if( !(psOptionsForBinary->bCreateOutput) )
    {
        CPLPushErrorHandler(CPLQuietErrorHandler);
        hDstDS = GDALOpenEx(
            psOptionsForBinary->pszDest,
            GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR | GDAL_OF_UPDATE,
            nullptr, nullptr, nullptr );
        CPLPopErrorHandler();
    }

    if( psOptionsForBinary->pszFormat != nullptr &&
        (psOptionsForBinary->bCreateOutput || hDstDS == nullptr) )
    {
        GDALDriverManager *poDM = GetGDALDriverManager();
        GDALDriver *poDriver =
            poDM->GetDriverByName(psOptionsForBinary->pszFormat);
        char** papszDriverMD = (poDriver) ? poDriver->GetMetadata(): nullptr;
        if( poDriver == nullptr ||
            !CPLTestBool(CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_RASTER,
                                              "FALSE")) ||
            !CPLTestBool(CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE,
                                              "FALSE")) )
        {
            fprintf(stderr,
                    "Output driver `%s' not recognised or does not support "
                    "direct output file creation.\n",
                    psOptionsForBinary->pszFormat);
            fprintf(stderr,
                    "The following format drivers are configured and "
                    "support direct output:\n" );

            for( int iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++ )
            {
                GDALDriver* poIter = poDM->GetDriver(iDriver);
                papszDriverMD = poIter->GetMetadata();
                if( CPLTestBool(
                        CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_RASTER,
                                             "FALSE")) &&
                    CPLTestBool(
                        CSLFetchNameValueDef(papszDriverMD, GDAL_DCAP_CREATE,
                                             "FALSE")) )
                {
                    fprintf(stderr,  "  -> `%s'\n", poIter->GetDescription());
                }
            }
            exit(1);
        }
    }

    int bUsageError = FALSE;
    GDALDatasetH hRetDS = GDALRasterize(psOptionsForBinary->pszDest,
                                        hDstDS,
                                        hInDS,
                                        psOptions, &bUsageError);
    if(bUsageError == TRUE)
        Usage();
    const int nRetCode = hRetDS ? 0 : 1;

    GDALClose(hInDS);
    GDALClose(hRetDS);
    GDALRasterizeOptionsFree(psOptions);
    GDALRasterizeOptionsForBinaryFree(psOptionsForBinary);

    GDALDestroyDriverManager();

    return nRetCode;
}