Hi,
I'm working on a code that I would like to share and, ideally, I want to use relative paths. However, I've tried several approaches and found that it only works when I use the complete path.
Here's a simplified example where I attempt to insert a feature from a geodatabase:
# Path to your feature class in the geodatabase feature_class_path = feature_path # Get the current project project = arcpy.mp.ArcGISProject("CURRENT") # Get the active map (or specify which map to use) map_object = project.listMaps()[0] # Assuming you want the first map # Add the feature class to the map map_object.addDataFromPath(feature_class_path) print(f"Feature class '{feature_class_path}' added to the map.")
When I set feature_class_path using a relative path like this:
feature_class_path = r".\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"
it doesn't work. If I use:
feature_class_path = os.path.join(".", "UpdatePoints", "UpdatePoints.gdb", "Earthquakes_points")
it still doesn't work.
The code only functions correctly when I use the full path:
feature_class_path = r"C:\Users\YourUsername\Documents\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"
Why is that?
Solved! Go to Solution.
So, two things:
import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")
# Change working directory
os.chdir(os.path.dirname(project.filePath))
# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute
# for use in geoprocessing
feature_class_path = r".\Default.gdb\example"
feature_class_path = os.path.abspath(feature_class_path)
print(feature_class_path)
# Get the active map (or specify which map to use)
map_object = project.listMaps()[0] # Assuming you want the first map
# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)
This worked fine for me with "example", but "example2" failed because it doesn't exist.
So you'll want to add an if arcpy.Exists(feature_class_path) in there somewhere.
p = arcpy.mp.ArcGISProject('CURRENT')
#Project information
print(f'Project: {p.filePath}')
perhaps, then strip off the filename
hmmm.
Try something like this:
import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")
# Change working directory
os.chdir(os.path.dirname(project))
# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute
# for use in geoprocessing
feature_class_path = r".\ex.gdb\feature_path"
feature_class_path = os.path.abspath(feature_class_path)
# Get the active map (or specify which map to use)
map_object = project.listMaps()[0] # Assuming you want the first map
# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)
print(f"Feature class '{feature_class_path}' added to the map.")
No, didn't work either
So, two things:
import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")
# Change working directory
os.chdir(os.path.dirname(project.filePath))
# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute
# for use in geoprocessing
feature_class_path = r".\Default.gdb\example"
feature_class_path = os.path.abspath(feature_class_path)
print(feature_class_path)
# Get the active map (or specify which map to use)
map_object = project.listMaps()[0] # Assuming you want the first map
# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)
This worked fine for me with "example", but "example2" failed because it doesn't exist.
So you'll want to add an if arcpy.Exists(feature_class_path) in there somewhere.
I try to avoid using chdir in my scripts usually because it can make debugging a bit difficult.
Just use full paths for everything, but name the segments (e.g. project_directory, working_database, active_dataset, etc.) that way you can see exactly what is being accessed. I usually use pathlib.Path objects so I can use inline operators and avoid messy os.path.join calls.
If you need relative pathing, you can accomplish that using pathlib too.
Here's a code example:
from pathlib import Path
import arcpy
from arcpy._mp import Map
# Get Project
prj = arcpy.mp.ArcGISProject("CURRENT")
# Set Paths
home_folder = Path(prj.homeFolder)
database: Path = home_folder / "Data.gdb"
feature_class: Path = database / "FeatureClass"
# Get Map
_map: Map = prj.listMaps("Map Name")[0]
# Add Featureclass
_map.addDataFromPath(str(feature_class))