Hi, I am a new ArcGis user.
I try to get a lan/lon of the neghborhoods of a region. Although my code runs w/o any problem, the output file which is a .txt file is not created/ or not seen in the dedicated directory.
Here is my script:
'''
## (i) Load arcpy and set working directory
#
import arcpy, os, getpass, shutil, copy, re
from shutil import copyfile
if getpass.getuser()=='xyz':
working_dir = 'fill in path'
elif getpass.getuser()=='esm':
working_dir = 'Z:/Documents/GitHub/amman'
os.chdir(working_dir)
arcpy.env.workspace = working_dir
arcpy.env.overwriteOutput = True #allows for overwriting files
#
## (ii) Useful shortcuts
#
temp_dir = '/tex/theory/pop_increase/temp/'
output_dir = working_dir + '/tex/theory/pop_increase/input/travel_time/'
spatial_reference = arcpy.SpatialReference("WGS 1984")
#
## (iii) Set up GDB for calculations
#
gdbDir = temp_dir
gdbName = 'traveltime'
gdbPath = gdbDir + gdbName + '.gdb'
if arcpy.Exists(gdbPath):
arcpy.Delete_management(gdbPath)
arcpy.CreateFileGDB_management(gdbDir, gdbName)
gdbPath =gdbPath +'/'
geography='Amman_zarqa'
source_dir=working_dir + '/raw/DoS/jordan_nh_shapfile/jordan_nh.shp'
# Step 2: Loading and transforming necessary layers
#-------------------------------------------------------
## (i) Projecting poligon layer for amman
#
in_data = source_dir
out_data = gdbPath + geography + '_projected'
arcpy.Project_management(in_data, out_data, spatial_reference)
grid_projected = out_data
## (ii) Transforming into the layer of centroids
#
centroids_name = geography + '_centroids'
arcpy.FeatureToPoint_management(grid_projected, gdbPath + centroids_name, "CENTROID")
centroids_projected = gdbPath + centroids_name
#
## (iii) Latitude and longitude
#
#arcpy.AddGeometryAttributes_management(centroids_projected, "POINT_X_Y_Z_M")
fieldnames = ['GRID_ID']
outfile = output_dir + "Amman_zarqa_latlon.txt"
if arcpy.Exists(outfile):
arcpy.Delete_management(outfile)
arcpy.ExportXYv_stats(centroids_name,fieldnames,"COMMA", outfile, "ADD_FIELD_NAMES")
#--------------------------------------------------------------
# Step 3: Delete unnecessary xml files
#--------------------------------------------------------------
directory = output_dir
file_list = os.listdir( directory )
for file in file_list:
if file.endswith(".xml"):
os.remove( os.path.join( directory, file ) )
In the ../input/travel_time, I can't see the file "Amman_zarqa_latlon.txt". What can be the reason for this ?
Thanks, in advance
Solved! Go to Solution.
I don't see where you are creating the dataset with the 'zarqa_nh_centroids' name in your original code so was it changed?
Going off the original code- Is 'centroids_projected' variable used anywhere? Is that supposed to be the input for the ExportXYv instead of 'centroids_name', which I don't think is in the working_dir/ env.workspace so you would have to use the complete path to it (i.e. 'centroids_projected' instead of just using the files name.
Have you tried using the debugger and seeing what arcpy.Exists() does? Does it even reach the arcpy.ExportXYv()? You could comment out the arcpy.Exists conditional to test it (or try os.path.exists() since it’s a text file your checking for and nothing specific to gis) and run the script.
Hi, JeffK. I commented out it as you said, but still I don't see any output file in the output directory.
Also, I put print() right before arcpy.ExportXTv() . it actually doesn't reach there.
Do you have any suggestion? Thanks
If its not reaching there then you need to find where the script is failing. Set a breakpoint near the beginning of the script and step through your code's execution.
I revised the code a bit, after checking indentations (corrected line 57). Now im getting the error.
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Feature Class: Dataset zarqa_nh_centroids does not exist or is not supported
Failed to execute (ExportXYv).
Do you have any idea what is going on ? Thanks!
I don't see where you are creating the dataset with the 'zarqa_nh_centroids' name in your original code so was it changed?
Going off the original code- Is 'centroids_projected' variable used anywhere? Is that supposed to be the input for the ExportXYv instead of 'centroids_name', which I don't think is in the working_dir/ env.workspace so you would have to use the complete path to it (i.e. 'centroids_projected' instead of just using the files name.
I see, I just noticed that. Your suggestion that using centroids_projected solved the problem. Thanks a lot!