Select to view content in your preferred language

Trying to create a shapefile from a Text File

821
3
06-18-2010 11:52 AM
ShaneHubbard
Deactivated User
I am trying to create a shapefile from a text file that has the X, Y coordinates and an additional field for an attribute.

Example,

42.05323833668680,-92.55533115727803,1.00
42.71293930381023,-92.20234874538450,0.75
43.52984639634028,-91.56604875561564,0.75

I have found a lot of code and examples on how to read in the X,Y pair and create the Point shapfile from the pair, but not how to also bring in the additional variable and its values.  Can someone help me out?  I am able to create a new field called maxSize without a problem, but tried to add the value using, pnt.maxSize = maxSize and that doesn't work

Here is what I have,


import sys, os, string, csv, re, arcgisscripting

gp = arcgisscripting.create()
gp.OverWriteOutput = True

# Set workspace
gp.workspace = "C:/Hail"

# Set local variables
out_path = "C:/Hail"
out_name = "points.shp"
geometry_type = "POINT"
template = "DISABLED"
has_m = "DISABLED"
has_z = "DISABLED"

#Creating a spatial reference object
spatial_reference = gp.CreateSpatialReference('C:/Program Files/ArcGIS/Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj')

#Execute CreateFeatureclass
gp.CreateFeatureclass_management(out_path, out_name, geometry_type, '#', has_m, has_z, spatial_reference)
gp.AddField_management("points.shp", "maxSize", "DOUBLE", 11, 4)
f = open("C:/Hail/30minpts.txt")
csvReader =  csv.reader(f, delimiter=',', quotechar='|')
for row in csvReader:
        xCoord = row[0]
        yCoord = row[1]
        maxSize = row[2]
        print(xCoord,yCoord,maxSize)

        cur = gp.InsertCursor(out_name)
        row = cur.NewRow()
        pnt = gp.CreateObject('POINT')
        pnt.x = xCoord
        pnt.y = yCoord
        pnt.maxSize = maxSize
        row.shape = pnt
        cur.InsertRow(row)


del cur,row,pnt,gp
f.close()
0 Kudos
3 Replies
KimOllivier
Honored Contributor
import sys, os, string, csv, re, arcgisscripting
gp = arcgisscripting.create()
gp.OverWriteOutput = True
# Set local variables
out_path = "d:/work"
out_name = "points.shp"
geometry_type = "POINT"
template = "DISABLED"
has_m = "DISABLED"
has_z = "DISABLED"
# Set workspace after variables
gp.workspace = out_path
#Creating a spatial reference object
spatial_reference = gp.CreateSpatialReference('Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj')
# use relative path for this path is an undocumented trick
#Execute CreateFeatureclass
if gp.Exists(out_name) : gp.Delete(out_name)
gp.CreateFeatureclass_management(out_path, out_name, geometry_type, '#', has_m, has_z, spatial_reference)
gp.AddField_management(out_name, "maxSize", "DOUBLE", 11, 4)
f = open("D:/work/x30minpts.txt")
# never start file or folder names with a digit, even if Windows allows it
cur = gp.InsertCursor(out_name) # open outside loop once only
pnt = gp.CreateObject('POINT') # only need one instance outside loop
csvReader = csv.reader(f) # defaults ok for example
for row in csvReader:
    xCoord = row[0] # reads as strings, not numbers
    yCoord = row[1]
    size = row[2]
    # print(xCoord,yCoord,size),type(xCoord),type(yCoord),type(size)
    row = cur.NewRow()
    pnt.x = float(xCoord)
    pnt.y = float(yCoord)
    row.maxSize = float(size) # attributes are property of the row, not the pmt
    row.shape = pnt
    cur.InsertRow(row)
del cur,row,pnt
f.close() 
0 Kudos
RDHarles
Regular Contributor
You can use these tools to create a shp from a csv while bringing the attributes along:

# Convert text (csv) to dbf
gp.TableToDbase_conversion()           

# Convert the dbf file to an Event Layer
gp.MakeXYEventLayer_management()

# Make a shapefile from the Event Layer
gp.CopyFeatures_management()

Here's some code I've used in the past to do it...

import arcgisscripting, os
gp = arcgisscripting.create()

# For each csv file
for csv in os.listdir(''):
    # Looks for the csv files
    if csv.endswith('.csv'):
        # Convert csv to dbf
        print "\nConverting "+csv+" to "+csv[:-4]+".dbf"
        gp.TableToDbase_conversion(csv, os.getcwd())            
        # Convert the csv file to an Event Layer
        print "Creating Event Layer from "+csv[:-4]+"_csv.dbf"
        gp.MakeXYEventLayer_management(csv[:-4]+"_csv.dbf", "longitude", "latitude", csv[:-4], csv[:-4]+".prj")
        # Make a shapefile from the Event Layer
        print "Creating shapefile "+csv[:-4]+".shp"
        gp.CopyFeatures_management(csv[:-4], os.getcwd()+"/"+csv[:-4]+".shp")

print "\nDone.\n"
0 Kudos
GerryGabrisch
Frequent Contributor
Have a look at Add xy coordinates found in ArcToolbox-Data Management Tools-Features.
0 Kudos