# source:cows/trunk/cows/bbox_util.py@4912

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/bbox_util.py@4917
Revision 4912, 3.0 KB checked in by spascoe, 11 years ago (diff)

Added LICENSE file and a short banner at the top of each python file.
Use add_license.py to add a license to new files.

NOTE: also contains a couple of files missed at last commit.

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