[GIS] How to batch process .txt files with X,Y data (in Arc or QGIS)

arcgis-desktopbatchmodelbuilderqgisqgis-processing

I have a long list of .txt files that I need to convert to feature class. I can bring the table into ArcMap, right-click, Display X,Y Data, and it converts to a feature no problem.

Is anyone aware of a way to convert all of them at once, either in QGIS or ArcGIS? I was hoping to do this in ModelBuilder but there is no "tool" in the search option for Display XY Data.

Best Answer

You can do it using QGIS, this way:

  1. Open QGIS and open the QGIS Python console (Plugins->Python console).

  2. Check the following Python code snippet. Adjust the lines below the block Settings (i.e., myDir, myTargetDir, refSys, separator, xField, and cyField) to configure your own data:

    import os
    import glob
    
    # Settings
    myDir = '/path/to/txt/files/' # For Windows paths: C:\\dir1\\subdir1\\
    myTargetDir = '/output/path/'
    refSys = 'epsg:4326'
    separator = ','
    xField = 'Long'
    yField = 'Lat'
    
    for path in glob.glob( os.path.join(myDir, '*.txt') ):
        uri = "file:///%s?delimiter=%s&xField=%s&yField=%s&crs=%s" % (path, separator, xField, yField, refSys)
        filename = os.path.basename(path)
        (layerName, extension) = os.path.splitext(filename)
        vLayer = QgsVectorLayer(uri, layerName, "delimitedtext")
        QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
            os.path.join(myTargetDir, vLayer.name() + ".shp"), "utf-8", 
            vLayer.crs(), "ESRI Shapefile" )
    
  3. Copy the adjusted code snippet above and paste it into the QGIS Python console.

You should now have all your Shapefiles available in the folder myTargetDir, ready to be loaded to QGIS. If you have any trouble just let us all know.

Related Question