Curious though it may seem, even though people knew that black powder was potentially explosive from the earliest days, it was made for a considerable time within towns, probably because towns were often under siege and needed the factory to be inside to supply the gunpowder for the guns mounted on the town walls. Of course, there were accidents: For instance, in 1360, it is recorded that the town-hall of Lubeck, one of the largest and richest cities in the Hanseatic league (now in Northern Germany) was burned to the ground, thanks to the carelessness of the gunpowder makers of that town. In 1528, the town leaders of Breslau finally issued a law prohibiting manufacturing gunpowder within the town. In 1490, Venice passed a law to move gunpowder manufacture from the city center to the Venetian Arsenal (which was not in the city center, but pretty darn close to it), but many of its other factories (such as in Padua, Treviso, Verona, Brescia etc.) were located practically at the center of town. It took a major fire at the Venetian Arsenal in 1569, which forced the Council of Ten to pass a law to make both gunpowder manufacturing and storage outside the urban area of Venice. The new site was the small island of Sant'Angelo di Concordia (later renamed to Sant'Angelo della Polvere because of the gunpowder factory), which was located to the south-west of the main islands of Venice.
The Island of Sant'Angelo della Polvere in the Venetian lagoon.
Click on the image to enlarge. Image is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license by Maurice Ohana
However, Venice was more of an anomaly because it had a strong navy and didn't need city walls because of protection by the sea. Most towns still continued to locate their gunpowder factories within their walls. And if the factories were located outside the heavily populated areas, the buildings were often located haphazardly.
It was only in the nineteenth century that regulations governing factory safety went into place in many countries. Laws were passed specifying the distance of the factory buildings from citizens' houses and from each other and also how much quantity of ingredients or powder may be worked on at a time in each building, materials to be used to construct the buildings and so on.
Since powder factories need a considerable amount of machinery to pulverize, mix and combine powder ingredients, they were generally erected in places where a large amount of water power is available, such as a fast flowing river or canal. In places where water power could not be reasonably applied, animal power (e.g. oxen, horses etc.) was used instead. For instance, we know that horses were used in Venice because various records of purchases of horses and hay for the Venetian powder factories from 1560-1570 have survived. Later on, when the steam engine was perfected in the 1760s by James Watt (although it was invented and worked on by other people many years before, James Watt made it much more practical for factory use), some gunpowder factories began to use steam power to drive their machinery, which made it possible to locate the factory away from flowing water.
Where water power was used, the machinery required for a particular operation were arranged in pairs, so that there would be one machine on the left side and the other on the right side. The water wheel would be located at the center, or individual wheels would be placed on each side and the water routed via canals to either side. Power would be transmitted from the wheel to the machine using gears and gear shafts. In some cases, a large water wheel would transmit its power to various buildings using wire ropes and chain arrangements.
Animal power was used where water power was not readily available, but the machines in these factories were smaller. Running costs were higher because not only did the machines require repairs and greasing, but the factory also had to pay for the animals, their food, their stables and the people who were caring for those animals.
When steam power was used, there were two methods to transmit the power. The first method consisted of installing a large steam engine located centrally, which produced all the power required and this was transmitted to the various buildings using wire ropes. In this case, the steam engine was located in a central building and the other buildings were arranged in a circle around it, as was done in France in the factory at Sevran-Livry in Paris. The other option was that steam was produced in a large boiler plant and the steam was transported via an arrangement of pipes into the various buildings, where it was fed into smaller steam engines. With this method, the fire that produced the steam was kept away from the buildings containing the steam engines.
Water wheels are somewhat low in efficiency compared to other types of power, but they are very cheap to build and running costs are very low, with only application of grease and repairs to be done periodically, the water (which is the source of power) generally costing nothing. The technology of water wheels was well understood for many centuries, being used by the Egyptians, Greeks, Romans, Chinese, Indians, Arabs, Medieval Europe etc. The only thing to watch out for is floods and droughts, which could either wreck the factory or cause it to stop functioning. However, because of the low costs, water wheels were used anywhere that water power was readily available, and were competitive with steam engines well into the Industrial revolution.
Steam engines are higher in efficiency compared to water power and could be used as long as there was an adequate supply of fuel and water. Transmitting the power from a central engine to various buildings via wire ropes was fine over shorter distances, however the power losses can be considerable over larger distances. There is also much more lubrication needed for the ropes and pulleys and in case a rope breaks, a whole section of a factory could stop functioning. Therefore, many factories got around this by using a system of insulated pipes to transmit the steam to smaller steam engines located inside each building. Of course, the pipes had to be checked to ensure that they were not leaking steam and over-pressurization could cause pipes to break and stop work immediately, but using a system of smaller pipes and valves solved this issue somewhat because the valve of one pipe could be shut down for maintenance, while another parallel pipe or two could continue to carry steam to the machine while the first pipe was being examined and repaired and so on.