Arcpy write geometry error

2062
10
02-16-2017 08:06 AM
DEVAPP
by
New Contributor III

Hi guys,

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[0])+"'"
    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_[0]
        irow.ID = row_[3]
        icur.insertRow(irow)

if i try the code without insert geometry works finie but if i try also to write the geometry with:

irow.Shape = row_[0]

i recive this error:

irow.Shape = row_[0]
  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???

Thanks

0 Kudos
10 Replies
IanMurray
Frequent Contributor

First off, please see this post about posting code blocks in GeoNet.

https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=9d48f...

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

DEVAPP
by
New Contributor III

Hi Murray,

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?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
by Anonymous User
Not applicable

Hey do you know how you would invoke the 'within' method? Been struggling with that one.

It's on the Geometry site, but, alas no good sample 😕 Geometry—Help | ArcGIS for Desktop 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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? .

by Anonymous User
Not applicable

Thanks bixb0012‌ 

And thanks for the blurb about cross-posting... someone told the CIO an app that was scheduled to be released in September will be done next week...

0 Kudos
by Anonymous User
Not applicable

bixb0012‌ Also, do you know if this is compatible calling feature classes? 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
by Anonymous User
Not applicable

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!

0 Kudos