In this document, I will convert the GPS way-points (with photo-link, name, latitude, longitude) into shapefile using arcpy and python modules. Find the example gpx file from this link. Use the following example code and modify according to your need. The photos are in the same folder as the gpx file.
import arcpy import os from lxml import etree ws = '/home/ubuntu/gpx' arcpy.env.workspace = ws arcpy.env.overwriteOutput = True def parse(source): try: parser = etree.XMLParser( huge_tree = True, no_network = False, remove_blank_text = True, ) result = etree.parse(source, parser) return result except Exception as e: print('XML Parse error: {}'.format(e)) return None def stringify_children(node): from lxml.etree import tostring from itertools import chain parts = ([node.text] + list(chain(*([c.text, tostring(c), c.tail] for c in node.getchildren()))) + [node.tail]) return ''.join(filter(None, parts)) table_name = 'table' files = os.listdir(ws) gpx_file = [os.path.join(ws, _file) for _file in files if _file.endswith('.gpx')][0] gpx_tree = parse(gpx_file).getroot() gdb = arcpy.CreateFileGDB_management(ws, '{}'.format(table_name)) # way points result = arcpy.CreateFeatureclass_management (gdb, arcpy.ValidateTableName('{}_wp.shp'.format(table_name)), geometry_type='POINT', spatial_reference=arcpy.SpatialReference(4326)) arcpy.AddField_management(result, 'name', 'TEXT') arcpy.AddField_management(result, 'link', 'TEXT') cursor = arcpy.da.InsertCursor(result, ['name', 'link', 'SHAPE@XY']) way_points = gpx_tree.xpath('//ns:wpt', namespaces = {'ns':'http://www.topografix.com/GPX/1/1'}) for way_point in way_points: lat = float(way_point.attrib['lat']) lon = float(way_point.attrib['lon']) wp_shp = arcpy.Point() wp_shp.Y = lat wp_shp.X = lon point_geometry = arcpy.PointGeometry(wp_shp, arcpy.SpatialReference(4326)) name = way_point.xpath('./ns:name', namespaces = {'ns': 'http://www.topografix.com/GPX/1/1'})[0] name = stringify_children(name) link = None if 'Photo' in name: link = way_point.xpath('./ns:link', namespaces = {'ns': 'http://www.topografix.com/GPX/1/1'})[0] link = os.path.join(os.path.normpath(ws), _dir, link.attrib['href']) link = link.replace(os.sep, '/') cursor.insertRow([name, link, point_geometry])
Leave a Reply