Пример #1
0
def get_map(x_ul, y_ul, x_lr, y_lr, p_size):
    """the function 'get_map()' retrieves an open street map within the
       given bounding box and the zoom level that corresponds best to
       the desired pixel size.

       p_size=C*cos(lat)/2**(zoom_lvl+8)

       p_size   - pixel size in m
       C        - length of equator
       lat      - latitude
       zoom_lvl - OSM zoom level

       this formula has been manipulated and constant values have been
       included and precalculated to derive the zoom level from a given
       pixel size
       The map is returned as numpy array.
    """
    zoom_lvl = round(
        np.log2(np.cos(np.deg2rad(y_lr))) - np.log2(p_size) +
        17.256199796589126, 0)
    imgr = PILImageManager('RGB')
    osm = OSMManager(image_manager=imgr)
    image, bnds = osm.createOSMImage((y_lr, y_ul, x_ul, x_lr), zoom_lvl)
    osm.getTileCoord(x_ul, y_ul, zoom_lvl)
    # image.show()
    map_arr = np.array(image)
    return (map_arr, zoom_lvl)
Пример #2
0
def test_pil():
    imgr = PILImageManager('RGB')
    osm = OSMManager(image_manager=imgr)
    image, bnds = osm.createOSMImage((30, 35, -117, -112), 9)
    wh_ratio = float(image.size[0]) / image.size[1]
    image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
    del image
    image2.show()
Пример #3
0
def test_pil():
    image_manager = PILImageManager("RGB")
    osm = OSMManager(image_manager=image_manager)
    image, bounds = osm.create_osm_image((30, 31, -117, -116), 9)
    wh_ratio = float(image.size[0]) / image.size[1]
    image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
    del image
    image2.show()
Пример #4
0
def _GetOSMImage(bbox, zoom):
    # Just a wrapper for osm.createOSMImage to translate coordinate schemes
    try:
        from osmviz.manager import PILImageManager, OSMManager
        osm = OSMManager(image_manager=PILImageManager('RGB'),
                         server=options.osm_base)
        ((lat1, lon1), (lat2, lon2)) = bbox.Corners()
        image, bounds = osm.createOSMImage((lat1, lat2, lon1, lon2), zoom)
        (lat1, lat2, lon1, lon2) = bounds
        return image, BoundingBox(corners=((lat1, lon1), (lat2, lon2)))
    except ImportError, e:
        logging.error(
            "ImportError: %s.\n"
            "The --osm option depends on the osmviz module, available from\n"
            "http://cbick.github.com/osmviz/\n\n" % str(e))
        sys.exit(1)
Пример #5
0
def _get_osm_image(bbox, zoom, osm_base):
    # Just a wrapper for osm.createOSMImage to translate coordinate schemes
    try:
        from osmviz.manager import PILImageManager, OSMManager
        osm = OSMManager(image_manager=PILImageManager('RGB'), server=osm_base)
        (c1, c2) = bbox.corners()
        image, bounds = osm.createOSMImage((c1.lat, c2.lat, c1.lon, c2.lon),
                                           zoom)
        (lat1, lat2, lon1, lon2) = bounds
        return image, Extent(coords=(LatLon(lat1, lon1), LatLon(lat2, lon2)))
    except ImportError as e:
        logging.error(
            "ImportError: %s.\n"
            "The --osm option depends on the osmviz module, available from\n"
            "http://cbick.github.com/osmviz/\n\n" % str(e))
        sys.exit(1)
Пример #6
0
def pos_map(x_ul, y_ul, zoom_lvl, projection=31469):
    """
       calculates the pos for the cutting
    """
    imgr = PILImageManager('RGB')
    osm = OSMManager(image_manager=imgr)
    xtile, ytile = osm.getTileCoord(x_ul, y_ul, zoom_lvl)
    n = 2.0**zoom_lvl
    lon_deg = xtile / n * 360.0 - 180.0
    lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
    lat_deg = math.degrees(lat_rad)
    control = "epsg:%d" % projection
    #print control
    inProj = Proj(init='epsg:4326')
    outProj = Proj(init=control)
    mapx_ul, mapy_ul = transform(inProj, outProj, lon_deg, lat_deg)
    return (mapx_ul, mapy_ul)
Пример #7
0
 def __init__(self, center_y, center_x):
     self.center_x = center_x
     self.center_y = center_y
     min_lat, max_lat, min_lon, max_lon = center_y - \
         cf.BOARDER_Y, center_y + cf.BOARDER_Y, center_x - \
         cf.BOARDER_X, center_x + cf.BOARDER_X
     imgr = PILImageManager('RGB')
     osm = OSMManager(image_manager=imgr)
     # min_lat, max_lat, min_lon, max_lon, OSM-zoom
     image, _ = osm.createOSMImage((min_lat, max_lat, min_lon, max_lon),
                                   cf.MAP_ZOOM)
     wh_ratio = float(image.size[0]) / image.size[1]
     image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
     mode = image2.mode
     size = image2.size
     data = image2.tobytes()
     self.image = pg.image.fromstring(data, size, mode)
     pg.image.save(self.image, 'background.jpg')
