Skip to content

Commit

Permalink
Merge pull request #21 from 12rambau/master
Browse files Browse the repository at this point in the history
handle non NICFI contracts
  • Loading branch information
12rambau authored Jun 18, 2021
2 parents cad2dc8 + 9dcb4ef commit edab9fa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 46 deletions.
4 changes: 3 additions & 1 deletion component/message/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"not_found": "Image {} not found",
"done": "Image {} downloaded to SEPAL",
"end": "Out of {} requested images, {} have been downloaded, {} skipped and {} failed",
"exist": "The image {} already exist in your sepal directory, aborting"
"exist": "The image {} already exist in your sepal directory, aborting",
"no_access": "You don't have the right access to download this resource",
"view_only": "At least one of the missing tile was not downloaded because you don't have the right access to download the resources. Please see the module documentation or your Planet Lab profile to check their availability."
}
},
"map": {
Expand Down
1 change: 1 addition & 0 deletions component/parameter/planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
default_planet_key = f.read().strip()

planet_colors = [
'default',
'rgb',
'cir',
'ndvi',
Expand Down
64 changes: 30 additions & 34 deletions component/scripts/planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# parameters
planet.url = 'https://api.planet.com/auth/v1/experimental/public/my/subscriptions'
planet.basemaps = "https://tiles.planet.com/basemaps/v1/planet-tiles/{mosaic_name}/gmap/{{z}}/{{x}}/{{y}}.png?api_key={key}&proc={color}"
planet.basemaps = "https://tiles.planet.com/basemaps/v1/planet-tiles/{mosaic_name}/gmap/{{z}}/{{x}}/{{y}}.png?api_key={key}"
planet.attribution = "Imagery © Planet Labs Inc."

# attributes
Expand Down Expand Up @@ -67,29 +67,31 @@ def order_basemaps(key, out):
planet.client = api.ClientV1(api_key=planet.key)

# get the basemap names
mosaics = [m['name'] for m in planet.client.get_mosaics().get()['mosaics']]

# to use when PLanet decide to update it's API, until then I manually retreive the mosaics
#mosaics = planet.client.get_mosaics().get()['mosaics']
url = planet.client._url('basemaps/v1/mosaics')
mosaics = planet.client._get(url, api.models.Mosaics, params={'_page_size': 1000}).get_body().get()['mosaics']

out.add_msg(cm.planet.mosaic.complete, 'success')

return mosaics
return [m['name'] for m in mosaics]

def display_basemap(mosaic_name, m, out, color=None):
def display_basemap(mosaic_name, m, out, color):
"""display the planet mosaic basemap on the map"""

out.add_msg(cm.map.tiles)

# set the color if necessary
if not color:
color = cp.planet_colors[0]
color_option = "" if color == "default" else f"&proc={color}"

# remove the existing layers with planet attribution
for layer in m.layers:
if layer.attribution == planet.attribution:
m.remove_layer(layer)
m.remove_layer(layer)

# create a new Tile layer on the map
layer = TileLayer(
url=planet.basemaps.format(key=planet.key, mosaic_name=mosaic_name, color=color),
url=planet.basemaps.format(key=planet.key, mosaic_name=mosaic_name)+color_option,
name="Planet© Mosaic",
attribution=planet.attribution,
show_loading = True
Expand All @@ -107,12 +109,13 @@ def display_basemap(mosaic_name, m, out, color=None):
def download_quads(aoi_name, mosaic_name, grid, out):
"""export each quad to the appropriate folder"""

# a bool_variable to trigger a specifi error message when the mosaic cannot be downloaded
view_only = False

out.add_msg(cm.planet.down.start)

# get the mosaic from the mosaic name
mosaics = planet.client.get_mosaics().get()['mosaics']
mosaic_names = [m['name'] for m in mosaics]
mosaic = mosaics[mosaic_names.index(mosaic_name)]
mosaic = planet.client.get_mosaic_by_name(mosaic_name).get()['mosaics'][0]

# construct the quad list
quads = []
Expand All @@ -137,17 +140,27 @@ def download_quads(aoi_name, mosaic_name, grid, out):
time.sleep(.3)
continue

# catch error relative of quad existence
try:
quad = planet.client.get_quad_by_id(mosaic, quad_id).get()

except Exception as e:
out.append_msg(cm.planet.down.not_found.format(quad_id))
fail += 1
time.sleep(.3)
continue

out.append_msg(cm.planet.down.done.format(quad_id)) #write first to make sure the message stays on screen
planet.client.download_quad(quad).get_body().write(file)

# specific loop (yes it's ugly) to catch people that didn't use a key allowed to download the asked tiles
try:
planet.client.download_quad(quad).get_body().write(file)
except Exception as e:
out.append_msg(cm.planet.down.no_access)
fail += 1
view_only = True
time.sleep(.3)
continue

down += 1

# adapt the color to the number of image effectively downloaded
Expand All @@ -157,24 +170,7 @@ def download_quads(aoi_name, mosaic_name, grid, out):
elif fail > .5*len(quads): # we missed more than 50%
color = "warning"

out.add_msg(cm.planet.down.end.format(len(quads), down, skip, fail), color)

return
















out.add_msg(cm.planet.down.end.format(len(quads), down, skip, fail), color)
if view_only: out.append_msg(cm.planet.down.view_only, type_=color)


return
2 changes: 1 addition & 1 deletion component/widget/down_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self):

# create the extra widget
self.state = sw.StateBar(done=True)
self.color = v.ListItemGroup(v_model = None, children=[v.ListItem(children= [c], value=c) for c in cp.planet_colors[:3]])
self.color = v.ListItemGroup(v_model = None, children=[v.ListItem(children= [c], value=c) for c in cp.planet_colors[:4]])
self.palette = v.Menu(
value=False,
v_slots=[{
Expand Down
7 changes: 4 additions & 3 deletions doc/en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ Once a mosaic is selected the tool will zoom on your AOI and display it in blue.

download activated

Click on the palette btn on the top-left side of the map. This button will expand and show the 3 different color combo available:
Click on the palette btn on the top-left side of the map. This button will expand and show the 4 different color combo available:

- **default** the default color combo defined by planet, it can be one of **rgb** or **cir**
- **rbg** (red, blue, green)
- **cir** (nir, red, green)
- **ndvi** a viridis representation of the ndvi index ((nir-red)/(nir+red)) see `wikipedia <https://en.wikipedia.org/wiki/Normalized_difference_vegetation_index>`_
Expand All @@ -124,7 +125,7 @@ The images will be stored in the following folder : :code:`/home/<sepalID>/modul

.. note::

If the requested image is not available (the grid point to water area, the image was to cloudy so filtered by Planet.. etc) the image will be skiped.
If the image already exist in your folder it will be skipped as well. This behaviour allow you to restart a process if your SEPAL conection crashed without restarting all the downloads.
If the requested image is not available (the grid point to water area, the image was to cloudy so filtered by Planet, you don't have the rights to download it.. etc) the image will fail.
If the image already exist in your folder it will be skipped. This behaviour allow you to restart a process if your SEPAL conection crashed without restarting all the downloads.


7 changes: 0 additions & 7 deletions no_ui.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@
"source": [
"explorer_tile"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
File renamed without changes.

0 comments on commit edab9fa

Please sign in to comment.