"GetElevation.py"의 두 판 사이의 차이
DH 교육용 위키
(새 문서: ===Python Source=== <pre> #!/usr/bin/python import sys import os import urllib.request import shutil address = "https://maps.googleapis.com/maps/api/elevation/json?key=AIzaSyBJ3hN7x...) |
|||
101번째 줄: | 101번째 줄: | ||
121.5789108 | 121.5789108 | ||
</pre> | </pre> | ||
+ | |||
+ | [[분류:튜토리얼]] |
2020년 10월 22일 (목) 16:22 판
Python Source
#!/usr/bin/python import sys import os import urllib.request import shutil address = "https://maps.googleapis.com/maps/api/elevation/json?key=AIzaSyBJ3hN7xLjNi96Es0-Tpb3K6XVnY5_pK2s&locations=" #Google Mas Api def findLocation( scratchFile, fp ): gp = open( scratchFile, 'rt' ) #{ # "results" : [ # { # "elevation" : 45.09235000610352, # "location" : { # "lat" : 37.624531, # "lng" : 127.130613 # }, # "resolution" : 19.08790397644043 # } # ], # "status" : "OK" #} while 1: line = gp.readline() if not line: break #checkline = line.upper() if( line.find("elevation") != -1 ): elevationString = line.strip() fp.write( '{0}\n'.format( elevationString[14:-1] )) return def downJson( location, scratchFile, fp ): url = address.__add__(location) try: urllib.request.urlretrieve(url, scratchFile) except: print( '{0}: Invalid Location!'.format( location ) ) fp.write( '{0}: Invalid Location!\n'.format( location ) ) return findLocation( scratchFile, fp ) def main(): try: filename = sys.argv[1] except: return list = filename.__add__( ".lst" ) folder = filename scratchFile = folder + '/' + 'scratch.json' outputFile = folder + '/' + 'elevation.lst' try: os.makedirs( folder ) except OSError: pass i = 0 input = open( list, 'rt' ) output = open( outputFile, 'wt' ) while 1: line = input.readline() if not line: break location = line.strip() print( "{0}: Processing....".format(location)) downJson( location, scratchFile, output ) input.close() output.close() os.remove(scratchFile) main()
입력 데이터 예시: *.lst
36.1947,127.453 36.1184,128.004 35.8403,128.28 35.1355,127.905
출력 데이터 예시: */elevation.lst
255.6740112 260.0301514 134.5969849 121.5789108