42 create cog
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import leafmap
import leafmap
Provide a dataset path or URL.
In [3]:
Copied!
url = "https://github.com/giswqs/data/raw/main/raster/srtm90.tif"
url = "https://github.com/giswqs/data/raw/main/raster/srtm90.tif"
Validate COG.
In [4]:
Copied!
leafmap.cog_validate(url)
leafmap.cog_validate(url)
The following warnings were found:
- The file is greater than 512xH or 512xW, it is recommended to include internal overviews
Out[4]:
(True, [], ['The file is greater than 512xH or 512xW, it is recommended to include internal overviews'])
In [5]:
Copied!
leafmap.cog_validate(url, verbose=True)
leafmap.cog_validate(url, verbose=True)
Out[5]:
Info(Path='https://github.com/giswqs/data/raw/main/raster/srtm90.tif', Driver='GTiff', COG=True, Compression='LZW', ColorSpace=None, COG_errors=None, COG_warnings=['The file is greater than 512xH or 512xW, it is recommended to include internal overviews'], Profile=Profile(Bands=1, Width=4269, Height=2465, Tiled=True, Dtype='int16', Interleave='BAND', AlphaBand=False, InternalMask=False, Nodata=None, ColorInterp=('gray',), ColorMap=False, Scales=(1.0,), Offsets=(0.0,)), GEO=Geo(CRS='EPSG:4326', BoundingBox=(-120.75592734926073, 36.63401097629554, -117.30451019614512, 38.6269234341147), Origin=(-120.75592734926073, 38.6269234341147), Resolution=(0.0008084837557075694, -0.0008084837557075694), MinZoom=7, MaxZoom=11), Tags={'Image Metadata': {'AREA_OR_POINT': 'Area'}, 'Image Structure': {'COMPRESSION': 'LZW', 'INTERLEAVE': 'BAND'}}, Band_Metadata={'Band 1': BandMetadata(Description='elevation', ColorInterp='gray', Offset=0.0, Scale=1.0, Metadata={})}, IFD=[IFD(Level=0, Width=4269, Height=2465, Blocksize=(256, 256), Decimation=0)])
Convert the image to tiled COG.
In [6]:
Copied!
out_cog = "cog.tif"
leafmap.image_to_cog(url, out_cog)
out_cog = "cog.tif"
leafmap.image_to_cog(url, out_cog)
Reading input: https://github.com/giswqs/data/raw/main/raster/srtm90.tif Adding overviews... Updating dataset tags... Writing output to: /home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif
Validate COG.
In [7]:
Copied!
leafmap.cog_validate(out_cog)
leafmap.cog_validate(out_cog)
Out[7]:
(True, [], [])
In [8]:
Copied!
leafmap.cog_validate(out_cog, verbose=True)
leafmap.cog_validate(out_cog, verbose=True)
Out[8]:
Info(Path='/home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif', Driver='GTiff', COG=True, Compression='DEFLATE', ColorSpace=None, COG_errors=None, COG_warnings=None, Profile=Profile(Bands=1, Width=4269, Height=2465, Tiled=True, Dtype='int16', Interleave='BAND', AlphaBand=False, InternalMask=False, Nodata=None, ColorInterp=('gray',), ColorMap=False, Scales=(1.0,), Offsets=(0.0,)), GEO=Geo(CRS='EPSG:4326', BoundingBox=(-120.75592734926073, 36.63401097629554, -117.30451019614512, 38.6269234341147), Origin=(-120.75592734926073, 38.6269234341147), Resolution=(0.0008084837557075694, -0.0008084837557075694), MinZoom=7, MaxZoom=11), Tags={'Image Metadata': {'AREA_OR_POINT': 'Area', 'OVR_RESAMPLING_ALG': 'NEAREST'}, 'Image Structure': {'COMPRESSION': 'DEFLATE', 'INTERLEAVE': 'BAND', 'LAYOUT': 'COG'}}, Band_Metadata={'Band 1': BandMetadata(Description='elevation', ColorInterp='gray', Offset=0.0, Scale=1.0, Metadata={})}, IFD=[IFD(Level=0, Width=4269, Height=2465, Blocksize=(512, 512), Decimation=0), IFD(Level=1, Width=2135, Height=1233, Blocksize=(512, 512), Decimation=2), IFD(Level=2, Width=1068, Height=617, Blocksize=(512, 512), Decimation=4), IFD(Level=3, Width=534, Height=309, Blocksize=(512, 512), Decimation=8)])
Add COG to map.
In [9]:
Copied!
m = leafmap.Map()
m.add_local_tile(out_cog, palette="dem", layer_name="Local COG")
m.add_cog_layer(url, palette="gist_earth", name="Remote COG")
m
m = leafmap.Map()
m.add_local_tile(out_cog, palette="dem", layer_name="Local COG")
m.add_cog_layer(url, palette="gist_earth", name="Remote COG")
m
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [9], in <cell line: 2>() 1 m = leafmap.Map() ----> 2 m.add_local_tile(out_cog, palette="dem", layer_name="Local COG") 3 m.add_cog_layer(url, palette="gist_earth", name="Remote COG") 4 m File ~/.local/lib/python3.9/site-packages/leafmap/foliumap.py:351, in Map.add_local_tile(self, source, band, palette, vmin, vmax, nodata, attribution, layer_name, **kwargs) 320 def add_local_tile( 321 self, 322 source, (...) 330 **kwargs, 331 ): 332 """Add a local raster dataset to the map. 333 334 If you are using this function in JupyterHub on a remote server (e.g., Binder, Microsoft Planetary Computer), (...) 348 layer_name (str, optional): The layer name to use. Defaults to 'Local COG'. 349 """ --> 351 tile_layer, tile_client = get_local_tile_layer( 352 source, 353 band=band, 354 palette=palette, 355 vmin=vmin, 356 vmax=vmax, 357 nodata=nodata, 358 attribution=attribution, 359 tile_format="folium", 360 layer_name=layer_name, 361 return_client=True, 362 **kwargs, 363 ) 364 self.add_layer(tile_layer) 366 bounds = tile_client.bounds() # [ymin, ymax, xmin, xmax] File ~/.local/lib/python3.9/site-packages/leafmap/common.py:3855, in get_local_tile_layer(source, port, debug, projection, band, palette, vmin, vmax, nodata, attribution, tile_format, layer_name, return_client, **kwargs) 3851 raise ValueError("The source must either be a string or TileClient") 3853 if isinstance(palette, str): -> 3855 palette = get_palette_colors(palette, hashtag=True) 3857 if tile_format not in ["ipyleaflet", "folium"]: 3858 raise ValueError("The tile format must be either ipyleaflet or folium.") File ~/.local/lib/python3.9/site-packages/leafmap/common.py:4876, in get_palette_colors(cmap_name, n_class, hashtag) 4873 import matplotlib as mpl 4874 import matplotlib.pyplot as plt -> 4876 cmap = plt.cm.get_cmap(cmap_name, n_class) 4877 colors = [mpl.colors.rgb2hex(cmap(i))[1:] for i in range(cmap.N)] 4878 if hashtag: File ~/.local/lib/python3.9/site-packages/matplotlib/cm.py:286, in get_cmap(name, lut) 284 if isinstance(name, colors.Colormap): 285 return name --> 286 _api.check_in_list(sorted(_cmap_registry), name=name) 287 if lut is None: 288 return _cmap_registry[name] File ~/.local/lib/python3.9/site-packages/matplotlib/_api/__init__.py:129, in check_in_list(_values, _print_supported_values, **kwargs) 127 if _print_supported_values: 128 msg += f"; supported values are {', '.join(map(repr, values))}" --> 129 raise ValueError(msg) ValueError: 'dem' is not a valid value for name; supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'
Last update:
2022-03-14