I was wondering if there is any repository or anything available online or something someone has that can run outside of ArcGIS pro which will create a map
Something basic like this,
import arcpy
# Set the paths for the project, layer, and template
new_project_path = r"C:\Temp\new_project.aprx"
layer_path = r"C:\Temp\R36131012A4_Parcel.shp" #path to the shapefile
template_project_path = r"C:\Temp\template.aprx"
# Create a new project by copying the template
aprx = arcpy.mp.ArcGISProject(template_project_path)
aprx.saveACopy(new_project_path)
# Open the newly created project
new_aprx = arcpy.mp.ArcGISProject(new_project_path)
# Get the first map from the project
try:
first_map = new_aprx.listMaps()[0]
first_map.addDataFromPath(layer_path)
print(f"Layer '{layer_path}' added to map in new project at: {new_project_path}")
except IndexError:
print("No maps found in the template project. Could not add layer.")
# Save the new project
new_aprx.save()
print(f"New project created and saved successfully at: {new_project_path}")
what happens to the .gdb when creating a new project from a template? do i need to programmatically copy it over -- i dont want to have to use the template gdb -- i assume i need to use the new projects gdb and atbx etc
You could add some code to create a database in the .aprx folder.
# Directory of the new project
new_project_dir = os.path.dirname(new_project_path)
# Create a new file geodatabase in .aprx directory
gdb_name = "new_project.gdb" # Name of the geodatabase
gdb_path = os.path.join(new_project_dir, gdb_name)
if not os.path.exists(gdb_path): # Check if the GDB already exists
arcpy.CreateFileGDB_management(new_project_dir, gdb_name)
print(f"File geodatabase created at: {gdb_path}")
else:
print(f"File geodatabase already exists at: {gdb_path}")
Hi @YahyaMasri,
When you say "create a map", do you mean create a Map in ArcGIS Pro (aprx) or create a Map by exporting a Layout to PDF for example.
To create a new Map in an existing .aprx you can use the below code snippet.
import arcpy
## the path to the existing aprx
aprx_path = r"C:\path\to\my\Project.aprx"
## get the aprx as an ArcGISProject object
aprx = arcpy.mp.ArcGISProject(aprx_path=aprx_path)
## create the new Map object
new_map = aprx.createMap(name="NEW_MAP",map_type="Map")
## do what you want with the new_map, add layers and tables for example
## save the aprx
aprx.save()
I'm not sure I understand the desired workflow, but here is a script I use to make a total copy of an existing Project (aprx, gdb, and atbx).
import arcpy
import os
# Define paths and names for the new project
new_project_folder = r"path to folder that will contain new project"
new_project_name = "ProjectName.aprx"
new_gdb_name = "ProjectName.gdb"
new_tbx = "ProjectName.atbx"
new_project_path = os.path.join(new_project_folder, new_project_name)
new_gdb_path = os.path.join(new_project_folder, new_gdb_name)
new_tbx_path = os.path.join(new_project_folder, new_tbx)
# Open the original, existing template project
old_project_folder = r"path to folder containing old project"
old_project_name = "TemplateProject.aprx"
old_gdb_name = "TemplateProject.gdb"
old_tbx = "TemplateProject.atbx"
old_project_path = os.path.join(old_project_folder, old_project_name)
old_gdb_path = os.path.join(old_project_folder, old_gdb_name)
old_tbx_path = os.path.join(old_project_folder, old_tbx)
old_project = arcpy.mp.ArcGISProject(old_project_path)
# Create the folder if necessary
if not os.path.exists(new_project_folder):
os.makedirs(new_project_folder)
# Copy GDB and tbx
arcpy.management.Copy(old_gdb_path, new_gdb_path)
arcpy.management.Copy(old_tbx_path, new_tbx_path)
# Update data sources
old_project.updateConnectionProperties(old_gdb_path,new_gdb_path)
for map_ in old_project.listMaps():
for layer in map_.listLayers():
if layer.supports("dataSource"):
layer.updateConnectionProperties(old_gdb_path, new_gdb_path)
# Update project defaults
old_project.defaultGeodatabase = new_gdb_path
old_project.defaultToolbox = new_tbx_path
old_project.homeFolder = new_project_folder
# Save a copy of the aprx
old_project.saveACopy(new_project_path)