2

I have been working on a project and have gotten to the following point: I have been able to create a Feature Class, here named GeocodedDonationLocations. This was successfully done with arcpy.geocoding.GeocodeAddresses.

Next, I am attempting to add this to the current map in ArcGIS Pro. So far, I have been able to come up with the following:

ResultPath1=r"E:\OneDriveMain\OneDrive\_GISProjects\2023Projects\ProjectLeo\Default.gdb\GeocodedDonationLocations"

layername="Donation Locations"
arcpy.management.MakeFeatureLayer(ResultPath1,layername)
p=arcpy.mp.ArcGISProject("current")
m=listMaps("Map")[0]
m.addlayer(layername,"TOP")

I know that I want to create a layer out of the feature class first, which is why I am using MakeFeatureLayer above. It is the 'Actually adding it to the map' part that I seem to have a lot of trouble with.

The main error that I am getting is OSError: current, which makes me think I am not understanding that particular line. I am also unsure if I am using listMaps correctly...

I have been writing this is PyCharm, and not directly inside ArcGIS Pro (no python window or toolbox or notebook so far).

1 Answer 1

5

This code won't work in PyCharm. The "CURRENT" (currently open) project is only applicable when run from within ArcGIS Pro. When running outside of ArcGIS Pro such as from PyCharm, there is no open project.

If running in PyCharm, you need to pass the full path to a .aprx file.

p=arcpy.mp.ArcGISProject("/path/to/existingproject.aprx")

And then after you add your layer, you need to save.

p.save()

Note that any changes won't be visible if you have that aprx open in ArcGIS Pro (and the p.save() will probably fail as the project will be locked by ArcGIS Pro, like you can't make changes to a Word doc if someone else has it open). You would run this code, pointing at an aprx that's not open, then you can open it in Pro to see the changes.

Your listMaps("Map") is fine as long as your map is named exactly "Map". If it's named something else, then that won't work. Though you need to use p.listMaps as it's a method of the arcpy.mp.ArcGISProject object p.

You can also just use p.listMaps() to list all maps, or p.listMaps("Map*") to list maps with names that start with "Map" e.g "Map 2"

You also need to get the layer object from arcpy.management.MakeFeatureLayer. Note that geoprocessing tools return Result objects which contain all the outputs of a tool as well as information about the tool execution. You need to get the layer output from this `Result.

So you would end up with something like:

layername='GeocodeDonationLocations'
layerfile1=arcpy.management.MakeFeatureLayer(ResultPath1,layername)[0]  # get the layer object
# Or 
# layerfile1=arcpy.management.MakeFeatureLayer(ResultPath1,layername).getOutput(0)  # get the layer object
p=arcpy.mp.ArcGISProject(r"~filepath ~to ~.aprx")
m=p.listMaps()[0]
m.addLayer(layerfile1,"TOP")
p.save()
5
  • Thank you for the quick reply. I read the part about current and bonked my own head. I will make these changes and give it a go. Mar 15 at 21:42
  • Would it be possible to add it to the map with a wildcard, something like: listmaps("*") ? I am only using the one map, so it seems like it could work? Mar 15 at 22:28
  • I think I may have also made a syntax error somewhere. Now, I keep getting name 'listMaps' is not defined. Mar 15 at 23:15
  • p.listMaps as it's a method of the ArcGISProject object p
    – user2856
    Mar 15 at 23:49
  • @XanderXombie you can mark this as answering your question instead of editing to say thanks.
    – user2856
    Mar 16 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.