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.
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.
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
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)
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])
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)
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)
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.
Map image: Geospatial Information Authority of Japan
Recommended Posts