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 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 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()
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 _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 _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 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)
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)
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 cut_map(self): """ Combines the definition of the map_ulx, map_uly and cutting of the map and resize of the map Parameters: ----------------- self.ulx,self.uly, self.zoom_lvl, self.epsg Returns: -------- new_map/None depending on the operations """ if(self.map is None): # check if map is loaded return None imgr = PILImageManager('RGB') osm = OSMManager(image_manager=imgr) xtile, ytile = osm.getTileCoord(self.ulx, self.uly, self.zoom_lvl) n = 2.0 ** self.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) map_ulx,map_uly = transform(self.wgs84,self.epsg,lon_deg,lat_deg) # print(lon_deg,lat_deg,map_ulx,map_uly) # cut the map p_size_map=40075017.*np.cos(np.deg2rad(self.uly))/2**(self.zoom_lvl+8) x1=self.llx y1=self.lly+(self.nrows*self.csize) dx=int((x1-map_ulx)/p_size_map) dy=int((map_uly-y1)/p_size_map) # print('cut_map:',dx,dy, p_size_map) map_new=self.map[dy:dy+ (self.nrows*int(self.csize/p_size_map)), dx:dx+(self.ncols*int(self.csize/p_size_map)),:] # resize the map map_final=scipy.misc.imresize(map_new, (self.nrows, self.ncols)) return map_final
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__
# print(min_lat, max_lat, min_lon, max_lon) return (min_lat, max_lat, min_lon, max_lon) 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
""" 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()
""" 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()
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 setUp(self): imgr = PILImageManager("RGB") self.osm = OSMManager(image_manager=imgr)
""" 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()
def osm_manager(): image_manager = PILImageManager("RGB") osm_manager = OSMManager(image_manager=image_manager) yield osm_manager