2

I have two vector layer files, 'hausnummer_cropped5' and 'gebaudepoints_cropped5 - Kopie2'. Both have a point geometry and I am trying to shift the locations of the gebaudepoints_cropped5 points onto the corresponding hausnummer_cropped5 points (there is a one-to-one correlation between them) if certain conditions are met (their original locations are slightly different). There are 68 gebaudepoints and 62 hausnummers.

The code I have written is this:

import arcpy

gebaudepoints_no = 68
hausnummer_no = 62
count = 0

with arcpy.da.UpdateCursor('hausnummer_cropped5', ['HAUSNR','STRASSE','SHAPE@XY']) as cursor1:

    for row1 in cursor1:

      with arcpy.da.UpdateCursor('gebaudepoints_cropped5 - Kopie2', ['ZA3',     'ZA2','GN5','SHAPE@XY']) as cursor2: 

         for row2 in cursor2:    

                if (row2[0] == row1[0]) and (row2[1] == row1[1]): # and (row2[2] > 0):

                  X = row1[2][0] 
                  Y = row1[2][1]  
                  count += 1 

                  cursor2.updateRow([row2[0], row2[1], row2[2], [(X),(Y)]])
                  #cursor2.deleteRow()

                  print count, gebaudepoints_no - count

There are 2 things I don't understand:

  1. After the code has been run, the value of 'count' is 92 and not 68. That means the loop has run 92 times even though it should have run only 68 times.

  2. Most of the gebaudepoints actually get placed on top of the hausnummer points, but around 5 to 6 don't, even though they meet the required conditions. However, if I use the cursor2.deleteRow() statement (which is written as a comment above), all the gebaudepoints get removed so that indicates that the code is actually going through those points as well, but it's just not shifting them.

I didn't have these two problems when I was using a different set of gebaudepoints and hausnummers so I'm wondering why this is occurring in this case.

I am using ArcGis 10.2.2

  • 1
    I would reccomend double checking your data, it's most likely that you have some duplicate or incorrect IDs that you are using for comparison between the two files that you are not expecting. Also, I think you have misunderstood what is happening with your count variable. Your inner loop is being run for every record in your outer loop for a total of 4216 (62 * 68) iterations. You have found a match 92 times, so you are getting some cases where more than one record is matching per iteration of your outer loop. – dblanchett Dec 9 '14 at 12:00
  • Yes, you are right about the number of times the loop is run. I didn't phrase that part properly. I'll double-check the data and see if there's anything wrong with those points. Thank you for your help. – Usman Dec 9 '14 at 12:25
  • I've gone through both sets of data but still can't figure out what the cause of the problem is. Those two conditions are unique between one point from the hausnummer set and its corresponding point in the gebaudepoint set. So I really shouldn't get more matches (92) than the number of points. – Usman Dec 9 '14 at 12:44
  • 1
    Try adding the following line of code after count += 1 print("Count: {0} Row 1 Data: {1},{2} Row 2 Data: {3},{4}".format(count,row1[0],row1[1],row2[0],row2[1])) – dblanchett Dec 9 '14 at 12:50
  • 1
    No problem. Just a couple of suggestions for your code. You can replace the outer UpdateCursor with a SearchCursor. It's safer since you are not changing the data in the outer cursor and it should also run faster. You can also add a where clause to your inner UpdateCursor. This will allow you to remove the if condition which will clean up the code a bit and it may run a bit faster since you will not be performing uneeded iterations. – dblanchett Dec 9 '14 at 13:12
2

First of all if I were you, I would add two counters, one for each loop. After the script finishes you should simply print them out. Also note that the counter

Secondly the outer for loop runs your inner loop that compares your data. That way your count is increased for each match the inner loop finds. If you really want to see the real count value, you should put the line of code where you increment the count variable outside the if clause.

import arcpy
gebaudepoints_no = 68
hausnummer_no = 62
count = 0

with arcpy.da.UpdateCursor('hausnummer_cropped5', ['HAUSNR','STRASSE','SHAPE@XY']) as cursor1:

for row1 in cursor1:
  count1 += 1
  with arcpy.da.UpdateCursor('gebaudepoints_cropped5 - Kopie2', ['ZA3',     'ZA2','GN5','SHAPE@XY']) as cursor2: 
     count2=0
     for row2 in cursor2:    
            count2 +=1 #this one will increase waaaay too 
                       #much so just set it to zero before the loop and you're good
                       #but also note that it will show the length of 
                       #the last item in cursor1 within the cursor2
            if (row2[0] == row1[0]) and (row2[1] == row1[1]): # and (row2[2] > 0):
              #woo! the if condition is met lets print why here
              X = row1[2][0] 
              Y = row1[2][1]  
              count += 1 

              cursor2.updateRow([row2[0], row2[1], row2[2], [(X),(Y)]])
              #cursor2.deleteRow()

print("Counter for outer loop: "+str(count1)+" and the inner one "+str(count2))

What I would add to your code is, some sort of result printing, so you can display data when your if condition is met. Then you can see what points are missing and print that data out to see why they dont match and change the if clause correctly. So to direct you into where to look on what to print, I would the check values of your row2 list maybe you can find a label to print in there.

  • I tried what you suggested but I'm getting this message: 'TypeError: object of type "da.UpdateCursor" has no len()' – Usman Dec 9 '14 at 12:26
  • 1
    Ah, that is because my knowledge of types of data you're handling with is equal to 0. I guess you should just use two counters then, one for your outer loop and one for your inner loop. Then simply print them out after your script finishes Ill fix my answer now... – Piskr Dec 9 '14 at 12:29
  • I understand. It's something that's not commonly used in normal Python applications. :) – Usman Dec 9 '14 at 12:32
  • Edit: I just included the lines you wrote so now I have an idea as to how many times the loops are running. Thanks. – Usman Dec 9 '14 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.