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 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 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
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))