Select to view content in your preferred language

Insert Cursor placing points from dictionary on top of each other

1577
9
Jump to solution
01-10-2018 11:06 AM
JosephRoberts4
Deactivated User

I am trying to push xy locations that are being pulled from a dictionary into an insert cursor.  The cursor is reading the xy locations correctly but is putting them all in the same location instead of placing them in the actual location when pulled into a map.

What I have tried:

1. changing the SHAPE@XY - all still come in the same location on top of each other

2. removing out of try statement - all still come in the same location on top of each other

3. removing edit session - returns error saying that I have to use one

I have attached a test GDB that includes the test data. To run code, all you have to do is change the location to where ever you save the GDB.  If you have any solution, PLEASE RESPOND

Here is the my code:

import arcpy

#Trigger: When it sees a change in date, it will then add frequency to that date
#What needs to be populated: compound, maintenance task, and park

#Attributes for Each Maintenance Task feature
CompAttribs = {
#Compound Name : [Location of Point, Compound, Park]
"Mississippi Shelter": [(751925.962298,279303.484179) ,"Mississippi Shelter", "Walter Chandler"],
"Willow Road": [(815946.106614, 815946.106614), "Willow Road", "Hickory Hill Park"],
"MLK Riverside": [(747845.301725, 301161.452951),"MLK Riverside", "King"],
"City Center Armory":[(771656.687393, 309099.380722),"City Center Armory", "Glenview"],
"Armory":[(791820.996015, 313373.492286),"Armory","Galloway"],
"Kennedy":[(800029.764326, 336054.100493),"Kennedy","Kennedy"],
"Frayser":[(769122.906674, 359343.580918),"Frayser","Firestone"]

}

MaintFreq = {
"Empty Trash Receptacle": ["Maint",2],
"Inventory Report": ["Maint",30],
"Leaf Mulching": ["Maint",14,],
"Litter Pick Up":["Maint",2],
"Mow Edge Trim Blow": ["Maint",1],
#"Preventative Maint on Equipment": 1,
"Pruning Hedges": ["Maint",14],
"Safety Assessment": ["MaintOps",30],
"Asset Inspection": ["MaintOps",30],
"Playground Inspection": ["MaintOps",30]

}

arcpy.env.workspace =r"C:\\Users\\josephr.roberts\\Documents\\ArcGIS\\JR_Working\\TestDatabase_1.gdb"
workspace = r"C:\\Users\\josephr.roberts\\Documents\\ArcGIS\\JR_Working\\TestDatabase_1.gdb"

fc = r"C:\\Users\\josephr.roberts\\Documents\\ArcGIS\\JR_Working\\TestDatabase_1.gdb\\MaintenanceRequest_1"
Fields = ["SHAPE@XY","Compound_Name","Maintenance_Task","Maintenance_Task_MandO"]


#Opening Editing Session
print "Editing Session Opened"
edit = arcpy.da.Editor(workspace)
edit.startEditing(True, False)
edit.startOperation()

try:
   newMaintTask = arcpy.da.InsertCursor(fc,Fields)
   for Comps in CompAttribs:
      for Task in MaintFreq:
         if MaintFreq[Task][0] == "Maint":
            newMaintTask.insertRow([CompAttribs[Comps][0],CompAttribs[Comps][1],Task,""])
         else:
            newMaintTask.insertRow([CompAttribs[Comps][0],CompAttribs[Comps][1],"",Task])


del newMaintTask

print "point added"

except Exception, e:
   print "Unexpected error:", sys.exc_info()[0]
   raise

edit.stopOperation()
edit.stopEditing(True)

0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

I deleted the relationship class to the attached table, and the following code worked for me.  I haven't tested it with the relationship intact. 

import arcpy

#Trigger: When it sees a change in date, it will then add frequency to that date
#What needs to be populated: compound, maintenance task, and park

#Attributes for Each Maintenance Task feature
CompAttribs = {
"Mississippi Shelter": [(751925.962298,279303.484179) ,"Mississippi Shelter", "Walter Chandler"],
"Willow Road": [(815946.106614, 815946.106614), "Willow Road", "Hickory Hill Park"],
"MLK Riverside": [(747845.301725, 301161.452951),"MLK Riverside", "King"],
"City Center Armory":[(771656.687393, 309099.380722),"City Center Armory", "Glenview"],
"Armory":[(791820.996015, 313373.492286),"Armory","Galloway"],
"Kennedy":[(800029.764326, 336054.100493),"Kennedy","Kennedy"],
"Frayser":[(769122.906674, 359343.580918),"Frayser","Firestone"]}
 
MaintFreq = {
"Empty Trash Receptacle": ["Maint",2],
"Inventory Report": ["Maint",30],
"Leaf Mulching": ["Maint",14,],
"Litter Pick Up":["Maint",2],
"Mow Edge Trim Blow": ["Maint",1],
"Pruning Hedges": ["Maint",14],
"Safety Assessment": ["MaintOps",30],
"Asset Inspection": ["MaintOps",30],
"Playground Inspection": ["MaintOps",30]}
 
 
arcpy.env.workspace =r""
workspace = r""

fc = r""
Fields = ["SHAPE@XY","Compound_Name","Maintenance_Task","Maintenance_Task_MandO"]
 