Пример #8
0
    def map_update(self, center_y, center_x):
        """Fetch the map tiles at the current coordinate.

        Arguments:
            center_y {float} -- the latitudinal coordinate
            center_x {float} -- the longitudinal coordinate
        """
        sys.stdout = open(os.devnull, "w")
        min_lat, max_lat, min_lon, max_lon = center_y - \
            cf.BOARDER_Y, center_y + cf.BOARDER_Y, center_x - \
            cf.BOARDER_X, center_x + cf.BOARDER_X
        imgr = PILImageManager('RGB')
        osm = OSMManager(image_manager=imgr)
        # min_lat, max_lat, min_lon, max_lon, OSM-zoom
        image, _ = osm.createOSMImage((min_lat, max_lat, min_lon, max_lon),
                                      cf.MAP_ZOOM)
        wh_ratio = float(image.size[0]) / image.size[1]
        image2 = image.resize((int(1000 * wh_ratio), 800), Image.ANTIALIAS)
        mode = image2.mode
        size = image2.size
        data = image2.tobytes()
        self.image = pg.image.fromstring(data, size, mode)
        pg.image.save(self.image, 'background.jpg')
        sys.stdout = sys.__stdout__
Пример #9
0
"""
This example demonstrates how to create and show a PIL image
of OSM tiles patched together.
"""

import PIL.Image as Image
from osmviz.manager import OSMManager, PILImageManager

imgr = PILImageManager("RGB")
osm = OSMManager(image_manager=imgr)
image, bnds = osm.createOSMImage((30, 35, -117, -112), 9)
wh_ratio = float(image.size[0]) / image.size[1]
image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
del image
image2.show()
Пример #10
0
"""
This example demonstrates how to create and show a PIL image
of OSM tiles patched together.
"""

from osmviz.manager import PILImageManager, OSMManager
import PIL.Image as Image

imgr = PILImageManager('RGB')
osm = OSMManager(image_manager=imgr)
image, bnds = osm.createOSMImage((30, 35, -117, -112), 9)
wh_ratio = float(image.size[0]) / image.size[1]
image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
del image
image2.show()
Пример #11
0
def osm_manager():
    image_manager = PILImageManager("RGB")
    osm_manager = OSMManager(image_manager=image_manager)
    yield osm_manager
Пример #12
0
 def setUp(self):
     mode = "RGB"
     self.imgr = PILImageManager(mode)
Пример #13
0
class TestPILImageManager(unittest.TestCase):
    def setUp(self):
        mode = "RGB"
        self.imgr = PILImageManager(mode)

    def test_prepare_image(self):
        # Arrange
        width, height = 200, 100

        # Act
        self.imgr.prepare_image(width, height)

        # Assert
        self.assertEqual(self.imgr.image.size, (200, 100))

    def test_prepare_image__twice(self):
        # Arrange
        width, height = 200, 100

        # Act
        self.imgr.prepare_image(width, height)

        # Assert
        self.assertRaises(Exception, lambda: self.imgr.prepare_image(width, height))

    def test_destroy_image__no_image(self):
        # Arrange
        # Act
        self.imgr.destroy_image()

        # Assert
        self.assertIsNone(self.imgr.image)

    def test_destroy_image__with_image(self):
        # Arrange
        width, height = 200, 100
        self.imgr.prepare_image(width, height)
        self.assertEqual(self.imgr.image.size, (200, 100))

        # Act
        self.imgr.destroy_image()

        # Assert
        self.assertIsNone(self.imgr.image)

    def test_paste_image_file__image_not_prepared(self):
        # Arrange
        imagef = "dummy.jpg"
        xy = (0, 0)

        # Act / Assert
        self.assertRaises(Exception, lambda: self.imgr.paste_image_file(imagef, xy))

    def test_paste_image_file__could_not_load_image(self):
        # Arrange
        width, height = 200, 100
        self.imgr.prepare_image(width, height)
        self.assertEqual(self.imgr.image.size, (200, 100))
        imagef = "dummy.jpg"
        xy = (0, 0)

        # Act / Assert
        self.assertRaises(Exception, lambda: self.imgr.paste_image_file(imagef, xy))

    def test_paste_image_file(self):
        # Arrange
        width, height = 200, 100
        self.imgr.prepare_image(width, height)
        self.assertEqual(self.imgr.image.size, (200, 100))
        imagef = "test/images/bus.png"
        xy = (0, 0)

        # Act
        self.imgr.paste_image_file(imagef, xy)

        # Assert
        self.assertEqual(self.imgr.image.size, (200, 100))

    def test_getImage(self):
        # Arrange
        width, height = 200, 100
        self.imgr.prepare_image(width, height)
        self.assertIsNotNone(self.imgr.image)

        # Act
        im = self.imgr.getImage()

        # Assert
        self.assertEqual(im.size, (200, 100))
Пример #14
0
 def setUp(self):
     imgr = PILImageManager("RGB")
     self.osm = OSMManager(image_manager=imgr)
Пример #15
0
def image_manager():
    mode = "RGB"
    image_manager = PILImageManager(mode)
    yield image_manager
Пример #16
0
"""
This example demonstrates how to create and show a PIL image
of OSM tiles patched together.
"""

from PIL import Image

from osmviz.manager import OSMManager, PILImageManager

image_manager = PILImageManager("RGB")
osm = OSMManager(image_manager=image_manager)
image, bounds = osm.create_osm_image((30, 35, -117, -112), 9)
wh_ratio = float(image.size[0]) / image.size[1]
image2 = image.resize((int(800 * wh_ratio), 800), Image.ANTIALIAS)
del image
image2.show()