Spread the Geographical Survey Map tiles in Python

background

If you want to make a route map with a map in the background, you used to copy and paste the Geographical Survey Institute map tile image Excel grid paper by yourself, but it is a very troublesome task, so let Python do it. I made it.

What is Geospatial Information Authority of Japan map tile?

Details are described in here, but in short, the topographic map created by the Geographical Survey Institute can be used as a tile image. That is. The tile image has coordinate values, and this time I will get a map of the area I want by specifying it. Please check the coordinates on the Tiles Coordinate Confirmation Page of the Geographical Survey Institute.

Get a tile image

First, import the required libraries. Please install it if necessary. (eg pip install requests)

chiriinMap.py


import cv2
import numpy as np
import os
import requests

Define the function to get the tile image and save it as follows: In the following, a folder called tile is prepared and saved in the current directory. By the way, it's only for one sheet 15_28250_13142.jpg It is like this.

chiriinMap.py


def get_tile(z, x, y):
    """
This time, specify the URL to get the standard map.
Save the Geographical Survey Map tile image corresponding to the given coordinates.
    """
    url = "https://cyberjapandata.gsi.go.jp/xyz/std/{}/{}/{}.png ".format(z, x, y)
    file_name = "tile/{}_{}_{}.jpg ".format(z, x, y)
    
    response = requests.get(url)
    image = response.content
    
    with open(file_name, "wb") as aaa:
        aaa.write(image)

Get a tile image by specifying an area

The previous get_tile function can only get one map tile, so try to get the entire area. For the future, if the image you want already exists, it will be skipped. However, if too much time has passed since the last acquisition, the map may have been updated, so in that case it is better to delete the image. In the next section, we will combine multiple tile images into one, so you may want to erase individual images each time. I'll leave that to you.

chiriinMap.py


def get_tile_area(north_west, south_east):
    """
Obtain the tile image after specifying the tile coordinates of the northwest end and southeast end.
    """
    assert north_west[0] == south_east[0], "Check the zoom level z."
    zoom = north_west[0]
    im_v_lst = []
    for i in range(south_east[1]-north_west[1]+1):
        for j in range(south_east[2]-north_west[2]+1):
            filepath = path = "tile/{}_{}_{}.jpg ".format(zoom, i+north_west[1], j+north_west[2])
            if os.path.exists(filepath) == True:
                continue
            get_tile(zoom, i+north_west[1], j+north_west[2])

Combine tile images in the area into one image

The laying work that took a huge amount of time can be overcome as follows. Feel free to rename the last image.

chiriinMap.py


def cat_tile(north_west, south_east):
    zoom = north_west[0]
    im_v_lst = []
    for i in range(south_east[2]-north_west[2]+1):
        im_h_lst = []
        for j in range(south_east[1]-north_west[1]+1):
            path = "tile/{}_{}_{}.jpg ".format(zoom, j+north_west[1], i+north_west[2])
            im1 = cv2.imread(path,-1)
            im_h_lst.append(im1)
        im_h = cv2.hconcat(im_h_lst)
        im_v_lst.append(im_h)
    im_v = cv2.vconcat(im_v_lst)
    cv2.imwrite("tile/tile.png ", im_v)

Try

I'll try.

chiriinMap.py


north_west = (15, 28223, 13124)
south_east = (15, 28244, 13144)
get_tile_area(north_west, south_east)
cat_tile(north_west, south_east)

tile.png

It is like this. It is the Itoshima Peninsula in Fukuoka Prefecture. How many hours would it take to do it manually ... You can enjoy it with this. By the way, this image is about 30MB as it is. If you don't mind reducing the image quality, you may want to reduce the size.

Source

Map image: Geospatial Information Authority of Japan

Recommended Posts

Spread the Geographical Survey Map tiles in Python
Find the difference in Python
Getting the arXiv API in Python
Python in the browser: Brython's recommendation
Save the binary file in Python
Hit the Sesami API in Python
Get the desktop path in Python
Self-organizing map in Python NumPy version
Get the script path in Python
[Python] Adjusted the color map standard
Hit the web API in Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
The first step in Python Matplotlib
I wrote the stack in Python
Master the weakref module in Python
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Load the remote Python SDK in IntelliJ
Specify the color in the matplotlib 2D map
Check the behavior of destructor in Python
Learn the design pattern "Flyweight" in Python
Try using the Kraken API in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Write the test in a python docstring
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
Tweet using the Twitter API in Python
Learn the design pattern "Visitor" in Python
Try to image the elevation data of the Geographical Survey Institute with Python
Learn the design pattern "Bridge" in Python
Check if the URL exists in Python
Learn the design pattern "Mediator" in Python
Associate the table set in python models.py
Learn the design pattern "Decorator" in Python
Run the Python interpreter in a script
The result of installing python in Anaconda
What is "mahjong" in the Python library? ??
Read the file line by line in Python
Read the file line by line in Python
MongoDB for the first time in Python
Learn the design pattern "Iterator" in Python
The basics of running NoxPlayer in Python
Open an Excel file in Python and color the map of Japan
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
Solve the maximum subarray problem in Python
In search of the fastest FizzBuzz in Python
Learn the design pattern "State" in Python
Try hitting the YouTube API in Python
Get the EDINET code list in Python
Learn the design pattern "Adapter" in Python
Install the Python module in any directory
[Python] Read the specified line in the file
[Python] Combine all the elements in the array
Location information data display in Python --Try plotting with the map display library (folium)-