#Opening Editing Session
try:
    print "Editing Session Opened"
    edit = arcpy.da.Editor(workspace)
    edit.startEditing(True, False)
    edit.startOperation()

    newMaintTask = arcpy.da.InsertCursor(fc,Fields)
    for Comps in CompAttribs:
      for Task in MaintFreq:
         if MaintFreq[Task][0] == "Maint":
             row = [CompAttribs[Comps][0],CompAttribs[Comps][1],Task,""]
             #print row
             newMaintTask.insertRow(row)
         elif MaintFreq[Task][0] == "MaintOps":
             row1 = [CompAttribs[Comps][0],CompAttribs[Comps][1],"",Task]
             #print row1
             newMaintTask.insertRow(row1)

except Exception as e:
    print e

finally:
    edit.stopOperation()
    edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

9 Replies
DanPatterson_Retired
MVP Emeritus

I am not finding anything that resembles this in your code

with arcpy.da.InsertCursor(fc, ('SHAPE@', 'Name')) as icur:
    icur.insertRow([(7642471.100, 686465.725), 'New School'])

are your following an example in the arcpy.da.Editor help?

0 Kudos
JosephRoberts4
Deactivated User

Hi Dan, 

Thanks for your response.  

The parameters of the insert cursor are being pulled from the first dictionary.  What I want is 9 points for each of the 8 locations. 

0 Kudos
MitchHolley1
MVP Regular Contributor

I don't understand what's going on in this code snippet.  You're iterating over two dictionaries, but they don't seem to be related at all.  

try:
   newMaintTask = arcpy.da.InsertCursor(fc,Fields)
   for Comps in CompAttribs:
      for Task in MaintFreq:
         if MaintFreq[Task][0] == "Maint":
            newMaintTask.insertRow([CompAttribs[Comps][0],CompAttribs[Comps][1],Task,""])
         else:
            newMaintTask.insertRow([CompAttribs[Comps][0],CompAttribs[Comps][1],"",Task])

I assume the output would be nine points (nine iterations of the MainFreq dict) at each X,Y from CompAttribs.  

Can you explain how these two dictionaries are related?

0 Kudos
JosephRoberts4
Deactivated User

To explain the relationship, each compound(CompAttribs) has 9 task(MaintTask) that need to be executed on a regular basis. 

0 Kudos
JosephRoberts4
Deactivated User

Hi Molly,

Thanks for your response.

Thats exactly what I am trying to do.  But I need nine points that are on top of each other for 8 different locations. 

0 Kudos
JosephRoberts4
Deactivated User

My code puts all 63 points on top of each other in one location

0 Kudos
DanPatterson_Retired
MVP Emeritus

I guess I am not seeing where the coordinates are going into the Shape field as in the example

0 Kudos
MitchHolley1
MVP Regular Contributor

I deleted the relationship class to the attached table, and the following code worked for me.  I haven't tested it with the relationship intact. 

import arcpy

#Trigger: When it sees a change in date, it will then add frequency to that date
#What needs to be populated: compound, maintenance task, and park

#Attributes for Each Maintenance Task feature
CompAttribs = {
"Mississippi Shelter": [(751925.962298,279303.484179) ,"Mississippi Shelter", "Walter Chandler"],
"Willow Road": [(815946.106614, 815946.106614), "Willow Road", "Hickory Hill Park"],
"MLK Riverside": [(747845.301725, 301161.452951),"MLK Riverside", "King"],
"City Center Armory":[(771656.687393, 309099.380722),"City Center Armory", "Glenview"],
"Armory":[(791820.996015, 313373.492286),"Armory","Galloway"],
"Kennedy":[(800029.764326, 336054.100493),"Kennedy","Kennedy"],
"Frayser":[(769122.906674, 359343.580918),"Frayser","Firestone"]}
 
MaintFreq = {
"Empty Trash Receptacle": ["Maint",2],
"Inventory Report": ["Maint",30],
"Leaf Mulching": ["Maint",14,],
"Litter Pick Up":["Maint",2],
"Mow Edge Trim Blow": ["Maint",1],
"Pruning Hedges": ["Maint",14],
"Safety Assessment": ["MaintOps",30],
"Asset Inspection": ["MaintOps",30],
"Playground Inspection": ["MaintOps",30]}
 
 
arcpy.env.workspace =r""
workspace = r""

fc = r""
Fields = ["SHAPE@XY","Compound_Name","Maintenance_Task","Maintenance_Task_MandO"]
 

#Opening Editing Session
try:
    print "Editing Session Opened"
    edit = arcpy.da.Editor(workspace)
    edit.startEditing(True, False)
    edit.startOperation()

    newMaintTask = arcpy.da.InsertCursor(fc,Fields)
    for Comps in CompAttribs:
      for Task in MaintFreq:
         if MaintFreq[Task][0] == "Maint":
             row = [CompAttribs[Comps][0],CompAttribs[Comps][1],Task,""]
             #print row
             newMaintTask.insertRow(row)
         elif MaintFreq[Task][0] == "MaintOps":
             row1 = [CompAttribs[Comps][0],CompAttribs[Comps][1],"",Task]
             #print row1
             newMaintTask.insertRow(row1)

except Exception as e:
    print e

finally:
    edit.stopOperation()
    edit.stopEditing(True)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

JosephRoberts4
Deactivated User

Thanks Molly, 

That fixed it.  

0 Kudos