==== Récupérer une tuile Top25 à partir des coordonnées GPS ====
Il faut tout d'abord utiliser les requêtes suivantes. Notez que ces requêtes ne fonctionnent que si le champs //referer// de la requête HTTP correspond bien au site web associé à votre clef gratuite ! Ici, ça marche parce que vous êtes sur mon site web :-) Tout est expliqué ici : http://api.ign.fr/tech-docs-js/fr/developpeur/wmts.html et là : http://professionnels.ign.fr/sites/default/files/DT_APIGeoportail.pdf
* Requête GetCapabilities : [[http://wxs.ign.fr/vq6d6en2x8s74yrkwryupvru/geoportail/wmts?SERVICE=WMTS&REQUEST=GetCapabilities | test]]
* Requête GetTile : [[http://wxs.ign.fr/c19uzdyrb5yttrgmhr2mmyij/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS&STYLE=normal&TILEMATRIXSET=PM&TILEMATRIX=15&TILEROW=12061&TILECOL=16388&FORMAT=image/jpeg | test ]]
/* */
/* wget --referer="http://aurelien.esnard.emi.u-bordeaux1.fr" "http://wxs.ign.fr/c19uzdyrb5yttrgmhr2mmyij/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS&STYLE=normal&TILEMATRIXSET=PM&TILEMATRIX=11&TILEROW=706&TILECOL=1035&FORMAT=image/jpeg" -O zoom11-x706-y1035.jpeg */
/*
*/
Voici un exemple d'affichage d'une tuile (row=12061, col=16388, zoom=15) correspondant au Col de Marraut (lon=0.052279, lat=42.829535).
#!/usr/bin/env python
# Download one single jpeg tile of 256x256 pixels from geoportail at zoom level 15 (Top25) given lon/lat in decimal degrees.
# Usage: ./downloadTileTop25.py lon lat (in decimal degree)"
# Sample (col de Marraut): ./downloadTileTop25.py 0.052279 42.829535
import sys
import math
import urllib2
APIKEY = "YOUR OWN KEY"
REFERER = "YOUR OWN REFERER"
LAYER = "GEOGRAPHICALGRIDSYSTEMS.MAPS"
# usage
if len(sys.argv) != 3 :
print sys.argv[0], "lon lat (in decimal degree)"
sys.exit(2)
LON= sys.argv[1]
LAT= sys.argv[2]
print "LON=", LON, "LAT=", LAT
# Top25 zoom level
ZOOM="15"
##### Compute (COL,ROW) from (LON,LAT) for ZOOM=15 #####
# Documentation: http://api.ign.fr/tech-docs-js/fr/developpeur/wmts.html
# EPSG:3857 ("WGS 84 / Pseudo-Mercator")
# The GetCapabilities request gives the following values for EPSG:3857 at zoom level 15:
# 10944
# 21176
# 163 31695
# 17061.8366707982724577
# -20037508 20037508
# 256
# 256
# 32768
# 32768
def lonlat2xy(lon_deg, lat_deg):
lon_rad = math.radians(lon_deg)
lat_rad = math.radians(lat_deg)
# rayon equatorial (demi grand axe) de l'ellipsoide
a = 6378137.0 # in meters
x = a * lon_rad
y = a * math.log(math.tan(lat_rad/2 + math.pi/4))
return (x, y) # return coordinates in meters
# convert (lon,lat) to (x,y) web-mercator coordinates in meters
lon = float(sys.argv[1])
lat = float(sys.argv[2])
(x, y) = lonlat2xy(lon,lat)
# Top Left Corner for ZOOM=15
x0 = -20037508
y0 = 20037508
# Shift coordinates according to the "Top Left Corner"
xx = x-x0
yy = y0-y
# Scale Denominator for ZOOM=15
scaledenominator = 17061.8366707982724577 # scale 1:scaledenominator
# The standardized rendering pixel size is defined to be 0.28mm x 0.28mm.
renderingpixelsize = 0.00028 # meters / rendering pixel ???
pixelsize = renderingpixelsize * scaledenominator # meters / pixel
pixelspertile = 256 # pixels
tilesize = pixelspertile * pixelsize # meters
tilecol = int(xx / tilesize)
tilerow = int(yy / tilesize)
##### Generate URL from (COL,ROW,ZOOM) for GetTile request #####
COL = str(tilecol)
ROW = str(tilerow)
print "COL=", COL, "ROW=", ROW, "ZOOM=", ZOOM
URL = "http://wxs.ign.fr/"+APIKEY+"/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER="+LAYER+"&STYLE=normal&TILEMATRIXSET=PM&TILEMATRIX="+ZOOM+"&TILEROW="+ROW+"&TILECOL="+COL+"&FORMAT=image/jpeg"
print "URL=", URL
FILE = "zoom"+ZOOM+"-row"+ROW+"-col"+COL+".jpeg"
print "FILE=", FILE
##### launch HTTP request and save output #####
req = urllib2.Request(URL)
req.add_header('Referer', REFERER)
ans = urllib2.urlopen(req)
output = open(FILE,'wb')
output.write(ans.read())
output.close()
# EOF