Hellerick_Ferlibay

Boreal triaxial projection

Dec 9th, 2013
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import math
  2.  
  3. pi=3.14159265358979
  4. degrees=pi/180
  5.  
  6. ############################ ENTER DATA ############################
  7. mapin=open('c:\KPV\Maps\Wikipedia blank maps\BlankMap-Equirectangular.svg', 'rb')
  8. mapout=open('c:\KPV\Maps\Wikipedia blank maps\BlankMap-BorealTriaxial.svg', 'wb')
  9. ####################################################################
  10.  
  11. l4c="1234" # the last four characters
  12. readcoordinates=False
  13. watchit=False
  14. numberstatus=0  # 0: not within a coordinate pair
  15.                 # 1: read the first number
  16.                 # 2: between the two coordinates
  17.                 # 3: read the second number
  18. num1=''
  19. num2=''
  20. numchars=['0','1','2','3','4','5','6','7','8','9','.','-','+','e']
  21.  
  22. mainlon=[-70*degrees,+20*degrees,+110*degrees]
  23. midlon=[-25*degrees,+65*degrees,-160*degrees]
  24.  
  25. def roundit (sn=3.14159265358979):
  26.     rnum=round(sn,5) # the number stands for the number of digits after the point
  27.     position=str(sn).find('.')
  28.     snum=str(sn)[:position+6]
  29.     return snum
  30.  
  31. def rounddist(rda,rdb):
  32.     rdd=rda-rdb
  33.     if rdd>pi: rdd=rdd-2*pi
  34.     if rdd<-pi: rdd=rdd+2*pi
  35.     return rdd
  36.  
  37. def calculatecoordinates (x_i, y_i):
  38.     R = pi/2 - y_i
  39.     s0 = 0
  40.     s1 = 0
  41.     for i in range(3):
  42.         ximi0=rounddist(x_i,mainlon[i])
  43.         xims0=rounddist(x_i,mainlon[s0])
  44.         if abs(ximi0)<=abs(xims0): s0 = i
  45.         #print i, ximi0/degrees, xims0/degrees
  46.     xims0=rounddist(x_i,mainlon[s0])
  47.     for i in range(3):
  48.         ximi1=(rounddist(x_i,midlon[i]))
  49.         xims1=(rounddist(x_i,midlon[s1]))
  50.         if (ximi1*xims0<=0) and (abs(ximi1)<=abs(xims1)): s1 = i
  51.         #print i, ximi1/degrees, xims1/degrees
  52.     sh=rounddist(x_i,mainlon[s0])/rounddist(midlon[s1],mainlon[s0])
  53.     sign = 1
  54.     if sh < 0:
  55.         sign = -1
  56.         sh = -sh
  57.     if sh > 1: sh = 1
  58.     sh = 1 - (1-sh) ** (1-R/pi)
  59.     alpha = sign * sh * (rounddist(midlon[s1],mainlon[s0]))+mainlon[s0]-mainlon[1]
  60.     x = R * math.sin(alpha)
  61.     y = -R * math.cos(alpha)
  62.     mapout.write(roundit(x/degrees)+","+roundit(y/degrees))
  63.  
  64. while True:
  65.     try:
  66.         ch=mapin.read(1)
  67.         if ch=='': break
  68.         if ch=='$': watchit=True
  69.         l4c=l4c[1:]+str(ch)
  70.         if readcoordinates and (ch=='"'):
  71.             readcoordinates=False
  72.             if (l4c[2] in numchars) and (numberstatus==3):
  73.                 numberstatus=0
  74.                 calculatecoordinates(eval(num1)*pi/180,(eval(num2))*pi/180)
  75.                 num1=""
  76.                 num2=""
  77.         if l4c==' d="': readcoordinates=True
  78.         if readcoordinates:
  79.             if (numberstatus==0) and (ch in numchars): numberstatus=1
  80.             if (numberstatus==1) and (not (ch in numchars)): numberstatus=2
  81.             if (numberstatus==2) and (ch in numchars): numberstatus=3
  82.             if (numberstatus==3) and (not (ch in numchars)):
  83.                 numberstatus=0
  84.                 calculatecoordinates((eval(num1))*pi/180,(eval(num2))*pi/180)
  85.                 num1=""
  86.                 num2=""
  87.             if numberstatus==1: num1=num1+ch
  88.             if numberstatus==3: num2=num2+ch
  89.             if numberstatus==0: mapout.write(ch)
  90.         else:
  91.             mapout.write(ch)
  92.     except KeyboardInterrupt: break
  93.  
  94. mapout.close()
  95. mapin.close()
Advertisement
Add Comment
Please, Sign In to add comment