# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import leafmap.foliumap as leafmap

m = leafmap.Map(center=[35, -100], zoom=5)
m.add_basemap("CartoDB.DarkMatter")
m.add_tile_layer(
    url="https://data.source.coop/cboettig/mobi/tiles/grey/species-richness-all/{z}/{x}/{y}.png",
    name="Imperiled Species Richness (Greyscale)",
    attribution="NatureServe",
    opacity=0.99
)
m.to_html("mobi-grey.html")
m

# +

m = leafmap.Map(center=[35, -100], zoom=4)
#m.add_basemap("Esri.WorldGrayCanvas")
m.add_basemap("Esri.WorldShadedRelief")

m.add_tile_layer(
    url="https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
    name="Species Richness",
    attribution="NatureServe",
    opacity=0.9
)
m.to_html("mobi-red.html")
m


# +
import leafmap.foliumap as leafmap
m = leafmap.Map(center=[35, -100], zoom=4)
m.add_basemap("Esri.WorldShadedRelief")

m.add_tile_layer(
    url="https://data.source.coop/cboettig/mobi/tiles/green/range-size-rarity-all/{z}/{x}/{y}.png",
    name="Range-Size-Rarity",
    attribution="NatureServe",
    opacity=0.9
)
m.to_html("mobi-green.html")
m

# +

m = leafmap.Map(center=[35, -100], zoom=4)
#m.add_basemap("Esri.WorldGrayCanvas")
m.add_basemap("Esri.WorldShadedRelief")

m.add_tile_layer(
    url="https://data.source.coop/cboettig/mobi/tiles/red/species-richness-all/{z}/{x}/{y}.png",
    name="Species Richness",
    attribution="NatureServe",
    opacity=0.9
)
m.add_tile_layer(
    url="https://data.source.coop/cboettig/mobi/tiles/green/range-size-rarity-all/{z}/{x}/{y}.png",
    name="Range-Size-Rarity",
    attribution="NatureServe",
    opacity=0.9
)
m.to_html("mobi-layers.html")

m
# -

url = "https://data.source.coop/cboettig/mobi/species-richness-all/SpeciesRichness_All.tif"
leafmap.cog_validate(url, verbose=True)

# +
cog = "species-richness-all/SpeciesRichness_All.tif"
leafmap.image_to_cog(url, cog)
leafmap.cog_validate(cog, verbose=True)


# -

url = "https://data.source.coop/cboettig/mobi/range-size-rarity-all/RSR_All.tif"
cog = "range-size-rarity-all/RSR_All.tif"
leafmap.image_to_cog(url, cog)

# +
import numpy as np
m = leafmap.Map(center=[35, -100], zoom=4)
m.add_basemap("Esri.WorldShadedRelief")

#m.add_cog_layer("https://data.source.coop/cboettig/mobi/species-richness-all/SpeciesRichness_All.tif", palette="Reds", name="Richness", nodata=0, transparent_bg=True, opacity = 0.9)
m.add_cog_layer("https://data.source.coop/cboettig/mobi/range-size-rarity-all/RSR_All.tif", palette="greens", name="RSR", transparent_bg=True, opacity = 0.8)

m.to_html("cogs.html")

m
