从图片批量下载图块

Batch download tiles from a pictures

我想从 macOS 上使用 bash 的网站下载图片,这些图片被细分为更小的方形图片,即所谓的图块。每个方块的link为:

http://whateverdomain.is/TileGroup$y/$z-$i-$x.jpg

其中 $y 是通常在 1 到 6 之间的图块组的编号,$z 是一个常数 (= 6),并且 $i$x 分别描述每列 (i) 和行 (x) 的位置。

我有两个问题,我希望我的脚本先从一列转到另一列,然后再更改行。现在它完全相反,请参见下面的脚本。它会从 6-0-06-1-0,而不是 6-0-1,以及 6-2-0,而不是 6-0-2

第二个问题是,每 95 个图块,图块组 $y 发生变化,迭代 1,从 1 到 2,从 2 到 3,等等。但是,我在脚本中的条件没有似乎起作用。 (我不是程序员。)

我如何实现我的脚本,首先,在更改行之前从一列转到另一列,其次,我如何调整脚本以在每下载 95 个图块后将图块组更改 1?

这是我的脚本:

((z = 6)) # zoom level
((k = 0)) # counter
((y = 1)) # tile group at start

for x in `seq 0 30`; do
  for i in `seq 0 38`; do
      wget -c -nc -E -nd http://whateverdomain.is/TileGroup$y/$z-$i-$x.jpg
      k = k + 1 # count each tile
      if [[ $k -eq 95 ]];
      then
        y = y + 1 # change tile group each 95th tile
      fi
      if [[ $y -eq 7 ]]; # tile group maxed out; stop
      then
        break
      fi
  done
done

您只需要在循环中对循环进行排序 - 例如,这应该有效:

x=6
y=0
z=0
ymax=7 # y bound
zmax=3 # z bound

while [ $y -lt $ymax ]
do
  z=0 #reset z
    while [ $z -lt $zmax ]
    do
      echo "x = $x y = $y z = $z"  # do stuff here
      z=$(( $z + 1 )) #increment z
    done
  y=$(( $y + 1 )) #increment y
done

输出:

x = 6 y = 0 z = 0
x = 6 y = 0 z = 1
x = 6 y = 0 z = 2
x = 6 y = 1 z = 0
x = 6 y = 1 z = 1
x = 6 y = 1 z = 2
x = 6 y = 2 z = 0
x = 6 y = 2 z = 1
x = 6 y = 2 z = 2
x = 6 y = 3 z = 0
x = 6 y = 3 z = 1
x = 6 y = 3 z = 2
x = 6 y = 4 z = 0
x = 6 y = 4 z = 1
x = 6 y = 4 z = 2
x = 6 y = 5 z = 0
x = 6 y = 5 z = 1
x = 6 y = 5 z = 2
x = 6 y = 6 z = 0
x = 6 y = 6 z = 1
x = 6 y = 6 z = 2

您可以将它们更改为更好的值(例如从 1 开始计数,将 lt 更改为 =< 等)以适合。

更多循环:

x=6
y=0
z=0
xmax=10 # x bound
ymax=7 # y bound
zmax=3 # z bound

while [ $x -lt $xmax ]
do
y=0 # reset y
  while [ $y -lt $ymax ]
  do
  z=0 #reset z
    while [ $z -lt $zmax ]
    do
      echo "x = $x y = $y z = $z"  # do stuff here
      z=$(( $z + 1 )) #increment z
    done
  y=$(( $y + 1 )) #increment y
done
x=$(( $x + 1))
done

输出:

