~foxtrotgps-team/foxtrotgps/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3
from __future__ import print_function

# Simple script to convert GPX logs to OpenSteetMap for easy importation.
# Sets some basic defaults to speed up the process a bit.
# Author David Bannon but borrows heavily from a perl script by Markus Bauer
import sys, os, datetime, subprocess
import argparse

TagHighway = "\t\t<tag k='highway' v='road' />\n"
TagSurface = "\t\t<tag k='surface' v='unpaved' />\n"
TagTracktype = "\t\t<tag k='tracktype' v='grade3' />\n"
Tag4wd_only = "\t\t<tag k='4wd_only' v='yes' />\n"
TagLanes = "\t\t<tag k='lanes' v='1' />\n"
TagSource = "\t\t<tag k='source' v='survey' />\n"
TagName = "\t\t<tag k='name' v='no name yet' />\n"
HDOP = "10"
	# hdop is a measure of how accurate GPS might be, smaller the better.
	# Markus used 3.1 but I found that walking tracks with overhead trees
	# can easily generate tracks in the 6 range.
	# https://wiki.openstreetmap.org/wiki/Converting/NMEA_to_GPX suggests 10
	# If you are really careless you can describe HDOP as a multiplier to the
	# intrinsic error of the full hardware chain, typically 5 to 8 metres.
	# So, again, very careless, HDOP of 10 means an uncertainty of 50 to 80 metres.

def Header():
	return "<?xml version='1.0' encoding='UTF-8'?>\n<osm version='0.5' generator='JOSM'>\n";

def TrackTags():		# Useful for all unsealed roads
	return TagName + TagHighway + TagTracktype + TagSurface + TagLanes + TagSource

def RoadTags():			# default tag set
	return TagName + TagHighway + TagSource

def Footer():
	return "\t</way> \n</osm> \n";


def WriteOSM(InFile):
	Count = 1;
	OF =open(InFile+'.osm', 'w')
	OF.write(Header())
	IF = open(InFile + '.csv')
	for Line in IF:
		Pos = Line.split(', ')
		Node = "\t<node id='-" + str(Count) + "' visible='true' lat='" + Pos[0] + "' lon='" + Pos[1] + "' />\n"
		OF.write(Node)
		Count = Count + 1
	OF.write("\t<way id='-" + str(Count) + "' action='modify' visible='true'>\n")
	for WLine in range(1, Count):
		Way = "\t\t<nd ref='-" + str(WLine) + "' />\n"
		OF.write(Way);
	if args.TRACK:
		OF.write(TrackTags())
	else:
		OF.write(RoadTags())
	IF.close
	OF.write(Footer())
	OF.close
	os.unlink(InFile + '.csv')
	return Count

def HowManyPoints(FileName):
	Count = 0
	BadCount = 0
	IF = open(FileName)
	for Line in IF:
		Count = Count + Line.count("<hdop>") # careful, this is case sensitive !
		# print("CNT", Count, Line)
		HDOPs = Line.split('<hdop>')
		for i in HDOPs:
			try:
				# print("testing ", i, i[:3])
				if 10.0 < float(i[:i.find('<')]):
					BadCount = BadCount+1
			except ValueError:
				continue
	IF.close
	return Count, BadCount

# -------- ------------------------
# -------- Main Function ----------
# ---------------------------------

# use = " %prog [ option ]."

parser = argparse.ArgumentParser(description='convert gpx logs to OSM files')
parser.add_argument('-v', dest='VERBOSE', action='store_true', help='be verbose')
parser.add_argument('-t', dest='TRACK', action='store_true', help='treat it as a track')
parser.add_argument('FileList', metavar='gpxfile', nargs='+', help='some gpx file')
args = parser.parse_args()

for Infile in args.FileList:
	Base,ext = os.path.splitext(Infile)
	Points, BadPoints = HowManyPoints(Infile)
	if BadPoints*2 > Points:
		print("Warning,", Infile, " has excessive points with poor HDOP")
	if args.VERBOSE:
		print(Infile, Points, "points logged", BadPoints, "with excessive HDOP")
	Cmd = 'gpsbabel', '-i', 'gpx', '-f', Infile, '-x', 'simplify,error=0.003k', '-x', 'discard,hdop='+HDOP, '-o', 'csv', '-F', Base+'.csv'
	try:
		Pipe = subprocess.Popen(Cmd, stdout=subprocess.PIPE)
	except OSError:
		print("Looks like you do not have gpsbabel installed, or maybe not in your path.")
		print("Please make gpsbabel available and try again.")
		sys.exit()
	Lines = Pipe.communicate()[0]
	if len(Lines) > 0:
		print('Something bad happened, beats me !')
		print(Lines)
		sys.exit()
	if args.VERBOSE:
		print(WriteOSM(Base), end=" ")
		print("nodes saved")
	else:
		WriteOSM(Base)