POST
|
Well, that was my mistake it seems. It worked perfectly with another test name. Thank you for your patience, it's really very appreciated.
... View more
02-11-2015
10:04 AM
|
0
|
4
|
714
|
POST
|
"TABLE" was simply my test name so I did not spend all of my time sorting out the parameters of my toolbox instead of trying to debug the script. when it's complete, the name of the table will be assigned by the user when the tool is run. Also, please note that I have updated my full code above with comments.
... View more
02-11-2015
09:56 AM
|
0
|
7
|
3030
|
POST
|
Sorry, I'm really quite new to programming and am entirely self taught. I'll add some explanations to the reply above, however the DEC field holds that identifier, and the "match" field will hold an object ID of an orps point, which will be no higher than 5-6 digits.
... View more
02-11-2015
09:41 AM
|
0
|
9
|
3030
|
POST
|
Richard, I just want to thank you again for taking the time to help me out. The DECWell field stores a unique identifier, this identifier is always two letters followed by four numbers, like so: AL1905, RS3000, CH0560 The table being created will only hold that identifier and the objectID of an orps point, so that the well data can be related to matching orps through this table. Attached is my full code. Note that the feature layers were created because I was interested in selecting orps within a certain distance of the wells and only checking those, as opposed to the entire set of orps. import arcpy
from arcpy import env
arcpy.env.overwriteOutput = 1
## define all user inputs for creation of arcmap toolbox
a = arcpy.GetParameterAsText(0) ## wells
b = arcpy.GetParameterAsText(1) ## orps
g = arcpy.GetParameterAsText(2) ## output table
c = arcpy.GetParameterAsText(3) ## well owner name field
d = arcpy.GetParameterAsText(4) ## orps owner name field
h = arcpy.GetParameterAsText(5) ## workspace
e = "OBJECTID" ## objectid of both well and orps data
f = "DECWell_" ## dec well identifier
env.workspace = h
##################### BEGINNING OF MATCH #########################################
## Make feature layers
arcpy.MakeFeatureLayer_management(a, "wells")
arcpy.MakeFeatureLayer_management(b, "orps")
wellsFL = "wells"
orpsFL = "orps"
## Create dictionary of well owners
well_owners = {}
wellnum = {}
## cursor to look through well data, getting DEC well number, owner name and object ID of well.
rows = arcpy.da.SearchCursor(wellsFL, [e, c, f])
for row in rows:
## split the owner's name in FIRST LAST format at a space, creating a list
owner = row[1].split(" ")
## take the last entry in the list and make that the entry for well_owners[objectID]
well_owners[row[0]] = "{}".format(owner.pop().upper())
## take the well identifier and make that the entry for wellnum[objectid]
wellnum[row[0]] = str(row[2])
del rows
## create dictionary of orps owners
orps_owners = {}
## cursor to look through orps data and return name of owner and objectID
rows = arcpy.da.SearchCursor(orpsFL, [e, d])
for row in rows:
## split owner's name in LAST FIRST format at the space, creating a list
owner = row[1].split(" ")
## take the first item in this list and make that the entry for orps_owners[objectid]
orps_owners[row[0]] = "{}".format(owner.pop(0).upper())
del rows
## create dictionary of matches
match = {}
## search through owner's names to find matching names
for o in well_owners:
for p in orps_owners:
if orps_owners
== well_owners :
if not o in match:
match = []
match .append(p)
## create an empty table to fill with matches
arcpy.CreateTable_management(h, "TABLE")
## add relevant fields to the new table
arcpy.AddField_management("TABLE", "DECWell_", "TEXT")
arcpy.AddField_management("TABLE", "match", "LONG")
## create insert cursor to insert a new row in the newly created table for each match found
rows = arcpy.da.InsertCursor("TABLE", ["DECWell_", "match"])
## for each entry in the dictionary of matches
for key in match:
## assign a variable for the unique ID of the well in the first for loop
ID = wellnum[key]
## iterate through each value in the list of matches
for s in match[key]:
## assign a tuple of the values the cursor will insert into the fields of each row
insert = (ID, s)
## insert new row
rows.insertRow(insert)
del rows I have used an old variation of this script to produce a similar result in the past, and have observed no errors with regards to matching the correct well to the correct orps and getting the right well identifier out of the dictionary. In the ArcGIS help for insert cursors, it says that you can select all fields in the table using a " * " in the "field_name" parameter of the cursor, and I thought that perhaps it was messing up because I was using all fields by name instead of using the " * ". So I had the script add a third field and try to use only those two, and also attempted to use the " * " with only two fields, and both still returned the same error that I mentioned above, of incorrect SQL syntax.
... View more
02-11-2015
08:58 AM
|
0
|
11
|
3030
|
POST
|
Ahhh, dammit, that's why I was only getting one match for each well, I figured it was an issue with my lists. Many thanks for your input!! However, when I run the script with the logic you suggested, it gave the error that the "INT object not iterable". I think that this part: for s in match[key]: for item in s: rows.insertRow((wellnum[key], item)) it's already understanding "match[key]" as a list object, and "s" as an integer in that list. However, when I took that out and let it run like this: for s in match[key]:
rows.insertRow((wellnum[key], s))
del rows it's giving me an SQL error of... "RuntimeError: An invalid SQL statement was used. [SELECT * FROM TABLE]" in line 71, which, in my script, is this line: rows.insertRow((wellnum[key], s)) Any idea what might cause this?
... View more
02-11-2015
08:04 AM
|
0
|
13
|
3030
|
POST
|
Hi folks, I'm having some weird issues with an insert cursor. Basically I have a dictionary, where each key corresponds to a list of orps. I want the inset cursor to iterate over each list in each dictionary key and insert a row with the corresponding values. Seems like it should be simple, but for some reason I keep getting an SQL error. I've used search and update cursors extensively, but haven't had a need for insert cursors before now, so any advice would be very greatly appreciated. Here is the offending code: match = {}
for o in well_owners:
for p in orps_owners:
if orps_owners
== well_owners :
match = []
match .append(p)
arcpy.CreateTable_management(h, "TABLE")
arcpy.AddField_management("TABLE", "DECWell_", "TEXT")
arcpy.AddField_management("TABLE", "match", "LONG")
for key in match:
rows = arcpy.da.InsertCursor("TABLE", ["DECWell_", "match"])
for s in match[key]:
rows.insertRow((wellnum[key], s))
del rows where well_owners and orps_owners are dictionaries of last names of owners and wellnum is a dictionary of unique identifiers to be written to the table. Thanks in advance.
... View more
02-11-2015
07:19 AM
|
0
|
15
|
8701
|
POST
|
Hi Sol, Thanks for your response, however I just figured out that the function came across an empty tuple and then crashed. Apparently select by attribute can't deal with an empty tuple in the argument, I assumed it would just skip it. Oops. Have a great day!
... View more
01-20-2015
09:52 AM
|
0
|
2
|
736
|
POST
|
Hi folks, I'm having an issue with an SQL statement for the select layer by attribute function and I can't figure out what's going on, because it worked perfectly on a smaller dataset, but when I try to run it on a larger selection of data it fails and gives me an error. Here is the offending piece of code: for key in matches:
arcpy.SelectLayerByAttribute_management(wellsFL, "NEW_SELECTION", "U_ID = {}".format(key))
select_id = tuple(matches[key])
arcpy.SelectLayerByAttribute_management(orpsFL, "NEW_SELECTION", "U_ID IN {}".format(select_id)) the issue is line # 4. "matches" is a dictionary that stores a well ID with a list of ID's of potential orps matches. I want line 4 to select all the orps that have U_ID's in a particular entry of the dictionary. Like I said, when testing this on a smaller dataset, it worked perfectly fine. But now that I try it on a full town's worth of wells/orps, it fails, giving the following error code: Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> arcpy.SelectLayerByAttribute_management(orpsFL, "NEW_SELECTION", "U_ID = {}".format(select_id)) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6494, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). What is suddenly making this expression invalid?? Thanks in advance, Tom
... View more
01-20-2015
09:13 AM
|
0
|
4
|
5164
|
POST
|
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!
... View more
01-07-2015
12:50 PM
|
0
|
0
|
1119
|
POST
|
Hi Joshua, Please note that I've updated my post. Some of my problems have been solved. Thanks, Tom
... View more
01-07-2015
10:08 AM
|
0
|
2
|
1119
|
POST
|
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.
... View more
01-07-2015
08:23 AM
|
0
|
0
|
1119
|
POST
|
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.
... View more
01-07-2015
08:16 AM
|
0
|
0
|
1119
|
POST
|
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.
... View more
01-07-2015
08:13 AM
|
0
|
0
|
1119
|
POST
|
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
... View more
01-07-2015
07:09 AM
|
0
|
9
|
5140
|
POST
|
ah! Many thanks, my friend. I appreciate your help!
... View more
01-06-2015
11:37 AM
|
0
|
0
|
2527
|
Title | Kudos | Posted |
---|---|---|
1 | 10-01-2014 11:53 AM | |
1 | 01-06-2015 07:46 AM | |
1 | 07-22-2014 09:28 AM | |
2 | 07-22-2014 12:39 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|