2

I want to convert my .txt files to shapefile. I want the system to be able to move to the next .txt file in the folder, other than me inputting one after the other. This is what I have done but I have to input the files one after the other which takes a lot of time

 import arcpy

newfcName ="532724.shp" outpath = r"C:/FLOOD/IKEJA AND KOSOFE"

Declaration

arcpy.env.overwriteOutput=True arcpy.env.workspace= outpath

Create new Shapefile and add FIELDS

newfc = arcpy.CreateFeatureclass_management(outpath, newfcName, "Point") arcpy.AddField_management(newfc, "X", "STRING", field_length = 50) arcpy.AddField_management(newfc, "Y", "STRING", field_length = 50) arcpy.AddField_management(newfc, "Z", "STRING", field_length = 50) arcpy.AddField_management(newfc, "I", "FLOAT", field_length = 50)

Reference Cursors

cursor=arcpy.da.InsertCursor(newfc, [ "SHAPE@XY", "X", "Y", "Z", "I"])

Read File

a = open("C:/FLOOD/IKEJA AND KOSOFE/DEM/532724.txt","r") inputF = a.readlines()

for line in inputF: xCoordinate, yCoordinate, zValue, iValue = line.split(" ") xy = (float(xCoordinate), float(yCoordinate)) newRow = (xy, str(xCoordinate), str(yCoordinate), str(zValue), float(iValue)) cursor.insertRow(newRow)

a.close() infc = r"C:/FLOOD/IKEJA AND KOSOFE/532724.shp" sr = arcpy.SpatialReference("Minna UTM Zone 31N") arcpy.DefineProjection_management(infc, sr)

Vince
  • 20,017
  • 15
  • 45
  • 64
lloyd
  • 160
  • 9
  • 3
    Shapefile name should not start with numbers. Please refer to this question regarding naming convention: https://gis.stackexchange.com/questions/7886/is-starting-names-with-numbers-a-bad-data-naming-convention – ahmadhanb Aug 11 '17 at 14:38
  • 2
    indent the contents of the 'for' loop. Most other languages indenting inside a loop is a convention, but with python, it's required. – P.T. Curran Oct 06 '17 at 12:41
  • 1
    Are you interested in merging the contents of multiple TXT file into a single shape file or do you want each TXT file to become its own, new shape file? If the latter, do you want the shape file to have the same name as the text file? – bixb0012 Mar 10 '22 at 18:56
  • Can you add a sample of the txt file? – BERA Mar 31 '23 at 11:15

1 Answers1

0

You just need to iterate over the files in the directory.

See the code here: https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory

Brad
  • 857
  • 4
  • 13