def test_1_1(self): with self.settings(COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100), COMPOSITE_CELLS=(2, 2)): # Topleft of (1, 1) is (200, 200) x, y = projections.topleft_of_composite_pixel(1, 1) self.assertEquals(x, 200) self.assertEquals(y, 200)
def test_1_2(self): with self.settings(COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100), COMPOSITE_CELLS=(2, 2)): # (1, 2) doesn't exist, so should raise ValueError self.assertRaises( ValueError, lambda: projections.topleft_of_composite_pixel(1, 2))
def test_0_0(self): with self.settings(COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100), COMPOSITE_CELLS=(2, 2)): # Topleft of (0, 0) is the topleft of the whole extent x, y = projections.topleft_of_composite_pixel(0, 0) self.assertEquals(x, 100) self.assertEquals(y, 300)
def test_2_2(self): with self.settings( COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100)): # (2, 2) doesn't exist, so should raise ValueError self.assertRaises( ValueError, lambda: projections.topleft_of_composite_pixel(2, 2))
def test_1_1(self): with self.settings( COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100)): # Topleft of (1, 1) is (200, 200) x, y = projections.topleft_of_composite_pixel(1, 1) self.assertEquals(x, 200) self.assertEquals(y, 200)
def test_0_0(self): with self.settings( COMPOSITE_EXTENT=(100, 300, 300, 100), COMPOSITE_CELLSIZE=(100, 100)): # Topleft of (0, 0) is the topleft of the whole extent x, y = projections.topleft_of_composite_pixel(0, 0) self.assertEquals(x, 100) self.assertEquals(y, 300)
def extent_for_user(cls, user): if not user.is_authenticated(): return None if user.has_perm('lizard_neerslagradar.view_entire_region'): # Setting these to None leads to entire extent being set below # Usually only superuser will have the permission topleft = bottomright = None else: regions = user.region_set.all() if not regions: return None wgs84extents = [region.geometry.extent for region in regions] extents = [{ 'left': xmin, 'bottom': ymin, 'right': xmax, 'top': ymax } for xmin, ymin, xmax, ymax in wgs84extents] extent = { 'left': min(extent['left'] for extent in extents), 'bottom': min(extent['bottom'] for extent in extents), 'right': max(extent['right'] for extent in extents), 'top': max(extent['top'] for extent in extents) } # Now we want to translate the topleft and bottomright to # grid cells, and use their topleft and bottomright as the # _actual_ extent, so that it will contain whole grid # cells. topleft = projections.coordinate_to_composite_pixel( extent['left'], extent['top']) bottomright = ( projections.coordinate_to_composite_pixel( extent['right'], extent['bottom'])) if topleft is None: topleft = (0, 0) if bottomright is None: cells_x, cells_y = settings.COMPOSITE_CELLS bottomright = (cells_x - 1, cells_y - 1) left, top = projections.topleft_of_composite_pixel( *topleft, to_projection=coordinates.google_projection) right, bottom = projections.bottomright_of_composite_pixel( *bottomright, to_projection=coordinates.google_projection) return { 'left': str(left), 'bottom': str(bottom), 'right': str(right), 'top': str(top) }
def extent_for_user(cls, user): if not user.is_authenticated(): return None regions = user.region_set.all() if not regions: return None wgs84extents = [region.geometry.extent for region in regions] extents = [{ 'left': xmin, 'bottom': ymin, 'right': xmax, 'top': ymax } for xmin, ymin, xmax, ymax in wgs84extents] extent = { 'left': min(extent['left'] for extent in extents), 'bottom': min(extent['bottom'] for extent in extents), 'right': max(extent['right'] for extent in extents), 'top': max(extent['top'] for extent in extents) } # Now we want to translate the topleft and bottomright to grid cells, # and use their topleft and bottomright as the _actual_ extent, so that # it will contain whole grid cells. topleft = projections.coordinate_to_composite_pixel( extent['left'], extent['top']) bottomright = ( projections.coordinate_to_composite_pixel( extent['right'], extent['bottom'])) if topleft is None: topleft = (0, 0) if bottomright is None: cells_x, cells_y = settings.COMPOSITE_CELLS bottomright = (cells_x - 1, cells_y - 1) left, top = projections.topleft_of_composite_pixel( *topleft, to_projection=coordinates.google_projection) right, bottom = projections.bottomright_of_composite_pixel( *bottomright, to_projection=coordinates.google_projection) return { 'left': str(left), 'bottom': str(bottom), 'right': str(right), 'top': str(top) }