How to open local layer on ArcGIS Pro using ArcGIS API for Python

190
9
a week ago
JMarvinWirjomartono
New Contributor II

I am learning ArcGIS API for python, which I run using the notebook inside the ArcGIS Pro. I opened some layers in ArcGIS Pro (local files: rasters an vectors).

On the ArcGIS API for Python documentations, it shows the way to open layers from portals. How to get the layers on ArcGIS API for Python that I open locally on my ArcGIS Pro?

My aim is to process the layers with arcgis.learn libraries. I would like to run the process using ArcGIS API for Python libraries using the layer from my local files.

Looking forward to your advice.

0 Kudos
9 Replies
JayantaPoddar
MVP Esteemed Contributor

The addDataFromPath method provides a way to add a layer to a map in a similar way to how the Add Data From Path button works in the application; it places each layer based on layer weight rules and geometry type.

addDataFromPath (data_path)

Map Class (Methods) - ArcPy 

 

e.g. Adding feature class to Map in CURRENT project using ArcPy in ArcGIS Pro? 

lyrTest = r"C:\data\test.gdb\Layer1" 
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("MainMap")[0] 
aprxMap.addDataFromPath(lyrTest)

  

0 Kudos
JMarvinWirjomartono
New Contributor II

@JayantaPoddar  Thank you very much for your information. So I need to use arcpy instead. Now I can get the layer from my ArcGIS Pro from my map object.

But looking back to my original purpose, to use it inside arcgis.learn libraries, I think I have some knowledge gap here. Now I am not sure I can use this layer object from arcpy to input as parameters inside the export_training_data function. 

My first attempt running the export_training_data function with the local layers produce error: "ArcGIS Online does not support export_training_data function." The error sounds like not related to ArcPy though.

Here is my script:

from arcgis.gis import GIS
my_gis = GIS()
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
for lyr in aprxMap.listLayers():
    if lyr.name == "clipped_building":
        boundaryLayer = lyr
    elif lyr.name == "20200428SA1_B05_grid.tif":
        imageLayer = lyr
from arcgis import GIS, learn
from arcgis.learn import export_training_data
outFolder = r"E:\Data\Out\building"
export = export_training_data(input_raster=imageLayer,
                                           output_location=outFolder,
                                           input_class_data=boundaryLayer, 
                                           chip_format="PNG", 
                                           tile_size={"x":448,"y":448}, 
                                           stride_size={"x":224,"y":224},                                           metadata_format="PASCAL_VOC_rectangles",                                        
                                           classvalue_field = "CLASSNO",
                                           buffer_radius = 6
                                           )
0 Kudos
DanPatterson
MVP Honored Contributor

you should be running arcgis.learn from a local machine according to the error.  Are you trying to run the analysis through arcgis online?


... sort of retired...
0 Kudos
JMarvinWirjomartono
New Contributor II

Hi Dan. I am aiming for running the arcgis.learn from my local machine. Do you know where is the part on my script I can switch the setting from AGOL to my local machine?

0 Kudos
DanPatterson
MVP Honored Contributor

What python IDE are you using? or are you trying to run this through Pro's notebook.

Fire up Pro and use the python window or a notebook or use a python IDE (eg Spyder, VS etc etc


... sort of retired...
0 Kudos
JMarvinWirjomartono
New Contributor II

Currently I am using the Notebook inside the Pro. But I can run the notebook from the Python command prompt too (the one installed together with Pro). Are they different?

0 Kudos
DanPatterson
MVP Honored Contributor

There is no need to use a notebook, work with local data and your python IDE, then use Pro to view results if needed

Python in ArcGIS Pro—ArcGIS Pro | Documentation

Using IDEs — Anaconda documentation


... sort of retired...
0 Kudos
JMarvinWirjomartono
New Contributor II

Hi Dan, thank you for your suggestion. The reason why I am using a notebook is that I am using a shared PC on my company that was only installed ArcGIS Pro in it. There was no other Python and Python IDE installation made previously. Will be complicated to ask the admin to install this and that for now.

So I guess if I can stick to the notebook that will be fine for me. In your opinion, do you think running the ArcGIS API for Python libraries on a separated python IDE can solve the "ArcGIS Online does not support export_training_data function." error, Dan?

0 Kudos
JMarvinWirjomartono
New Contributor II

After few days of trial and error, I then realised that the easiest way to start the arcgis.learn libraries with local data (not portal data) is by running the "Export Training Data For Deep Learning" tool. This will make our local data into the format that can be consumed by the model training libraries inside the arcgis.learn module.

The "Export Training Data For Deep Learning" itself maybe able to be run on python too, but I did not try it. I ran the tool manually using the Spatial Analyst License on the Geoprocessing tool. 

So sorry the title may lead to a mislead interpretation, I should have mentioned about the arcgis.learn too on the title. But I don't know how to edit the title.

0 Kudos