Opened 16 years ago

Closed 15 years ago

Last modified 11 years ago

#2531 closed defect (fixed)

OGRStyleMgr::AddStyle() can't work in V1.5.2

Reported by: 27949218 Owned by:
Priority: normal Milestone:
Component: OGR_SF Version: 1.5.2
Severity: normal Keywords: OGRStyleMgr, AddStyle
Cc: Mateusz Łoskot

Description (last modified by Mateusz Łoskot)

I try to delete features from a set of shape file, sometimes get error. I use this version of ogrshapelayer.cpp (r10969). Delete feature in two ways would get different result, and both don't work smoothly:

  • 1
    for(iFeat=0;iFeat<nFeat;iFeat++)
    {
    	int cplE = pLayer->DeleteFeature(0);
    	if(cplE==CE_None)
    	{ 
    		OGRShapeLayer* pShpLayer = (OGRShapeLayer*)pLayer;
    		cplE = pShpLayer->Repack();
    		if(cplE!=CE_None)
    		{
                printf("delete feature failed !\n");
                return false;
    		}
    	}
    }
    int nFeatRest = pLayer->GetFeatureCount();
    

These codes could delete each feature with returned value CE_None, and at the end nFeatRest=0. But when ran to pShpLayer->Repack() second, the error occurs: "Failure writing DBF record 0", it jumped untill all features were deleted.

  • 2
    for(iFeat=nFeat-1;iFeat>=0;iFeat--)
    {
    	int cplE = pLayer->DeleteFeature(iFeat);
    	if(cplE==CE_None)
    	{ 
    		OGRShapeLayer* pShpLayer = (OGRShapeLayer*)pLayer;
    		cplE = pShpLayer->Repack();
    		if(cplE!=CE_None)
    		{
    			printf("delete feature failed !\n");
    			return false;
    		}
    	}
    }
    int nFeatRest = pLayer->GetFeatureCount();
    

These codes could delete each feature with returned value CE_None, but at the end nFeatRest>0 and nFeatRest<nFeat, some features were NOT deleted yet! Same as (1), when ran to pShpLayer->Repack() second, the error occurs: "Failure writing DBF record iFeat", it jumped untill all features were deleted. Trace the error "Failure writing DBF record ..." into Repack():

/* -------------------------------------------------------------------- */
/*      Build a list of records to be dropped.                          */
/* -------------------------------------------------------------------- */
...
for( iShape = 0; iShape < nTotalShapeCount; iShape++ )
{
	if( DBFIsRecordDeleted( hDBF, iShape ) )//------------ERROR jumped ---------
		panRecordsToDelete[nDeleteCount++] = iShape;
}

I'm confused. Here's the minimum project example: min-projV1.4.rar

Change History (9)

comment:1 by 27949218, 16 years ago

i try to delete features from a set of shape file, sometimes get error. I use this version of ogrshapelayer.cpp: http://trac.osgeo.org/gdal/changeset/10969 . Delete feature in two ways would get different result, and both don't work smoothly:

(1) for(iFeat=0;iFeat<nFeat;iFeat++)

{

int cplE = pLayer->DeleteFeature?(0); if(cplE==CE_None)

{

OGRShapeLayer* pShpLayer = (OGRShapeLayer*)pLayer; cplE = pShpLayer->Repack(); if(cplE!=CE_None) {

printf("delete feature failed !\n"); return false;

}

}

}

int nFeatRest = pLayer->GetFeatureCount?();

These codes could delete each feature with returned value CE_None, and at the end nFeatRest=0. But when ran to pShpLayer->Repack() second, the error occurs: "Failure writing DBF record 0", it jumped untill all features were deleted.

(2) for(iFeat=nFeat-1;iFeat>=0;iFeat--)

{

int cplE = pLayer->DeleteFeature?(iFeat);

if(cplE==CE_None) {

OGRShapeLayer* pShpLayer = (OGRShapeLayer*)pLayer; cplE = pShpLayer->Repack(); if(cplE!=CE_None)

{

printf("delete feature failed !\n");

return false; }

}

}

int nFeatRest = pLayer->GetFeatureCount?();

These codes could delete each feature with returned value CE_None, but at the end nFeatRest>0 and nFeatRest<nFeat, some features were NOT deleted yet! Same as (1), when ran to pShpLayer->Repack() second, the error occurs: "Failure writing DBF record iFeat", it jumped untill all features were deleted. Trace the error "Failure writing DBF record ..." into Repack():

/* -------------------------------------------------------------------- */ /* Build a list of records to be dropped. */ /* -------------------------------------------------------------------- */

