GPS to GPX 변환

2innnnn0·2020년 4월 24일
0

온라인 상에서 위경도 데이터를 .GPX로 변환해주는 파이썬 코드를 찾아보았는데 비슷하지만 정작 작동하지 않는 코드여서 참고하여 Garmin Virb가 인식할 수 있도록 일부 수정하였다.

아래는 샘플 CSV이고,
[csv2gpx.py] 로 GPX확장자로 변환시켰다.

gps_csv_sample.csv

  • [csv2gpx.py] 코드
## csv2gpx.py
import os
import sys
import csv
import datetime
import requests

c1 = "# csv2gpx V.3 : a program to convert .csv-tables to Garmin-.gpx-format"
c2 = "# written and copyright by Till Handel, January 22, 2018"
c3 = "# this software is licensed under GPLv3"

print(c1+'\n'+c2+'\n'+c3+'\n\n')

finame = []
foname = []

path = os.getcwd();
for filename in os.listdir(path):
	if(filename[-4:]=='.csv'):
		print('file found: '+os.path.join(path,filename))
		finame.append(os.path.join(path,filename))
		foname.append(os.path.join(path,filename[:-4])+'.gpx')

def rectify(text):
        #STUB - no rectification of UTF-8 chars needed
        return text

#try:
for n in range(0,len(finame)):
	print('opening file %s',finame)
	with open(finame[n],encoding='utf-8',mode='r') as csvfile: #closes file automatically with completion of block
		i=0
		csvdata = csv.reader(csvfile, delimiter=',')

		name = [] #0
		# addr = [] #1
		# city = [] #2
		# stat = [] #3
		# coun = [] #4
		# zipc = [] #5
		# phon = [] #6
		# desc = [] #7
		# cate = [] #8
		# symb = [] #9
		lati = [] #10
		long = [] #11
		time = [] #12

		for row in csvdata:
			#skip first row of csv-file
			if (i>0):
				name.append(rectify(str(row[0])))
				lati.append(rectify(float(row[1])))
				long.append(rectify(float(row[2])))
				time.append(rectify(row[3]))
			i=i+1

	with open(foname[n],encoding='utf-8',mode='w') as of:
		print('\n\n++writing file: '+foname[n]+'\n')

		#header
		of.write('<?xml version="1.0" encoding="UTF-8"?>\n')
		of.write('<gpx creator="StravaGPX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3">\n')
		#metadata
		of.write("  <metadata>\n")
		of.write("    <link href=\"http://www.garmin.com\">\n")
		of.write("      <text>Garmin International</text>\n")
		of.write("    </link>\n")

		#time now
		# of.write("    <time>" + datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ") + "</time>\n")

		#gps bounds

		of.write("  </metadata>\n\n")
		of.write("  <trk>\n")
		of.write("	  <name>Untitled</name>\n")
		of.write("	  <trkseg>\n")

		#write waypoints here
		for i in range(0,len(name)):
			#lat,lon
			of.write("	    <trkpt lat=\"")
			of.write('%.8f' % lati[i])
			of.write("\" lon=\"")
			of.write('%.8f' % long[i])
			of.write("\">\n")
			#time
			if(len(time[i])>0):
				of.write("	    	<time>")
				of.write(time[i])
				of.write("Z</time>\n")
			#name
			# if(len(name[i])>0):
			# 	of.write("	    	<name>")
			# 	of.write(name[i])
			# 	of.write("</name>\n")
			#type
			# of.write("    <type>user</type>\n")
			#extensions
			of.write("	    	<extensions>\n")
			#gpxx extension
			of.write("	      	<gpxtpx:TrackPointExtension>\n")
			of.write("	      	</gpxtpx:TrackPointExtension>\n")
			of.write("	    	</extensions>\n")
			of.write("	    </trkpt>\n\n")
			#print waypoint-data to screen
			print('%.8f' % lati[i],'%.8f' % long[i],time[i])

		#end of file
		of.write("	  </trkseg>\n")
		of.write("  </trk>\n")
		of.write("</gpx>\n")
		print('\n--end of file')

print('\n\nDone!')

input("Press Enter to close...")
profile
성장하고 싶은 데이터분석가.

0개의 댓글