1

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 the arcpy.management.AddField() tool to add the following additional fields into the new shapefile after creating it:

enter image description here

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.

1
  • You have too many code blocks here. It's extremely difficult to see what you are trying to do, much less formulate an answer.
    – Vince
    Commented Nov 27, 2022 at 21:22

1 Answer 1

1

You are very close, good start.

You have point coordinates in EPSG:4326 (WGS84). You need to create a Point object from the X (LONGITUDE) and Y (LATITUDE) fields, add the Point and the appropriate SpatialReference to a PointGeometry object, then insert that geometry using the appropriate shape token (plus the other fields). Hint - it's not shape@xy (look for "geometry" in the list of shape tokens).

For bonus points, you could also specify an appropriate datum transformation to transform from WGS84 to NAD83.

Re. your first update,

  1. # sr2 = arcpy.SpatialReference(4356) should uncommented and the correct code is 4326 not 4356, e.g. sr2 = arcpy.SpatialReference(4326).
  2. You are trying to add fields inside the loop. Don't. Add the fields straight after you create the featureclass, then loop.
  3. In your updated arcpy.da.InsertCursor you haven't included any of the required fields e.g ["SHAPE@","NAME","ADDRESS","ZIP","DISTRICT"]
  4. You've assigned the wrong spatial reference to the PointGeometry it's WGS84 (i.e. sr2)
  5. You forgot to insertRow

Re. your 2nd update - with cursor: is wrong. Instead use:

cursor = arcpy.da.InsertCursor(schpts,["SHAPE@", "ID","LONGITUDE","LATITUDE","NAME","ADDRESS","ZIP","DISTRICT"])
for line in fileinput.input(infi):
    etc...

Note I reversed ID and shape@ above because you used that order in this line:

newRow = (pointgeom,ID,XCoordinate,YCoordinate,NAME,ZIP,DISTRICT)
5
  • As this is a school work question, I can't provide the code. You need to figure it out, but I will edit my answer shortly to address some issues in your update.
    – user2856
    Commented Nov 27, 2022 at 3:30
  • my professor sent me an update as well. But I keep throwing an "InsertCursor() takes no arguments, I have no idea how to rectify other than that my code is cleaner. now I can post that update as well
    – PieCharmer
    Commented Nov 27, 2022 at 3:44
  • my professor sent another update, similar to yours, yet I am still getting this error no matter what. Traceback (most recent call last): File "C:\Pythonpro\pythonProject2\anothercreateschools.py", line 19, in <module> with arcpy.da.InsertCursor(schpts, ["SHAPE@", "ID", "LONGITUDE", "LATITUDE", "NAME", "ADDRESS", "ZIP", "DISTRICT"]) as cursor: TypeError: InsertCursor() takes no arguments
    – PieCharmer
    Commented Nov 27, 2022 at 5:40
  • thanks again for your help. I think this is close but it still is failing, I can't find much info about the error that is being thrown
    – PieCharmer
    Commented Nov 27, 2022 at 5:44
  • 1
    Try it without the with (the doc examples don't use with)
    – user2856
    Commented Nov 27, 2022 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.