PROBLEM

Getting the correct number of records from a shapefile

Last Published: April 25, 2020

Description

After a record is deleted from a shape file using 'com.esri.mo.file.shp.Shapefile.delRecord(int recNum)' method and 'getNumRecs()' method is called on the same object, the total number of records will remain same.

Note:
In MapObjects Java 2.0, Shapefile class is within com.esri.mo2.file.shp package.

Cause

Shapefile.delRecord(int rec) only flags the record as deleted but does not remove it permanently from the file. Shapefile.getNumRecs() method will return the number of records stored in the header of the shape file, which only stores the total number of records, including those flagged as deleted.

Solution or Workaround

Use for-loop to go through all the records in the shapefile and count only those that return the status as OK as shown in the following example:


Code:
shapefile.reset();
int rec=0;
for(int i=0;i<shapefile.getNumRecs();i++){
int isOK=shapefile.getRecord();
if (isOK==Shapefile.OK)rec++;
}
//total # of records = rec
System.out.println(rec);

Article ID:000005619

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic