def create_gpx_features(path2GPXFiles): gpxDir = path2GPXFiles arcpy.CreateFileGDB_management(gpxDir, "heatmap", "CURRENT") arcpy.env.workspace = os.path.join(gpxDir, "heatmap.gdb") # Iterate over gpx files, generate feature classes featuresToMerge = [] #create empty list for file in os.listdir(gpxDir): if file.endswith(".gpx"): ptFeatureClass = file.replace(".gpx", "_pt") # point feature class name try: arcpy.GPXtoFeatures_conversion(os.path.join(gpxDir, file), ptFeatureClass) print str(file) + " converted to features" except Exception as E: print str(file) + " failed..." print E lineFeatureClass = file.replace(".gpx", "_line") # line feature class name try: arcpy.PointsToLine_management(ptFeatureClass, lineFeatureClass, "", "", "NO_CLOSE") print str(file) + " converted to features" except Exception as E: print str(file) + " failed..." print E featuresToMerge.append(lineFeatureClass) mergedFeatureClass = "merged_lines" arcpy.Merge_management(featuresToMerge, mergedFeatureClass, "") # merge line feature classes arcpy.gp.LineDensity_sa(mergedFeatureClass, "NONE", "heat_map", ".00001", "0.0001", "SQUARE_MAP_UNITS") # do the heatmap
import arcpy arcpy.env.workspace = "C:/Users/Matt Layman/Downloads/activities" arcpy.env.overwriteOutput = True list = arcpy.ListFiles() print list for f in list: arcpy.GPXtoFeatures_conversion(f, f + ".shp") print arcpy.GetMessages() newlist = arcpy.ListFiles("*.shp") arcpy.Merge_management(newlist, "merge_shp") arcpy.MakeFeatureLayer_management("merge_shp.shp", "merge_lyr") arcpy.LayerToKML_conversion("merge_lyr", "activities.kmz")
print "{} created...".format(scratch_dir) # iterate through all files in root directory for subdir, dirs, files in os.walk(root_dir): for file in files: in_file_path = os.path.join(subdir, file) # if .gpx, convert to feature class if in_file_path[-4:] == ".gpx": print file out_file_name = file.replace(".gpx", "") out_file_path = os.path.join(scratch_dir, out_file_name) try: arcpy.GPXtoFeatures_conversion(in_file_path, out_file_path) except Exception: e = sys.exc_info()[1] print e.args[0] exit(1) # check if file already exists in scratch, if it does, delete and replace if arcpy.Exists(out_file_path): arcpy.Delete_management(out_file_path) arcpy.GPXtoFeatures_conversion(in_file_path, out_file_path) else: arcpy.GPXtoFeatures_conversion(in_file_path, out_file_path)
def treatGPX(file, crs): arcpy.GPXtoFeatures_conversion(file, r'in_memory\GPX_points') # TODO : Separate Waypoints, routes and tracks (generate lines for track's and routes' points) return [r'in_memory\GPX_points', None, None]
time.sleep(random.choice(delays)) #Wait for the converted file to finish downloading to the selected output folder while not (gpxFolder / outfile).exists(): time.sleep(15) #Rename the newly converted file to match the input file's name renamedFile = (gpxFolder / 'Renamed' / (f.stem + '.gpx')) #Mark the file as a duplicate if a gpx file by the same name exisits in the output folder if renamedFile.exists(): renamedFile = (gpxFolder / 'Renamed' / (f.stem + '_Dup.gpx')) (gpxFolder / outfile).rename(renamedFile) #Import the gpx file as a feature class in ArcGIS and log the file name in a new field in the feature class try: arcpy.GPXtoFeatures_conversion( str(renamedFile), str(geoDataset / f'{renamedFile.stem}_May18')) arcpy.AddField_management(str(geoDataset / f'{renamedFile.stem}_May18'), 'vidName', 'TEXT', field_length=50, field_alias='Video Name') arcpy.CalculateField_management( str(geoDataset / f'{renamedFile.stem}_May18'), 'vidName', f'{renamedFile.stem}', 'PYTHON3') #If the file cannot be imported to a feature class, log the file name in a tracking text file and continue on to the next gps file except: with open(fileFolder / 'notGIS.txt', 'a') as ngis: ngis.write(renamedFile.stem + '\n') time.sleep(random.choice(delays)) continue
import os import arcpy gpx_directory_path = r"C:\Users\Fran\Downloads\export_22552451\activities" gpx_list = os.listdir(gpx_directory_path) for gpx_file in gpx_list: if gpx_file.endswith(".gpx"): full_gpx_path = os.path.join(gpx_directory_path, gpx_file) print(f"Converting {full_gpx_path} to shapefile") output_shapefile = os.path.splitext(full_gpx_path)[0] + ".shp" try: arcpy.GPXtoFeatures_conversion(full_gpx_path, output_shapefile) print(f"Finished Converting {full_gpx_path} to shapefile \n") except Exception as e: print("failed to convert") print(e) print("Finished converting all gpx to shp")
### 2. Create Shapefile to hold all runs and add all runs from GPX files # set workspace to directory of interest arcpy.env.workspace = runFiles # create list of all files ending in .shp list_gpxfiles = arcpy.ListFiles("*.gpx") # create blank Shapefile arcpy.CreateFeatureclass_management(GeoDatabase, "RunTracks", "POLYLINE") # this loop will accomplish steps 3-5 for file in list_gpxfiles: print(file + " started") ### 3. Convert the GPX file into in_memory features arcpy.GPXtoFeatures_conversion(file, 'in_memory\\runs') # Select only the track points arcpy.SelectLayerByAttribute_management('in_memory\\runs', 'NEW_SELECTION', "\"Type\" = 'TRKPT'") ### 4. Convert the tracks into lines. The 'Name' field creates unique tracks. arcpy.PointsToLine_management('in_memory\\runs', GeoDatabase + "\\tempRun", 'Name', '#', 'NO_CLOSE') print(file + " converted") ### 5. Add line to shapefile of all runs arcpy.Append_management(GeoDatabase + "\\tempRun", GeoDatabase + "\\RunTracks", schemaType, fieldMappings, subtype)
# 4. Run 'GPX to Feature Class' tool #### syntax: #### GPXtoFeatures_conversion (Input_GPX_File, Output_Feature_class) #5. Feature class to geodatabase #6. Repeat script through list of rides. import arcpy import os try: arcpy.env.workspace = r"D://PT/BusStops" arcpy.env.overwriteOutput = True # Create new geodatabase to store created polylines. arcpy.CreateFileGDB_management( "D:/MSGT/Data/PythonBook/Exercise06/Results", "Routes.gdb") # Iterate over files in folder to convert GPX file to Feature Class and then copy to Geodatabase path = 'rides/' folder = os.listdir(path) for Input_GPX_File in folder: arcpy.GPXtoFeatures_conversion(Input_GPX_File, Output_Feature_class) arcpy.CopyFeatures_management( Output_Feature_class, "Routes.gdb/" + Output_Feature_class.basename) # Catch errors. except: print(arcpy.GetMessages())
print "starting up..." import arcpy from arcpy import env # The folder where the .gpx files are and where we create the file geodatabase workspace_folder = r'C:\Data\student\gpxrun' env.workspace = workspace_folder gdb_name = "gpx.gdb" outputgdb = workspace_folder + '\\' + gdb_name + '\\' if arcpy.Exists(outputgdb): print "Deleting the old geodatabase..." arcpy.Delete_management(outputgdb) print "Creating a file geodatabase to store the feature classes" arcpy.CreateFileGDB_management(workspace_folder, gdb_name) env.workspace = outputgdb print "Converting the .gpx files to Feature Classes" gpxlist = glob.glob(workspace_folder + "\\*.gpx") for gpxfile in gpxlist: outfile = os.path.splitext(os.path.basename(gpxfile))[0] print "Converting " + gpxfile arcpy.GPXtoFeatures_conversion(gpxfile, outputgdb + outfile) #TODO: Merge the feature classes #featureclasses = arcpy.ListFeatureClasses() #arcpy.Merge_management(featureclasses,env.workspace + '\\' + 'all_runs')
# Description: Converts GPX file into a polyline feature # Feature class can be a shapefile or geodatabase feature class # Created by: Patrick McKinney, Cumberland County GIS # Contact: [email protected] # "Telling the stories of our world through the power of maps" # Import system models import arcpy, sys try: # Define parameter variables for use in ArcGIS toolbox script inputGPX = arcpy.GetParameterAsText(0) outputFeatureClass = arcpy.GetParameterAsText(1) # Convert the GPX file into in_memory features arcpy.GPXtoFeatures_conversion(inputGPX, 'in_memory\gpx_layer') # Add message that GPX file has been succesfully converted to layer in Geoprocessing window arcpy.AddMessage("GPX file converted to feature class") # Convert the tracks into lines. arcpy.PointsToLine_management('in_memory\gpx_layer', outputFeatureClass) # Add message that Polyline feature has been created in Geoprocessing window arcpy.AddMessage("GPX file converted to polyline feature class") # If an error occurs running geoprocessing tool(s) capture error and write message # handle error outside of Python system except EnvironmentError as e: tbE = sys.exc_info()[2] # Write the error message to tool's dialog window
# Batch Conversion- GPX to Feature Class import arcpy # make sure to put the file path in inverted commas # 'D:\\z_input' # make sure to put double slash after output folder link # 'D:\\z_output\\' arcpy.env.workspace = input('Input Folder Path: ') out_folder = input('Output Folder Path: ') for gps in arcpy.ListFiles(): outgps = out_folder + arcpy.Describe(gps).baseName + '.shp' arcpy.GPXtoFeatures_conversion(gps, outgps)
arcpy.env.overwriteOutput = True gpxFolder = "[filepath]" outputFolder = "[filepath]" # Iterate input filepaths and add them to a list def filepaths(gpxFolder): fullPaths = [] for root, dirs, files in os.walk(gpxFolder): for filename in files: if filename.endswith(".gpx"): fullpath = os.path.join(root, filename) fullPaths.append(fullpath) # adds filepaths to the list return fullPaths files = filepaths(gpxFolder) # Iterate input files for track in files: filename = os.path.basename(track) ## split = filename.split("_file_") ## name = "%s" % split[1] ## newName = name.replace(".gpx",".shp") newName = filename.replace(".gpx", ".shp") # Define output path out_fp = os.path.join(outputFolder, newName) # gpx to shp conversion arcpy.GPXtoFeatures_conversion(track, out_fp)
def mainFunction( dataFile, geometryType, inputCoordinateSystemName, outputCoordinateSystemName, xField, yField, spreadsheetUniqueID, output ): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) try: # --------------------------------------- Start of code --------------------------------------- # # Get coordinate systems and transformation inputCoordinateSystem, outputCoordinateSystem, transformation = getCoordinateDetails( inputCoordinateSystemName, outputCoordinateSystemName) # If url set as data file urlCheck = ['http', 'https'] if any(file in dataFile for file in urlCheck): printMessage("Downloading file from - " + dataFile + "...", "info") # Download the file from the link file = urllib2.urlopen(dataFile) fileName, fileExt = os.path.splitext(dataFile) # Download in chunks fileChunk = 16 * 1024 with open(os.path.join(arcpy.env.scratchFolder, "Data" + fileExt), 'wb') as output: while True: chunk = file.read(fileChunk) if not chunk: break # Write chunk to output file output.write(chunk) output.close() dataFile = os.path.join(arcpy.env.scratchFolder, "Data" + fileExt) # If data type is excel if dataFile.lower().endswith(('.xls', '.xlsx')): # If x and y fields provided if ((xField) and (yField)): # Get geometry type - line or polygon if ((geometryType.lower() == "line") or (geometryType.lower() == "polygon")): # If unique Id provided if (spreadsheetUniqueID): # Call function to get layer from spreadsheet output = spreadsheetToLinePolygon( dataFile, geometryType, xField, yField, spreadsheetUniqueID, inputCoordinateSystemName, inputCoordinateSystem, outputCoordinateSystemName, outputCoordinateSystem, transformation) else: printMessage( "Please provide a ID field in the spreadsheet to uniquely identify each feature...", "error") sys.exit() # Get geometry type - point else: # If projection needed if (transformation.lower() != "none"): printMessage("Importing Excel sheet...", "info") arcpy.ExcelToTable_conversion(dataFile, "in_memory\\Dataset", "") arcpy.MakeXYEventLayer_management( "in_memory\\Dataset", xField, yField, "InputLayer", inputCoordinateSystem, "") printMessage( "Projecting layer from " + inputCoordinateSystemName + " to " + outputCoordinateSystemName + "...", "info") arcpy.Project_management( "InputLayer", os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), outputCoordinateSystem, transformation, inputCoordinateSystem, "NO_PRESERVE_SHAPE", "") printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), "Layer", "", "", "") else: printMessage("Importing Excel sheet...", "info") arcpy.ExcelToTable_conversion(dataFile, "in_memory\\Dataset", "") printMessage("Creating layer...", "info") output = arcpy.MakeXYEventLayer_management( "in_memory\\Dataset", xField, yField, "Layer", inputCoordinateSystem, "") else: printMessage( "Please provide an X and Y field for the Excel file...", "error") sys.exit() # If data type is shapefile elif dataFile.lower().endswith('.zip'): printMessage("Importing Shapefile...", "info") # Extract the zip file to a temporary location zip = zipfile.ZipFile(dataFile, mode="r") tempFolder = arcpy.CreateFolder_management( arcpy.env.scratchFolder, "Data-" + str(uuid.uuid1())) zip.extractall(str(tempFolder)) # Get the extracted shapefile shapefile = max(glob.iglob(str(tempFolder) + r"\*.shp"), key=os.path.getmtime) # If projection needed if (transformation.lower() != "none"): printMessage( "Projecting layer from " + inputCoordinateSystemName + " to " + outputCoordinateSystemName + "...", "info") arcpy.Project_management( shapefile, os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), outputCoordinateSystem, transformation, inputCoordinateSystem, "NO_PRESERVE_SHAPE", "") printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), "Layer", "", "", "") else: printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( shapefile, "Layer", "", "", "") # If data type is gpx elif dataFile.lower().endswith('.gpx'): printMessage("Importing GPX...", "info") arcpy.GPXtoFeatures_conversion(dataFile, "in_memory\\Dataset") # If projection needed if (transformation.lower() != "none"): printMessage( "Projecting layer from " + inputCoordinateSystemName + " to " + outputCoordinateSystemName + "...", "info") arcpy.Project_management( "in_memory\\Dataset", os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), outputCoordinateSystem, transformation, inputCoordinateSystem, "NO_PRESERVE_SHAPE", "") printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), "Layer", "", "", "") else: printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( "in_memory\\Dataset", "Layer", "", "", "") # If data type is kml elif dataFile.lower().endswith(('.kml', '.kmz')): # If kml geometry type provided if (geometryType): printMessage("Importing KML...", "info") arcpy.KMLToLayer_conversion(dataFile, arcpy.env.scratchFolder, "KML", "NO_GROUNDOVERLAY") outputGeodatabase = os.path.join(arcpy.env.scratchFolder, "KML.gdb") # Get the kml dataset as specified if (geometryType.lower() == "line"): kmlDataset = os.path.join(outputGeodatabase, "Placemarks\Polylines") elif (geometryType.lower() == "polygon"): kmlDataset = os.path.join(outputGeodatabase, "Placemarks\Polygons") else: kmlDataset = os.path.join(outputGeodatabase, "Placemarks\Points") # If projection needed if (transformation.lower() != "none"): printMessage( "Projecting layer from " + inputCoordinateSystemName + " to " + outputCoordinateSystemName + "...", "info") arcpy.Project_management( kmlDataset, os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), outputCoordinateSystem, transformation, inputCoordinateSystem, "NO_PRESERVE_SHAPE", "") printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), "Layer", "", "", "") else: printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( kmlDataset, "Layer", "", "", "") else: printMessage( "Please provide a geometry type for the KML file...", "error") sys.exit() # If data type is csv elif dataFile.lower().endswith('.csv'): # If x and y fields provided if ((xField) and (yField)): # Get geometry type - line or polygon if ((geometryType.lower() == "line") or (geometryType.lower() == "polygon")): # If unique Id provided if (spreadsheetUniqueID): # Call function to get layer from spreadsheet output = spreadsheetToLinePolygon( dataFile, geometryType, xField, yField, spreadsheetUniqueID, inputCoordinateSystemName, inputCoordinateSystem, outputCoordinateSystemName, outputCoordinateSystem, transformation) else: printMessage( "Please provide a ID field in the spreadsheet to uniquely identify each feature...", "error") sys.exit() # Get geometry type - point else: # If projection needed if (transformation.lower() != "none"): printMessage("Importing CSV...", "info") arcpy.MakeXYEventLayer_management( dataFile, xField, yField, "InputLayer", inputCoordinateSystem, "") printMessage( "Projecting layer from " + inputCoordinateSystemName + " to " + outputCoordinateSystemName + "...", "info") arcpy.Project_management( "InputLayer", os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), outputCoordinateSystem, transformation, inputCoordinateSystem, "NO_PRESERVE_SHAPE", "") printMessage("Creating layer...", "info") output = arcpy.MakeFeatureLayer_management( os.path.join(arcpy.env.scratchGDB, "Layer_Projected"), "Layer", "", "", "") else: printMessage("Importing CSV...", "info") printMessage("Creating layer...", "info") output = arcpy.MakeXYEventLayer_management( dataFile, xField, yField, "Layer", inputCoordinateSystem, "") else: printMessage( "Please provide an X and Y field for the CSV file...", "error") sys.exit() else: printMessage( "Not a valid data file. Please use .csv,.xls,.xlsx,.zip,.gpx,.kml or .kmz...", "error") sys.exit() # --------------------------------------- End of code --------------------------------------- # # If called from gp tool return the arcpy parameter if __name__ == '__main__': # Return the output if there is any if output: # If ArcGIS desktop installed if (arcgisDesktop == "true"): arcpy.SetParameter(7, output) # ArcGIS desktop not installed else: return output # Otherwise return the result else: # Return the output if there is any if output: return output # Logging if (enableLogging == "true"): # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] # If arcpy error except arcpy.ExecuteError: # Build and show the error message errorMessage = arcpy.GetMessages(2) printMessage(errorMessage, "error") # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # If python error except Exception as e: errorMessage = "" # Build and show the error message # If many arguments if (e.args): for i in range(len(e.args)): if (i == 0): # Python version check if sys.version_info[0] >= 3: # Python 3.x errorMessage = str( e.args[i]).encode('utf-8').decode('utf-8') else: # Python 2.x errorMessage = unicode(e.args[i]).encode('utf-8') else: # Python version check if sys.version_info[0] >= 3: # Python 3.x errorMessage = errorMessage + " " + str( e.args[i]).encode('utf-8').decode('utf-8') else: # Python 2.x errorMessage = errorMessage + " " + unicode( e.args[i]).encode('utf-8') # Else just one argument else: errorMessage = e printMessage(errorMessage, "error") # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage)
import arcpy import os #Folder containing gpx files gpxFolder = r"C:/gpx_folder/" #Output Folder destinationFolder = r"Q:/dest_folder/" #Folder containing the GPX files. ONLY HAVE GPX FILES IN THIS FOLDER! Remove the .zip file if it's here fcs = os.listdir(gpxFolder) for fc in fcs: #Rename the data path to the folder with the GPX files inp = os.path.join(gpxFolder, fc) #Rename the data path to the output folder output = (destinationFolder + fc + ".shp") arcpy.GPXtoFeatures_conversion(inp, output)
# Batch process multiple ASCII to Raster files # Method from: https://geonet.esri.com/thread/46801 # See bottom of page, comment by user "madanksuwal" (15 Mar 2013). # Import system modules import arcgisscripting, os # Create the Geoprocessor object gp = arcgisscripting.create() import arcpy import os import glob # Path to ascii files filepath = r"E:/downloads/kayplaza" # Path where to put rasters outFolder = r"E:/downloads/kayplaza/kayplaza.gdb" ascList = glob.glob(filepath + "/*.gpx") print ascList for ascFile in ascList: outRaster = outFolder + "/" + os.path.split(ascFile)[1][:-4] print outRaster arcpy.GPXtoFeatures_conversion(ascFile, outRaster)