Gem Formation

Traveling through the crust of the earth we come across gem deposits; how did they form?