Monday, October 3, 2016

jython script to converter coordinate format

the following will get a library call jcoord download it and place inside the same directory as the jython script with the name as jcoord.jar , to download it, the link is provided below,

http://www.jstott.me.uk/jcoord/jcoord-1.0.jar

script:

import sys
import re
sys.path.append("./jcoord.jar")
from uk.me.jstott.jcoord import *
latlon = sys.argv[1]
lat = latlon.split(",")[0].strip()
lan = latlon.split(",")[1].strip()
def numdir(direction):
    ndir = LatLng.NORTH
    if direction =="s" or direction == "w":
        ndir = LatLng.SOUTH
    return ndir
if re.search("n|s|e|w", latlon.lower()):
    ilat = lat.split()
    d = int(ilat[0])
    m = int(ilat[1])
    s = float(ilat[2])
    di = numdir(ilat[3])
    ilan = lan.split()
    nd = int(ilan[0])
    nm = int(ilan[1])
    ns = float(ilan[2])
    ndi = numdir(ilat[3])
    nlocate4 = LatLng(d,m,s,di,nd,nm,ns,ndi)
    print str(nlocate4.getLatitude()) + "," + str(nlocate4.getLongitude())

else:
    nlocate5 = LatLng(float(lat),float(lan))
    print nlocate5.toDMSString()



1- list directory
$ ls
jcoord.jar  latlong.py

2- example convert degree to decimal
$ jython latlong.py "11 20 6.0 N, 13 23 27.24 E"
"my" variable $jythonHome masks earlier declaration in same scope at /usr/bin/jython line 15.
11.335,13.3909




3- example convert decimal to degree
$ jython latlong.py "11.335,13.3909"
"my" variable $jythonHome masks earlier declaration in same scope at /usr/bin/jython line 15.
11 20 6.0000000000030695 N 13 23 27.24000000000089 E

No comments:

Post a Comment