Fourth update, per suggestions from comments and professor. Still getting the "takes no arguments" error "Traceback (most recent call last): File "C:\Pythonpro\pythonProject2\anothercreateschools.py", line 19, in with arcpy.da.InsertCursor(schpts, ["SHAPE@", "ID", "LONGITUDE", "LATITUDE", "NAME", "ADDRESS", "ZIP", "DISTRICT"]) as cursor: TypeError: InsertCursor() takes no arguments"
updated script
import arcpy
import fileinput
import os
wrkspce = "C:/lab11/pt2"
arcpy.env.workspace = wrkspce
arcpy.env.overwriteOutput = True
schpts = "schools.shp"
sr = arcpy.SpatialReference(102649)
sr2 = arcpy.SpatialReference("WGS 1984")
arcpy.CreateFeatureclass_management(wrkspce, schpts, "Point", spatial_reference=sr)
infi = os.path.join(wrkspce, "schools.txt")
arcpy.management.AddField(schpts,"ID")
arcpy.management.AddField(schpts,"LONGITUDE", "DOUBLE")
arcpy.management.AddField(schpts,"LATITUDE", "DOUBLE")
arcpy.management.AddField(schpts,"NAME","TEXT",field_length = 50)
arcpy.management.AddField(schpts,"ADDRESS","TEXT",field_length = 50)
arcpy.management.AddField(schpts,"ZIP","TEXT",field_length = 10)
arcpy.management.AddField(schpts,"DISTRICT")
with arcpy.da.InsertCursor(schpts, ["SHAPE@", "ID", "LONGITUDE", "LATITUDE", "NAME", "ADDRESS", "ZIP", "DISTRICT"]) as cursor:
for line in fileinput.input(infi):
if "ID" in line:
continue
else:
mysplit = line.split(",")
ID = (mysplit[0])
XCoordinate = (mysplit[1])
YCoordinate = (mysplit[2])
NAME = (mysplit[3])
ADDRESS = (mysplit[4])
ZIP = (mysplit[5])
DISTRICT = (mysplit[6])
point = arcpy.Point(XCoordinate, YCoordinate)
pointgeom = arcpy.PointGeometry(point, sr2)
newRow = (pointgeom, ID, XCoordinate, YCoordinate, NAME, ADDRESS, ZIP, DISTRICT) # the number and order of values must match fields in the list you specify in the cursor
cursor.insertRow(newRow)
Third update per my professors edits, Throwing "TypeError: InsertCursor() takes no arguments"
import arcpy
import fileinput
import os
wrkspce = "C:/lab11/pt2"
arcpy.env.workspace = wrkspce
arcpy.env.overwriteOutput = True
schpts = "schools.shp"
sr = arcpy.SpatialReference(102649)
sr2 = arcpy.SpatialReference("WGS 1984")
arcpy.CreateFeatureclass_management(wrkspce,schpts,spatial_reference=sr)
infi = os.path.join(wrkspce, "schools.txt")
arcpy.management.AddField(schpts,"ID")
arcpy.management.AddField(schpts,"NAME","TEXT",field_length = 50)
arcpy.management.AddField(schpts,"ADDRESS","TEXT",field_length = 50)
arcpy.management.AddField(schpts,"ZIP","TEXT",field_length = 10)
arcpy.management.AddField(schpts,"DISTRICT")
cursor = arcpy.da.InsertCursor(schpts,["ID","SHAPE@","LONGITUDE","LATITUDE","NAME","ADDRESS","ZIP","DISTRICT"])
with cursor:
for line in fileinput.input(infi):
mysplit = line.split(",")
ID = float(mysplit[0])
XCoordinate = float(mysplit[1])
YCoordinate = float(mysplit[2])
NAME = float(mysplit[3])
ADDRESS = float(mysplit[4])
ZIP = float(mysplit[5])
DISTRICT = float(mysplit[6])
point = arcpy.Point(XCoordinate, YCoordinate)
pointgeom = arcpy.PointGeometry(point, sr2)
newRow = (pointgeom, ID,XCoordinate,YCoordinate,NAME,ZIP,DISTRICT)
cursor.insertRow(newRow)
del cursor
Here is my updated script per suggestions, I am still pretty lost.
import arcpy, fileinput
wrkspce = "C:/lab11/pt2"
arcpy.env.workspace = wrkspce
arcpy.env.overwriteOutput = True
schpts = "schools.shp"
txt = "C:/lab11/pt2/schools.txt"
sr = arcpy.SpatialReference(102649)
arcpy.CreateFeatureclass_management(wrkspce,schpts,spatial_reference=sr)
#sr2 = arcpy.SpatialReference("WGS 1984") # GCS_WGS_1984, factory code 4326
cursor = arcpy.da.InsertCursor(schpts, ["SHAPE@"]) # creates an insert cursor
for line in fileinput.input(txt): # open the file
if 'ID' in line: # if the line is the file header
continue # do nothing and continue to next iteration
else: # if the line is a data line
point = arcpy.Point() # creates a point object
ID, point.X, point.Y,NAME, ADDRESS, ZIP, DISTRICT = line.split(",") # sets point coordinates
pointgeom = arcpy.PointGeometry(point, sr) # makes a PointGeometry
cursor.insertRow([pointgeom]) # inserts a new row. note a row is a list [ ]
arcpy.management.AddField(schpts, ID, "SHORT")
arcpy.management.AddField(schpts, NAME, "TEXT", field_length=50)
arcpy.managment.AddField(schpts, ADDRESS, "TEXT", field_length=50)
arcpy.managment.AddField(schpts, ZIP, "TEXT", field_length=10)
arcpy.management.AddField(schpts, DISTRICT, "SHORT")
# close the GPS file and perform clean up
fileinput.close()
del point
del pointgeom
del cursor
I am working on a lab right now and very lost on how to move forward. we need to create a shapefile from the following text file
ID,LONGITUDE,LATITUDE,NAME,ADDRESS,ZIP,DISTRICT
1,-111.585741,35.225927,Christensen Elementary School,4000 N. Cummings St,86004,9
2,-111.522826,35.269495,Cromer Elementary School,7150 E Silver Saddle Rd,86004,8
.....
Two things our professor wanted us to consider. I think I managed the first but not the second.
When you create a blank shapefile, make sure it is assigned the spatial reference of NAD 1983 SPCS for AZ Central Zone FIPS 0202. b. When you create a point geometry for a school, make sure it is assigned the spatial reference of WGS 1984.
The output shapefile must have all the attributes shown in the text file. Note that when you create a blank shapefile using the
arcpy.management.CreateFeatureclass()tool, the output shapefile only has three fields: FID, Shape, and Id. You will store the school IDs in the Id field. But you must use thearcpy.management.AddField()tool to add the following additional fields into the new shapefile after creating it:
Here is the code I have so far. I do not know what more I can do.
import arcpy
import fileinput
import os
wrkspce = "C:/lab11/pt2"
arcpy.env.workspace = wrkspce
arcpy.env.overwriteOutput = True
schpts = "schools.shp"
sr = arcpy.SpatialReference(102649)
#sr2 = arcpy.SpatialReference(4356)
arcpy.CreateFeatureclass_management(wrkspce,schpts,spatial_reference=sr)
infi = os.path.join(wrkspce, "schools.txt")
arcpy.management.AddField(schpts,"ID","SHORT")
arcpy.management.AddField(schpts,"NAME","TEXT",field_length = 50)
arcpy.managment.AddField(schpts,"ADDRESS","TEXT",field_length = 50)
arcpy.managment.AddField(schpts,"ZIP","TEXT",field_length = 10)
arcpy.management.AddField(schpts,"DISTRICT","SHORT")
cursor= arcpy.da.InsertCursor(schpts,["ID","LONGITUDE","LATITUDE","NAME","ADDRESS","ZIP","DISTRICT"])
#cursor = arcpy.da.InsertCursor(schpts,"shape@XY")
for line in fileinput.input(infi):
mysplit = line.split(",")
ID = float(mysplit[0])
XCoordinate = float(mysplit[1])
YCoordinate = float(mysplit[2])
NAME = float(mysplit[3])
ADDRESS = float(mysplit[4])
ZIP = float(mysplit[5])
DISTRICT = float(mysplit[6])
newRow = (ID,XCoordinate,YCoordinate,NAME,ZIP,DISTRICT)
cursor.insertRow(newRow)
I know I am probably adding too many fields and some could be the ones that are in the shape file, but I do not know how to rename them and fill them in properly per the instructions.
