Once you've discovered NYPD Blue was filmed in Los Angeles, and - perhaps more shockingly - large parts of the island paradise of Balamory were constructed in Maryhill, it should come as no surprise to find Ken Stott sitting in a breaker's yard in Glasgow, taking time out from playing a character as intrinsically associated with Edinburgh as Jenners and Arthur's Seat.

Inspector John Rebus, Ian Rankin's wildly successful, iconic literary cop, is hardwired to the dark, narrow streets and subterranean secrets of the capital, but in the ruthlessly unromantic world of television the budget rules. The interiors for the first two films in the new Rebus series - The Falls and Fleshmarket Close - are being shot in Glasgow for financial reasons, with only external scenes being filmed in Edinburgh.

Stott is taking a break from stomping around a disused police station in Govan - the inside of which looks like something you'd find below deck on an Australian transportation ship in the 1800s. Cocooned in his trailer, he is enjoying an actor's elevenses of strong coffee and back-to-back Gitanes (French cigarettes; what would Rebus say? ) He conforms - thrillingly - to expectations: grumpy, a touch precious, obviously on the hunt for a bit of sparring to liven up his day. Or perhaps he is simply still in character - his short, stocky frame looks so natural in a dark shirt and crumpled black suit that it's impossible to tell whether the clothes belong to him or to ITV.

He concedes that he and Rebus have had an increasingly inevitable date with destiny for a while. "I'm doing it because I want to, but also because of popular demand, " he explains. "Whenever I came up to Scotland, people would say, 'Hey you! Gonnae play that part?' And, you know, it wasn't difficult to twist my arm. Filming has been very enjoyable, especially in Edinburgh, because people have a very proprietorial view of it. We are potentially disrupting their day, but when they ask what we're filming and we tell them, they're delighted."

Stott was born in Edinburgh in 1955 and knows Rebus's terrain well. Although he argues that the books could work just as well in any city with "a dark history", he eventually accepts that his home town is "exciting, unique and illuminating", and that returning to work in Scotland was a major factor in his decision to take the role. He has lived in London for many years, but his son is studying at Glasgow University and his mother still lives in Edinburgh.

He was first approached for the part while playing Inspector Pat Chappel in the television drama The Vice, but turned it down. Having also recently portrayed DCI Red Metcalfe in Messiah, he was wary of becoming typecast. "I've been resisting it for a long time, " he says. "I was playing a cop at the time and I was thinking, 'No more, no more. Enough, enough, enough!' But the pressure was too much."

Everyone breathed a sigh of relief when Stott finally signed up. The task of transferring one of Scottish literature's most acclaimed recent creations successfully to the screen has been a long and troubled one, largely dependent on the casting of the central character.

"He was always my choice. I couldn't really see anyone else, " says Eric Coulter, the executive producer of the series. "If Ken had decided not to do it, I don't know where I'd have gone, to be honest. He wasn't falling over himself to do another cop, but the pull of Rebus and the script was too strong. He gets the tone of it perfectly - the darkness, the troubled soul, but also the humour."

SMG TV productions bought the rights to the Rebus books in the mid-1990s, entering into a partnership with Clerkenwell Films, a production company part-owned by the Scottish actor John Hannah. Hannah played Rebus himself, shooting four feature-length films for ITV, three of which aired in 2000 and 2001. The final film was pulled from the schedules at the last minute.

Everyone working on the new series has a tendency to look at the floor or plead amnesia when the previous Rebus films are mentioned, but Coulter points out some mitigating factors, the main one being that the third show aired only two days after the bombing of the World Trade Centre in New York on September 11, 2001. The fourth episode was quietly dropped, he explains, for reasons of sensitivity. Contemporary reviews weren't actually all that bad, but Hannah's portrayal has, perhaps unfairly, become shorthand for how not to do Rebus, simply because his face didn't fit for those who were familiar with the books. He was too young, too thin, too good-looking.

Coulter is at pains to stress the new adaptations are a fresh start: new books, new scripts, new cast, no references to the previous films whatsoever. Stott has already been commonly accepted - sight unseen, as it were - as the perfect embodiment of Rankin's hard-boiled, irascible anti-hero. Watching him glowering at his side-kick Siobhan Clarke, played by Claire Price, as he acts out his part in a peculiarly interdependent relationship that teeters between paternal, avuncular and something more primal and needy, it is hard to argue.

In reality Stott might prefer red wine to Rebus's whisky and pints of heavy, but he carries deep within him the restlessness, sensitivity and barely concealed rage of the dissatisfied and disappointed middle-aged Scottish male. If Rebus doesn't work with Stott then it will not work at all.

