{"id":345,"date":"2006-06-04T17:32:58","date_gmt":"2006-06-04T22:32:58","guid":{"rendered":"http:\/\/ywwg.com\/wordpress\/?p=345"},"modified":"2007-05-04T23:51:10","modified_gmt":"2007-05-05T04:51:10","slug":"creating-gps-route-maps-from-googles-maps","status":"publish","type":"post","link":"https:\/\/ywwg.com\/wordpress\/archives\/345","title":{"rendered":"Creating GPS route maps from google maps"},"content":{"rendered":"<p>This is an expansion of the excellent work done by Mr. Davis at his <a href=\"http:\/\/www.marengo-ltd.com\/gps\/index.php\">Open Source GPS HOW TO<\/a> page.  He has a google maps hack <a href=\"http:\/\/www.marengo-ltd.com\/map\/\">here<\/a> that allows one to make a GPX xml file by clicking on intersections.  This is a very quick way to make a route map.<\/p>\n<p>I wanted to expand on his work in two ways:<\/p>\n<ol>\n<li>I wanted to fill in waypoint names for items that were left blank, and<\/li>\n<li>I wanted to automatically check for waypoint name duplication, because duplicates cause data to be overwritten in Etrex GPS devices.<\/li>\n<\/ol>\n<p>I wrote a python script that does both things.  All one has to do now is:<\/p>\n<ol>\n<li>Create a map on his website<\/li>\n<li>Name major waypoints on the map (I like to name intersections using the name of the street I&#8217;ll be turning on to)<\/li>\n<li>Save the GPX file to disk<\/li>\n<li>Run gpxrecode.py [inputfile] [outputfile]<\/li>\n<\/ol>\n<p>gpxrecode removes the leading number and hyphen from the waypoint name.  If the waypoint is blank it gives it the same name as the last one.  If the waypoint is already in a cache of waypoints, it adds a number to the end of the name.  The cache is maintained between runs of the program, so multiple routes shouldn&#8217;t clobber each-other&#8217;s names.<\/p>\n<p>Source after the break.<\/p>\n<p><!--more--><\/p>\n<pre>#!\/usr\/bin\/python\r\n\r\n#take a gpx xml file from http:\/\/www.marengo-ltd.com\/map\/ and recode it to fill out blanks\r\n#and remove numbers from beginning.  Eventually use an sqlite db or something to check for dupes over all routes\r\n\r\nimport string\r\nimport pickle\r\nimport os,os.path,sys\r\nimport math\r\n\r\nimport xml.sax\r\nfrom xml.sax.saxutils import XMLFilterBase,  XMLGenerator\r\n\r\nwaypoints = []\r\n\r\nNAME=0\r\nLAT=1\r\nLON=2\r\n\r\nclass gpxfilter(XMLFilterBase):\r\n   def __init__(self, In, Out):\r\n      XMLFilterBase.__init__(self, In)\r\n      self.element_stack = []\r\n      self.Out = Out\r\n      self.last_waypoint = None\r\n\r\n   def startElement(self, name, attrs):\r\n      self.Out.characters(\"\\\\n\"+\"   \"*len(self.element_stack))\r\n      self.Out.startElement(name, attrs)\r\n      if not attrs.has_key('lat'):\r\n         self.element_stack.append([name,None, None])\r\n      else:\r\n         self.element_stack.append([name,attrs['lat'],attrs['lon']])\r\n      \r\n   def characters(self, content):\r\n      if len(content)==0:\r\n         return\r\n      white=True\r\n      for c in content:\r\n         if not isWhitespace(c):\r\n            white=False\r\n            break\r\n      if white:\r\n         return\r\n         \r\n      if self.element_stack[-1][NAME] == \"name\":\r\n         name = content\r\n         \r\n         if name.isdigit():\r\n            name = self.last_waypoint\r\n         \r\n         split = name.split('-')\r\n         if len(split) == 2 and split[0].isdigit() and len(split[1])>0: #get rid of 03-BLAH\r\n            name = split[1]\r\n         \r\n         if len(name)>6:\r\n            name = name[0:6]\r\n\r\n         self.last_waypoint = name\r\n         basename = name\r\n      \r\n         match_index = -1\r\n         \r\n         #first try original name\r\n         #if name is unique, add it to cache and break\r\n         #else if coords are close, break (use dupe name)\r\n         #else loop (try new name)\r\n\r\n         found = False\r\n         for i in range(0,9999):\r\n            if i>0:\r\n               name = basename[0:6-len(str(i))]+str(i)\r\n            try:\r\n               match_index = [n[NAME] for n in waypoints].index(name)\r\n            except: #didn't find a match\r\n               if self.element_stack[-2][LAT] is not None:\r\n                  waypoints.append([name,self.element_stack[-2][LAT],self.element_stack[-2][LON]])\r\n               found = True\r\n               break\r\n            \r\n            diff_lat = abs(float(self.element_stack[-2][LAT]) - float(waypoints[match_index][LAT]))\r\n            diff_lon = abs(float(self.element_stack[-2][LON]) - float(waypoints[match_index][LON]))\r\n            if diff_lat < = .0003 and diff_lon <= .0003:\r\n               found = True\r\n               break\r\n         if not found:\r\n            print \"ran out of tries??? (should never happen)\"\r\n         self.Out.characters(name)\r\n   \r\n   def endElement(self, name):\r\n      if name != self.element_stack[-1][NAME]:\r\n         print \"ERROR expected \"+str(self.element_stack[-1])[0]+\" got \"+str(name)\r\n      self.element_stack.pop(-1)\r\n      self.Out.endElement(name)\r\n      self.Out.characters(\"\\\\n\"+\"   \"*(len(self.element_stack)-1))\r\n      \r\ndef isWhitespace(s):\r\n   \"\"\"Is 's' a single character and whitespace?\"\"\"\r\n   if s in string.whitespace:\r\n      return 1\r\n   return 0\r\n      \r\n      \r\nif __name__ == \"__main__\":\r\n   if len(sys.argv) < 3:\r\n      print \"need input file and output file\"\r\n      sys.exit(1)\r\n   try:\r\n      #try to load waypoint cache\r\n      f = open(os.getenv('HOME')+\"\/.gpswaypoints\",\"r\")\r\n      contrib,waypoints = pickle.load(f)\r\n   except:\r\n      contrib = []\r\n      waypoints = []\r\n   try:\r\n      out = open(sys.argv[2],\"w\")\r\n   except:\r\n      print \"couldn't open output file\"\r\n   In = xml.sax.make_parser()\r\n   Out = XMLGenerator(out)\r\n   filter_handler = gpxfilter(In, Out)\r\n   filter_handler.parse(sys.argv[1])\r\n   f = open(os.getenv('HOME')+\"\/.gpswaypoints\",\"w\")\r\n   #save the cache\r\n   if os.path.split(sys.argv[1])[1] not in contrib:\r\n      contrib = contrib+[os.path.split(sys.argv[1])[1]]\r\n   pickle.dump([contrib,waypoints],f)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is an expansion of the excellent work done by Mr. Davis at his Open Source GPS HOW TO page. He has a google maps hack here that allows one to make a GPX xml file by clicking on intersections. This is a very quick way to make a route map. I wanted to expand &hellip; <a href=\"https:\/\/ywwg.com\/wordpress\/archives\/345\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating GPS route maps from google maps&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,2,13],"tags":[],"_links":{"self":[{"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":0,"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"wp:attachment":[{"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ywwg.com\/wordpress\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}