def testInvalidPathConversions(self):
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, None)
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, 0)
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, '0,0,1,1.5')
     with self.assertRaises(ValueError):
         slippy_util.convert_path_to_tiles(0, [])
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, [(0), (1)])
Esempio n. 2
0
 def testInvalidPointConversions(self):
     with self.assertRaises(ValueError):
         slippy_util.convert_point_to_tile(-1, 0, 0)
     with self.assertRaises(ValueError):
         slippy_util.convert_point_to_tile(21, 0, 0)
     with self.assertRaises(ValueError):
         slippy_util.convert_point_to_tile(1, 91, 10)
     with self.assertRaises(ValueError):
         slippy_util.convert_point_to_tile(1, 10, 191)
     with self.assertRaises(TypeError):
         slippy_util.convert_point_to_tile(1, 10, None)
     with self.assertRaises(ValueError):
         slippy_util.convert_path_to_tiles(0, [(0, 0)])
     with self.assertRaises(OverflowError):
         slippy_util.convert_path_to_tiles(15, [(0, 0), (1, 1.5)])
Esempio n. 3
0
 def testInvalidPathConversions(self):
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, None)
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, 0)
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, '0,0,1,1.5')
     with self.assertRaises(ValueError):
         slippy_util.convert_path_to_tiles(0, [])
     with self.assertRaises(TypeError):
         slippy_util.convert_path_to_tiles(0, [(0), (1)])
     # test a lot of tiles calculation
     with self.assertRaises(OverflowError):
         slippy_util.convert_polygon_to_tiles(15, [(47.5, -103),
                                                   (47.5, -101.8),
                                                   (48, -101.8), (48, -103),
                                                   (47.5, -103)])
 def testValidPathConversions(self):
     self.assertEqual(
         1, len(slippy_util.convert_path_to_tiles(0, [(0, 0), (1, 1.5)])))
     self.assertEqual(
         2, len(slippy_util.convert_path_to_tiles(5, [(0, 0), (1, 1.5)])))
     self.assertEqual(
         229, len(slippy_util.convert_path_to_tiles(15, [(0, 0),
                                                         (1, 1.5)])))
     # One segment should be the same as two segments that overlapp
     self.assertEqual(
         len(slippy_util.convert_path_to_tiles(10, [(0, 0), (1, 1.5)])),
         len(
             slippy_util.convert_path_to_tiles(10, [(0, 0), (1, 1.5),
                                                    (0, 0)])))
     # 4 points are in 4 separate grids,
     # and there are 2 grids underlapping the path
     self.assertEqual(
         6,
         len(
             slippy_util.convert_path_to_tiles(9, [(47.5, -103),
                                                   (47.5, -102.5),
                                                   (48, -102.5), (48, -103),
                                                   (47.5, -103)])))
     # Corner cutter case that two points are in two grids, but they cut
     # a corner and that grid should be included
     self.assertEqual(
         3,
         len(
             slippy_util.convert_path_to_tiles(
                 9, [(37.936541030367316, -122.377713074509),
                     (37.69672993401783, -122.10422390269278)])))
    def testSlippyConversionsForSpecialCases(self):
        # 4x4 grid used for these tests at zoom 4
        # 8,8   9,8   10,8    11,8
        # 8,9   9,9   10,9    11,9
        # 8,10  9,10  10,10   11,10
        # 8,11  9,11  10,11   11,11
        # points of interest
        point_8x8 = (-19.808, 20.039)
        point_8x11 = (-65.730, 19.160)
        point_11x11 = (-58.263, 71.367)
        point_11x8 = (-6.839, 82.441)
        # all 16 for all four by polygon
        self.assertEqual(
            16,
            len(
                slippy_util.convert_polygon_to_tiles(
                    4, [point_8x8, point_8x11, point_11x11, point_11x8])))
        # only 10 by path (no closing the path)
        self.assertEqual(
            10,
            len(
                slippy_util.convert_path_to_tiles(
                    4, [point_8x8, point_8x11, point_11x11, point_11x8])))
        # corner to corner should be 7
        self.assertEqual(
            7,
            len(slippy_util.convert_path_to_tiles(4,
                                                  [point_8x8, point_11x11])))
        self.assertEqual(
            7,
            len(slippy_util.convert_path_to_tiles(4,
                                                  [point_8x11, point_11x8])))

        # triangle to the bottom is 11
        self.assertEqual(
            11,
            len(
                slippy_util.convert_polygon_to_tiles(
                    4, [point_11x8, point_8x11, point_11x11, point_11x8])))
 def testInvalidPointConversions(self):
     with self.assertRaises(ValueError):
         self.assertIsNone(slippy_util.convert_point_to_tile(-1, 0, 0))
     with self.assertRaises(ValueError):
         self.assertIsNone(slippy_util.convert_point_to_tile(21, 0, 0))
     with self.assertRaises(ValueError):
         self.assertIsNone(slippy_util.convert_point_to_tile(1, 91, 10))
     with self.assertRaises(ValueError):
         self.assertIsNone(slippy_util.convert_point_to_tile(1, 10, 191))
     with self.assertRaises(TypeError):
         self.assertIsNone(slippy_util.convert_point_to_tile(1, 10, None))
     with self.assertRaises(ValueError):
         self.assertEqual(
             1, len(slippy_util.convert_path_to_tiles(0, [(0, 0)])))
