1

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.

  • 4
    Have you tried 'Make XY Event Layer'? *In ArcGIS – MyFamily Feb 15 '15 at 17:45
  • Agreed! the only other (sensible) way is to use arcpy and read the text file line-by-line creating a new point geometry on an arcpy.da.insertCursor to a new shapefile. Although you've mentioned batch there is no python or arcpy tag so an arcpy answer may be unsuitable. If the XY event layer is unsuitable then that may be the only option. – Michael Stimson Feb 15 '15 at 22:22
3

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.

1

Make XY Event Layer is the tool you're looking for. You could import your text files into a file geodatabase and iterate through the feature classes, creating a new feature layer for each of your text files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.