Select to view content in your preferred language

Counting records in a shapefile that has 0 records

3190
16
Jump to solution
05-03-2022 09:16 AM
ASw93
by
Regular Contributor

I am trying to write a Python script that will only run processes on a shapefile if there are more than 0 records in it.

I tried to use the Get Count tool (rows = arcpy.GetCount_management(file)) but if you try to run this on an empty shapefile (0 records), it returns:

 

 

 

ERROR 000229: Cannot open [insert shapefile path]
Failed to execute (GetCount).

 

 

 

It only runs on shapefiles that contain records. So what alternate code can I use to check a shapefile has records in it before attempting to run a process?

I have attached the empty shapefile.

0 Kudos
16 Replies
ASw93
by
Regular Contributor

I've attached the empty shapefile now.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Looking at the attached shape file *.shp file and comparing it to an new, empty shape file *.shp file from ArcGIS Pro, there are 3 fields that are different (see ESRI Shapefile Technical Description)

  • Byte 32 - Shape Type is "Null Shape" in the attached but "Polygon" in the new Pro
  • Byte 36 - Bounding Box XMin is 1.0 in attached but 0.0 in new Pro
  • Byte 44 - Bounding Box YMin is 1.0 in attached but 0.0 in new Pro

I really doubt the discrepancy in the bounding box values is causing the issue, it is most likely ArcGIS Pro (I haven't tested ArcMap) doesn't like the "Null Shape" for the shape type.

0 Kudos
by Anonymous User
Not applicable

@JoshuaBixbycould you describe how you did this inspection?

I had this exact problem yesterday - where shapefile writer implementations didn't play well between several programs. It'd be great community knowledge to have because this issue almost always expresses itself indirectly (like in OP's case).

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You have to start with the Shapefile Technical Description, which I linked to in my earlier comment.  That describes the binary structure of the file contents.  From there, you have to open the file and read the raw contents.  There are several tools for doing such work.  I happened to use PowerShell to read the file in and then used BitConverter Class (System) | Microsoft Docs to do the conversion.

by Anonymous User
Not applicable

Also interesting, QGIS prompts "Select Items to Add", and shows the empty Unknown data in OP's sample if you add it to a map.

Simple ogr2ogr like

ogr2ogr -f "ESRI Shapefile" .../EmptyShapefile_ogr.shp .../EmptyShapefile.shp

 

forced the input to Linestring and set the extents both to 0.0. QGIS opened that file with no prompt.

So, multiple ways to handle this. Might be worth talking to Alteryx as well.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Maybe try "continue" instead? It'll pass to the next item in the for() loop.

0 Kudos
by Anonymous User
Not applicable

Bummer!  Maybe try BaseException instead of Exception.  ... You can see what type of exception is being thrown in the debugger and you can use that type to target the exception that is being thrown. For my testing, I'm getting a 000732 ExecuteError on a file that it cant open and 000229 may be some other exception type.

 

Note that printing 'err' will print all of the exception including the Failed to execute (GetCount). as a way to let you know that file failed... you can remove it if you want. If its catching and passing correctly (pass isnt really needed in there), you should see the rows value is:  x printed.

0 Kudos