Advertisement
Hellerick_Ferlibay

Mercator to rectangular conversion

Dec 11th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #! python2
  2. # http://pastebin.com/mZ35nEKz
  3. # http://ge.tt/4fWcPA91/v/0
  4.  
  5. import png, sys, os
  6. from math import pi, log, tan
  7.  
  8. address = eval(os.path.normpath(str(sys.argv)))[-1]
  9. #print 'address', address
  10. datain = png.Reader(address).read()
  11. #f = datain = png.Reader('/media/ToshikHD/Dropbox/Programming/Python/Maps/RasterMecatorToRectangle/TestMercator.out.png').read()
  12. bitmap = list(datain[2])
  13. planes = datain[3]['planes']
  14. size = datain[3]['size']
  15. deg = pi/180
  16.  
  17. def peek (x, y):
  18.     global planes, bitmap
  19.     #if grayscale == True: pass
  20.     return (bitmap[y][x*planes],bitmap[y][x*planes+1],bitmap[y][x*planes+2])
  21.  
  22. sizeout = (size[0],size[0]//2)
  23. bitmapout = range(sizeout[1])
  24. for i in range(sizeout[1]):
  25.     bitmapout[i]=()
  26.     lat = (1-(i+0.5)/sizeout[1]*2)*pi/2
  27.     latmer = log (tan (pi/4 + lat/2))
  28.     imer = size[1]//2 - latmer/(pi/2) * sizeout[1]/2 - 0.5
  29.     if imer < 0: imer = 0
  30.     if imer > size[1]-2: imer = size[1]-2
  31.     #print 'i', i, 'imer', imer, 'lat', lat/deg, 'latmer', latmer/deg
  32.     w0 = abs (int (imer+1) - imer)
  33.     w1 = abs (int (imer) - imer)
  34.     for j in range(sizeout[0]):
  35.         m0 = peek (j, int (imer))
  36.         m1 = peek (j, int (imer+1))
  37.         m = (min(int(m0[0]*w0+m1[0]*w1+0.5),255), min(int(m0[1]*w0+m1[1]*w1+0.5),255), min(int(m0[2]*w0+m1[2]*w1+0.5),255))
  38.         bitmapout[i] += m
  39.  
  40. f = open('.rect.png'.join(address.split('.png')), 'wb')
  41. #f = open('/media/ToshikHD/Dropbox/Programming/Python/Maps/RasterMecatorToRectangle/TestMercator.out.png', 'wb')
  42.  
  43. w = png.Writer(size[0],size[0]//2)
  44. w.write(f, bitmapout)
  45. f.close()
  46.  
  47. description = """
  48. Mercator to equirectangular projection projection converter
  49.  
  50. Hi. I have noticed a common mistake among the worldbuilders here. Often they choose the equirectangular projection for the base map of their conworld. (Equirectangular projection is a rectangle 360°×180°, with X and Y coordinates directly corresponding to the degrees of longitude and latitude.) This projection is quite good to work with, and it can be feed into G.Projector to produce many other neat projections, but the trouble is it isn't the best choice when you're *designing* a world, because nearly nobody can produce plausible distorsions of it (the horizontal stretching in the polar zones).
  51.  
  52. Here is one of the examples: http://i.imgur.com/miWaWpU.png At first glance it looks okay. But let's see how the north pole looks like. That' how: http://i.imgur.com/m0PZem2.png It has an unnatural stretching of land shapes towards the pole.
  53.  
  54. In fact, for shaping the continents you should rather use one of the conformal projections, with Mercator projection being most well known of them. They are good because they preserve the shapes (at the expense of conformity of scale), so you don't have to worry about further distorsions, and a little round island on your map will remain a round island on the globe (probably not as little though).
  55.  
  56. So, I decided to make a program for converting Mercator maps into equirectangular projection. I'm not a good programmer, but it seems to working. It's written in Python 2, and here is its code: http://pastebin.com/mZ35nEKz , and here you can download a compiled version for Windows: http://ge.tt/4fWcPA91/v/0
  57.  
  58. It can be used to convert full-color (not grayscale) PNG files. To run the EXE program from command prompt you can type something like "Mercator2Rectangle.exe MyMap.png" or simply drag-and-drop the map file to the program icon. If everything was done correctly, there will appear yet one file, named something like "MyMap.rect.png".
  59.  
  60. The program presumes that your Mercator map is symmetrical, i.e. the the equator runs through its center. The rectangle for Mercator map normally should be higher than for the equirectangular map — I suggest to use a square.
  61.  
  62. So, ultimately this program is supposed to convert something like this: http://i.imgur.com/fEbRnjG.png
  63. Into something like this: http://i.imgur.com/M4zothB.png
  64. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement