Python 滑动地图瓦片生成

Python Slippy-map Tile Generation

我从一位前同事那里继承了两个 Python 脚本(更像是片段)。该代码应该将高分辨率图像 (png) 转换为可缩放的图块(slippy-map) that can be used by Leaflet.js 在 goographic 坐标 space (lat/long).

我不是 Python(或 GIS)专家,我正在努力让它们正常工作。我无法确定问题是我缺乏知识还是代码...

第一个脚本按预期工作,从输入图像 (myImgMaxRez.png) 中生成五个分辨率逐渐降低的图像文件。我相信这被称为 'Image Pyramid'。每个 z[1..5].png 的尺寸都可以被 256 整除。

from skimage import io
from skimage import transform
import skimage

z1 = io.imread("myImgMaxRez.png", as_grey=True)
io.imsave("z1.png",skimage.img_as_uint(z1))

z2 = transform.pyramid_reduce(z1)
io.imsave("z2.png",skimage.img_as_uint(z2))

z3 = transform.pyramid_reduce(z2)
io.imsave("z3.png",skimage.img_as_uint(z3))

z4 = transform.pyramid_reduce(z3)
io.imsave("z4.png",skimage.img_as_uint(z4))

z5 = transform.pyramid_reduce(z4)
io.imsave("z5.png",skimage.img_as_uint(z5))

下一个生成 slippy-map 瓦片的脚本抛出类型转换错误(float 到 int)。

import math
import os
from skimage import io

def createTiles(xStart, yStart, zoom, theData):
    ncols = theData.shape[1]/256
    nrows = theData.shape[0]/256
    print(nrows, ncols)
    topDir = "%d" % (zoom)
    os.mkdir(topDir)
    for i in range(0, ncols):
        theDir = topDir + "/%d" % (i+xStart)
        print (theDir)
        os.mkdir(theDir)
        for j in range(0, nrows):
            theFile = topDir + "/%d/%d.png" % (i + xStart, j + yStart)
            print (theFile)
            io.imsave(theFile, theData[j*256:(j+1)*256, i*256(i+1)*256])

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)

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.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
    return (xtile, ytile)

# Take center point of interest (MCK). Since z tile is 4x3 go 1 tile up and 2 left
startZoom = 18
centerLoc = deg2num(43.533942709166325, -96.71487003564836, startZoom)
startCol = centerLoc[0]
startRow = centerLoc[1]

# Now get lat/long of upper left tile corner
geoCoord = num2deg(startCol, startRow, startZoom)

loc = deg2num(geoCoord[0], geoCoord[1],18)
# EDIT: reading the .png thanks to JH comment below
z1 = io.imread("z1.png", as_grey=True)
createTiles(loc[0], loc[1], 18, z1)

loc = deg2num(geoCoord[0], geoCoord[1],19)
# EDIT: reading the .png thanks to JH comment below
z2 = io.imread("z2.png", as_grey=True)
createTiles(loc[0], loc[1], 19, z2)

loc = deg2num(geoCoord[0], geoCoord[1],20)
# EDIT: reading the .png thanks to JH comment below
z3 = io.imread("z3.png", as_grey=True)
createTiles(loc[0], loc[1], 20, z3)

loc = deg2num(geoCoord[0], geoCoord[1],21)
# EDIT: reading the .png thanks to JH comment below
z4 = io.imread("z4.png", as_grey=True)
createTiles(loc[0], loc[1], 21, z4)

loc = deg2num(geoCoord[0], geoCoord[1],22)
# EDIT: reading the .png thanks to JH comment below
z5 = io.imread("z5.png", as_grey=True)
createTiles(loc[0], loc[1], 22, z5)

CreateTiles() 在行 for i in range(0, ncols): 上抛出一个错误,说 TypeError: float object cannot be interpreted as an integer... 有趣的是,它在抛出错误之前创建了第一个子目录 18/。我再次确认 z[1..5].png 的尺寸是 2 的幂(或 256 的倍数)。

为什么我在将浮点数转换为整数时出现此错误?

当第一个脚本终止时,

z1 超出范围,因此它对第二个脚本不可用。你需要找到一种方法来传递它,例如在调用 createTiles() 之前,您可能会这样做:

z1 = io.imread("z1.png", as_grey=True)

这些脚本的结构很差。将 imports 保持在顶层(无缩进)是有道理的,但最好将其他语句移动到它们自己的函数中。至少他们应该在这个下面:

if __name__ == '__main__':

这样一来,您就有机会让一个脚本重用另一个脚本中的函数,因为您可以 import 而没有副作用。

原来是Python版本问题..在2.7中,如果除法的结果是xx.0,它是一个int。在 Python 3.x 中,它是一个浮点数。所以 Python 2.7 中的 512/256 = 2 而 python 3.x.

中的 = 2.0

通过将除法的结果转换为 INT,我能够得到它 运行。

ncols = int(theData.shape[1]/256)
nrows = int(theData.shape[0]/256)