wqjzzgci / pyshp

Automatically exported from code.google.com/p/pyshp
0 stars 0 forks source link

Better error reporting for missing shx #10

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I was using pyshp today on a set of test shapefiles.  When I copied my test 
shapefiles into the test folder, I missed one of the .shx files.  I received an 
exception when the code attempted to access one of the shapefiles.  Summarized, 
my code looked like this:

import shapefile
reader = shapefile.Reader(r'D:\work\code\test\streets.shp')
reader.shape(0)

I received the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shapefile.py", line 220, in shape
    offset = self.__shapeIndex(i)
  File "shapefile.py", line 196, in __shapeIndex
    f = self.__getFileObj(self.shx)
  File "shapefile.py", line 115, in __getFileObj
    raise ShapefileException("Required file not available.")
shapefile.ShapefileException: Required file not available.

Now, technically, there is nothing wrong with this message.  I'm dealing with 
an "exceptional case" and an Exception was thrown in this case.  I just wish it 
was a bit more specific about which file was missing.  So, I patched 
shapefile.py to report the issue more cleanly for the missing shx case.

I have attached a diff with my change.

Joseph Armbruster

Original issue reported on code.google.com by josepharmbruster@gmail.com on 30 Jun 2011 at 7:39

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks - I'll update all of the exceptions. Actually it shouldn't even throw an 
exception for an shx as it's not really required to read the shapefile. It's 
just and efficiency helper. 

Original comment by import.s...@gmail.com on 30 Jun 2011 at 7:48

GoogleCodeExporter commented 8 years ago
Right.  But, if I remember correctly, the read functionality would throw hard 
and the None case wouldn't get caught.  I don't have it all checked out right 
now but that's what I remember.  I'll look into it in a bit.  I can put 
together a patch if you'd like :-)

Original comment by josepharmbruster@gmail.com on 1 Jul 2011 at 1:24

GoogleCodeExporter commented 8 years ago
Ok. I'll take any patches. The None exception should be caught in the reader to 
make it as robust as possible.  I'm not in front of my laptop but I'll check it 
out too. Good catch!

Original comment by import.s...@gmail.com on 1 Jul 2011 at 2:22

GoogleCodeExporter commented 8 years ago
hi, i would like to ask you what python version are you ussing?
thanks

Original comment by hidrom...@gmail.com on 28 Jul 2011 at 3:52

GoogleCodeExporter commented 8 years ago

Original comment by geospati...@gmail.com on 26 Aug 2011 at 7:42