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)
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)
def get_map(self): """ Retrieves the map from the Internet and estimate the zoom level. Parameters: ----------- self.ulx,self.uly,self.lrx,self.lry, self.csize Object Variables: ----------------- self.map, self.zoom_lvl Returns: -------- True/False according the load was successful """ if(self.scalef == 0.0): # check if scalef and ulx,uly,... are defined return False self.zoom_lvl=round(np.log2(np.cos(np.deg2rad(self.lry)))- np.log2(self.csize)+17.256199796589126,0) print('zomm level:', self.zoom_lvl) imgr = PILImageManager('RGB') osm = OSMManager(image_manager=imgr) print(self.lry,self.uly,self.ulx,self.lrx) image,bnds = osm.createOSMImage((self.lry, self.uly, self.ulx, self.lrx), self.zoom_lvl) osm.getTileCoord(self.ulx, self.uly, self.zoom_lvl) # image.show() self.map=np.array(image) return True
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()
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()
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')) ((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: sys.stderr.write("\nImportError: %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)
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)
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)
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)
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')
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__
""" 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()
""" 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()
class TestOSMManager(unittest.TestCase): def setUp(self): imgr = PILImageManager("RGB") self.osm = OSMManager(image_manager=imgr) def test_getTileCoord(self): # Arrange lon_deg = 24.945831 lat_deg = 60.192059 zoom = 15 # Act coord = self.osm.getTileCoord(lon_deg, lat_deg, zoom) # Assert self.assertEqual(coord, (18654, 9480)) def test_getTileURL(self): # Arrange tile_coord = (18654, 9480) zoom = 15 # Act url = self.osm.getTileURL(tile_coord, zoom) # Assert self.assertEqual(url, "https://tile.openstreetmap.org/15/18654/9480.png") def test_getLocalTileFilename(self): # Arrange tile_coord = (18654, 9480) zoom = 15 # Act filename = self.osm.getLocalTileFilename(tile_coord, zoom) # Assert self.assertTrue(filename.endswith("-15_18654_9480.png")) def test_retrieveTileImage(self): # Arrange tile_coord = (18654, 9480) zoom = 15 # Act filename = self.osm.retrieveTileImage(tile_coord, zoom) # Assert self.assertTrue(filename.endswith("-15_18654_9480.png")) def test_tileNWLatlon(self): # Arrange tile_coord = (18654, 9480) zoom = 15 # Act lat_deg, lon_deg = self.osm.tileNWLatlon(tile_coord, zoom) # Assert self.assertEqual((lat_deg, lon_deg), (60.19615576604439, 24.93896484375)) def test_createOSMImage(self): # Arrange minlat = 59.9225115912 maxlat = 60.297839409 minlon = 24.7828044415 maxlon = 25.2544966708 bounds = (minlat, maxlat, minlon, maxlon) zoom = 8 # Act im, new_bounds = self.osm.createOSMImage(bounds, zoom) # Assert self.assertEqual( new_bounds, (59.5343180010956, 60.930432202923335, 23.90625, 25.3125) ) self.assertEqual(im.size, (256, 512))
def get_osm_image((lat, lon), screensize, zoom): """ Create, crop and save a PIL image of OSM tiles patched together. """ lat, lon = float(lat), float(lon) metres_per_pixel = get_metres_per_pixel(lat, zoom) width_in_metres = screensize[0] * metres_per_pixel height_in_metres = screensize[1] * metres_per_pixel # print("bbox (metres):", width_in_metres, height_in_metres) bbox = get_bounding_box((lat, lon), (height_in_metres, width_in_metres)) osm = OSMManager(image_manager=PILImageManager('RGB'), server=args.url_base) img, bnds = osm.createOSMImage(bbox, zoom) print("Pre-crop size: ", img.size) img = ImageOps.fit(img, screensize, Image.ANTIALIAS) print("Post-crop size:", img.size) # outfile = os.path.join(tempfile.gettempdir(), "satellite_pies.bmp") img.save(outfile) return outfile def set_wallpaper(filename): """ Set an image as wallpaper. """ # TODO only needed if monitors == 1 # Multiple monitors need tiling to span