Hello folks,
So I've actually got a couple issues here, and some help would be greatly appreciated as I've been working on this for a few days now and it needs to be wrapping up. I'm still learning python and am admittedly not very good yet.
So basically, this is a script that should:
1) Look at a point that represents an unverified well location on a map
2) Select all centroids within a given distance of that point
3) Compare the last names of centroid owners and well owners to see if any of them match
4) If there is a match, write "YES" in a column called match
5) Move on to the next well and repeat
Some issues I've run into:
1) I was having trouble making the "select by location" function work. I had a search cursor and the arcpy.SelectLayerByLocation_management acting on the row in the cursor, but it would instead select all centroids nearest all wells, and then iterate to the next well record and do the same again. I got it to select from one well at a time using advice on this forum but then it started deleting rows in my attribute tables, which leads to..
2) The title problem.
Can any kind soul lend me a hand?
***EDIT***
While I haven't figured out why things were being deleted, I did determine that if I "select all" in the wells1 feature layer and then save as a new featureclass, the attribute tables in all of my layers (wells, wells1 and the new output) all remain intact. I'm not sure why this works, but I'll take it for now. My only remaining issue is that my for loops with the cursors exit after two iterations, one "if" and one "else"; because I do get a single YES in my match column, and the "print owner, orpsowner" is only called once before the script selects and saves.
Here is my full, updated script:
import arcpy import datetime current_time = datetime.datetime.now().time() print current_time arcpy.env.overwriteOutput = 1 # define a workspace arcpy.env.workspace = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb" # Define input files orps09 = r"C:\Users\tmc18\Desktop\comp_orps\centroids\madirps_point1.shp" wells = r"C:\Users\tmc18\Desktop\comp_orps\NYS_Wells.gdb\Madison_DEC_Well_Logs_3_14_14_MASTER_COPY1" # Make a feature layer arcpy.MakeFeatureLayer_management(orps09, "orps09_FL") arcpy.MakeFeatureLayer_management(wells, "wells1") wells1 = "wells1" # Create dictionary of last names of all well owners well_owners = {} rows = arcpy.da.SearchCursor(wells1, ["OBJECTID", "OWNER_L"]) for row in rows: well_owners[row[0]] = str(row[1]) del rows # Create search cursor which will iterate through wells with arcpy.da.SearchCursor(wells1, ["OBJECTID"]) as cursor: for row in cursor: # set well owner name for this record record = row[0] owner = well_owners[record] # select by attribute arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID = {}".format(row[0])) # select by location arcpy.SelectLayerByLocation_management("orps09_FL", "WITHIN_A_DISTANCE", wells1, "0.5 kilometers", "NEW_SELECTION") # create search cursor which will iterate through selected orps owners with arcpy.da.SearchCursor("orps09_FL", ["OWNER_L"]) as orpscur: for row in orpscur: # set orps owner name orpsowner = row[0] # compare owner's names if owner != orpsowner: pass print owner, orpsowner else: print "YES" # select the row that the main well cursor is currently on arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID = {}".format(record)) # update the match field with arcpy.da.UpdateCursor(wells1, ["match"]) as cur: for row in cur: row[0] = "YES" cur.updateRow(row) del cur # clear selection arcpy.SelectLayerByAttribute_management(wells1,"CLEAR_SELECTION") break # if the well cursor has exhausted the list and the .next() method returns a stop iteration... try: error = cursor.next() except StopIteration: # select all features in the FL and save to a new featureclass arcpy.SelectLayerByAttribute_management(wells1,"NEW_SELECTION","OBJECTID >= 1") arcpy.CopyFeatures_management(wells1, "Madison_well_logs_match") arcpy.SelectLayerByAttribute_management(wells1,"CLEAR_SELECTION") print current_time
The 'u' in front of the string values means the string has been represented as unicode. It is a way to represent more characters than normal ascii can manage.
Source: python - What does the 'u' symbol mean in front of string values? - Stack Overflow
Can you explain the title problem to me (problem 3)? I am a little confused what you mean by this.
What I mean by problem 3 is that literally, once it hits the area past the line of ####, all of the rows in both my original "wells" file, as well as in the feature layr simply disappear. If I run the script up to before that point, I get two feature layers that contain the full set of data included within both of the original files. However, after I let it run further than that, every row, all of the data in both of my "wells" (featureclass and feature layer) is gone. When I open the attribute table, it is simply empty.
One more thing, is the else loop ever hit when you run this? Line 44 should have YES in quotes like it is on line 47.
When I queried the dictionary, I recieved the u'LASTNAME' notation, which I thought was stopping a match, but as the script progressed it did in fact hit the else loop, so I guess the dictionary thing isn't an issue anymore. And then I fixed that "YES". So now I'm just trying to get it to stop deleting all of my data, lol.
I will have to look at the script more later, but a quick response to your #1. What is it about u'LASTNAME' that you see as wrong? Is it all caps and it shouldn't be? Is it the Unicode notation in front of the first quote? If it is the latter, you have nothing to worry about. The 'u' isn't part of the actual string, it is simply informing you that the string has been stored as Unicode. You shouldn't have any issues passing that Unicode string back into a feature class, but on the remote chance there is an issue, you can always convert it to ASCII.
Hi Josh, it ws just the quotes, I thought it was stopping it from reading it as a match, but that ended up not being the case. Now I'm simply trying to figure out how to stop it from deleting all of my data. A change that I'm making is that I was originally trying to make the update cursor act on the original "wells" and not the "wells1" feature layer, however I believe it will be easier to do all of my selection and changes in the created feature layer and then save it as a feature class at the end of the script.
Thanks for your help. I'd love to hear more from you later if you have the opportunity.
Hi Joshua,
Please note that I've updated my post. Some of my problems have been solved.
Thanks,
Tom
Honestly, I think your earlier code was easier to read and fairly close to working/workable. The error trapping at the end confuses me because the try statement is at the same indentation as the for statement that loops through the cursor, so cursor.next() should always generate an error by the time it gets to that line. Also, I think the break statement is prematurely breaking you out of one of your cursors. Without having access to sample data to walk through the code with, I have to bow out. Best of luck.
Hi Josh,
I actually ended up figuring it out. That whole "try" nonsense was just a flight of fancy. It now all works the way I had hoped. Thanks for your help!