Territorial aggression in dogs is something, that we - the city dwellers - actually meet quite often, while walking our dogs. Most of the dogs naturally tend to protect things: their yard, apartment, house, and, for example, a car, especially if the dog is sitting inside it.
Protective instinct in dogs will be expressed most clearly if the area has a clearly defined boundaries: a house, an aviary, a chain, a car or a fenced yard. If you are using electric fencing, it will be better to remember, that it does not reduce the danger from the dog's territorial aggression, because the dog is good at remembering the borders, but the lack of visual markers for visitors can be dangerous.
Dogs are usually protecting the territory, where is situated their source of food (ex. bowl) and the place, where they rest. In the house it is quite possible that your dog will not protect the entire apartment, but only his own special place (crate, blanket, carpet). Sometimes animals are guarding the place where they are at the moment, the change of the location in this case is not changing aggressive behavior, the dog is guarding some space around himself, though usually exactly removing the dog from the certain area is used by trainers for the differentiation of the aggression type.
Territorial aggression is peculiar first of all to the herding breeds of dogs, at a lesser degree - to the guarding breeds, it is normally seen at a much less extend in the companion dogs and hunting dogs.
Quite often the owner himself unwittingly reinforces the territorial behavior in his dog. When you take your dog to the park - for example - and keep walking the dog "around the perimeter of the fence" all the time, thus you reinforces in the dog - that is already naturally inclined to protect "his" territory - the feeling of some sort of "ownership" for this park. In this situation it may be better to advise the owner to change routes of the walks more often for the dog not to take the common areas as "his" own.
At the new place the dog feels less confident and is not inclined to aggression. The same behavior can be attributed to many small dogs that are aggressive, let's say, in the familiar cafes and behave pretty well - in the new ones.
When the manifestation of the territorial aggression happens in the house, the dog before the arrival of any visitors should be closed in a room in advance and released only after the dog calms down, perhaps at the beginning of the training for a change in the behavior the dog must be leashed and muzzled and then only muzzled (without a leash). Guests must not show any interest in the dog's behavior, must not show any excitement, the atmosphere should be calm, so that the dog will not feel threatened.
You should not hug the dog, trying to calm things down, your dog will perceive such behavior (holding, hugging) as an encouragement for his actions. Released from the room the dog must be under the full control of the owner. Of course, the obedience of your dog in this case must be at the highest level, if the dog is not controlled by voice, then you need to work on the correct hierarchy. Then you can let your dog to sniff the guests, but first make sure, that he will do them no harm.
When the dog learns to quietly leave the room and greet the guests, he can be left in the corridor to meet the guests, but before their arrival make your dog to sit on your command, initially, it will probably be better to put the dog on a leash. Over time, when the dog will demonstrate a stable calm behavior, guests can interact with him: give him a toy or, for example, a tasty treat. Still your guest must not try to stroke your dog or to play any "competitive" games.
In the street you must teach the dog to react calmly to the unfamiliar dogs (intraspecific aggression is also unacceptable) and people. To do this in advance, at a distance where the dog feels safe and calm, she is given the command "sit", a person or a dog is passing by, than the owner should praise and treat the dog for calm behavior. Over time, the distance can be gradually reduced.