Yet the actor has his own concerns about adapting such a beloved, complex and elusive character. "I've been nervous all my life about all kinds of things, and I'm no more nervous about this than I am doing anything else; everything I do is important to me, " he says tersely. "However, my personal feeling is that the shows would benefit from being longer. It's very difficult to do a book in 90 minutes - or actually less after the adverts. That's my concern. It becomes all story and no character: you have to remember this is a literary character, he's not an invention for television, and it is absolutely character-driven. But I think we manage to get the spirit of it, the core of it."

Given his strong opinions about the character, does Stott have any fresh insights into what makes Rebus tick? "I've got a feeling that with the best coppers - and in fact the best people in any field of work - what sets them apart is a maverick quality. People who are not afraid to bend the rules in order to achieve the universally desired end."

Stott isn't keen on the suggestion that the notion of the maverick cop is perhaps a little cliched; that there is surely more substance to Rebus than that. "Rankin's character is popular, he's been in the top ten for the past few years, the character has dominated this genre, " he says, each word sounding like a twig snapping. "Rankin is a fine writer. Very fine and very popular. It's very good to have both. I don't see Rebus as a cliche, I see him as a popular character."

I try to explain it's not the character of Rebus that is a cliche, merely the definition of him as a maverick, but Stott feigns bemusement and we reach an impasse. Soon after, he is whisked off to the set, shoving his cigarettes into his suit pocket. He's got Rebus down pat.

Fifty miles east, Ian Rankin sits in the airy front room of the House That Rebus Built - his large, detached Victorian property in the Merchiston area of Edinburgh - and ponders his creation. His easy conversation is the antithesis of Stott's uncomfortable verbal shuffling, but it is also clear that, nearly six million book sales down the line, his cantankerous cop remains almost as much of a mystery to his creator as he does to everyone else.

"I don't know what readers really like about Rebus, " he says, smiling. "I do know that a lot of women of a certain age think they're the one to change him - to calm him down and get him off the booze and the fags. And I know a lot of men think he has all their vices - it's like living your life voyeuristically through this character who does all your smoking and drinking for you and has all your hangovers, which is great. The men don't want him to change and the women do, whereas teenagers are getting a whole different thing out of the books - and what it is I'm not sure."

Rankin created his pension plan sitting in a ground-floor tenement flat in Marchmont in 1986. He was in the process of completing his PhD at Edinburgh University on Muriel Spark, whose favourite writer was Robert Louis Stevenson. During his research, Rankin delved into the mysteries of Burke and Hare and Deacon Brodie, rummaging around in the dark underbelly of the city and finally reaching the conclusion that its intrinsically contradictory nature hadn't changed so much from the days of Stevenson and James Hogg.

This was pre-Trainspotting, and in many ways Edinburgh was under-represented as a modern literary construct; Glasgow, with James Kelman, Alasdair Gray, William McIlvanney and Tom Leonard, ruled the roost. From the start, Rankin consciously placed the capital at the heart of the novels, revelling in depicting a city sharply at odds with the postcards and tartan tat sold to tourists on the High Street. He also wanted to create a character free of autobiographical resonance; although they are both Fifers by birth, Rankin was a student and writer in his mid-twenties, while Rebus was a policeman, ex-SAS, about 40, jaded, and cynical about the place where he lived: "Edinburgh is a lovely, beautiful city but he doesn't see it."

Rankin simply looked out of his window and across the road, gazed up two floors, and deposited Rebus there. In the ensuing two decades the author lived in London and France before moving back to Edinburgh, but Rebus is still holed up in that second-floor tenement on Arden Street. He has changed, though. What emerges from re-reading the books is how much of a work-in-progress he has been, Rankin feeling his way into the character, only ever one or two steps ahead of the reader.

In the first book in the series, Knots and Crosses, published in 1987, Rebus reads Walt Whitman poems and listens to Radio 3 and late-night jazz, conforming to a pre-existing stereotype rather than settling into his own identity. "I thought, 'What do existential loner cops listen to late at night in their armchair with a glass of whisky?'" says Rankin, laughing. "Probably Art Pepper and John Coltrane. But I knew very little about them, so I shifted him away from jazz to rock. And that's a sudden shift. In the early books he prefers the Beatles to the Stones and then suddenly - bang! - he's a Stones guy. I felt more comfortable because that meant there was a bit of the inside of his head I knew something about."

Published in 1997, Black and Blue, the eighth Rebus novel, was the first one where everything really clicked. Since then the character has become deeper and richer, but has remained essentially elusive. Rebus's internal dilemmas are a large part of what makes him so attractive; those long, dark nights of the soul spent staring out from his window with only a glass of whisky, Leonard Cohen and his reflection in the glass for company. He is a "tarnished knight", to use Raymond Chandler's phrase, driven by his own moral code but constantly tempted to cross the line between good and evil.

