Hi, i have the following code:
import arcgisscripting
gp = arcgisscripting.create()
input = "c:/Borders/home.txt"
inSep = "."
output = "c:/Borders/outtext.txt"
gp.CreateFeaturesFromTextFile(input, inSep, output, "#")
And my point txt is in that form:
Point
0 8674596.8 9856351.9 100.967430980
End
But the point i am getting doesn't have the third column. Why is that happening?
Solved! Go to Solution.
This example assumes the text file is comma separated (X, Y, Value). You will also need to change the spatial reference id and output field name to suit your needs:
import arcpy, os # read data from file (x, y, custom value) row_values = [] with open(r'G:\Temp\GeoNet\Script\test.txt') as f: for line in f: tmp = line.split(',') row_values.append((float(tmp[0]),float(tmp[1]),float(tmp[2]))) # Define the shapefile shp = r'G:\Temp\GeoNet\Script\pts.shp' # Define spatial reference and create shapefile. sr = arcpy.SpatialReference(28355) arcpy.CreateFeatureclass_management (out_path = os.path.dirname(shp), out_name = os.path.basename(shp), geometry_type = "POINT", spatial_reference = sr) arcpy.AddField_management(shp, "DataField", "DOUBLE") # Use InsertCursor to update rows cursor = arcpy.da.InsertCursor(shp, ['SHAPE@X','SHAPE@Y','DataField']) for row in row_values: cursor.insertRow(row) # Clean up del cursor
Have you tried putting in a zero for the m value?
The documentation states that if the z and m values are not provided then they will be assigned a null value but the tool may be expecting none or both to be specified.
Would be a simple test:
Point
0 8674596.8 9856351.9 100.967430980 0.0
End
the 3rd column is the z value if the first is an id. You have to creat pointZ file if you want the z values within the point. I would suggest that you create the point with X and Y and assign the z as a field.
Also, separating by space would be smarter than a period
also see other suggestions here arcgis desktop - Creating shp from txt files using ArcPy? - Geographic Information Systems Stack Exc... in case someone is just starting to research a solution
the last column is just a value for the specific point not a z or an m value. Can't that be?
With the old gp.CreateFeaturesFromTextFile tool you are limited to just creating points with id, x, y, z and m values. I wouldn't bother using this old tool - getting the sample you found to work is a much better option.
unfortunately nothing changes
this is an iteresting code i 've found from (http://gis.stackexchange.com/questions/149240/creating-shp-from-txt-files-using-arcpy?noredirect=1#c... ) but for some reason is not working for my data. I am posting it here:
import arcpy, os
# Read txt file
tmp = [line.split() for line in open(r'C:\temp\txtfile.txt')]
"""
Clean txt file and prepare for Insert Cursor
Needs to be in the following format for this example:
[('60.9090909091', ('347172.2', '4269952.7')),
('85.7334909091', ('337172.8', '4278952.4')),
('123.909673091', ('344572.3', '4547952.2')),
('456.467809091', ('347532.8', '8777952.8'))]
"""
row_values = []
for t in tmp:
row_values.append((float(t[3]), (float(t[1]), float(t[2]))))
# Define the shapefile
shp = r'C:\temp\points.shp'
# Define spatial reference and create shapefile. Factory code for NAD83 UTM 15N is used here
sr = arcpy.SpatialReference(26915)
arcpy.CreateFeatureclass_management (out_path = os.path.dirname(shp), out_name = os.path.basename(shp), geometry_type = "POINT", spatial_reference = sr)
arcpy.AddField_management(shp, "Field1", "DOUBLE")
# Use InsertCursor to update rows
cursor = arcpy.da.InsertCursor(shp, ['Field1','SHAPE@XY'])
for row in row_values:
cursor.insertRow(row)
# Clean up
del cursor
This example assumes the text file is comma separated (X, Y, Value). You will also need to change the spatial reference id and output field name to suit your needs:
import arcpy, os # read data from file (x, y, custom value) row_values = [] with open(r'G:\Temp\GeoNet\Script\test.txt') as f: for line in f: tmp = line.split(',') row_values.append((float(tmp[0]),float(tmp[1]),float(tmp[2]))) # Define the shapefile shp = r'G:\Temp\GeoNet\Script\pts.shp' # Define spatial reference and create shapefile. sr = arcpy.SpatialReference(28355) arcpy.CreateFeatureclass_management (out_path = os.path.dirname(shp), out_name = os.path.basename(shp), geometry_type = "POINT", spatial_reference = sr) arcpy.AddField_management(shp, "DataField", "DOUBLE") # Use InsertCursor to update rows cursor = arcpy.da.InsertCursor(shp, ['SHAPE@X','SHAPE@Y','DataField']) for row in row_values: cursor.insertRow(row) # Clean up del cursor
works excellent thank you Mr. Earley
Maybe I'm missing something here, but can't this just be done by using the Create XY Event Layer (ArcGIS Help 10.1)? You would need to export to a shapefile after, of course.