Select to view content in your preferred language

Provide ability to create new ArcGIS Pro projects from Python

8250
30
09-01-2021 09:54 AM
Status: Under Consideration
Labels (1)
MaryGraceMcClellan
Regular Contributor

I'm creating a script that will convert all of my MXDs to APRXs, and I find it extremely inconvenient that I can't create a new blank project from scratch form within ArcGIS Pro. This utility would make switching to ArcGIS Pro so much easier, along with the ability to create blank toolboxes. 

Edited to add: @Rhys-Donoghue posted a good workaround for this issue if anyone is interested. 

30 Comments
MErikReedAugusta

Just to be clear, my use case(s) above have nothing* to do with migrating files or file types.  The goal is to create a completely new project based on a provided template, and to be able to easily relate each of those "child" projects back to a master tracking database/project.  It's "Create new project based on a template" but with a couple extra back-end steps, essentially.

*The only place "migration" would hypothetically factor in would be in the workaround mentioned above, and migrating all the various linkages to the new project.

I'll have to do some testing.

rhys-kdrm

@MErikReedAugusta - I used "migration" as an example, because I have run several migrations and the workflow is the same, and the original question in this thread was about migrating from ArcMap to ArcGIS Pro, however, it was not meant to cause confusion.  It is very easy to create multiple ArcGIS Pro projects via a script with different parameters.  I do it regularly.  You can read a spreadsheet, database table, or whatever to create the files.  You create a blank aprx and then open that in-memory via ArcPy and save a copy.  You need to update the home folder and other parameters in the script before saving, e.g. 

aprx = mp.ArcGISProject("...")
aprx.homeFolder = "..."
aprx.defaultGeodatabase = "..."
aprx.defaultToolbox = "..."
               
# ...

aprx.saveACopy("...")
del aprx
 
This is very easy to set up.  Send me a message on LinkedIn if you want and we can share screens and do it fairly quickly: https://www.linkedin.com/in/rhys-donoghue/ 
MErikReedAugusta

Sounds great.  Are homeFolder and the two defaults the only things that you ever need to remember to manually set?  Or are they the only three you typically set in your workflow?

rhys-kdrm

That's only for the APRX itself.  For the layers, I also do stuff like adding the layers via Python from a layer file, then change the original source to the new source, i.e. you might have ten APRXs to add a buildings layer to but for the ten APRXs, the buildings could be coming from ten different feature classes (or one feature class with a different definition query for each APRX).  Alternatively, I might be downloading the data from a feature service, and in that case, I create the layer files (.lyrx) from the feature layers within the feature service, but then the data sources in the .lyrx files are wrong, so I use Python to update the data sources directly in the layer files.  This works great.

GIS_Spellblade

ArcPy should have a function that allows a user to create an .aprx (ArcGIS Pro project).

^^^ That's the idea. ^^^

 

Right now, we're in something of a chicken / egg situation.

    Egg: I want to automate some ArcGIS Pro things

    Chicken: Great. You just need to use an existing project.

    Egg: What if I want to automate the project creation?

    Chicken: Nah. You don't want that mate. Just have something already there.

 

These Python questions both ask about the existence of a function to create an .aprx:

And in both instances, the solution is some flavor of: yeah sure, you just need to have this thing that already exists to make it work.

Cue slow-blink meme.

Could we imagine a world where, say, Python couldn't generate a new file? No.

I mean, I can't. And what I mean to say, with this very belabored point, is, file handling and I/O is a standard part of any flavor of computer programming. It is basic functionality and expected. 

Can ArcPy implement this very standard pattern?

JordanCarmona_0-1755809941833.jpeg

HaydenWelch

An APRX is different from other stuff since it's a gzipped directory of relationships. You could technically just make it from scratch if you wanted.

HaydenWelch_0-1755875731138.png

Those files are all just json CIM definitions:

HaydenWelch_1-1755875863347.png

All relationships are managed by the Index.json and project settings are in GISProject.json

 

Here's an Index.json that I visualized with vizjs, it's incredibly complex:

HaydenWelch_0-1755875998284.png

A minimal APRX is just a DocumentInfo.xml, GISProject.json, and Index.json file in a zipped directory (using defalte compression) with the suffix .aprx so Windows knows to use Pro to open it. You can also just right click the zip and open in Pro and it'll work too.

 

To build the GISProject.json you can use the arcpy.cim module:

from arcpy.cim.CIMDocument import (
    CIMGISProject, 
    CIMModuleSettings, 
    CIMProjectItem,
    CIMDocumentInfo,
)

project = CIMGISProject()
doc_info = CIMDocumentInfo()
# Write out to files and zip

 

GIS_Spellblade

That's totally the point -> We don't build .mapx or .lyrx from scratch.

 

ArcPy lets us abstract that stuff away. If a sufficiently technical user can create an .aprx from scratch, then how can we rationalize that it is too complicated for the product owners to implement?

HaydenWelch

Oh I know, I agree that those small steps required to create an APRX should be abstracted behind a function. I was just pointing out the method by which it could be accomplished. The reason they haven't done this is likely because the other .x files are actually just files while .aprx is a zip which doesn't fit well with whatever system they're using to create the others.

This whole setup process could even be implemented in Python since you likely aren't going to need crazy fast performance on a project builder function and it's going to be IO bound anyways.

Was just hoping that showing that manual process could help some people in the meantime and maybe give ESRI a starting point on what to do

AlfredBaldenweck

Yeah I could really use this function.

DWR_BuckEhler

Great idea and great job presenting it!