0

When exporting files from ArcPy anything that is created gets instantly imported in ArcMap so it can be used in following processes.

This is because ArcPy needs the files that uses imported to ArcGIS in order to find them.

How to import files to ArcGIS from a folder so I can use them in ArcPy without manually importing them?

For example a folder called files1

#set environment
arcpy.env.workspace = r"C:\Users\user\Desktop\axc\files1"
for i in arcpy.ListFeatureClasses():
    print(i)

This is for printing. How can I import them to ArcGIS?

Vince
  • 20,017
  • 15
  • 45
  • 64
  • I think there is a need for terminology clarification here. Exporting files from arcpy means when you run a tool from arcpy, the result is added to your Table of Content in ArcMap and you can use this as an input even though it is a string OR if you assign a variable in python, such as my_fc = arcpy.Intersect_analysis(["My layer", "Another layer"], output_FC), after a arcpy tool run, this can be used as input for another tool, say another_fc = arcpy.Union_analysis(my_fc, "union_output")? – fatih_dur Aug 31 '18 at 14:38
  • Yes your example is what I am referring at. The output is imported in Arcgis . How to import files that have not been used in Arcpy and are not already imported in Arcgis to use them later in Arcpy? – user10300411 Aug 31 '18 at 14:40
  • There are two examples, which one? Basically, do you want to add all feature classes from a folder to ArcMap (not Arcgis, which is the name of the software suite) as layers? – fatih_dur Aug 31 '18 at 14:44
  • Yes I want to add shapefiles from a folder to ArcMap as layers so I can use them in ArcPy without errors. – user10300411 Aug 31 '18 at 14:46

1 Answers1

0

I think the fastest way is to convert everything into a feature layer via arcpy.MakeFeatureLayer_management. For example:

# Make sure the outputs will be added to TOC
arcpy.env.addOutputsToMap = True
# and will not complain about ERROR 0000725
my_old_ow_setting = arcpy.env.overwriteOutput
arcpy.env.overwriteOutput = True
for i in arcpy.ListFeatureClasses():
    arcpy.MakeFeatureLayer_management(i, i)

arcpy.env.overwriteOutput = my_old_ow_setting

should do the job. I should add that this is a workable solution, however it is not the most efficient way of using python.

If you want to avoid doubling-up layers in your TOC, you can use a try-except block as follows:

import sys
# Make sure the outputs will be added to TOC
arcpy.env.addOutputsToMap = True
for i in arcpy.ListFeatureClasses():
    try:
        arcpy.MakeFeatureLayer_management(i, i)
    except arcpy.ExecuteError as e:
        if 'ERROR 000725' in e.message:
            print "The layer is already loaded..."
        else:
            print sys.exc_info()
    except:
        print sys.exc_info()
fatih_dur
  • 4,983
  • 2
  • 16
  • 35
  • I thought about arcpy.MakeFeatureLayer_management but didn't do what I wanted. Now I ran this and it says that the files already exist, as error. Although I removed them from Table of contents. What is the problem? I want to put the shapefiles from this folder to table of contents in Arcpy? Is that the way for sure? – user10300411 Aug 31 '18 at 15:02
  • Can you please post the exact error message here? – fatih_dur Aug 31 '18 at 15:03
  • Runtime error Traceback (most recent call last): File "<string>", line 3, in <module> File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 6979, in MakeFeatureLayer raise e ExecuteError: ERROR 000725: Output Layer: Dataset ASTENOT already exists. This is the first file and it will say the same for the rest of files in that folder. – user10300411 Aug 31 '18 at 15:04
  • @user10300411, See my update on the post. Basically this is a setting in Geoprocessing preferences which lets tools to overwrite outputs. In your case, this is most probably False or unticked. The update saves your preference first, then sets output-overwriting to True and reset it back to the original value. – fatih_dur Aug 31 '18 at 15:18
  • It looks very good. I think its okay. I just don't understand what part does what?For example why should be overwrited when we just read from folder to ArcMap? Shouldn't it just be some sort of importing only? And what 'Make feature layer management' does?I read, It makes a new layer. Why is the need for making a new layer and not just importing the existing files in the folder to ArcMap? – user10300411 Sep 03 '18 at 07:18
  • This overwrite output switch is quite useful for you not to accidentally overwrite your interim outputs. However it causes unintentional issues, such as your one, time to time. What the script does, first makes sure the output of any operation will be added to TOC. And then saves the output overwrite parameter to revert it back later once we are done with adding layers. I strongly advise you to have a look at this thread. – fatih_dur Sep 03 '18 at 07:25
  • You are right MakeFeatureLayer_management creates a layer and its output added to your TOC as you asked in your question. However, if the layer exists in your TOC or there is any layer object with the same name in the memory, again it avoids overwriting by default. That's why we set this toTrue beforehand. – fatih_dur Sep 03 '18 at 07:27
  • The task to put the files in TOC is considered a calculation that has an output?It isn't just importing of some files to the ArcMap? – user10300411 Sep 03 '18 at 07:30
  • You are correct, I do not call it calculation in particular, any tool call (arcpy functions including MakeFeatureLayer_management) that creates an output in ArcMap checks your addOutputsToMap parameter and if it is True, it adds the Result to your TOC as (in many cases) a layer or a table view. – fatih_dur Sep 03 '18 at 07:33
  • And if we want to import to TOC only the shapefiles that are not already imported? How would we do that? Because that is the reason I posted the question. If you can add this condition to your answer.Thanks – user10300411 Sep 03 '18 at 07:43
  • Most probably a try - except block would help. I will update the answer. – fatih_dur Sep 03 '18 at 07:50
  • It still works with all the layers.Can't we do something using your first code but with something like: For i in arcpy.ListFeatureClasses(): if not in TOC : do the import with make feature layer management?? – user10300411 Sep 03 '18 at 08:16
  • Maybe you should ask this as a different question, most probably it will attract more attention than this one! Also it is a policy of this site, one question each post, please see https://gis.meta.stackexchange.com/questions/3649/where-does-it-state-one-question-per-post-in-the-gis-stack-exchange-instructio/3654#3654. – fatih_dur Sep 03 '18 at 08:22