Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active February 19, 2022 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/6523b832fc4d3fca8814ed92bc2cd16c to your computer and use it in GitHub Desktop.
Save ThomasG77/6523b832fc4d3fca8814ed92bc2cd16c to your computer and use it in GitHub Desktop.

Get IGN legends for WMS Raster, WMS Vecteur and WMTS webservices

Context

There is a CSV of available IGN webservices at https://geoservices.ign.fr/documentation/services/tableau_ressources

Most webservices for raster do not return the legend and point to a broken http://api.ign.fr/legendes/ as shown below

Results

From the French geoportal, we guessed the pattern to retrieve image legends and generate an "augmented" default file with the legend image URL link

To get end result look at:

You can also get each legend image as we retrieved them locally in the repository. There are in the zip file

Not all webservices do have a legend, 241 have a legend (present.txt) and 357 do not have (no_present.txt). We didn't analysed the cause for the missing legend e.g hosted somewhere or not useful like for imagery

How it was done

Done on linux using bash.

Look at file get_legends_ign.sh for the recipe

# "https://api.ign.fr/legendes" broken since a while
# mkdir legendes
# cd legendes
url=$(curl -k https://geoservices.ign.fr/documentation/services/tableau_ressources | grep -Eo "(http|https)://[a-zA-Z0-9./?=_%:-]*" | grep '.csv$')
filename="${url##*/}"
output_file_basename=ressources_de_services_web_with_legend
rm $filename
wget --no-check-certificate $url
iconv -f 'WINDOWS-1252' -t 'UTF-8' $filename > temp.txt
dos2unix temp.txt
mv temp.txt $filename
sed -e "1d" $filename > temp.txt
mv temp.txt $filename
echo -e "service;nom_cle;nom_commercial;nom_tech;url_acces\n$(cat $filename)" >| $filename
# true >| content.txt
IFS=$'\n'
for name in $(csvcut -d ";" -c 4 $filename);
do png_url="https://www.geoportail.gouv.fr/depot/layers/${name}/legendes/${name}-legend.png"
if curl --output /dev/null --silent --head --fail "$png_url"; then
echo "1;$png_url" >> content.txt
else
echo "0;" >> content.txt
fi;
done;
unset IFS
sed -e "1d" content.txt > temp.txt
mv temp.txt content.txt
echo -e "present;url_legend\n$(cat content.txt)" >| content.txt
paste -d ';' $filename content.txt >| "${output_file_basename}.csv"
rm content.txt $filename
grep 'WMS Vecteur\|WMS Raster\|WMTS' ""${output_file_basename}.csv"" | grep -v '0;$' >| present.txt
wc -l present.txt
# 241 avec une légende
grep 'WMS Vecteur\|WMS Raster\|WMTS' "${output_file_basename}.csv" | grep '0;$' >| no_present.txt
wc -l no_present.txt
# 357 sans une légende
# L'absence de légende peut se justifier selon les cas de figure.
# Il peut ne pas avoir ce besoin: exemple d'une orthophoto ou d'un contour simple avec une couche d'information
# Retrieve files
remote_urls=$(csvcut -d ';' -c 7 present.txt | sort | uniq)
for f in $remote_urls;
do wget "$f";
done;
zip legends.zip *.png
rm *.png
# csvtojson --delimiter=';' "${output_file_basename}.csv" | jq -c . | pv -b >/dev/null
csvtojson --delimiter=';' "${output_file_basename}.csv" | jq -c . >| "${output_file_basename}.json"
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment