source: cows/trunk/cows/bbox_util.py @ 4352

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/bbox_util.py@4398
Revision 4352, 2.8 KB checked in by domlowe, 11 years ago (diff)

Removing old bbox aggregator code

Line 
1"""
2Utilities for manipulating bounding boxes as a tuple (llx, lly, urx, ury)
3
4@note: This has no connection with the cows data model.  This module
5    will probably move eventually.
6   
7"""
8
9import math
10
11def intersection(bbox1, bbox2):
12    """
13    Return a bbox of the intersection of bbox1 and bbox2.
14
15    """
16    llx = max(bbox1[0], bbox2[0])
17    lly = max(bbox1[1], bbox2[1])
18    urx = min(bbox1[2], bbox2[2])
19    ury = min(bbox1[3], bbox2[3])
20
21    return llx, lly, urx, ury
22
23def union(bbox1, bbox2):
24    """
25    @ivar bbox1: bounding box tuple (llx, lly, urx, ury).
26    @ivar bbox2: bounding box tuple (llx, lly, urx, ury) to aggregate with bbox1
27    @return: union of bbox1 and bbox2 as tuple, (llx, lly, urx, ury)
28    """
29    llx = min(bbox1[0], bbox2[0])
30    lly = min(bbox1[1], bbox2[1])
31    urx = max(bbox1[2], bbox2[2])
32    ury = max(bbox1[3], bbox2[3])
33    return llx, lly, urx, ury   
34
35def relativeSize(bbox1, bbox2):
36    """
37    Calculate the relative size of bbox1 to bbox2.
38
39    @return: (sx, sy) where bbox1_width = sx * bbox2_width, etc.
40
41    """
42    sx = (bbox1[2]-bbox1[0]) / (bbox2[2]-bbox2[0])
43    sy = (bbox1[3]-bbox1[1]) / (bbox2[3]-bbox2[1])
44
45    return (sx, sy)
46
47def geoToPixel(x_g, y_g, bbox, width, height, roundUpX=False, roundUpY=False):
48    """
49    Calculate the pixel coordinate of a point within a bbox given the
50    width and height of the bbox in pixels.
51
52    This algorithm takes the origin of the bbox as the bottom left and
53    the origin of the image as top left.
54
55    @param x_g: x coordinate in goegraphic coordinates
56    @param y_g: y coordinate in geographic coordinates
57    @param bbox: The bounding box of the image
58    @param width: The width of the image in pixels
59    @param height: The height of the image in pixels.
60    @param roundUpX: Round the X pixel value upwards.
61    @param roundUpY: Round the y pixel value upwards.
62   
63    @return: (x,y) in pixel coordinates
64
65    """
66
67    x = (x_g - bbox[0])*width / (bbox[2]-bbox[0])
68    y = (bbox[3] - y_g)*height / (bbox[3]-bbox[1])
69
70    if roundUpX:
71        x = math.ceil(x)
72    if roundUpY:
73        y = math.ceil(y)
74
75    return int(x), int(y)
76
77def pixelToGeo(x, y, bbox, width, height):
78    """
79    Calculate the geographic coordinates of a pixel on an image
80    of given bbox and size.
81
82    This algorithm takes the origin of the bbox as the bottom left and
83    the origin of the image as top left.
84
85    @param x: x coordinate in pixel coordinates
86    @param y: y coordinate in pixel coordinates
87    @param bbox: The bounding box of the image
88    @param width: The width of the image in pixels
89    @param height: The height of the image in pixels.
90   
91    @return: (x_g, y_g) in geographic coordinates
92   
93    """
94
95    x_g = bbox[0] + ((bbox[2]-bbox[0]) / width)*x   
96    y_g = bbox[3] - ((bbox[3]-bbox[1]) / height)*y
97
98    return (x_g, y_g)
Note: See TracBrowser for help on using the repository browser.