
function GetTileId(x, y, z: Integer): String;
begin
  y := (1 shl z) - y - 1;
  Inc(z);
  SetLength(result, z);
  while z > 0 do
  begin
    result[z] := Chr(Ord('0') + (x and 1) + (y and 1) * 2);
    x := x shr 1;
    y := y shr 1;
    Dec(z);
  end;
end;

function SplitTileId(const tileId: String): String;
var
  i: Integer;
begin
  result := '';
  for i := 1 to Length(tileId) do
  begin
    result := result + tileId[i];
    if (i mod 3 = 0) and (i <> Length(tileId)) then
      result := result + '/';
  end;
end;

function MakeTilePath(x, y, z: Integer): String;
var
  tileId, urlPath: String;
begin
  tileId := GetTileId(x, y, z);
  urlPath := SplitTileId(tileId);
  result := Format('z1/itiles/%s.xy?342342', [urlPath]);
end;

begin
  if (GetZ >=4 ) and (GetZ <= 14) then
    ResultUrl := GetUrlBase + MakeTilePath(GetX, GetY, GetZ-1)
  else
    ResultUrl := '';
end.
