''' @author: olivier.massot, sept. 2018 ''' import shapefile SHAPES = {1:"Point", 3:"Polyligne", 5:"Polygone"} class ShapeError(IOError): pass class ShapeFile(): shapes = {1: "POINT", 3:"POLYLIGNE", 5:"POLYGONE"} def __enter__(self, path_): if not path_.isfile(): raise FileNotFoundError("Fichier introuvable") try: self.sf = shapefile.Reader(path_) except shapefile.ShapefileException: raise ShapeError("Fichier Shape illisible") try: _ = self.shapes[self.sf.shapeType] except KeyError: raise ShapeError("Type de géométrie non reconnue") return self def fields(self): return [f[0] for f in self.sf.fields if f[0] != 'DeletionFlag'] def records(self): return self.sf.shapeRecords() def __exit__(self): del self.sf