gis.py 951 B

12345678910111213141516171819202122232425262728293031323334353637
  1. '''
  2. @author: olivier.massot, sept. 2018
  3. '''
  4. import shapefile
  5. SHAPES = {1:"Point", 3:"Polyligne", 5:"Polygone"}
  6. class ShapeError(IOError): pass
  7. class ShapeFile():
  8. shapes = {1: "POINT", 3:"POLYLIGNE", 5:"POLYGONE"}
  9. def __enter__(self, path_):
  10. if not path_.isfile():
  11. raise FileNotFoundError("Fichier introuvable")
  12. try:
  13. self.sf = shapefile.Reader(path_)
  14. except shapefile.ShapefileException:
  15. raise ShapeError("Fichier Shape illisible")
  16. try:
  17. _ = self.shapes[self.sf.shapeType]
  18. except KeyError:
  19. raise ShapeError("Type de géométrie non reconnue")
  20. return self
  21. def fields(self):
  22. return [f[0] for f in self.sf.fields if f[0] != 'DeletionFlag']
  23. def records(self):
  24. return self.sf.shapeRecords()
  25. def __exit__(self):
  26. del self.sf