First, "calculate" is not the right word for it. There is nothing to calculate, it is simple mapping of IP to location. First, you would have to map IP blocks to ISPs. Then get the location of the ISPs. Manually. This is a huge task, and you need to constantly update the list, especially with IPv6 fast growing.
I see no reason to do the job yourself. There are APIs, use them. Maxmind does a great job at providing accurate information such as country, region, ISP name.