Esempio n. 7
0
def ConvertCoordinatesToSlippy(zoom):
    """Converts an CSV of coords to slippy tile format at the specified zoom.
  Args:
    zoom: zoom level to use for encapsulating the tiles
    Plus posted webarg coords: csv of lon,lat,long,lat,etc.
  Returns:
    200 with tiles array in JSON format,
    or the nominal 4xx error codes as necessary.
  """
    log.info('Convert coordinates to slippy instantiated for %sz...', zoom)
    try:
        zoom = int(zoom)
        tiles = []
        coords = _GetRequestParameter('coords', '')
        coord_type = _GetRequestParameter('coord_type', 'point')
        log.debug('Retrieved coords from web params and split to %s...',
                  coords)
        coordinates = slippy_util.convert_csv_to_coordinates(coords)
        if not coordinates:
            log.error('Invalid coords %s, must be a CSV of lat,lon...', coords)
            raise ValueError(
                'Invalid coords, must be a CSV of lat,lon,lat,lon...')
        if coord_type == 'point':
            for c in coordinates:
                tiles.append(
                    (slippy_util.convert_point_to_tile(zoom, c[0], c[1])))
        elif coord_type == 'path':
            tiles = slippy_util.convert_path_to_tiles(zoom, coordinates)
        elif coord_type == 'polygon':
            tiles = slippy_util.convert_polygon_to_tiles(zoom, coordinates)
        else:
            raise ValueError('Invalid coord_type, must be point/path/polygon')
        result = []
        for x, y in tiles:
            link = 'http://tile.openstreetmap.org/%d/%d/%d.png' % (zoom, x, y)
            tile = {'link': link, 'zoom': zoom, 'x': x, 'y': y}
            if tile not in result:
                result.append(tile)
    except (ValueError, TypeError) as e:
        log.error('/slippy error: %s...', e.message)
        abort(status.HTTP_400_BAD_REQUEST, e.message)

    return jsonify({
        'status': 'success',
        'data': {
            'zoom': zoom,
            'grid_cells': result,
        }
    })
Esempio n. 8
0
def _ConvertRequestToTiles(zoom):
    """Converts an CSV of coords into slippy tile format at the specified zoom
      and the specified coordinate type (path, polygon, point) """
    tiles = []
    coords = _GetRequestParameter('coords', '')
    coord_type = _GetRequestParameter('coord_type', 'point').lower()
    log.debug('Retrieved coords from web params and split to %s...', coords)
    coordinates = slippy_util.convert_csv_to_coordinates(coords)
    if not coordinates:
        log.error('Invalid coords %s, must be a CSV of lat,lon...', coords)
        raise ValueError('Invalid coords, must be a CSV of lat,lon,lat,lon...')
    if coord_type == 'point':
        for c in coordinates:
            tiles.append((slippy_util.convert_point_to_tile(zoom, c[0], c[1])))
    elif coord_type == 'path':
        tiles = slippy_util.convert_path_to_tiles(zoom, coordinates)
    elif coord_type == 'polygon':
        tiles = slippy_util.convert_polygon_to_tiles(zoom, coordinates)
    else:
        raise ValueError('Invalid coord_type, must be point/path/polygon')
    return tiles