int Raster::VectortoRaster(const char * sVectorSourcePath, const char * sRasterOutputPath, double dCellWidth, const char * psFieldName){ OGRRegisterAll(); OGRDataSource * pDSVectorInput; pDSVectorInput = OGRSFDriverRegistrar::Open( sVectorSourcePath, FALSE ); if (pDSVectorInput == NULL) return INPUT_FILE_ERROR; // Get the extents of the file before passing it off to the function that actually burns // the geometries // ------------------------------------------------------- // Note: we're just grabbing the first layer here. If we get into needing multiple layers // Then we'll need to re-think this. OGRLayer * poLayer = pDSVectorInput->GetLayer(0); if (poLayer == NULL) return VECTOR_LAYER_NOT_FOUND; OGREnvelope psExtent; poLayer->GetExtent(&psExtent, TRUE); double dMaxY, dMaxX, dMinY, dMinX; double cellWidth = fabs(dCellWidth); dMaxY = ceil(psExtent.MaxY / cellWidth) * cellWidth; dMaxX = ceil(psExtent.MaxX / cellWidth) * cellWidth; dMinY = floor(psExtent.MinY / cellWidth) * cellWidth; dMinX = floor(psExtent.MinX / cellWidth) * cellWidth; int nRows = (int)((dMaxY - dMinY) / cellWidth); int nCols = (int)((dMaxX - dMinX) / cellWidth); \ // We're going to create them without projections. The projections get set later. double fNoDataValue = (double) -std::numeric_limits<float>::max(); GDALDataType nDType = GDT_Float32; double dCellHeight = -dCellWidth; RasterMeta TemplateRaster(psExtent.MaxY, psExtent.MinX, nRows, nCols, &dCellHeight, &dCellWidth, &fNoDataValue, "GTiff", &nDType, ""); pDSVectorInput->Release(); return VectortoRaster(sVectorSourcePath, sRasterOutputPath, psFieldName, &TemplateRaster); }
static void OGR2SQLITE_ogr_datasource_load_layers(sqlite3_context* pContext, int argc, sqlite3_value** argv) { sqlite3* hDB = (sqlite3*) sqlite3_user_data(pContext); if( (argc < 1 || argc > 3) || sqlite3_value_type (argv[0]) != SQLITE_TEXT ) { sqlite3_result_int (pContext, 0); return; } const char* pszDataSource = (const char*) sqlite3_value_text(argv[0]); int bUpdate = FALSE; if( argc >= 2 ) { if( sqlite3_value_type(argv[1]) != SQLITE_INTEGER ) { sqlite3_result_int (pContext, 0); return; } bUpdate = sqlite3_value_int(argv[1]); } const char* pszPrefix = NULL; if( argc >= 3 ) { if( sqlite3_value_type(argv[2]) != SQLITE_TEXT ) { sqlite3_result_int (pContext, 0); return; } pszPrefix = (const char*) sqlite3_value_text(argv[2]); } OGRDataSource* poDS = (OGRDataSource*)OGROpenShared(pszDataSource, bUpdate, NULL); if( poDS == NULL ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot open %s", pszDataSource); sqlite3_result_int (pContext, 0); return; } CPLString osEscapedDataSource = OGRSQLiteEscape(pszDataSource); for(int i=0; i<poDS->GetLayerCount(); i++) { const char* pszLayerName = poDS->GetLayer(i)->GetName(); CPLString osEscapedLayerName = OGRSQLiteEscape(pszLayerName); CPLString osTableName; if( pszPrefix != NULL ) { osTableName = pszPrefix; osTableName += "_"; osTableName += OGRSQLiteEscapeName(pszLayerName); } else { osTableName = OGRSQLiteEscapeName(pszLayerName); } char* pszErrMsg = NULL; if( sqlite3_exec(hDB, CPLSPrintf( "CREATE VIRTUAL TABLE \"%s\" USING VirtualOGR('%s', %d, '%s')", osTableName.c_str(), osEscapedDataSource.c_str(), bUpdate, osEscapedLayerName.c_str()), NULL, NULL, &pszErrMsg) != SQLITE_OK ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot create table \"%s\" : %s", osTableName.c_str(), pszErrMsg); sqlite3_free(pszErrMsg); } } poDS->Release(); sqlite3_result_int (pContext, 1); }
int Raster::VectortoRaster(const char * sVectorSourcePath, const char * sRasterOutputPath, const char * psFieldName, RasterMeta * p_rastermeta ){ OGRRegisterAll(); OGRDataSource * pDSVectorInput; pDSVectorInput = OGRSFDriverRegistrar::Open( sVectorSourcePath, FALSE ); if (pDSVectorInput == NULL) return INPUT_FILE_ERROR; OGRLayer * poLayer = pDSVectorInput->GetLayer(0); // The type of the field. OGRFeature * feat1 = poLayer->GetFeature(0); int fieldindex = feat1->GetFieldIndex(psFieldName); OGRFieldType fieldType = feat1->GetFieldDefnRef(fieldindex)->GetType(); OGRFeature::DestroyFeature( feat1 ); // The data type we're going to use for the file GDALDataType OutputDataType = GDT_Byte; // Handle field types according to their type: switch (fieldType) { case OFTString: CSVWriteVectorValues(poLayer, psFieldName, sRasterOutputPath); break; case OFTInteger: break; case OFTReal: OutputDataType = GDT_Float64; default: throw RasterManagerException(VECTOR_FIELD_NOT_VALID, "Type of field not recognized."); break; } // Get our projection and set the rastermeta accordingly. // ------------------------------------------------------- char *pszWKT = NULL; OGRSpatialReference* poSRS = poLayer->GetSpatialRef(); poSRS->exportToWkt(&pszWKT); p_rastermeta->SetProjectionRef(pszWKT); CPLFree(pszWKT); OSRDestroySpatialReference(poSRS); // Create the output dataset for writing GDALDataset * pDSOutput = CreateOutputDS(sRasterOutputPath, p_rastermeta); // Create a list of burn-in values // ------------------------------------------------------- std::vector<OGRGeometryH> ogrBurnGeometries; std::vector<double> dBurnValues; poLayer->ResetReading(); OGRFeature * ogrFeat = poLayer->GetNextFeature(); // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory while( ogrFeat != NULL ){ OGRGeometry * ogrGeom = ogrFeat->GetGeometryRef(); // No geometry found. Move along. if( ogrGeom == NULL ) { OGRFeature::DestroyFeature( ogrFeat ); continue; } OGRGeometry * geoClone = ogrGeom->clone(); // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory AND LEAK 20 direct bytes // Push a clone of this geometry onto the list of shapes to burn ogrBurnGeometries.push_back( (OGRGeometryH) geoClone ); if (fieldType == OFTString){ // If it's a string type we burn the FID. The value is then placed in a CSV file dBurnValues.push_back( ogrFeat->GetFID() ); } else { // If it's a float type or a byte type we write it directly. dBurnValues.push_back( ogrFeat->GetFieldAsDouble(psFieldName) ); } // GetNextFeature() creates a clone so we must delete it. OGRFeature::DestroyFeature( ogrFeat ); // <------- DRMEM!!! UNADDRESSABLE ACCESS beyond heap bounds: ogrFeat = poLayer->GetNextFeature(); // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory } // Do the Actual Burning of Geometries. // ------------------------------------------------------- int band = 1; char **papszRasterizeOptions = NULL; papszRasterizeOptions = CSLSetNameValue( papszRasterizeOptions, "ALL_TOUCHED", "TRUE" ); CPLErr err = GDALRasterizeGeometries( pDSOutput, 1, &band, ogrBurnGeometries.size(), &(ogrBurnGeometries[0]), NULL, NULL, &(dBurnValues[0]), NULL, NULL, NULL ); if (err) { } ogrBurnGeometries.clear(); dBurnValues.clear(); // Done. Calculate stats and close file CalculateStats(pDSOutput->GetRasterBand(1)); CSLDestroy(papszRasterizeOptions); GDALClose(pDSOutput); pDSVectorInput->Release(); // <------- DRMEM!!! UNADDRESSABLE ACCESS beyond heap bounds: //This is where the implementation actually goes return PROCESS_OK; }