4

I have a table imported from Excel that has three attribute columns (City, State, ZIP) as well as X/Y columns that have been added.

I have a cursor that goes through the records in the table and creates points

point = arcpy.Point()
pointGeometryList = []

with arcpy.da.SearchCursor("in_memory/xxExcelOutput", ["POINT_X", "POINT_Y", "CITY", "STATE", "ZIP_CODE"]) as cursor:
    for row in cursor:
        point.X = row[0]
        point.Y = row[1]

        pointGeometry = arcpy.PointGeometry(point)
        pointGeometryList.append(pointGeometry)

arcpy.CopyFeatures_management(pointGeometryList, "in_memory/pointFC")

How can I add the attributes from my table when I create the points? I know the following doesn't work, but is there a way to do it within the same cursor?

    for row in cursor:
        point.X = row[0]
        point.Y = row[1] 
        point.ZIP = row[4]

The only way I've come up with so far is to AddField() to the new feature class, and then UpdateCursor() the extra attributes with another search cursor. While this way works OK for my test data (I have some data in my table, for proof of concept), I am concerned that I may end up with multiple points in the same location that have different attributes - I need to ensure that each point is created with the matching attributes.

  • Is there a specific reason to use arcpy for this instead of the standard "Add X and Y coordinates" function? That would retain all fields. Another arcpy alternative would be to compare X & Y values of the new pts layer to the excel and use an update cursor as you said where they match. – GISKid Mar 20 '17 at 17:20
  • @GISKid Has to be automated - ArcMap will not be opened. Your alternative is along the lines of how I'm doing it currently as per my "the only way..." comment at the end of my question. I'll add a little more information to my question to clarify. – Midavalo Mar 20 '17 at 17:25
  • 1
    Since it's point data, you could use numpy2FC. – Paul Mar 21 '17 at 5:14
  • @Paul I'm not familiar with NumPy to FC - the doc you linked to doesn't seem to indicate that other fields are possible – Midavalo Mar 21 '17 at 5:34
  • 1
    Other fields are possible, see the code sample. Here for more info on using numpy. – Paul Mar 21 '17 at 6:38
2

Okay I not sure if this is the answer that you want. but if you are not bound to using an excel sheet, just simply convert your file to csv and read it with the csv module.

import csv
import arcpy
import os
arcpy.env.overwriteOutput = True
gdb = r'gdb'
p = r'path\\' #for path
fcname = "nameyouwant"
centers  = arcpy.CreateFeatureclass_management(gdb,fcname,"POINT",'','',"ENABLED",4326)
arcpy.AddField_management(fcname,"City","TEXT")
arcpy.AddField_management(fcname,"State","TEXT")
arcpy.AddField_management(fcname,"Zip","TEXT")
arcpy.AddField_management(fcname,"X","TEXT")
arcpy.AddField_management(fcname,"Y","TEXT")
with open(os.path.join(p,'csvname.csv')) as noury:
    reader = csv.DictReader(noury)
    for row in reader:
        point = arcpy.PointGeometry(arcpy.Point(row["POINT_X"],row["POINT_Y"]),arcpy.SpatialReference(4326))
        #projected = point.projectAs(arcpy.SpatialReference(SRID)) if you want to project
        with arcpy.da.InsertCursor(fcname,("City","State","Zip","X","Y","SHAPE@")) as cur:
            sert = (row['CITY'],row['STATE'],row['ZIP_CODE'],row["POINT_X"],row["POINT_Y"],point) #or use the projected point if you want it projected
            cur.insertRow(sert)
  • truth is you can probably use the search cursor on the excel table, just modify some of my code – ziggy Mar 20 '17 at 18:48
  • 1
    My table has been pulled from Excel already - the cursors in my question don't touch Excel. That said, I think I can see what you're doing there with the insert cursor at the end. Instead of creating the point first and then creating the feature class of the points, I need to switch it around and create empty feature class (with relevant fields) and then add the points and attributes to it. I'll give it a go! – Midavalo Mar 20 '17 at 18:55
  • oh gotcha, yeah this should work. Its easier to just insert the rows in the loop so your not trying to store them in a list,variable or dictionary then reloop through that to create the feature class or shapefile – ziggy Mar 20 '17 at 18:59
  • 1
    The last 5 lines of your code sample are what worked for me - point = arcpy.PointGeometry() then a Insert Cursor and Search Cursor, inserting the attributes and the point at the same time. Thanks @ziggy – Midavalo Mar 22 '17 at 12:57
1

Work around without cursors:

table ='C:/Users/Downloads/test.xlsx'
arcpy.ExcelToTable_conversion(table,"table2.dbf")
table2='C:/Users/Documents/ArcGIS/table2.dbf'
arcpy.MakeXYEventLayer_management("table2","POINT_X","POINT_Y","table2_layer")
  • Sounds too simple! Unfortunately it is only creating records without geometry... I'll keep playing to see if I can find the cause. Thanks for the suggestion – Midavalo Mar 20 '17 at 18:56
  • Strange, I tested with a dummy excel file with made up integers for POINT_X and Y. Maybe try setting a spatial reference in MakeXYEventLayer_Management if the data is projected? – GISKid Mar 20 '17 at 19:25
  • There may be more wrong with my data, so I'll look further into that. There is a lot of clean-up that needs to happen on it, so I may have missed something. I'll let you know if I find it. – Midavalo Mar 20 '17 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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