# Validity test for a shapefile

**URL:** <https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858>\
**Category:** Uncategorized\
**Created:** [January 7, 2021, 1:49am UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858 "2021-01-07T01:49:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lang](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lang](https://mapwindow.discourse.group/u/lang)\
**Post date:** [January 7, 2021, 1:49am UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/1 "2021-01-07T01:49:41Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 7, 2021, 6:06pm UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/2 "2021-01-07T18:06:36Z")

</div>

Hello @lang

There is a [validation call](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ validation.html) for the Shapes in a Shapefile. Unless you are looking for something higher level.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![lang](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lang](https://mapwindow.discourse.group/u/lang)\
**Post date:** [January 7, 2021, 6:09pm UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/3 "2021-01-07T18:09:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lang](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lang](https://mapwindow.discourse.group/u/lang)\
**Post date:** [January 8, 2021, 8:18pm UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/4 "2021-01-08T20:18:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lang](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lang](https://mapwindow.discourse.group/u/lang)\
**Post date:** [January 8, 2021, 8:25pm UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/5 "2021-01-08T20:25:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 9, 2021, 6:14am UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/6 "2021-01-09T06:14:34Z")

</div>

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](https://www.mapwindow.org/documentation/mapwingis4.9/group __shapefile__ geoprocessing.html#ga196e63e19402ef16992fbe99c049d872), 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.

---

<div class="post-metadata">

**Author:** ![lang](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lang](https://mapwindow.discourse.group/u/lang)\
**Post date:** [January 11, 2021, 4:13am UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/7 "2021-01-11T04:13:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 10:50am UTC](https://mapwindow.discourse.group/t/validity-test-for-a-shapefile/858/8 "2022-01-05T10:50:16Z")

</div>

Just for reference.  
Use [Shapefile.HasInvalidShapes()](https://mapwindow.org/documentation/mapwingis4.9/group __shapefile__ validation.html#ga1b21d3ace037490c895b0d37b7785511) 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()](https://mapwindow.org/documentation/mapwingis4.9/group __shapefile__ geoprocessing.html#ga196e63e19402ef16992fbe99c049d872)
