Select to view content in your preferred language

geometry help w/ da.InsertCursor

5992
16
Jump to solution
12-29-2014 10:35 AM
KevinBell
Deactivated User

Ok, I'm stumped!  I have a list "xys" that if I do this:

xys.sort()

for i in xys:

    print i

I get this:  (id, xFrom, yFrom, xTo, yTo)

[225351, 1858897, 889223, 1883031, 882436]

[225396, 1908393, 871758, 1908090, 871791]

[225405, 1899127, 884235, 1885213, 861191]

[225423, 1891186, 878961, 1885811, 881262]

[225435, 1887101, 888042, 1894165, 884325]

[225438, 1888139, 885803, 1888050, 885319]

[225438, 1888139, 885803, 1888081, 884837]

[225441, 1885023, 888542, 1884356, 888364]

[225351, 1858897, 889223, 1883031, 882436]

and then i do this:

for j in xys:

    print j

   

    myList = []

    myList.append(arcpy.Point(j[1], j[2]))

    myList.append(arcpy.Point(j[3], j[4]))

    array = arcpy.Array(myList)

    polyline = arcpy.Polyline(array)

    cursor = arcpy.da.InsertCursor(output, ("SHAPE@"))

    cursor.insertRow((polyline,))

    print 'inserted row'

I get an empty table...  I had this working at one point.  Any obvious blunders here?

0 Kudos
16 Replies
XanderBakker
Esri Esteemed Contributor

interesting, but annoying... If I look at the help of the da.InsertCursor at 10.3:

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-data-access/insertcursor-class.htm#C_GUID-...

... I see that they use (as Richard Fairhurst‌ suggests) a list for the fields and insert the row as list. So maybe if you make some minor changes it will work in 10.3 (I can't verify this until next week):

import arcpy
fc = r"C:\Forum\DistLinePol\test.gdb\Polyline2"

xys = [[225351, 1858897, 889223, 1883031, 882436],
      [225396, 1908393, 871758, 1908090, 871791],
      [225405, 1899127, 884235, 1885213, 861191],
      [225423, 1891186, 878961, 1885811, 881262],
      [225435, 1887101, 888042, 1894165, 884325],
      [225438, 1888139, 885803, 1888050, 885319],
      [225438, 1888139, 885803, 1888081, 884837],
      [225441, 1885023, 888542, 1884356, 888364],
      [225351, 1858897, 889223, 1883031, 882436]]

with arcpy.da.InsertCursor(fc, ["SHAPE@"]) as cursor:
    for j in xys:
        array = arcpy.Array()
        array.add(arcpy.Point(j[1], j[2]))
        array.add(arcpy.Point(j[3], j[4]))
        polyline = arcpy.Polyline(array)
        cursor.insertRow([polyline])
0 Kudos
DanielSmith
Frequent Contributor

inserts worked for me at 10.3.

2014-12-29_12-24-39.png

refresh in catalog maybe?

are the coordinates posted way out of bounds for the coordinate system?

0 Kudos
RichardFairhurst
MVP Honored Contributor

I agree with Daniel that the code Xander posted (using tuples or lists) works with Python 2.7.8 and ArcMap 10.3.  I get the same results he showed.  The coordinates work even with my default state plane coordinate system which is not really designed for this data.

Sounds like a reboot is in order if it is not working.

0 Kudos
KevinBell
Deactivated User

Reinstalling now.  My upgrade must have gone wrong.

Thanks everyone!  The application here's pretty cool.  This will be like a live spider diagram showing police officers converging on a call for assistance.

0 Kudos
DanielSmith
Frequent Contributor

cool. share a final output when you can .

0 Kudos
KevinBell
Deactivated User

Ok, my police "calls for service" map is done with lines showing who's assigned to whatever call!  Thanks for your help everyone.  (this refreshes every 30 seconds)

rtcm.PNG

XanderBakker
Esri Esteemed Contributor

Cool! Nice Work!

0 Kudos