从tiler.php托管的WebMap中抓取Map图像瓦片

f5emj3cl  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(111)

我期待着从这里链接的交互式Map刮图像瓷砖:https://labs.thinkbroadband.com/local/broadband-map#6/53.120/0.857/virgin/
通常情况下,使用平铺Map服务器,我可以使用zoom/x/y/ url格式访问.png,将所有瓦片合并在一起,然后在Map软件中使用地理参考。在这种情况下,我可以看到预览的Map瓷砖在来源标签,但当我按照该网址它打开了一个空白页。

最终目标是在Python中自动化这个过程。
任何帮助将不胜感激,谢谢!
我尝试重新利用下面的代码来抓取图像瓷砖,但遇到了tiler.php格式不是源选项卡中链接的图像文件的问题。

# -*- coding: utf-8 -*-
"""
Created on Thu May 18 08:42:21 2023

@author: BX564AC
"""

import math

import urllib.request

import os

import gdal

import glob

import subprocess

from math import log, tan, radians, cos, pi, floor, degrees, atan, sinh

def sec(x):
    return(1/cos(x))

def latlon_to_xyz(lat, lon, z):
    tile_count = pow(2, z)
    x = (lon + 180) / 360
    y = (1 - log(tan(radians(lat)) + sec(radians(lat))) / pi) / 2
    return(tile_count*x, tile_count*y)

def bbox_to_xyz(lon_min, lon_max, lat_min, lat_max, z):
    x_min, y_max = latlon_to_xyz(lat_min, lon_min, z)
    x_max, y_min = latlon_to_xyz(lat_max, lon_max, z)
    return(floor(x_min), floor(x_max),
           floor(y_min), floor(y_max))


#---------- CONFIGURATION -----------#
# Add base tile url here up to the zoom parameter, maintain z x & y in brackets
#tile_server = "https://app.nperf.com/signal-6090-{z}-{x}-{y}.png"
tile_server = "https://tiles.thinkbroadband.com/map/tiler.php?layer=virginmedia/&z=6&x=30&y=20"

temp_dir = "C:\\Users\\bx564ac\\OneDrive - EY\\Desktop\\nPerf_Scraping\\temp"

output_dir = "C:\\Users\\bx564ac\\OneDrive - EY\\Desktop\\nPerf_Scraping\\output"

# Change to desired zoom level
zoom = 9

# Enter bounding box coords to the area of interest
lon_min = 13.894753927000068

lon_max = 25.226397906000045

lat_min = 48.74059685900005

lat_max = 55.24695288000004

#-----------------------------------#

 
#Functions
 

def download_tile(x, y, z, tile_server):
    url = tile_server.replace(

        "{x}", str(x)).replace(

        "{y}", str(y)).replace(

        "{z}", str(z))

    path = f'{temp_dir}\\{x}_{y}_{z}.png'
    print(path)
    print(url)    
    proxy = urllib.request.ProxyHandler({})
    opener = urllib.request.build_opener(proxy)
    opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36')]
    urllib.request.install_opener(opener)
    
    urllib.request.urlretrieve(url, path)

    return(path)

x_min, x_max, y_min, y_max = bbox_to_xyz(
    lon_min, lon_max, lat_min, lat_max, zoom)

##### Changed lat to lon here
def x_to_lon_edges(x, z):
    tile_count = pow(2, z)
    unit = 360 / tile_count
    lon1 = -180 + x * unit
    lon2 = lon1 + unit
    return(lon1, lon2)

def mercatorToLat(mercatorY):
    return(degrees(atan(sinh(mercatorY))))

def y_to_lat_edges(y, z):
    tile_count = pow(2, z)
    unit = 1 / tile_count
    relative_y1 = y * unit
    relative_y2 = relative_y1 + unit
    lat1 = mercatorToLat(pi * (1 - 2 * relative_y1))
    lat2 = mercatorToLat(pi * (1 - 2 * relative_y2))
    return(lat1, lat2)

def tile_edges(x, y, z):
    lat1, lat2 = y_to_lat_edges(y, z)
    lon1, lon2 = x_to_lon_edges(x, z)
    return[lon1, lat1, lon2, lat2]

def georeference_raster_tile(x, y, z, path):
    bounds = tile_edges(x, y, z)
    filename, extension = os.path.splitext(path)
    gdal.Translate(filename + '.tiff',
                   path,
                   outputSRS='EPSG:4326',
                   outputBounds=bounds)

#merge_py = 'C:\\Users\\bx564ac\\Anaconda3\\Scripts\\gdal_merge.py'    
#def merge_tiles(input_pattern, output_path):
#    merge_command = [merge_py, '-o', output_path]

#    for name in glob.glob(input_pattern):
#        merge_command.append(name)

#    subprocess.call(merge_command)
    

for x in range(x_min, x_max + 1):
    for y in range(y_min, y_max + 1):
        print(f"{x},{y}")
        png_path = download_tile(x, y, zoom, tile_server)
        georeference_raster_tile(x, y, zoom, png_path)

print("Download complete")

#print("Merging tiles")
#merge_tiles(temp_dir + '/*.tif', output_dir + '/merged.tif')
#print("Merge complete")

相关问题