|
POST
|
I have over 20 layers that I need to spatially join, doing separate arcpy.SpatialJoin_analysis for each one would be time consuming. fc1 is the layer I want to multi batch the other layers in the database to. I have tried the following but it only spatially joins the first layer in the database. Is it possible to do a multi batch spatial joins? I am going about it the wrong way? import arcpy, os
from arcpy import env
from datetime import datetime as d
startTime = d.now()
arcpy.env.workspace = r"C:\Temp\Default2.gdb"
fc1 = r"C:\Temp\Default2.gdb\Parcels"
arcpy.env.overwriteOutput = True
fcs = arcpy.ListFeatureClasses() #gets the feature classes
for fc in fcs:
print(fc)
arcpy.SpatialJoin_analysis(fc1,fc, "SP_Test", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "")
print ('(Elapsed time: ' + str(d.now() - startTime)[:-3] + ')') fc1
... View more
06-30-2021
03:35 PM
|
0
|
15
|
6561
|
|
POST
|
Thank you for the replies, after looking at the data a little I was wrong, I was working with the wrong data sorry about that. So the Source table has OBJECTID_1(not sure way but this is how I get the data) and the target has OBJECTID. So the InsurtCur 'Cannot find field 'OBJECTID_1'. The Source OBJECTID_1 should go into the targets OBJECTID but as I said it can't find it. Maybe I am still over thinking it... If I use append would the ObjectID vs ObjectID_1 be an issue? I was trying use understand search and insert cursors to understand them better.
... View more
06-23-2021
09:29 AM
|
0
|
1
|
3568
|
|
POST
|
I guess I was completely over thinking it. dsc = arcpy.Describe(Source)
fields = dsc.fields
#out_fields = [dsc.OIDFieldName]
fieldnames = [field.name for field in fields]
lstFields = [field.name for field in fields]
with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
with arcpy.da.InsertCursor(Target,lstFields) as iCur:
for row in sCur:
iCur.insertRow(row)
... View more
06-22-2021
08:15 AM
|
0
|
3
|
3581
|
|
POST
|
So can Field1A be skipped/not included when you pass the InsertCurosr, if so how?
... View more
06-21-2021
03:50 PM
|
0
|
5
|
3596
|
|
POST
|
The field Field1A doesn't exist in the source table only in the target. I guess I was thinking that line 12& 13 would take care of that issue for both the Source SearchCuror and the Target InsertCursor.
... View more
06-21-2021
01:33 PM
|
0
|
8
|
3611
|
|
POST
|
I am trying to use InsertCurosr to transfer rows from one table to another. Yes I could just use append but I am trying to see if the InsertCurosr is faster but I am getting the error "DescribeData: Method Field1AFieldName does not exist". Here is what I have currently. The Source Label currently does not have Field1A, Target does have the Field1A field. import arcpy,os
#### Transfer Source table rows into Target Table
arcpy.env.workspace = r'C:\Temp\Test.gdb'
Target = r"C:\Temp\Test.gdb\TargetTable"
Source = r"C:\Temp\Test.gdb\SourceTable"
arcpy.DeleteRows_management(Target)
dsc = arcpy.Describe(Target)
fields = dsc.fields
out_fields = [dsc.OIDFieldName, dsc.Field1AFieldName]
fieldnames = [field.name for field in fields if field.name not in out_fields]
with arcpy.da.SearchCursor(Source,fieldnames) as sCur:
with arcpy.da.InsertCursor(Target,fieldnames) as iCur:
for row in sCur:
iCur.insertRow(row)
... View more
06-21-2021
11:42 AM
|
0
|
10
|
4379
|
|
POST
|
I was some what able to get what I need with the following. How do I slice the path to stop at the second to last folder ? for example, C:\Cases\blah\2020 blah\yoyo\folder1 --> with the current code I get all of this. C:\Cases\blah\2020 blah\ yoyo --> I needs this populated in the path field rootPath = r'C:\Cases\2020 blah'
shps = []
field = 'Path'
for dirpath, dirnames, files in os.walk(rootPath):
for x in files:
if x == "Shapefile.shp":
shps.append(os.path.join(dirpath, x))
for fc in shps:
arcpy.AddField_management(fc, field, 'TEXT')
dirname = os.path.dirname(fc) #just the name of the folder dirname = os.path.basename(os.path.dirname(fc))
with arcpy.da.UpdateCursor(fc,field) as cur:
for row in cur:
row[0]= dirname
cur.updateRow(row)
arcpy.Merge_management(shps, r"C:\Temp\MergedTest.shp")
... View more
04-28-2021
10:31 AM
|
0
|
2
|
3625
|
|
POST
|
Oh no, my bad I have been messing with it for a few hours and must have missed it. I have the updated code and I don't get an error but it doesn't populate all of the features 'Path' field and on the ones it does populate its not the path to shapefile, it is to another folder. For example the a good path would be r'C:\Temo\blah\2020 blah\case\yoyo' but some of them are populated with r'C:\Temo\blah\2020 blah\case\Notifications' even though the shapefile is not in that 'Notifications' folder. rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []
field = 'Path'
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
arcpy.AddField_management(os.path.join(root,match),field, 'TEXT')
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
for row in cur:
row[0]= os.path.join(root, filename)
cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-27-2021
08:10 AM
|
0
|
0
|
3636
|
|
POST
|
With the following I can get to work with one single shapefile to a specific folder location but not with a sub folder. It also only works if the "Path' field is already there. It won't let me add a field with line 13, ERROR 000622: Failed to execute (Add Field). Parameters are not valid. ERROR 000628: Cannot set input into parameter field_precision. rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
#rootPath = r'C:\Temo\blah\2020 blah\case\yoyo' #works with specific folder location
counter = 0
matches = []
field = 'Path'
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
#arcpy.AddField_management(os.path.join(root,match),field, "Path", 'TEXT') #Erros with ERROR 000622: Failed to execute (Add Field). Parameters are not valid.
ERROR 000628: Cannot set input into parameter field_precision.
with arcpy.da.UpdateCursor(os.path.join(root,match),field) as cur:
for row in cur:
row[0]= rootPath
cur.updateRow(row)
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-26-2021
02:34 PM
|
0
|
0
|
3646
|
|
POST
|
I see but I am not sure how put it together, do I put the "shapefile.shp" in a directory then use with arcpy.da.UpdateCursor to update the "Path" field with the dirnames? I guess I am uncertain on how to put it together.
... View more
04-23-2021
07:51 AM
|
0
|
0
|
3666
|
|
POST
|
I need to merge certain shapefiles with a specific name in sub directories. I can do the merge but where I am stuck on is including each shapefile specific root path to each shapefile 'Path' field before the merge. I have the following. rootpath will like below, Shapefile.shp will always be the same C:\Tem\blah\2020 blah\Case1\Shapefile.shp C:\Tem\blah\2020 blah\Case2\Shapefile.shp C:\Tem\blah\2020 blah\Notice\Shapefile.shp C:\Tem\blah\2020 blah\Use\Shapefile.shp rootPath = r'C:\Temo\blah\2020 blah' # different folders aftre this
counter = 0
matches = []
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "shapefile.shp":
match = ( os.path.join(root, filename))
matches.append (match)
counter = counter + 1
arcpy.Merge_management(matches, r"C:\Temp\MergedTest.shp")
... View more
04-22-2021
02:51 PM
|
0
|
9
|
3718
|
|
POST
|
Duh, I feel dumb. Got it working with the following with your help, thanks! dict1 = dict()
with arcpy.da.SearchCursor(fc,['Pin','Perm_COUNT','P_Num']) as cursor:
for row in cursor:
if row[0] not in (None, "", " "): # forgot the [0]
dict1.setdefault(row[0],[]).append(str(row[2]))
#print (dict1)
with arcpy.da.UpdateCursor(fc,['Pin','Perm_COUNT','Permits']) as cursor:
for row in cursor:
if row[0] in dict1: #added to pass only attributes in dict
row[2] = ",".join(dict1[row[0]])
#print (",".join(dict1[row[0]]))
cursor.updateRow(row
... View more
04-22-2021
07:42 AM
|
0
|
0
|
5014
|
|
POST
|
I have the following and I having a hard time by passing some blank attributes. I know what the problem is, the problem is that row Pin has blanks and when the codes tries to populate the Permits row it throws the 'The row contains a bad value' Error. It's trying to bunch all of 'P_num' that have a blank into the Permits row. How can I get passed this? dict1 = dict()
with arcpy.da.SearchCursor(fc,['Pin','Perm_COUNT','P_Num']) as cursor:
for row in cursor:
if row not in (None, "", " "):
dict1.setdefault(row[0],[]).append(str(row[2]))
#print (dict1)
with arcpy.da.UpdateCursor(fc,['Pin','Perm_COUNT','Permits']) as cursor:
for row in cursor:
row[2] = ",".join(dict1[row[0]])
#print (",".join(dict1[row[0]]))
cursor.updateRow(row)
... View more
04-21-2021
03:04 PM
|
0
|
2
|
5086
|
|
POST
|
I have a table and in that table I have field Fld_1 and I would like to move the text after the comma to the front of the attribute and remove the comma. Not all attributes will have a comma. I am able to split the filed attrubutes but am not sure how to work with the comma ','. with arcpy.da.UpdateCursor(Table, ['Fld_1']) as cursor: for row in cursor: row[0] = " ".join(row[0].split()[:3]) cursor.updateRow(row) del cursor Example Island Village, The --> The Island Village
... View more
12-18-2020
10:06 AM
|
1
|
11
|
4130
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-01-2024 07:19 AM | |
| 1 | 07-26-2024 09:38 AM | |
| 1 | 01-08-2024 09:44 AM | |
| 1 | 03-07-2023 11:46 AM | |
| 1 | 11-02-2020 08:24 AM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
12:36 PM
|