[gdal-dev] problems with creating polygon

lengwei2008 421059144 at qq.com
Tue Jul 12 22:11:15 EDT 2011


Hi, 
I am a newer to OGR and I want to create a polygon feature, however it
failed. The code are pasted below 
    
code: 

 const char *pszDriverName = "ESRI Shapefile"; 
    OGRSFDriver *poDriver; 
        
    OGRRegisterAll(); 
        
    poDriver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName ); 
    if( poDriver == NULL ) 
    { 
        printf( "%s driver not available.\n", pszDriverName ); 
        exit( 1 ); 
    } 
        
    OGRDataSource *poDS; 
        
    poDS = poDriver->CreateDataSource( "Polygon.shp", NULL ); 
    if( poDS == NULL ) 
    { 
        printf( "Creation of output file failed.\n" ); 
        exit( 1 ); 
    } 
        
    OGRLayer *poLayer; 
        
    poLayer = poDS->CreateLayer( "polygon", NULL, wkbPoint, NULL ); 
    if( poLayer == NULL ) 
    { 
        printf( "Layer creation failed.\n" ); 
        exit( 1 ); 
    } 
        
    OGRFieldDefn oField( "Name", OFTString ); 
        
    oField.SetWidth(32); 
        
    if( poLayer->CreateField( &oField ) != OGRERR_NONE ) 
    { 
        printf( "Creating Name field failed.\n" ); 
        exit( 1 ); 
    } 
        
    double x, y; 
    char szName[33] = {"shishi"}; 
        x = 100; y = 100; 
        
        OGRFeature *poFeature; 
        
        poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); 
        poFeature->SetField( "Name", szName ); 
        
        OGRPolygon myPoligon; 
        OGRLinearRing MyRing; 

        MyRing.addPoint(x,y); 
        MyRing.addPoint(100,50); 
        MyRing.addPoint(50,50); 
        MyRing.addPoint(50,100); 
                
        myPoligon.addRing(&MyRing); 
        
        poFeature->SetGeometry( &myPoligon ); 
        
        if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE ) 
        { 
                printf( "Failed to create feature in shapefile.\n" ); 
                exit( 1 ); 
        } 
        
        OGRFeature::DestroyFeature( poFeature ); 
    OGRDataSource::DestroyDataSource( poDS ); 

I set a breakpoint at poLayer->CreateFeature( poFeature ). It shows that the
creation failed. But I don't know why. Many thanks!!

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/problems-with-creating-polygon-tp6577482p6577482.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list