# addLayer.py
# Purpose: Add a data layer to a map.
# Input: No arguments required.
import arcpy, os

# Initialize data variables.
arcpy.env.workspace = 'C:/gispy/data/ch24/maps/'
fileName = '../../data/ch24/USstates/MA.shp'
fileAbs = os.path.abspath(fileName)
mapDir = 'layerManipExample3'
mapName = 'layerManipExample3.aprx'

# Instantiate ArcGISProject and Map objects.
aprx = arcpy.mp.ArcGISProject(arcpy.env.workspace + '/' +
                                mapDir + '/' +
                                mapName)

maps = aprx.listMaps()

# Get the first map.
map = maps[0]

# Add a new layer to the map using absolute file path.
map.addDataFromPath(fileAbs)

# Save a copy of the map.
copyName = 'C:/gispy/scratch/' + os.path.splitext(mapName)[0] + '_V2.aprx'
aprx.saveACopy(copyName)
print('{0} created.'.format(copyName))

# Delete the MapDocument object to release the map.
del aprx