i have develop a python script that use the cursor to read the geometry by one FC and write this geometry into temporal layer create by Make Route Event Layer.
The FC where i read geometry is a Multipoint and also my temporal layer is Multipoint. I have write this code:
field_1 = ["ID"]
field_2 = ["SHAPE@","SHAPE@X","SHAPE@Y","ID","SHAPE@XY"]
for row in arcpy.da.SearchCursor("tab_loc_lyr", field_1):
where = "ID = '" + str(row)+"'"
select = arcpy.SelectLayerByAttribute_management("loc", "NEW_SELECTION", where)
for row_ in arcpy.da.SearchCursor(select, field_2):
icur = arcpy.InsertCursor("event_lyr")
irow = icur.newRow()
irow.Shape = row_
irow.ID = row_
if i try the code without insert geometry works finie but if i try also to write the geometry with:
irow.Shape = row_
i recive this error:
irow.Shape = row_
File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
return setattr(self._arc_object, attr, ao)
RuntimeError: ERROR 999999: Error executing function.
Any hep please???
First off, please see this post about posting code blocks in GeoNet.
Secondly, it seems like you are making this far more complicated than you need it to. The first search cursor is getting you each row one at a time, so why create a feature layer using the query for each row, then have to use another cursor on a new feature with a single record, why not just retrieve all the field values you need from that row with the first cursor? Also I'm not sure your syntax for setting the values for your insert cursor are correct, row don't have any methods called Shape or ID. You would need to use the method .setValue to assign a value to that row and you have to specify which field in the method(See the help here for examples, also here for documentation on row methods).
Finally, nested cursors are quite slow(especially with geoprocessing operations are placed within them), so I recommend looking at Richard Fairhurst's blog on using dictionaries with cursors to help make processing for operations like this much quicker.
Edit: Fixed hyperlink to Richard's Blog post
thanks for your replay.
I have also try the example 'update cursor' of Richard and the script works without error but not update the geometry field. Now my question is: the SHAPE field of FC that i would update have None value, is correct use the update or i have to use insert cursor?
You are mixing and matching the two different types of cursors. Although you can technically do so, it is bound to cause readability and maintainability issues. I would start by settling on a specific cursor type, then you can work out different methodologies/approaches.
A basic example:
>>> pg = arcpy.FromWKT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))') >>> pt = arcpy.FromWKT('POINT(5 5)') >>> pt.within(pg) True >>>
UPDATE: Cross posted to How to use the geometry within function in arcpy? .
The ArcPy Geometry classes are focused around individual geometries, not sets of geometries like a feature class. If you want to work with feature classes, I suggest using the Select Layer By Location—Help | ArcGIS Desktop geoprocessing tool.
That's what I was afraid of. The boolean return is a lot more scalable for my purposes (it's getting embedded into a pop-up and will execute around fifty of these requests). The 'within' geometry seemed like a nice alternative to SQL spatial; the 'Select by Location,' when run in batch I've found, can incur some latency.
I might try to convert the vertices to an array, save that array as a variable and use the 'within' functionality. We'll see how that works...
Thanks so much!