... for( iShape = 0; iShape < nTotalShapeCount; iShape++ ) {

if( DBFIsRecordDeleted( hDBF, iShape ) )------------ERROR jumped ---------

panRecordsToDelete[nDeleteCount++] = iShape;

}

I'm confused... Here's the minimum project example: http://xianexs.mail.qq.com/cgi-bin/downloadfilepart/svrid240/min-projV1.4.rar?svrid=240&fid=0005cdc7f9832359f9e4eb3efc621a7a0b6aed38b0648f15&&txf_fid=0f757a1c67e9d00af86bb29f362dca856bb234a4&&txf_sid=326af806a32d836c8f8ca536069d59f48fb8309e

comment:2 by Mateusz Łoskot, 16 years ago

Cc: Mateusz Łoskot added
Description: modified (diff)
Keywords: deletefeature ogrshapelayer shape added; delete feature shapelayer removed

in reply to:  2 ; comment:3 by 27949218, 16 years ago

Resolution: fixed
Status: newclosed
Type: defecttask

Replying to mloskot: The URL in the updating comment stores the example project. Before download you need to input this code:74cb36d4. I'm sorry for the rough operation of my first comment. Thank you for update.

in reply to:  3 comment:4 by 27949218, 16 years ago

Keywords: OGRStyleMgr AddStyle added
Resolution: fixed
Status: closedreopened
Summary: delete feature of shape layerdelete feature of shape layer, OGRStyleMgr
Type: taskdefect
Version: 1.4.11.5.2

Replying to 27949218:

Replying to mloskot: The URL in the updating comment stores the example project. Before download you need to input this code:74cb36d4. I'm sorry for the rough operation of my first comment. Thank you for update.

The error still exists in version V1.4.1. I try to update my program with version from 1.4.1 to 1.5.2, and use the OGRDatasource::ExecuteSQL() running "repack layer_name" to delete features, it works well.But i get another question: OGRStyleMgr::AddStyle() can't work in V1.5.2, it saves unexpected style string instead of user provided to *.tbl. I checked the style works smoothly in Ver1.4.1. I'm afraid there's a bug of OGRStyleMgr in ver1.5.2, since the source i download couldn't been compiled in my computer, please check it. Thank you.

comment:5 by Even Rouault, 16 years ago

Keywords: deletefeature ogrshapelayer shape removed
Summary: delete feature of shape layer, OGRStyleMgrOGRStyleMgr::AddStyle() can't work in V1.5.2

OK, I've tried your code on GDAL 1.5.2 and GDAL 1.6.0dev and it runs fine for version (1) and (2). I don't think it will be fixed in 1.4.X since 1.5.X is the stable branch now. So I consider this part of the ticket as resolved.

I can't really test for the OGRStyleMgr::AddStyle() issue so I only keep that as subject of that bug

comment:6 by Even Rouault, 16 years ago

Note that your (1) and (2) versions are very inefficient ! You should delete all the features and then compact just once. Like :

#include <stdio.h>

#include <ogr_api.h>
#include <cpl_error.h>

int main(int argc, char* argv[])
{
    OGRRegisterAll();

    OGRDataSourceH hDS = OGROpen( "uddf4322.shp", TRUE, NULL );
    OGRLayerH hLayer = OGR_DS_GetLayer(hDS, 0);
    int nFeat = OGR_L_GetFeatureCount(hLayer, FALSE);
    printf("nFeat = %d\n", nFeat);
    int iFeat;
    for(iFeat=0;iFeat < nFeat;iFeat++)  
    {
        int cplE = OGR_L_DeleteFeature(hLayer, iFeat);
        if(cplE !=CE_None)
        {
            fprintf(stderr, "error while deleting %d\n", iFeat);
        }
    }

    OGR_DS_ExecuteSQL(hDS, "REPACK uddf4322", NULL, NULL);

    nFeat = OGR_L_GetFeatureCount(hLayer, FALSE);
    printf("nFeat = %d\n", nFeat);

    OGR_DS_Destroy(hDS);

    return 0;
}

comment:7 by Even Rouault, 16 years ago

Owner: FrankW removed
Status: reopenednew

comment:8 by Even Rouault, 15 years ago

Milestone: 1.5.5
Resolution: fixed
Status: newclosed

The OGRStyleMgr::AddStyle() crash was fixed in trunk (r16196), branches/1.6 (r16197) and branches/1.5 (r16198). See #2810

comment:9 by Even Rouault, 11 years ago

Milestone: 1.5.5

Milestone 1.5.5 deleted

Note: See TracTickets for help on using tickets.