Validity test for a shapefile

Hi,
Is there a simple and reliable way (or code) to test if a shapefile is valid?

Thanks!

Hello @lang

There is a validation call for the Shapes in a Shapefile. Unless you are looking for something higher level.

Regards,
Jerry.

Jerry,
Thanks a lot for the quick response! This is helpful, yet do we have anything higher level?

Jerry,
Methods with built-in shapefile validation includes Shapefile.SaveAs, does it mean if a shapefile is saved successfully by using SaveAs, it is valid?
Thanks!

Also, what is the easiest way to check is a file is a shapefile?

Hello @lang

It is probably true in general that if you have a Shapefile already in memory, it is a valid Shapefile. Now that doesn’t mean all shapes within that file are valid.

So the first test is if you can successfully open a file as a Shapefile, then that’s probably the easiest way to know that it’s a Shapefile. You can then call FixupShapes, which will check the validity of each of the shapes within the file (there is also a Utils function of the same name).

And like you say, when you call SaveAs (which is often called on an in-memory Shapefile), it makes a call to validate the shapes prior to saving. Note that this function will also be creating a DBF file that had not yet existed, and will truncate field names to 10 characters, among other things. (In-memory Shapefiles can have Field names longer than 10 characters).

Regards,
Jerry.

Jerry,
Appreciate it! Really helpful. So when I use Shapefile.FixupShapes, it’ll return true when all shape is fixed. it’ll create a new shapefile in memory and I don’t need to save it, right? So just check if returns true, the shapefile is good.

This is the first time I know that a dbf is created by SaveAs function.

Thanks a lot!
lang

Just for reference.
Use Shapefile.HasInvalidShapes() to first check if you have invalid shapes.
This method will stop processing at the first invalid shape, making it faster when used on shapefiles with a lot of shapes.
When this method returns true you can call Shapefile.FixUpShapes()