How to create a shp with specific attributes from txt file

4352
14
Jump to solution
05-31-2015 11:37 AM
KONPETROV
Occasional Contributor III

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?

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

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

View solution in original post

14 Replies
OwenEarley
Occasional Contributor III

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
KONPETROV
Occasional Contributor III

the last column is just a value for the specific point not a z or an m value. Can't that be?

0 Kudos
OwenEarley
Occasional Contributor III

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.

0 Kudos
KONPETROV
Occasional Contributor III

unfortunately nothing changes

0 Kudos
KONPETROV
Occasional Contributor III

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

0 Kudos
OwenEarley
Occasional Contributor III

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
KONPETROV
Occasional Contributor III

works excellent thank you Mr. Earley

0 Kudos
SepheFox
Frequent Contributor

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.