0

I have like 6 .xyz files that are to be included in a single map (they sort of overlap). However, I'd like to save time and automatically merge them together through python code into one .XYZ before processing them through my script.

Is there a seamless way to do this in ArcGIS Desktop?

BOTTOM LINE: MERGE XYZ's into one XYZ file. Any suggestions or alternatives to this?

2
  • Arcpy, what have you tried? Do you just want someone to write it for you? is this a one time project? If so I would suggest create 6 files and intersect them?
    – Brad Nesom
    Commented Aug 17, 2015 at 21:41
  • Does each xyz file have a header as its first line? Commented Aug 17, 2015 at 22:48

3 Answers 3

5

If the XYZ files are just delineated text files, you could just copy/paste in notepad or some such editor. Six files doesn't seem like many. Sometimes you don't need to automate...

However, I think there are a few options that won't require ArcPy.

Things that come to mind:

  1. Load the data into a new feature class or table.

  2. Try the append tool.

  3. Try the merge tool.

  4. Try the union tool.

3

I am going to assume these xyz files have the same headers as the first line. If there is no header in your xyz files, delete the lines indicated with #remove me if no header in the code below. Place your xyz files in the same directory. Then you can iterate through the files with os.listdir. From there use open to open each file and write each line into a new file. In the script below, the first line of each xyz is skipped if it's not the first file.

inFolder is the folder with your .xyz files.

outXyz is the output file. if this file exists already, it will be overwritten.

# ---------- Locals

#input folder
inFolder = r"C:\Users\e1b8\Desktop\E1B8\GIS_Stackexchange"

#output file
#if file exists, file will be written over
outXyz = r"C:\Users\e1b8\Desktop\E1B8\GIS_Stackexchange\merge.xyz"

# ---------- End locals

#import modules
import os

#open out file for writing
outFile = open (outXyz, "w")

#Create list of xyz files in folder
xyzs = [xyz for xyz
        in os.listdir (inFolder)
        if xyz.lower().endswith (".xyz")]

#first variable for first line (header)
firstFile = True

#iterate xyz files
for xyz in xyzs:

    #xyz file full path
    xyzPath = os.path.join (inFolder, xyz)

    #open xyz file
    xyzFile = open (xyzPath, "r")

    #second variable for first line (header)
    firstLine = True

    #iterate lines in file
    for line in xyzFile:
        #Skip header if first line of not-the-first file
        if firstFile == False and firstLine == True: #remove me if no header

            #switch variable to false - first line
            firstLine = False #remove me if no header
            continue #remove me if no header

        #Write line to out file
        outFile.write (line)

    #switch varaible to false - not the first file
    firstFile = False

    #close xyz file
    xyzFile.close ()

#close out file
outFile.close ()
0
0

A very simple batch solution I use often (but it's not in Python) : https://stackoverflow.com/questions/3418549/need-to-combine-lots-of-files-in-a-directory

Assuming these are text files and that you are on Windows, you could fashion a simple batch script to concatenate them together.

For example, in the directory with all the text files, execute the following:

for %f in (*.txt) do type "%f" >> combined.txt

This will merge all files matching *.txt into one file called combined.txt.

For more information: http://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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