There's actually some hard research data on this, mostly collected over the past 35 years, and I also have experienced a few similar phenomenons, though not on a regular basis. See below for more.
There appears to be some but minor correlation based on research performed and summarized in the following works. As often with research though, the study models differ between studies and they should be closely reviewed to understand why results present differences in conclusions.
Exploring the psychological predictors of programming achievement [PDF] (Erdogan, Aydin, Kabaca, 2008)
Unfortunately this one is vague on details. It points to the "high-impact" of "aptitudes" in general, but then only points to other research without giving the results for each aptitude test, so we don't know how spatial ability fares. It's mostly a litterature review more than actual research.
Spatial ability and learning to program [PDF] (Jones, 2008)
From the results of this analysis, there is evidence that spatial ability
is important when learning to program. [...] While spatial ability has
been shown to be relevant, we do not feel that mental rotation capacity
should be used as a means of predetermining programming aptitude, but
should be considered while devising pedagogical interventions. Thought
needs to be given to teaching methods and software visualizations that
help students with low spatial ability to envisage abstract concepts
and build better mental models (Wiedenbeck et al., 2004).
Predictors of Success in a First Programming Course [PDF] (Simon, Fincher & al., 2006)
Only a small positive correlation was found between
scores in the spatial visualisation (paper folding) task and
programming marks. This suggests that components of
IQ other than spatial skills may account for most of the
effect of IQ on programming success (Mayer et al 1989).
Who is likely to acquire programming skills? (Shute, 1991)
Hemispheric Lateralization and Programming Ability, (Gasen, Morecroft, 1990)
Correlates of problem-solving in programming [PDF] (Choi-man, 1988)
Interesting one... Nice study model, and quantified results with several study groups and accounting for the reliability of study factors. It yields that:
[...] it could be
seen that, for the males, mathematics alone
could account for 30.90% of variance on
programming ability, and that spatial test
could account for 8.00%. [...]
[...] it could
also be seen that, for females, only the
performance of mathematics and spatial
tests had significant effect in predicting the
Results of this study revealed that
students who scored high in mathematics
test and spatial test would score high in
programming ability test.
Learning, research, and the graphical representation of programming (Taylor, Cunniff, Uchiyama, 1986)
Cognitive Requirements of Learning Computer Programming in Group and Individual Settings (Webb, 1985)
Cognitive correlates of programming tasks in novice programmers (Irons, 1982)
Research on aptitude for learning: A progress report [PDF] (RE Snow, 1976)
Take it with a pinch of salt: Some are relatively dated, IQ tests might have changed since. I haven't done an in-depth search to find citations of each article to see if they were confirmed or debunked later on.
Some links (especially the [PDF] kind) may not work for you if you don't have an affiliation to a library giving access to these online contents.
Warning and disclosure: I am NEITHER a psychologist NOR a neurologist, but I have been studying and teaching programming to both small kids (starting 6) and university students (up to 60!).
Having studied with AND taught students as university teacher myself, including some students affected by spatial problems (and others with stronger disabilities), I have to say that while it could have been (I didn't keep track of my students based on disabilities, obviously) that some would have registered in a lower part of the general curve, I still remember clearly some scoring high (and even one in particular being the class' major for at least 2 years).
My point is, while it may have an effect, and as shown by some of the research above, it doesn't account for the largest part of your ability to learn to program and think like a programmer. It's inconsequential, in that it won't stop you to learn if you really want to, and won't prevent you from working in the general case, though it could (as might be your case) make it slightly harder for you.
There's virtually no limit to what and how fast you can learn.
After all, no programmer doesn't like a good challenge, right? (I'm looking at you, RSI)
Personal (Possibly Unrelated) Experience
It might be that you are too passionate. How many hours do you work per day and per week? Do you take regular breaks?
A Similar Case?
At a period in my life, I worked days of at least 14 hours every day of the week, the whole year, to a point where it culminated to record weeks of 120 hours of work in front of a computer screen. Yes, that's only 48 hours left per week to eat, sleep, travel to and from work (tip: avoid driving!!), shower and other vital functions. At this particular point, I could pretty much go to sleep in a heart-beat (though usually having sleeping problems), but I would almost always keep dreaming of code, and I would also suddenly realize in the shower or even when walking or running or doing menial tasks that my mind went back to it in auto-pilot, as you said it yourself. Unfortunately, I wouldn't magically solve problems in my sleep; it would be closer to what you seem to describe and experience: a giant maelstrom of confused thoughts turning around in my head, which would sort of (seem to) make sense on a grander scale, but not clearly express any solution and without much success in grabbing one of these thoughts to focus on it, dissect it clearly and turn it into something useful. And this was usually rather tiresome and distressing.
Relaxation Might Help
Maybe you need to calm down just a bit, and relax and work less. Try to find something to take your mind off. Back then, I ended up often renouncing some precious hours of sleeping time to instead do something that would really stop this mad train of thought. It seems counterproductive, but I actually preferred to do a few thing where I would really relax than to sleep more and not be rested. The distraction for the nervous batteries, and the sleep for the physical batteries, in a sense.
If that's not your case, then maybe there's something else involved in triggering this state for you. Try to isolate elements that are present in these situations, and see if you can reproduce this condition in other environments, to see if you find these elements as well. Does it happen more at work or at home, etc...
Also, you may already have heard and tried this, but I have a friend with a minor spatial disability, and usually it helps for him, if working on computers, to be a in darker room, to avoid having too many complex views and windows open (to avoid distraction), and in general to keep things rather minimalistic (both in terms of design and colors, and in terms of content and representation).
Try also to take regular breaks, and to let your mind run free for short periods of time every 1 or 2 hours, based on what works best for you. Maybe adopt the Pomodoro technique or something similar (I don't have research on a correlation with this, but it could be helpful in forcing you to take breaks).