I have a destination (Feature Class):
C:/Users/XXX/Documents/ArcGIS/ABC.gdb/SamplePoints
I have a database table in the same file geodatabase that contains my source point data. I have columns with attributes in addition to the requisite coordinate data.
Using Arcpy, how do I retrieve the points from the source database table and create points in the destination Feature Class?
I tried creating a single point using this code:
import arcpy
cur = arcpy.InsertCursor(fc, ["SHAPE@XY"])
pnt = arcpy.Point()
pnt.x = 20000
pnt.y = 10000
feat = cur.newRow()
feat.shape = pnt
cur.insertRow(feat)
del cur
but, although a row is created, the coordinates are 0,0.
Current thoughts - I'll probably use a cursor to step through the records retrieved and for each row in the source, build a point and insert it into the destination. The logic seems to make sense.
3 Answers 3
I am not sure whether nested cursors (e.g. a SearchCursor for the source table and an InsertCursor for the destination feature class) will work, but it's worth a shot.
Specific to your problem of how to get geometry data into a feature class using an InsertCursor:
Use a da.InsertCursor (since you have access to 10.1+)
Take a look at the Help page for arcpy.da.InsertCursor, particularly the examples. I think that you are making it more complex than necessary.
This is their example. No geometry objects needed, just the numeric values.
import arcpy
# A list of values that will be used to construct new rows
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)),
('Andrews', (752000.2489000037, 1128929.8114))]
# Open an InsertCursor
cursor = arcpy.da.InsertCursor("C:/data/texas.gdb/counties", ("NAME", "SHAPE@XY"))
# Insert new rows that include the county name and a x,y coordinate
# pair that represents the county center
for row in row_values:
cursor.insertRow(row)
-
I could not get the two nested cursor approach to work. I tried but I'll admit I didn't spend much time trying to figure it out. In the end, I used a SearchCursor to populate an array and then an InsertCursor to insert the items from the array. It works and for 20K records, it finishes in < 3 seconds.DenaliHardtail– DenaliHardtail2015年02月04日 14:50:33 +00:00Commented Feb 4, 2015 at 14:50
-
An array for storing data is my usual go-to method, but it can be slightly cumbersome with many attributes to transfer. Glad you got it to work :)Erica– Erica2015年02月04日 16:04:35 +00:00Commented Feb 4, 2015 at 16:04
I think you would be better off using the Make XY Event Layer and then use Copy Features to export it to a new feature class.
import arcpy
def xy_table_to_fc(table, x, y, wkid, out_fc):
lyr = str(arcpy.management.MakeXYEventLayer(table, x, y, 'temp', wkid).getOutput(0))
arcpy.management.CopyFeatures(lyr, out_fc)
if __name__ == '__main__':
table = r'C:\TEMP\Headstones_table.dbf'
out = r'C:\TEMP\Headstone_points.shp'
xy_table_to_fc(table, 'long', 'lat', 103793, out)
You're just mixing up your cursors a bit. Some of your code is from the old cursor type. Some is from the new data analysis type.
"SHAPE@XY" isn't supported in the old version of an InsertCursor.
Use da.InsertCursor
instead of the old version and...
Instead of:
feat.shape = pnt
Try:
feat[0] = (20000, 10000)