Select to view content in your preferred language

aprx exportToMAPX relative paths

436
2
03-23-2023 12:15 PM
MatthewMassey
Occasional Contributor

My code below runs successfully, but the resulting .mapx file has absolute paths, whereas I need relative paths. Any idea how to solve this in Python? I feel like I'm missing something simple like changing the relative paths in the original .aprx? Any help is appreciated!

aprx_path = os.path.join(level2_gdb_path, 'aprx_template', 'aprx_template.aprx')
aprx = arcpy.mp.ArcGISProject(aprx_path)
m = aprx.listMaps()[0]

for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_submission_path, datatype=['Table', 'FeatureClass']):
    
    for file in filenames:
        path = os.path.join(dirpath, file)
        m.addDataFromPath(path)
        aprx.save()

# export to .mapx file...
mapx_path = os.path.join(level2_gdb_path, f"{quad['name'].replace(' ','')}.mapx")

m.exportToMAPX(mapx_path)

# delete aprx object
del aprx

# delete aprx_template folder...
if os.path.isdir(aprx_path):
    shutil.rmtree(aprx_path)
else:
    print('Folder does not exist')

 

0 Kudos
2 Replies
by Anonymous User
Not applicable

Not sure if this is what you are after, but you can use modules in the os package to build relative paths. This line is part of what I use for accessing a folder two levels up from the file that is being executed.

os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

 The innermost part of this: os.path.join(os.path.dirname(__file__), '..'))  second parameter '..' moves up directory tree and then from there you can map to the folder that you want, or you can do something like './folder/stuff'. but since you have the complete path from the .walk... it's better to be explicit.

I don't think the maps can store relative paths, but I've never tried it. I'd think it would add to the headache of moving projects to new locations because the app isn't smart enough to traverse through your whole structure to figure out where it's now saved in relation to the data it needs.

0 Kudos
MatthewMassey
Occasional Contributor

@Anonymous Userthanks for the reply, but that didn't help my specific problem..mapx files don't support relative paths. however, that is cool to know about the OS module trick, so thanks for that!

0 Kudos