Download Geographical Survey tiles from latitude and longitude

The Geospatial Information Authority of Japan provides maps, aerial photographs, etc. in the form of Geographical Survey Tiles. To get it, we need a coordinate value called tile coordinates that is not latitude and longitude, so we calculate it. I borrowed the code on the OpenStreetMap wiki.

#from
#https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Python

import math

#Calculate tile coordinates from latitude and longitude
def deg2num(lat_deg, lon_deg, zoom):
    lat_rad = math.radians(lat_deg)
    n = 2.0 ** zoom
    xtile = int((lon_deg + 180.0) / 360.0 * n)
    ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
    return (xtile, ytile)

#I will not use it this time, but from the tile coordinates to latitude and longitude
def num2deg(xtile, ytile, zoom):
    n = 2.0 ** zoom
    lon_deg = xtile / n * 360.0 - 180.0
    lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
    lat_deg = math.degrees(lat_rad)
    return (lat_deg, lon_deg)

Downloading from Geographical Survey tile looks like this

import requests
def download_from_gsi(z, x, y, def_url, fname):
    url = def_url.format(z,x,y)
    response = requests.get(url)
    if response.status_code == 200:
        image = response.content
        with open(fname, "wb") as f:
            f.write(image)
    else:
        raise Exception("{} returned {}".format(response.url, response.status_code))

How to use it is like this. This example is an aerial photograph of the coordinates of the Sky Tree.

#z is the zoom level.18 is the most expanded state
z = 17
lat = 35.710163
lon = 139.8105428

def_url = "https://cyberjapandata.gsi.go.jp/xyz/ort/{}/{}/{}.jpg "
fname ="test.png "

x,y = deg2num(lat, lon, z)

download_from_gsi(z, x, y, def_url, fname)

As a result, I got an image like this. test.png It's a photo of the time when it was still under construction.

For def_url, refer to the url in Geographical Survey Tiles List. (Note that it may be jpg or png depending on the object.)

Recommended Posts

Download Geographical Survey tiles from latitude and longitude
Get the address from latitude and longitude
Find the distance from latitude and longitude (considering the roundness of the earth).
Find the waypoint from latitude and longitude (considering the roundness of the earth).
[GPS] Use pyproj to calculate distance, azimuth, and elevation from GPS latitude and longitude
Spread the Geographical Survey Map tiles in Python
Obtain location information (latitude and longitude) from the address. Geocode in Python ~ Geocoder and pydams ~
Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data
YOLP: Extract latitude and longitude with Yahoo! Geocoder API.