POST
|
I find the ESRI help links confusing and unfortunately I cannot find the one I'm looking for. But basically there is a layer property connection that uses a python dictionary to store old/new connection info. That is the one you need. not the aprx properties method. I am not very good with python nor dictionaries so I had a colleague help me decipher all this. I have an excel sheet that stores the old & new fullpath names. It goes thru the aprx looking for broken links. If found it compares it with the spreadsheet to grab the new connection properties and uses the layer.updateConnectionProperties method to change it. I have some extra lines to document where I have a broken layer but it's not in the spreadsheet - just to alert myself that there's some other layers that probably warrant future investigation. There's also a bit about "bcgw.bcgov" which is our SDE. I didn't want to record those as I was running from an IDE and not connected to our database. I just wanted to ignore those. # go thru a folder and look for aprx
import os
import arcpy
import pandas as pd
# !!! change this for your folder and your Excel that shows your old and new link
my_workspace = r"W:\blah\temp\test3"
my_changes_excel = r"W:\blah\temp\test3\test_data_changes.xlsx"
# !!! this excel stores the broken links that were not found in the spreadsheet above
new_excel = my_workspace + '/broken_layers.xlsx'
if os.path.exists(new_excel):
os.remove(new_excel)
def make_aprx_list(wksp):
# search the main directory for the APRX files
aprx_list = [] # resetting the list
for root, dirs, files in os.walk(my_workspace):
for f in files:
if f.endswith(".aprx"):
print(os.path.join(root, f))
aprx_list.append(os.path.join(root, f))
return(aprx_list)
if __name__ == "__main__":
mylist = make_aprx_list(my_workspace)
df = pd.read_excel(my_changes_excel)
for aprx_name in mylist:
# create pandas dataframe to store broke lyrs with no fix
brokenlayers = pd.DataFrame(columns = ['broken layer'])
#print(aprx_name)
aprx = arcpy.mp.ArcGISProject(aprx_name)
for m in aprx.listMaps():
for l in m.listLayers():
if l.isGroupLayer == False:
if l.isBroken == True:
if l.dataSource in df['old'].values:
new_lyr = df.loc[df["old"]== l.dataSource, "new"].values[0]
#print(new_lyr)
new_connection = l.connectionProperties.copy()
old_connection = l.connectionProperties
new_connection['dataset'] = new_lyr.rsplit('\\', 1)[1] # new layer name
new_connection['connection_info']['database'] = new_lyr.rsplit('\\', 1)[0] #new layer location (i.e. gdb or shapefile in folder)
l.updateConnectionProperties(old_connection, new_connection)
print('Connection updated: ' + new_lyr.rsplit('\\', 1)[1])
else:
if l.dataSource.find('bcgw.bcgov') == -1:
print('Broken layer not found: ' + l.dataSource)
brokenlayers.loc[len(brokenlayers.index)] = l.dataSource
split_name = aprx_name.rsplit('.',1)
new_name = split_name[0] + '_brokenLayersFixed'
#aprx.saveACopy(new_name)
aprx.save()
# append record to Excel if the excel exists. if not create it then write to it.
if len(brokenlayers)>0:
if os.path.exists(new_excel):
with pd.ExcelWriter(new_excel, mode="a") as writer:
brokenlayers.to_excel(writer, sheet_name = aprx_name.rsplit('\\', 1)[1])
else:
with pd.ExcelWriter(new_excel, mode="w") as writer:
brokenlayers.to_excel(writer, sheet_name = aprx_name.rsplit('\\', 1)[1])
print('finito')
... View more
04-17-2024
08:31 AM
|
0
|
0
|
786
|
POST
|
so far no luck... I've tried ChatGPT too but it pointed me to a function that existed in ArcMap 10.8... not pro. anyone else?
... View more
03-18-2024
03:11 PM
|
0
|
0
|
885
|
POST
|
for line 19 re: shapefiles - actually we also have KMZs too which I'm not going to talk about... also line 19: I was hoping the search and replace function would have worked for the full path like searching for an entire sentence vs just replacing a single word. line 9; in the "real" script I'm getting the list of broken layers to review from the aprx.ListBrokenDatasources() and then comparing those to my excel sheet where I hold the old vs new. So the old/new will be a text string I'll try layer connections and let you know how it goes.
... View more
03-15-2024
08:22 AM
|
0
|
0
|
919
|
POST
|
I am about to break a lot of data sources in our team's .aprx files because we are overhauling the places we are storing our data. Our data is stored in shapefile and file geodatabase formats. Is there a way to programmatically do this? I have found snippets online that answer slightly different questions, like changing a gdb or SDE connection. But in my case it might be changing shapefile/feature class name, moving a GDB or shapefile to a different folder, or changing to a different GDB, different feature class, or some combo of the above. the examples I found online only go down to the gdb level, as in changing to a new GDB and all the other features inside will magically get their links fixed. Trying that method with the full path name to the feature class level didn't work for me. I have so far got a script that goes thru folders to look for .aprx files uses the aprx object to return broken links eg. my_broke_list = my_aprx.listBrokenDataSources() and I have stored my data changes in a spreadsheet. I use pandas to read the columns for old and new. and was hoping to use that info to change the data source. The missing piece is updating the data source. I've tried it different ways but each time the result is a new aprx file with the same old broken links. I've tried using the aprx property and the layer properties. So far my only conclusion is that lyr.updateConnectionProperties(old,new) doesn't do what it is supposed to be doing... I'm just testing on a code snippet trying to figure out how to do the layer connection fix and I've tried a few things. Latest version is: import arcpy
arcpy.env.overwriteOutput = True
aprx = r"Q:\blah\testBrokenLinks\small_test.aprx"
aprxNew = r"Q:\blah\testBrokenLinks\small_test4.aprx"
arpxObj = arcpy.mp.ArcGISProject(aprx)
old = r'Q:\blah\testBrokenLinks\lfsa000b21a_e.shp'
new = r'Q:\blah\testBrokenLinks\postal_codes.shp'
for m in arpxObj.listMaps():
for lyr in m.listLayers():
if lyr.isBroken:
print('broke')
if lyr.supports("DATASOURCE"):
if lyr.dataSource == old:
print(old)
lyr.updateConnectionProperties(old,new)
print(new)
arpxObj.saveACopy(aprxNew)
print ('finito') any help or examples on how to do this?
... View more
03-14-2024
10:48 AM
|
1
|
7
|
1017
|
IDEA
|
wanted to echo Meg's comment that email is the most preferred method of communication. I'm not constantly plugged in to AGO. Also it's a lot more plugged in to how we schedule our work around here. Our email is connected to our calendars.
... View more
03-21-2019
09:35 AM
|
1
|
1
|
1311
|
POST
|
hi John I took a look and so far so good... but I have an account on a different subscription (https://governmentofbc.maps.arcgis.com/home/signin.html ) that requires me to use a different log in. How do I access it via the "BCGOV" button instead of the usual ArcGIS Online login?
... View more
10-25-2018
09:27 AM
|
0
|
1
|
1699
|
POST
|
thanks. I'll have a look is ipynb only viewable in Jupyter notebook?
... View more
10-22-2018
04:06 PM
|
0
|
0
|
1699
|
POST
|
is there a script to see what feature services are linked to which web maps which are linked to which apps within your account? I vaguely remember seeing a demo of this by John Yaist and David Martinez during the ESRI Conference in San Diego this year. But for the life of me I'm unable to find the code anywhere. The idea is that I have a story map journal, and i'd like to create a list to see which of my AGO web maps are attached to that story map. And to go one level further, to see what feature layers are used in those AGO web maps. The demo created a text file - or maybe an excel sheet - of that - which is exactly what I was looking for. @jyaist@esri.com @davidmartinez
... View more
10-16-2018
08:35 AM
|
0
|
1
|
483
|
POST
|
P.S. i accidentally hit the button that said "Assumed Answered" but I don't know how to turn that off.... help?
... View more
10-15-2018
01:31 PM
|
0
|
0
|
1699
|
POST
|
#notAnsweredYet is there a script to see what feature services are linked to which web maps which are linked to which apps within your account? I vaguely remember seeing a demo of this by John Yaist and David Martinez during the ESRI Conference in San Diego this year. But for the life of me I'm unable to find the code anywhere. The idea is that I have a story map journal, and i'd like to create a list to see which of my AGO web maps are attached to that story map. And to go one level further, to see what feature layers are used in those AGO web maps. The demo created a text file - or maybe an excel sheet - of that - which is exactly what I was looking for.
... View more
10-15-2018
12:06 PM
|
1
|
9
|
2199
|
IDEA
|
many instances where this would be useful... please vote!
... View more
06-27-2018
08:37 AM
|
0
|
0
|
4505
|
POST
|
about 4 wks ago... but I've had to change projects since then please add that in the documentation - to never move the survey123 files out of their folder. that would have saved me half of the headaches I experienced here.
... View more
05-16-2018
10:08 AM
|
0
|
0
|
889
|
POST
|
a lot of things that says you can overwrite will come back with error messages. things like changing drop down lists. it's been a while - i've forgotten the details.
... View more
05-16-2018
08:17 AM
|
0
|
2
|
889
|
POST
|
Hi Shelby I haven’t received any emails from you before this one – so if there were any others please send my way. It would be great to have better documentation on what is and what isn’t allowed when republishing surveys. I find the existing document<https://doc.arcgis.com/en/survey123/desktop/create-surveys/publishsurvey.htm#ESRI_SECTION1_97A01E11C37D401EB95C7D22EF251F66> on this to be flat out wrong or at the very least, misleading. I ended up giving up and publishing a new survey, recreating my web map, relinking my dashboard, all the while cursing under my breath
... View more
05-02-2018
08:31 AM
|
3
|
5
|
889
|
Title | Kudos | Posted |
---|---|---|
1 | 03-14-2024 10:48 AM | |
1 | 10-15-2018 12:06 PM | |
1 | 03-21-2019 09:35 AM | |
1 | 04-17-2018 01:37 PM | |
3 | 05-02-2018 08:31 AM |
Online Status |
Offline
|
Date Last Visited |
04-17-2024
05:17 PM
|