GetElevation.py

DH 교육용 위키
Aks김지선 (토론 | 기여) 사용자의 2020년 10월 22일 (목) 16:34 판

(비교) ← 이전 판 | 최신판 (비교) | 다음 판 → (비교)
이동: 둘러보기, 검색

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 Maps 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