|
POST
|
Thanks Micah, I thought about doing a spatial join but my only concern is that I have a line feature and, though I haven't fully tested it out, I got a different result than what I was looking for. Some of the line features cross multiple polygons and I'm trying to determine how to best go about this. Some are entirely inside a polygon or cross either two or more polygons. So I tried to convert the feature to point so I could determine which part of the polygon either contained the largest portion of the line or at least it's determine where the middle of the line fell. I looked at other ways and I think I found a couple but I will give your example a try. I didn't think about the spatial join after converting to point so that might definitely be a better solution than what I thought to try or attempt.
... View more
10-22-2019
03:59 PM
|
0
|
0
|
2056
|
|
POST
|
Hi, I have a script where I spatially select another layer, and the selected records in that layer are then used to populate the fields in the layer that was used for the spatial selection. So specifically I am trying to transfer attributes, but I did some research and the only concern with scripting the Transfer Attributes is the additional field that may get added. I believe I could possibly delete the field to fix that issue but I am unfortunately working in an sde database and I don't want to risk messing with the schemas. If anyone has another possible solution for this I would be very open to it. with arcpy.da.UpdateCursor(fc, ["OID@", "LANDDISTRICT", "LANDLOT"], """LANDDISTRICT IS NULL OR LANDLOT IS NULL""") as selected_fc:
for row in selected_fc:
#Create temp fc copy
arcpy.MakeFeatureLayer_management(fc, r"in_memory\fcLayer")
#Select layer by attribute
arcpy.SelectLayerByAttribute_management (r"in_memory\fcLayer", "NEW_SELECTION", "OBJECTID = {}".format(row[0]))
#Select layer by location
arcpy.SelectLayerByLocation_management(r"in_memory\DLL_layer", "INTERSECT", r"in_memory\fcLayer", '', "NEW_SELECTION")
#If selected district and landlot in row are null
if row[1] is None and row[2] is None:
print '{} Land District is {} and Land Lot is {} '.format(row[0], row[1], row[2])
def DL():
for DL in arcpy.da.SearchCursor(r"in_memory\DLL_layer", ["LAND_DIST"]):
print DL
return DL[0]
def LL():
for LL in arcpy.da.SearchCursor(r"in_memory\DLL_layer", ["LAND_LOT"]):
print LL
return LL[0]
row[1] = DL()
row[2] = LL()
selected_fc.updateRow(row)
print '{} Land District is {} and Land Lot is {} '.format(row[0], row[1], row[2]) I have tried several attempts with this and I either run into the same error or an error referring to something else. Traceback (most recent call last):
File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District Test 2.py", line 65, in <module>
row[1] = DL()
File "U:\Models_Tools\Scripts related to Landlot and District\Populate Landlot and District Test 2.py", line 57, in DL
return DL[0]
UnboundLocalError: local variable 'DL' referenced before assignment
... View more
10-16-2019
09:57 AM
|
0
|
3
|
2164
|
|
POST
|
Thanks Joshua, I gave it some more thought this morning and took another look at the script. I was trying to figure out the best way to determine how to go about this script. Essentially I am trying to go through each of the fields and populate any null or blank values. I must have misunderstood what you meant by sequence unpacking and I think I improperly applied it. As for my other issue, I have been trying to figure out the best way to identify blank values in a field. For some reason using none didn't seem to work and trying to use ' ' to see if it could identify those values didn't seem to work either. I will modify the script with your suggestions and see if that makes a difference. The script I posted was a modified version of another script. The other script has each field broken down so I will modify that one. I will take a look at the link one I get a chance. I am juggling several things so I have been pretty busy and that is part of the reason I am looking to automate several tasks.
... View more
10-11-2019
12:26 PM
|
0
|
0
|
1195
|
|
POST
|
Hi Joshua, I have another question in regards to unpacking sequences. Is it possible to specify a range of sequences and then check to see if a certain condition is true for that range. So for instance, with arcpy.da.UpdateCursor(fc, ["fieldA", "fieldB", "fieldC", "fieldD", "COLLECTED_BY"]) as cursor:
for abandoned, facid, locate, survey, collected in cursor:
if abandoned is None or abandoned == '':
print field.name
abandoned = 'NO'
print '{} = {}'.format(field.name, abandoned)
elif facid is None or facid == '':
print field.name
x = str(abs(int(row[1])))[:4]
y = str(abs(int(row[2])))[:4]
facid = x + y # concatenate strings x and y
print '{} = {}'.format(field.name, x + y)
elif (locate, survey, collected) is None or (locate, survey, collected) == '':
locate = 'GPS'
survey = 'SUBMETER GPS'
collected = 'CONSULTANT'
print '{} = {}, {}, {}'.format(field.name, locate, survey, collected)
elif locate is None and (survey, collected) is not None:
locate = 'GPS'
print '{} = {}'.format(field.name, locate)
elif locate == 'GPS' and (survey is None or survey == '') and collected == 'CONSULTANT'
survey = 'SUBMETER GPS'
print '{} = {}'.format(field.name, locate)
elif locate == 'FIELD CHECK' and (survey is None or survey == '') and collected == 'CLIFF':
survey = 'ORTHO'
print '{} = {}'.format(field.name, survey)
elif (locate, survey) is not None and collected is None or collected == '':
collected = 'CONSULTANT'
print '{} = {}'.format(field.name, collected)
else:
print 'No null fields'
cursor.updateRow(abandoned, facid, locate, survey, collected) If this is by no means possible or you or Randy or anybody in general may have a better solution for this I would definitely like to hear it. I took a look at my previous script that I wrote and noticed I had the same lines of code repeating and thought that this solution( if it can be counted as one ) would be easier to utilize. Any thoughts or ideas on this would be very appreciated. Robert
... View more
10-11-2019
04:45 AM
|
0
|
2
|
1195
|
|
POST
|
Thanks again Joshua. I didn't even know that it was possible to unpack a sequence by simply assigning values to the sequence. I will try applying that to some of the other scripts since I've been looking for that kind of solution. So regardless of how many values are in a sequence, you can quintessentially assign values to any of the values in the sequence.
... View more
10-10-2019
04:03 PM
|
0
|
1
|
1195
|
|
POST
|
Thanks Randy for clarifying. I really wasn't quite sure if setting the update cursor with multiple fields listed as the cursor would result in all of those field records being updated, much less being able to specify which of the fields in the list need updating. I also didn't know that you could specify field names in a cursor as named variables like the one Joshua posted. I will try his script again, but I will also try it with another part of the script that I am working on. I wish there was more information like what you and Joshua provided available. I tried researching to find the best approach with the scripts that I am working on, and I wasn't able to find much. Thanks again Randy and Joshua for the much needed help. I haven't been scripting much and I found the opportunity to when I told my supervisor that it would be easier to automate populating certain fields rather than manually doing so. My scripting knowledge is still very limited so I tend to ask around and see if anyone else can provide a better solution for what I am trying to accomplish. If you, Joshua, or anyone else has any tips to that would help with improvements I would definitely be open to it.
... View more
10-10-2019
12:14 PM
|
0
|
6
|
2390
|
|
POST
|
Hi Randy, Your script worked and it also raised another question/concern that I have. I don't know if I should post another question or if it is fine posting it here. When there are several fields listed for the update cursors, like what you and Joshua posted, is there a way to specify which particular field gets updated? I wasn't quite sure if there was, but part of the reason I wrote some of my scripts like what I posted, is because I wasn't sure if there was a way to do so. Your example worked and I wasn't sure if something akin to that would work. So is it possible list multiple fields and only have specific records in one of the fields get populated. Also, does this also work for a set of records in multiple fields? Any clarity on this would help me better understand how to go about writing these kind of scripts better. Thanks, Robert
... View more
10-10-2019
11:09 AM
|
0
|
10
|
2390
|
|
POST
|
Thanks Randy. I tried just using the update cursor but I wasn't sure if I used the update cursor with the shape@x or shape@y if it would work. I will also try your script as well. Robert
... View more
10-10-2019
10:43 AM
|
0
|
0
|
2390
|
|
POST
|
Thank you very much Joshua. I thought for some reason that if I wrote the script to Search Cursor then it could only be written to search cursor. I wasn't quite sure how to combine the search and update cursors into one, or if there was a way to run one then the other. The other concern that I had (this is probably due to my limited understanding of python) is I was afraid that if I set the update cursor with the SHAPE@X and SHAPE@Y that those records in those fields might get populated. If that isn't the case then I worried for nothing. I will give your script a try and let you know how it goes. Also, I forgot to include the elif when I copied this script into my question. I will go back and change it so that way there is no confusion or anything.
... View more
10-10-2019
10:40 AM
|
0
|
0
|
2390
|
|
POST
|
Hi, So I have a slight predicament for this given scenario. I need to take the last 4 characters from the x coordinate and do the same for the y coordinate. I then need to combine the two and from the combined values, update another field. I have something akin but I keep running into errors. I can get it to list a set of values that I need but trying to get the update cursor to update the records in the field is tricky. if field.name == 'FACILITY_ID':
print field.name
for row in arcpy.da.SearchCursor(fc, ["FACILITY_ID", "SHAPE@X", "SHAPE@Y"], """FACILITY_ID IS NULL"""):
rowx = str(row[1])
rowy = str(row[2])
x = rowx[0:4]
y = rowy[0:4]
xy = x + y
for row in arcpy.da.UpdateCursor(fc, ["FACILITY_ID"], """FACILITY_ID IS NULL"""):
row.updateRow([xy])
print '{} = {}'.format(field.name, xy) Any help on this would be greatly appreciated.
... View more
10-10-2019
08:57 AM
|
0
|
15
|
3861
|
|
POST
|
Thanks Joshua, I checked out the article and it makes complete sense to use a dictionary for this instance. The only thing is is that I have never used or created a dictionary before. It seems like it is a list almost but I am completely unfamiliar with this. If you or anyone gets a chance, could you possibly show me what that would look like specifically. The other thing that I am trying to obtain a single value that is derived for each value in a given instance. If there is a way to achieve this I would be very interested. Thanks, Robert
... View more
10-09-2019
10:06 AM
|
0
|
0
|
6005
|
|
POST
|
Hi Joshua, Here is the updated script. Issue 1: I came some of the features that may be corrupted and so being able to identify those and deleting them is something I am still trying to figure out. Issue 2: I am not sure how to set up the if/else statements in such a way that the three conditions can be identified. import arcpy
import os
arcpy.env.overwriteOutput = True
workspace = r'Database Connections\....sde'
fcs = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
#make district landlot layer
Dist_LL = "Database Connections\....sde\.....fc"
arcpy.MakeFeatureLayer_management(Dist_LL, "DLL_layer")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
fcs.append(os.path.join(dirpath, filename))
for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
#Get feature class name
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print y
#Create lists (OID)
OID = []
if geometryType == 'Point':
print geometryType
with arcpy.da.SearchCursor(fc, ["OID@", "LANDDISTRICT", "LANDLOT"], """LANDDISTRICT IS NULL OR LANDLOT IS NULL""") as selected_fc:
for row in selected_fc:
print row
#Create temp fc layer
arcpy.MakeFeatureLayer_management(fc, "fcLayer")
#Select layer by attribute
arcpy.SelectLayerByAttribute_management ("fcLayer", "NEW_SELECTION", "OBJECTID = {}".format(row[0]))
#Select layer by location
arcpy.SelectLayerByLocation_management("DLL_layer", "INTERSECT", "fcLayer", '', "NEW_SELECTION")
if (row[1] is None) and (row[2] is None):
print '{} District is {} and Land Lot is {} '.format(row[0], row[1], row[2])
for selectrows in arcpy.da.SearchCursor("DLL_layer", ["OID@", "LAND_DIST", "LAND_LOT"]):
if selectrows is None:
print selectrows
elif (row[1] is None) and (row[2] is not None):
print '{} Land District is {} '.format(row[0], row[1])
for selected_Lot in arcpy.da.SearchCursor("DLL_layer", ["OID@", "LAND_DIST"]):
print '{} Land District = {}'.format(row[0], row[1])
elif (row[1] is not None) and (row[2] is None):
print '{} Land Lot is {}'.format(row[0], row[2])
for selected_District in arcpy.da.SearchCursor("DLL_layer", ["OID@", "LAND_LOT"]):
print '{} Land Lot = {}'.format(row[0], row[1])
else:
print 'No null values'
del OID[:] The script works but I think there are areas where I could easily simplify this script. Any help on simplifying/identifying the best ways to resolve my issues would be greatly appreciated. Thanks, Robert
... View more
10-09-2019
08:06 AM
|
0
|
2
|
6005
|
|
POST
|
Thanks Joshua, I actually was able to work out creating a temporary feature layer (not class). I found a simple solution and it seems to be working just fine. If need be I can re-post the code to show. For my if/else statements I was concerned about the evaluations. I wasn't sure if there was a way to write the statements so that if both fields are null it returns the null fields, or if one of the fields is null it returns one of the null fields. I think I may figured it out but any assistance would be well appreciated. Robert
... View more
10-08-2019
02:41 PM
|
0
|
4
|
6005
|
|
POST
|
Hi, I have a script that seems to work but is unfortunately returning incorrect results. I am trying to figure out the best way to go about it. I have tried several other alternatives but those alternatives either return an incorrect number of results or doesn't have the data I need to work with. What I am trying to do is: simply iterate through a set of features that have like fields iterate through each null value in the fields, selecting another layer based on location to that null record in the feature class field derive a certain field value from the feature that is selected based on the location of the feature class with null field value import arcpy
import os
arcpy.env.overwriteOutput = True
workspace = r'Database Connections\......sde'
fcs = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
#make district landlot layer
Dist_LL = "Database Connections\.....sde\......Landlots"
DistLL_layer = arcpy.MakeFeatureLayer_management(Dist_LL, 'DLL_layer')
for dirpath, dirnames, filenames in walk:
for filename in filenames:
fcs.append(os.path.join(dirpath, filename))
for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
#Get feature class name
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print y
#Create lists (OID)
OID = []
if geometryType == 'Point':
print geometryType
for row in arcpy.da.SearchCursor(fc, ["OID@", "LANDDISTRICT", "LANDLOT"], """LANDDISTRICT IS NULL OR LANDLOT IS NULL"""):
appendOID = OID.append(row[0])
OIDstrnum = str(len(OID))
#Create unique naming conventions
a = '{}{}'.format(y, OIDstrnum)
if (row[1] is None or row[2] is None):
#Create temp fc copy
b = arcpy.MakeFeatureLayer_management(fc, a)
#Select layer by location
selection = arcpy.SelectLayerByLocation_management(DistLL_layer, "INTERSECT", b, '', "NEW_SELECTION")
print '{} District is {} and Land Lot is {} '.format(row[0], row[1], row[2])
for selected in arcpy.da.SearchCursor(selection, ["LAND_DIST", "LAND_LOT"]):
print selected
break
elif (row[1] is None):
print '{} Land District is {} '.format(row[0], row[1])
for selected_Lot in arcpy.da.SearchCursor(selection, ["LAND_DIST"]):
print selected_Lot
break
elif (row[2] is None):
print '{} Land Lot is {}'.format(row[0], row[2])
for selected_District in arcpy.da.SearchCursor(selection, ["LAND_LOT"]):
print selected_District
break
else:
print 'No null values'
del OID[:] I have something that works(partially) but does not return the correct features with a corresponding spatial location. If anyone could assist with this I would greatly appreciate it. Robert
... View more
10-08-2019
07:38 AM
|
0
|
6
|
6403
|
|
POST
|
Thanks Micah, Transferring the field values form the Dist_LL feature class to the other feature classes is what I am trying to accomplish. I wasn't quite sure if a spatial join would work in this instance, because I tried that method using modelbuilder and unfortunately I could not get any ID fields to match. The other method that I tried was to do an intersect on the features and then use the add join method to match the IDs which works, but I think another issue is that some of the features may overlap one another (long story as to why) or there may be another reason. I just also noticed that we have 0 for some values in the fields so I will need to make some slight modifications to the script to update those values as well. Just another quick question(still corresponding to this thread). What is the difference between using the SearchCursor and specifying a query to get certain values vs. using the SearchCursor and running if/else statements to identify certain values. I am not quite sure which one is used over the other, and which one do would work best in this situation or does it not matter. Robert
... View more
10-04-2019
09:41 AM
|
0
|
0
|
1564
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 2 weeks ago | |
| 1 | 02-10-2026 06:09 AM | |
| 1 | 03-04-2026 01:08 PM | |
| 1 | 02-24-2026 12:59 PM | |
| 3 | 03-03-2026 10:33 AM |
| Online Status |
Offline
|
| Date Last Visited |
Wednesday
|