void Shapefile::add_field (const char *field_name, OGRFieldType type, int width, int precision) { OGRFieldDefnH fld = OGR_Fld_Create(field_name, type); OGR_Fld_SetWidth(fld, width); OGR_Fld_SetPrecision(fld, precision); if( OGR_L_CreateField( layer, fld, 1 ) != OGRERR_NONE ) { throw runtime_error(name + " field creation failed."); } OGR_Fld_Destroy(fld); }
QgsVectorLayer* QgsSLDConfigParser::contourLayerFromRaster( const QDomElement& userStyleElem, QgsRasterLayer* rasterLayer ) const { QgsDebugMsg( "Entering." ); if ( !rasterLayer ) { return nullptr; } //get <ContourSymbolizer> element QDomNodeList contourNodeList = userStyleElem.elementsByTagName( QStringLiteral( "ContourSymbolizer" ) ); if ( contourNodeList.size() < 1 ) { return nullptr; } QDomElement contourSymbolizerElem = contourNodeList.item( 0 ).toElement(); if ( contourSymbolizerElem.isNull() ) { return nullptr; } double equidistance, minValue, maxValue, offset; QString propertyName; equidistance = contourSymbolizerElem.attribute( QStringLiteral( "equidistance" ) ).toDouble(); minValue = contourSymbolizerElem.attribute( QStringLiteral( "minValue" ) ).toDouble(); maxValue = contourSymbolizerElem.attribute( QStringLiteral( "maxValue" ) ).toDouble(); offset = contourSymbolizerElem.attribute( QStringLiteral( "offset" ) ).toDouble(); propertyName = contourSymbolizerElem.attribute( QStringLiteral( "propertyName" ) ); if ( equidistance <= 0.0 ) { return nullptr; } QTemporaryFile* tmpFile1 = new QTemporaryFile(); tmpFile1->open(); mFilesToRemove.push_back( tmpFile1 ); QString tmpBaseName = tmpFile1->fileName(); QString tmpFileName = tmpBaseName + ".shp"; //hack: use gdal_contour first to write into a temporary file /* todo: use GDALContourGenerate( hBand, dfInterval, dfOffset, nFixedLevelCount, adfFixedLevels, bNoDataSet, dfNoData, hLayer, 0, nElevField, GDALTermProgress, nullptr );*/ //do the stuff that is also done in the main method of gdal_contour... /* -------------------------------------------------------------------- */ /* Open source raster file. */ /* -------------------------------------------------------------------- */ GDALRasterBandH hBand; GDALDatasetH hSrcDS; int numberOfLevels = 0; double currentLevel = 0.0; if ( maxValue > minValue ) { //find first level currentLevel = ( int )(( minValue - offset ) / equidistance + 0.5 ) * equidistance + offset; while ( currentLevel <= maxValue ) { ++numberOfLevels; currentLevel += equidistance; } } double *adfFixedLevels = new double[numberOfLevels]; int nFixedLevelCount = numberOfLevels; currentLevel = ( int )(( minValue - offset ) / equidistance + 0.5 ) * equidistance + offset; for ( int i = 0; i < numberOfLevels; ++i ) { adfFixedLevels[i] = currentLevel; currentLevel += equidistance; } int nBandIn = 1; double dfInterval = equidistance, dfNoData = 0.0, dfOffset = offset; int /* b3D = FALSE, */ bNoDataSet = FALSE, bIgnoreNoData = FALSE; hSrcDS = GDALOpen( rasterLayer->source().toUtf8().constData(), GA_ReadOnly ); if ( !hSrcDS ) { delete [] adfFixedLevels; throw QgsMapServiceException( QStringLiteral( "LayerNotDefined" ), QStringLiteral( "Operation request is for a file not available on the server." ) ); } hBand = GDALGetRasterBand( hSrcDS, nBandIn ); if ( !hBand ) { CPLError( CE_Failure, CPLE_AppDefined, "Band %d does not exist on dataset.", nBandIn ); } if ( !bNoDataSet && !bIgnoreNoData ) dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet ); /* -------------------------------------------------------------------- */ /* Try to get a coordinate system from the raster. */ /* -------------------------------------------------------------------- */ OGRSpatialReferenceH hSRS = nullptr; const char *pszWKT = GDALGetProjectionRef( hBand ); if ( pszWKT && strlen( pszWKT ) != 0 ) hSRS = OSRNewSpatialReference( pszWKT ); /* -------------------------------------------------------------------- */ /* Create the outputfile. */ /* -------------------------------------------------------------------- */ OGRDataSourceH hDS; OGRSFDriverH hDriver = OGRGetDriverByName( "ESRI Shapefile" ); OGRFieldDefnH hFld; OGRLayerH hLayer; int nElevField = -1; if ( !hDriver ) { //fprintf( FCGI_stderr, "Unable to find format driver named 'ESRI Shapefile'.\n" ); delete [] adfFixedLevels; throw QgsMapServiceException( QStringLiteral( "LayerNotDefined" ), QStringLiteral( "Operation request is for a file not available on the server." ) ); } hDS = OGR_Dr_CreateDataSource( hDriver, tmpFileName.toUtf8().constData(), nullptr ); if ( !hDS ) { delete [] adfFixedLevels; throw QgsMapServiceException( QStringLiteral( "LayerNotDefined" ), QStringLiteral( "Operation request cannot create data source." ) ); } hLayer = OGR_DS_CreateLayer( hDS, "contour", hSRS, /* b3D ? wkbLineString25D : */ wkbLineString, nullptr ); if ( !hLayer ) { delete [] adfFixedLevels; throw QgsMapServiceException( QStringLiteral( "LayerNotDefined" ), QStringLiteral( "Operation request could not create contour file." ) ); } hFld = OGR_Fld_Create( "ID", OFTInteger ); OGR_Fld_SetWidth( hFld, 8 ); OGR_L_CreateField( hLayer, hFld, FALSE ); OGR_Fld_Destroy( hFld ); if ( !propertyName.isEmpty() ) { hFld = OGR_Fld_Create( propertyName.toUtf8().constData(), OFTReal ); OGR_Fld_SetWidth( hFld, 12 ); OGR_Fld_SetPrecision( hFld, 3 ); OGR_L_CreateField( hLayer, hFld, FALSE ); OGR_Fld_Destroy( hFld ); nElevField = 1; } /* -------------------------------------------------------------------- */ /* Invoke. */ /* -------------------------------------------------------------------- */ GDALContourGenerate( hBand, dfInterval, dfOffset, nFixedLevelCount, adfFixedLevels, bNoDataSet, dfNoData, hLayer, 0, nElevField, GDALTermProgress, nullptr ); delete [] adfFixedLevels; OGR_DS_Destroy( hDS ); GDALClose( hSrcDS ); //todo: store those three files elsewhere... //mark shp, dbf and shx to delete after the request mFilePathsToRemove.push_back( tmpBaseName + ".shp" ); mFilePathsToRemove.push_back( tmpBaseName + ".dbf" ); mFilePathsToRemove.push_back( tmpBaseName + ".shx" ); QgsVectorLayer* contourLayer = new QgsVectorLayer( tmpFileName, QStringLiteral( "layer" ), QStringLiteral( "ogr" ) ); //create renderer QgsFeatureRenderer* theRenderer = rendererFromUserStyle( userStyleElem, contourLayer ); contourLayer->setRenderer( theRenderer ); //add labeling if requested labelSettingsFromUserStyle( userStyleElem, contourLayer ); QgsDebugMsg( "Returning the contour layer" ); return contourLayer; }
static int msContourLayerGenerateContour(layerObj *layer) { OGRSFDriverH hDriver; OGRFieldDefnH hFld; OGRLayerH hLayer; const char *elevItem; char *option; double interval = 1.0, levels[1000]; int levelCount = 0; GDALRasterBandH hBand = NULL; CPLErr eErr; contourLayerInfo *clinfo = (contourLayerInfo *) layer->layerinfo; OGRRegisterAll(); if (clinfo == NULL) { msSetError(MS_MISCERR, "Assertion failed: Contour layer not opened!!!", "msContourLayerCreateOGRDataSource()"); return MS_FAILURE; } if (!clinfo->hDS) { /* no overlap */ return MS_SUCCESS; } hBand = GDALGetRasterBand(clinfo->hDS, 1); if (hBand == NULL) { msSetError(MS_IMGERR, "Band %d does not exist on dataset.", "msContourLayerGenerateContour()", 1); return MS_FAILURE; } /* Create the OGR DataSource */ hDriver = OGRGetDriverByName("Memory"); if (hDriver == NULL) { msSetError(MS_OGRERR, "Unable to get OGR driver 'Memory'.", "msContourLayerCreateOGRDataSource()"); return MS_FAILURE; } clinfo->hOGRDS = OGR_Dr_CreateDataSource(hDriver, NULL, NULL); if (clinfo->hOGRDS == NULL) { msSetError(MS_OGRERR, "Unable to create OGR DataSource.", "msContourLayerCreateOGRDataSource()"); return MS_FAILURE; } hLayer = OGR_DS_CreateLayer(clinfo->hOGRDS, clinfo->ogrLayer.name, NULL, wkbLineString, NULL ); hFld = OGR_Fld_Create("ID", OFTInteger); OGR_Fld_SetWidth(hFld, 8); OGR_L_CreateField(hLayer, hFld, FALSE); OGR_Fld_Destroy(hFld); /* Check if we have a coutour item specified */ elevItem = CSLFetchNameValue(layer->processing,"CONTOUR_ITEM"); if (elevItem && strlen(elevItem) > 0) { hFld = OGR_Fld_Create(elevItem, OFTReal); OGR_Fld_SetWidth(hFld, 12); OGR_Fld_SetPrecision(hFld, 3); OGR_L_CreateField(hLayer, hFld, FALSE); OGR_Fld_Destroy(hFld); } else { elevItem = NULL; } option = msContourGetOption(layer, "CONTOUR_INTERVAL"); if (option) { interval = atof(option); free(option); } option = msContourGetOption(layer, "CONTOUR_LEVELS"); if (option) { int i,c; char **levelsTmp; levelsTmp = CSLTokenizeStringComplex(option, ",", FALSE, FALSE); c = CSLCount(levelsTmp); for (i=0;i<c && i<(int)(sizeof(levels)/sizeof(double)) ;++i) levels[levelCount++] = atof(levelsTmp[i]); CSLDestroy(levelsTmp); free(option); } eErr = GDALContourGenerate( hBand, interval, 0.0, levelCount, levels, FALSE, 0.0, hLayer, OGR_FD_GetFieldIndex(OGR_L_GetLayerDefn( hLayer), "ID" ), (elevItem == NULL) ? -1 : OGR_FD_GetFieldIndex(OGR_L_GetLayerDefn( hLayer), elevItem ), NULL, NULL ); if (eErr != CE_None) { msSetError( MS_IOERR, "GDALContourGenerate() failed: %s", "msContourLayerGenerateContour()", CPLGetLastErrorMsg() ); return MS_FAILURE; } msConnPoolRegister(&clinfo->ogrLayer, clinfo->hOGRDS, msContourOGRCloseConnection); return MS_SUCCESS; }
int Gdal_Contour(maps*& conf,maps*& inputs,maps*& outputs) #endif { fprintf(stderr,"DEBUG HELLO %f %d\n",__FILE__,__LINE__); fflush(stderr); GDALDatasetH hSrcDS; int i, b3D = FALSE, bNoDataSet = FALSE, bIgnoreNoData = FALSE; int nBandIn = 1; double dfInterval = 0.0, dfNoData = 0.0, dfOffset = 0.0; const char *pszSrcFilename = NULL; const char *pszDstFilename = NULL; const char *pszElevAttrib = NULL; const char *pszFormat = "ESRI Shapefile"; char **papszDSCO = NULL, **papszLCO = NULL; double adfFixedLevels[1000]; int nFixedLevelCount = 0; const char *pszNewLayerName = "contour"; int bQuiet = FALSE; GDALProgressFunc pfnProgress = NULL; fprintf(stderr,"DEBUG HELLO %f %d\n",__FILE__,__LINE__); fflush(stderr); #ifndef ZOO_SERVICE /* Check that we are running against at least GDAL 1.4 */ /* Note to developers : if we use newer API, please change the requirement */ if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400) { fprintf(stderr, "At least, GDAL >= 1.4.0 is required for this version of %s, " "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME); exit(1); } #endif GDALAllRegister(); OGRRegisterAll(); #ifndef ZOO_SERVICE argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); /* -------------------------------------------------------------------- */ /* Parse arguments. */ /* -------------------------------------------------------------------- */ for( i = 1; 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")); return 0; } else if( EQUAL(argv[i], "--help") ) Usage(); else if( EQUAL(argv[i],"-a") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszElevAttrib = argv[++i]; } else if( EQUAL(argv[i],"-off") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); dfOffset = atof(argv[++i]); } else if( EQUAL(argv[i],"-i") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); dfInterval = atof(argv[++i]); } else if( EQUAL(argv[i],"-fl") ) { if( i >= argc-1 ) Usage(CPLSPrintf("%s option requires at least 1 argument", argv[i])); while( i < argc-1 && nFixedLevelCount < (int)(sizeof(adfFixedLevels)/sizeof(double)) && ArgIsNumeric(argv[i+1]) ) adfFixedLevels[nFixedLevelCount++] = atof(argv[++i]); } else if( EQUAL(argv[i],"-b") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); nBandIn = atoi(argv[++i]); } else if( EQUAL(argv[i],"-f") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszFormat = argv[++i]; } else if( EQUAL(argv[i],"-dsco") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); papszDSCO = CSLAddString(papszDSCO, argv[++i] ); } else if( EQUAL(argv[i],"-lco") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); papszLCO = CSLAddString(papszLCO, argv[++i] ); } else if( EQUAL(argv[i],"-3d") ) { b3D = TRUE; } else if( EQUAL(argv[i],"-snodata") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); bNoDataSet = TRUE; dfNoData = atof(argv[++i]); } else if( EQUAL(argv[i],"-nln") ) { CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1); pszNewLayerName = argv[++i]; } else if( EQUAL(argv[i],"-inodata") ) { bIgnoreNoData = TRUE; } else if ( EQUAL(argv[i],"-q") || EQUAL(argv[i],"-quiet") ) { bQuiet = TRUE; } else if( pszSrcFilename == NULL ) { pszSrcFilename = argv[i]; } else if( pszDstFilename == NULL ) { pszDstFilename = argv[i]; } else Usage("Too many command options."); } #else bQuiet = TRUE; bIgnoreNoData = TRUE; map* tmpMap=NULL; tmpMap=NULL; tmpMap=getMapFromMaps(inputs,"a","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ pszElevAttrib = strdup(tmpMap->value); } tmpMap=getMapFromMaps(inputs,"off","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ dfOffset = atof(tmpMap->value); } tmpMap=getMapFromMaps(inputs,"i","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ dfInterval = atof(tmpMap->value); } tmpMap=getMapFromMaps(inputs,"b","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ nBandIn = atoi(tmpMap->value); } tmpMap=getMapFromMaps(inputs,"InputDSN","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ pszSrcFilename = strdup(tmpMap->value); } tmpMap=getMapFromMaps(inputs,"OutputDSN","value"); if(tmpMap!=NULL && strncmp(tmpMap->value,"NULL",4)!=0){ pszDstFilename = strdup(tmpMap->value); } #endif if( dfInterval == 0.0 && nFixedLevelCount == 0 ) { Usage("Neither -i nor -fl are specified."); } if (pszSrcFilename == NULL) { Usage("Missing source filename."); } if (pszDstFilename == NULL) { Usage("Missing destination filename."); } if (!bQuiet) pfnProgress = GDALTermProgress; /* -------------------------------------------------------------------- */ /* Open source raster file. */ /* -------------------------------------------------------------------- */ GDALRasterBandH hBand; hSrcDS = GDALOpen( pszSrcFilename, GA_ReadOnly ); if( hSrcDS == NULL ){ #ifndef ZOO_SERVICE exit( 2 ); #else setMapInMaps(conf,"lenv","message","Unable to open the file"); #endif } hBand = GDALGetRasterBand( hSrcDS, nBandIn ); if( hBand == NULL ) { #ifndef ZOO_SERVICE CPLError( CE_Failure, CPLE_AppDefined, "Band %d does not exist on dataset.", nBandIn ); exit(2); #else char tmp[1024]; sprintf(tmp,"Band %d does not exist on dataset.",nBandIn); setMapInMaps(conf,"lenv","message",tmp); return SERVICE_FAILED; #endif } if( !bNoDataSet && !bIgnoreNoData ) dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet ); /* -------------------------------------------------------------------- */ /* Try to get a coordinate system from the raster. */ /* -------------------------------------------------------------------- */ OGRSpatialReferenceH hSRS = NULL; const char *pszWKT = GDALGetProjectionRef( hSrcDS ); if( pszWKT != NULL && strlen(pszWKT) != 0 ) hSRS = OSRNewSpatialReference( pszWKT ); /* -------------------------------------------------------------------- */ /* Create the outputfile. */ /* -------------------------------------------------------------------- */ OGRDataSourceH hDS; OGRSFDriverH hDriver = OGRGetDriverByName( pszFormat ); OGRFieldDefnH hFld; OGRLayerH hLayer; if( hDriver == NULL ) { #ifndef ZOO_SERVICE fprintf( stderr, "Unable to find format driver named %s.\n", pszFormat ); exit( 10 ); #else char tmp[1024]; sprintf( tmp, "Unable to find format driver named %s.\n", pszFormat ); setMapInMaps(conf,"lenv","message",tmp); return SERVICE_FAILED; #endif } hDS = OGR_Dr_CreateDataSource( hDriver, pszDstFilename, papszDSCO ); if( hDS == NULL ){ #ifndef ZOO_SERVICE exit( 1 ); #else setMapInMaps(conf,"lenv","message","Unable to create the file"); return SERVICE_FAILED; #endif } hLayer = OGR_DS_CreateLayer( hDS, pszNewLayerName, hSRS, b3D ? wkbLineString25D : wkbLineString, papszLCO ); if( hLayer == NULL ) exit( 1 ); hFld = OGR_Fld_Create( "ID", OFTInteger ); OGR_Fld_SetWidth( hFld, 8 ); OGR_L_CreateField( hLayer, hFld, FALSE ); OGR_Fld_Destroy( hFld ); if( pszElevAttrib ) { hFld = OGR_Fld_Create( pszElevAttrib, OFTReal ); OGR_Fld_SetWidth( hFld, 12 ); OGR_Fld_SetPrecision( hFld, 3 ); OGR_L_CreateField( hLayer, hFld, FALSE ); OGR_Fld_Destroy( hFld ); } /* -------------------------------------------------------------------- */ /* Invoke. */ /* -------------------------------------------------------------------- */ CPLErr eErr; eErr = GDALContourGenerate( hBand, dfInterval, dfOffset, nFixedLevelCount, adfFixedLevels, bNoDataSet, dfNoData, hLayer, OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), "ID" ), (pszElevAttrib == NULL) ? -1 : OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( hLayer ), pszElevAttrib ), pfnProgress, NULL ); OGR_DS_Destroy( hDS ); GDALClose( hSrcDS ); if (hSRS) OSRDestroySpatialReference( hSRS ); #ifndef ZOO_SERVICE CSLDestroy( argv ); CSLDestroy( papszDSCO ); CSLDestroy( papszLCO ); GDALDestroyDriverManager(); OGRCleanupAll(); return 0; #else GDALDestroyDriverManager(); OGRCleanupAll(); char tmp[1024]; sprintf(tmp,"File %s successfully created.",pszDstFilename); setMapInMaps(outputs,"Result","value",tmp); return SERVICE_SUCCEEDED; #endif }