gis.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. '''
  2. @author: olivier.massot, sept. 2018
  3. '''
  4. import shapefile
  5. POINT = 1
  6. POLYLINE = 3
  7. POLYGON = 5
  8. SHAPE_NAMES = {1: "POINT", 3:"POLYLIGNE", 5:"POLYGONE"}
  9. class ShapeError(IOError): pass
  10. class ShapeFile():
  11. def __init__(self, path_):
  12. self.path_ = path_
  13. def __enter__(self):
  14. if not self.path_.isfile():
  15. raise FileNotFoundError("Fichier introuvable")
  16. try:
  17. self.sf = shapefile.Reader(self.path_)
  18. except shapefile.ShapefileException:
  19. raise ShapeError("Fichier Shape illisible")
  20. if not self.sf.shapeType in (POINT, POLYLINE, POLYGON):
  21. raise ShapeError("Type de géométrie non reconnue")
  22. return self
  23. def shape(self):
  24. return self.sf.shapeType
  25. def fields(self):
  26. return [f[0] for f in self.sf.fields if f[0] != 'DeletionFlag']
  27. def records(self):
  28. for record in self.sf.shapeRecords():
  29. yield record
  30. def __exit__(self, exc_type, exc_val, exc_tb):
  31. del self.sf