Coverage for hml/findExtent.py: 7%

15 statements  

« prev     ^ index     » next       coverage.py v7.8.1, created at 2025-05-23 08:31 +0000

1#!/usr/bin/env python3 

2 

3# Define function ... 

4def findExtent(sfObj, /, *, x1 = 1.0e10, x2 = 0.0, y1 = 1.0e10, y2 = 0.0): 

5 """ 

6 Update the supplied bounding box so that it encompasses the overall bounding 

7 box of the Polygons in the supplied ShapeFile. 

8 

9 Arguments: 

10 sfObj -- a shapefile.Reader of a ShapeFile 

11 

12 Keyword arguments: 

13 x1 -- lower x position of bounding box (default 1.0e100) 

14 y1 -- left y position of bounding box (default 1.0e100) 

15 x2 -- upper x position of bounding box (default 0.0) 

16 y2 -- right y position of bounding box (default 0.0) 

17 """ 

18 

19 # Import special modules ... 

20 try: 

21 import shapefile 

22 except: 

23 raise Exception("\"shapefile\" is not installed; run \"pip install --user pyshp\"") from None 

24 

25 # Check argument ... 

26 if not isinstance(sfObj, shapefile.Reader): 

27 raise TypeError("\"sfObj\" is not a shapefile.Reader") 

28 

29 # Loop over shape+record pairs ... 

30 for shapeRecord in sfObj.iterShapeRecords(): 

31 # Crash if this shape+record is not a shapefile polygon ... 

32 if shapeRecord.shape.shapeType != shapefile.POLYGON: 

33 raise Exception("\"shape\" is not a POLYGON") from None 

34 

35 # Update extents ... 

36 x1 = min(x1, shapeRecord.shape.bbox[0]) # [m] 

37 y1 = min(y1, shapeRecord.shape.bbox[1]) # [m] 

38 x2 = max(x2, shapeRecord.shape.bbox[2]) # [m] 

39 y2 = max(y2, shapeRecord.shape.bbox[3]) # [m] 

40 

41 # Return answer ... 

42 return x1, y1, x2, y2