We know a little of his back story as an ex-special forces man who served in Northern Ireland, and a little about his disabled daughter and his ex-wife, but on the whole his past remains hazy. As in many relationships between close friends, much remains submerged. "I still don't know everything there is to know about him, " says Rankin. "I know quite a lot of it, but he is still holding stuff back from me. Am I holding stuff back from the reader, though? No, not at all. Not at all."

Rankin often talks like this: as though his fictional creation were a real man with his own free will. Perhaps that's the only way for the character to evolve - to allow him to surprise the writer. Rankin mentions how he recently "discovered" Rebus's Polish ancestry, which influenced the detective's attitude to asylumseekers and immigration in Fleshmarket Close.

He says he has tried to help Rebus find a woman but just can't make it work. He has no clearer idea than the rest of his readers as to the true nature of the relationship between Rebus and Siobhan, but fears the worst. "A lot of fans want them to get into bed together. It can't happen until the very end of the final book, but even then it probably can't happen. He's nearly 60, for chrissakes. He's not Clint Eastwood. In real life he probably would be on his own."

Many of Rebus's essential traits are unfilmable, and for the character to work on screen he had to become more defined, less fluid. He has never been physically described in any of the 16 books; Rankin watched only five minutes of the John Hannah films, and won't watch any of Stott's characterisation because he doesn't want other voices or faces interfering with his perception of the character. "I don't know what his face looks like. For a long time I thought he looked a bit like Brian Cox - and then I met Brian Cox recently at a dinner and I thought, no, he's still not quite there. I still haven't met anybody who is Rebus."

Rankin sold the television options on his novels for several reasons: greater exposure for the books; pressure from fans who wanted to see their man on screen; the fact that every other crime writer seemed to be getting snapped up too; basic financial considerations. But he was never under any illusion that the process would shed light on Rebus's character, and in fact concedes that his novels are sometimes a little "too convoluted" to make sense on television. Daniel Boyle - a writer on Hamish Macbeth and Inspector Morse - has written scripts for The Falls and Fleshmarket Close which simply use the books as a launch pad to go off in other directions. When Rankin accepted an invitation to go along to the last day of shooting for The Falls, at the Royal Museum in Edinburgh, he was offered a cameo as a man who stops a woman from being mugged.

"I'm talking to the director, and he's saying, 'Right, Miranda is walking down the alley . . .' And I say, 'Miranda? That's my wife's name, that's odd.' Then the director says, 'This redhaired mugger comes along and grabs her. You're a passer-by and you shout and run after him and he runs off. Okay?' I said, 'I don't remember this scene in the book.' 'Oh no, this scene isn't in the book.' 'And the red-haired guy?' 'He's not in the book either.' 'And Miranda's not in the book?' 'No, no.' None of this plot line was in the book. And I thought, ah, right, okay. That's fine. That's fine."

Rankin is philosophical about the process, making the often overlooked point that television is the dominant partner in this relationship: the novel is still the poor country cousin. The author duly views the films as an opportunity to attract new readers, rather than a way to annoy current ones.

Eric Coulter claims that an absolute maximum of 20 per cent of the television audience will have read the books prior to watching the films, but promises he does not take the task of adaptation lightly. "You feel a responsibility, " he says. "A bit like Taggart, people in Scotland feel they own Rebus, and they'd be quick to tell you if they thought you were doing a disservice to it. You have to be faithful to the spirit of the book." And there is no reason why the two versions of this compelling character can't co-exist. Rebus will become a more concrete entity on our screens, fleshed out with precision and subtlety by Stott, who promises: "We'll do more if people like them."

The series will gather its own fans among those who enjoy the medium of the well-made prime-time ITV drama. Others - like those who go to gigs to hear exact replicas of their favourite songs - will no doubt bemoan the lack of loyalty to the twisting plots of the books, not to mention all the little betrayals of the nuances of the novels, such as the fact they couldn't shoot in Rebus's beloved local, the Oxford Bar. "It's too small, and strangely unphotogenic, " says Stott.

Meanwhile, in his natural habitat on the printed page, Rebus will be chasing shadows for two more books only. Rankin will pull the plug on the character in 2007, when Rebus will be 60 and the stories will be 20 years old. As yet, he has no idea what will happen to his creation. "I probably won't know until the final few days of writing the final book whether he's alive or dead or ends up in bed with Siobhan; happy ending or sad ending, " he admits. "I always leave room for serendipity and chance."

Until he meets his end, John Rebus will keep rooting out the bad apples in the alleyways and bars of Edinburgh, returning home alone to the bosom of a malt whisky and a Blue Nile album. And the readers will keep reading, for the same reason Rankin has been compelled to keep writing for the last two decades: searching not for the villains, but for Rebus himself.

The Falls is due to be screened on January 2 on ITV1.