Exemple #1
0
    def test_adjacent(self):
        cell = geocell.compute(geotypes.Point(37, -122), 14)
        box = geocell.compute_box(cell)

        # adjacency tests using bounding boxes
        self.assertEquals(
            box.north,
            geocell.compute_box(geocell.adjacent(cell, (0, 1))).south)
        self.assertEquals(
            box.south,
            geocell.compute_box(geocell.adjacent(cell, (0, -1))).north)
        self.assertEquals(
            box.east,
            geocell.compute_box(geocell.adjacent(cell, (1, 0))).west)
        self.assertEquals(
            box.west,
            geocell.compute_box(geocell.adjacent(cell, (-1, 0))).east)

        self.assertEquals(8, len(geocell.all_adjacents(cell)))

        # also test collinearity
        self.assertTrue(
            geocell.collinear(cell, geocell.adjacent(cell, (0, 1)), True))
        self.assertFalse(
            geocell.collinear(cell, geocell.adjacent(cell, (0, 1)), False))
        self.assertTrue(
            geocell.collinear(cell, geocell.adjacent(cell, (1, 0)), False))
        self.assertFalse(
            geocell.collinear(cell, geocell.adjacent(cell, (1, 0)), True))
    def test_adjacent(self):
        cell = geocell.compute(geotypes.Point(37, -122), 14)
        box = geocell.compute_box(cell)

        # adjacency tests using bounding boxes
        self.assertEquals(box.north,
                geocell.compute_box(geocell.adjacent(cell, (0, 1))).south)
        self.assertEquals(box.south,
                geocell.compute_box(geocell.adjacent(cell, (0, -1))).north)
        self.assertEquals(box.east,
                geocell.compute_box(geocell.adjacent(cell, (1, 0))).west)
        self.assertEquals(box.west,
                geocell.compute_box(geocell.adjacent(cell, (-1, 0))).east)

        self.assertEquals(8, len(geocell.all_adjacents(cell)))

        # also test collinearity
        self.assertTrue(
                geocell.collinear(cell, geocell.adjacent(cell, (0, 1)), True))
        self.assertFalse(
                geocell.collinear(cell, geocell.adjacent(cell, (0, 1)), False))
        self.assertTrue(
                geocell.collinear(cell, geocell.adjacent(cell, (1, 0)), False))
        self.assertFalse(
                geocell.collinear(cell, geocell.adjacent(cell, (1, 0)), True))
Exemple #3
0
def distance_sorted_edges(cells, point):
    """Returns the edges of the rectangular region containing all of the
  given geocells, sorted by distance from the given point, along with
  the actual distances from the point to these edges.

  Args:
    cells: The cells (should be adjacent) defining the rectangular region
        whose edge distances are requested.
    point: The point that should determine the edge sort order.

  Returns:
    A list of (direction, distance) tuples, where direction is the edge
    and distance is the distance from the point to that edge. A direction
    value of (0,-1), for example, corresponds to the South edge of the
    rectangular region containing all of the given geocells.
  """
    # TODO(romannurik): Assert that lat,lon are actually inside the geocell.
    boxes = [geocell.compute_box(cell) for cell in cells]

    max_box = geotypes.Box(max([box.north for box in boxes]),
                           max([box.east for box in boxes]),
                           min([box.south for box in boxes]),
                           min([box.west for box in boxes]))
    return zip(*sorted(
        [((0, -1),
          geomath.distance(geotypes.Point(max_box.south, point.lon), point)),
         ((0, 1),
          geomath.distance(geotypes.Point(max_box.north, point.lon), point)),
         ((-1, 0),
          geomath.distance(geotypes.Point(point.lat, max_box.west), point)),
         ((1, 0),
          geomath.distance(geotypes.Point(point.lat, max_box.east), point)
          )], lambda x, y: cmp(x[1], y[1])))
Exemple #4
0
def distance_sorted_edges(cells, point):
    """Returns the edges of the rectangular region containing all of the
    given geocells, sorted by distance from the given point, along with
    the actual distances from the point to these edges.

    Args:
        cells: The cells (should be adjacent) defining the rectangular region
                whose edge distances are requested.
        point: The point that should determine the edge sort order.

    Returns:
        A list of (direction, distance) tuples, where direction is the edge
        and distance is the distance from the point to that edge. A direction
        value of (0,-1), for example, corresponds to the South edge of the
        rectangular region containing all of the given geocells.
    """
    # TODO(romannurik): Assert that lat,lon are actually inside the geocell.
    boxes = [geocell.compute_box(cell) for cell in cells]

    max_box = geotypes.Box(max([box.north for box in boxes]),
                                                 max([box.east for box in boxes]),
                                                 min([box.south for box in boxes]),
                                                 min([box.west for box in boxes]))
    return zip(*sorted([
            ((0,-1), geomath.distance(geotypes.Point(max_box.south, point.lon),
                                                                point)),
            ((0,1),    geomath.distance(geotypes.Point(max_box.north, point.lon),
                                                                point)),
            ((-1,0), geomath.distance(geotypes.Point(point.lat, max_box.west),
                                                                point)),
            ((1,0),    geomath.distance(geotypes.Point(point.lat, max_box.east),
                                                                point))],
            lambda x, y: cmp(x[1], y[1])))
Exemple #5
0
def find_center_and_radius_of(square):  
  bbox = geocell.compute_box(square)
  south_west_bound = bbox.south_west
  north_east_bound = bbox.north_east
  midpoint = mid_point(south_west_bound, north_east_bound)
  radius = distance(midpoint, north_east_bound)
  return midpoint, radius
Exemple #6
0
def find_center_and_radius_of(square):
    bbox = geocell.compute_box(square)
    south_west_bound = bbox.south_west
    north_east_bound = bbox.north_east
    midpoint = mid_point(south_west_bound, north_east_bound)
    radius = distance(midpoint, north_east_bound)
    return midpoint, radius
Exemple #7
0
def max_box(cells):
  """Returns the rectangular region containing all of the given geocells.
  
  Args:
    cells: A list of adjacent geocells.
  
  Returns:
    A geotypes.Box representing the maximum bounds of the set of adjacent cells.
  """
  boxes = [geocell.compute_box(cell) for cell in cells]
  return geotypes.Box(max([box.north for box in boxes]),
                      max([box.east for box in boxes]),
                      min([box.south for box in boxes]),
                      min([box.west for box in boxes]))
Exemple #8
0
def max_box(cells):
    """Returns the rectangular region containing all of the given geocells.
  
  Args:
    cells: A list of adjacent geocells.
  
  Returns:
    A geotypes.Box representing the maximum bounds of the set of adjacent cells.
  """
    boxes = [geocell.compute_box(cell) for cell in cells]
    return geotypes.Box(
        max([box.north for box in boxes]),
        max([box.east for box in boxes]),
        min([box.south for box in boxes]),
        min([box.west for box in boxes]),
    )
Exemple #9
0
    def test_compute_box(self):
        cell = geocell.compute(geotypes.Point(37, -122), 14)
        box = geocell.compute_box(cell)

        self.assertTrue(box.south <= 37 and 37 <= box.north
                        and box.west <= -122 and -122 <= box.east)
    def test_compute_box(self):
        cell = geocell.compute(geotypes.Point(37, -122), 14)
        box = geocell.compute_box(cell)

        self.assertTrue(box.south <= 37 and 37 <= box.north and
                                        box.west <= -122 and -122 <= box.east)