That's my question. The AI just barges in and will build anywhere there is enough land to do so. If you don't have enough units to halt their movement, then what do you do? Going to war could be dangerous with your cities extended out to the enemy border. I've gotten so fed up eith the AI doing the above, that I go to war very soon so that my map of my land is just my color.