x = 6 y = 0 z = 0
x = 6 y = 0 z = 1
x = 6 y = 0 z = 2
x = 6 y = 1 z = 0
x = 6 y = 1 z = 1
x = 6 y = 1 z = 2
x = 6 y = 2 z = 0
x = 6 y = 2 z = 1
x = 6 y = 2 z = 2
x = 6 y = 3 z = 0
x = 6 y = 3 z = 1
x = 6 y = 3 z = 2
x = 6 y = 4 z = 0
x = 6 y = 4 z = 1
x = 6 y = 4 z = 2
x = 6 y = 5 z = 0
x = 6 y = 5 z = 1
x = 6 y = 5 z = 2
x = 6 y = 6 z = 0
x = 6 y = 6 z = 1
x = 6 y = 6 z = 2
x = 7 y = 0 z = 0
x = 7 y = 0 z = 1
x = 7 y = 0 z = 2
x = 7 y = 1 z = 0
x = 7 y = 1 z = 1
x = 7 y = 1 z = 2
x = 7 y = 2 z = 0
x = 7 y = 2 z = 1
x = 7 y = 2 z = 2
x = 7 y = 3 z = 0
x = 7 y = 3 z = 1
x = 7 y = 3 z = 2
x = 7 y = 4 z = 0
x = 7 y = 4 z = 1
x = 7 y = 4 z = 2
x = 7 y = 5 z = 0
x = 7 y = 5 z = 1
x = 7 y = 5 z = 2
x = 7 y = 6 z = 0
x = 7 y = 6 z = 1
x = 7 y = 6 z = 2
x = 8 y = 0 z = 0
x = 8 y = 0 z = 1
x = 8 y = 0 z = 2
x = 8 y = 1 z = 0
x = 8 y = 1 z = 1
x = 8 y = 1 z = 2
x = 8 y = 2 z = 0
x = 8 y = 2 z = 1
x = 8 y = 2 z = 2
x = 8 y = 3 z = 0
x = 8 y = 3 z = 1
x = 8 y = 3 z = 2
x = 8 y = 4 z = 0
x = 8 y = 4 z = 1
x = 8 y = 4 z = 2
x = 8 y = 5 z = 0
x = 8 y = 5 z = 1
x = 8 y = 5 z = 2
x = 8 y = 6 z = 0
x = 8 y = 6 z = 1
x = 8 y = 6 z = 2
x = 9 y = 0 z = 0
x = 9 y = 0 z = 1
x = 9 y = 0 z = 2
x = 9 y = 1 z = 0
x = 9 y = 1 z = 1
x = 9 y = 1 z = 2
x = 9 y = 2 z = 0
x = 9 y = 2 z = 1
x = 9 y = 2 z = 2
x = 9 y = 3 z = 0
x = 9 y = 3 z = 1
x = 9 y = 3 z = 2
x = 9 y = 4 z = 0
x = 9 y = 4 z = 1
x = 9 y = 4 z = 2
x = 9 y = 5 z = 0
x = 9 y = 5 z = 1
x = 9 y = 5 z = 2
x = 9 y = 6 z = 0
x = 9 y = 6 z = 1
x = 9 y = 6 z = 2

正在回答您的问题。

How can I achieve that my script, first of all, goes from column to column before changing rows?

您必须在 for 循环中更改 xi,如下所示:

for i in `seq 0 30`; do
  for x in `seq 0 38`; do
      ...
  done
done

how can I adjust the script to change the tile group by 1 after each 95 tile being downloaded?

像这样:

((k%95)) || ((y++))

具有此改进的最终脚本:

((z = 6)) # zoom level
((k = 0)) # counter
((y = 1)) # tile group at start

for i in `seq 0 30`; do
  for x in `seq 0 38`; do
      wget -c -nc -E -nd http://whateverdomain.is/TileGroup$y/$z-$i-$x.jpg
      k = k + 1 # count each tile
      ((k%95)) || ((y++)) # change tile group each 95th tile
      if [[ $y -eq 7 ]];  # tile group maxed out; stop
      then
        break
      fi
  done
done

还有一个稍微改进的版本:

z=6 # zoom level
k=0 # counter
y=1 # tile group at start

for i in {0..30}; do
  for x in {0..38}; do
      wget -c -nc -E -nd http://whateverdomain.is/TileGroup$y/$z-$i-$x.jpg
      ((k++))             # count each tile
      ((k%95)) || ((y++)) # change tile group each 95th tile
      ((y>z))  &&  break  # tile group maxed out; stop
  done
done