Adding shapefiles to a project in PRO using python

744
11
Jump to solution
10-08-2020 12:52 PM
JamesWhite5
New Contributor III

Good afternoon.

I have a python script that I am using to obtain kml data and then turn that into a geodatabase, extract the information and create 3 shapefiles which I then need to publish as a web layer.  I have the script so that it does not automatically add the created geodatabase or shapefile automatically as some cannot be published as feature layers.  I am trying to get just the shapefiles to the map.  The snippet I am using is this:

aprx = arcpy.mp.ArcGISProject("Current")
map = aprx.listMaps()[0]
map.addDataFromPath('C:/directory/filename.shp')

I found a post from bixb0012 which I also tried, from 2017, which is this:

shp_path = # path to shape file

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]  # assumes data to be added to first map listed
map.addDataFromPath(shp_path)

 And it did not work either.

The error I receive on both is this:

Traceback (most recent call last) In  [3]: Line 44:    map.addDataFromPath('C:/GIS_Projects/VMRC_KMZ/VMRC_Oyster_Clam_Data/shp/VMRC_Public_Clamming_Grounds.shp')  File c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py, in addDataFromPath: Line 1779:  return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True))) RuntimeError: ---------------------------------------------------------------------------

 

I am using PRO version 2.6.1.

Thank you for any help / suggestions.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I just tested your, or my, code snippet in Pro 2.6.2 and it works from both the interactive Python Window and Python Notebook.  I see you are running Pro 2.6.1.  It never hurts to apply the latest patch, although I doubt that will change the outcome here.

Instead of

'C:/GIS_Projects/VMRC_KMZ/VMRC_Oyster_Clam_Data/shp/VMRC_Public_Clamming_Grounds.shp'

try using a raw-encoded normal Windows path like

r'C:\GIS_Projects\VMRC_KMZ\VMRC_Oyster_Clam_Data\shp\VMRC_Public_Clamming_Grounds.shp'

View solution in original post

11 Replies
JaredPoe1
New Contributor III

Hi James,

Hope you are doing well today. I took a look at the issue you are seeing and I think I might have an idea as to what could be going on. Based on your code, it looks like you are trying to add a shapefile to the map using the addDataFromMap function, however I believe this function might require a feature layer.

If you try converting the shapefile to a feature layer first, then import, do you still experience issues? Example below.

aprx = arcpy.mp.ArcGISProject("Current")
map = aprx.listMaps()[0]

layer_path = <path>

lyr = arcpy.MakeFeatureLayer_management(layer_path, "layer")

map.addDataFromPath(lyr)

Hope this helps!

0 Kudos
JamesWhite5
New Contributor III

Jared,

Good morning.  I tried adding the above in and I get the same error as before.

Since I am creating a geodatabase, I redid the script to just name the feature class within the geodatabase to the name I want and then tried adding that layer into PRO and received the same error trying it that way as well.

I am not using an IDE.  I am working from the project notebook, so not sure if that might be the issue or not.

Thank you.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I just tested your, or my, code snippet in Pro 2.6.2 and it works from both the interactive Python Window and Python Notebook.  I see you are running Pro 2.6.1.  It never hurts to apply the latest patch, although I doubt that will change the outcome here.

Instead of

'C:/GIS_Projects/VMRC_KMZ/VMRC_Oyster_Clam_Data/shp/VMRC_Public_Clamming_Grounds.shp'

try using a raw-encoded normal Windows path like

r'C:\GIS_Projects\VMRC_KMZ\VMRC_Oyster_Clam_Data\shp\VMRC_Public_Clamming_Grounds.shp'

View solution in original post

RandyBurton
MVP Regular Contributor

I also tested the code snippet and was able to generate the same error message only when the path was incomplete or contained an error.  With a correct path to the shapefile, the file loaded fine.

0 Kudos
BingZheng
Esri Contributor

Hello James,

Your script works for me. 

But it only works when I run the script within ArcGIS Pro.

Did you run it outside of the ArcGIS Pro? If yes, you can't use "current", you have to create an empty Pro project and add the file in.

Regards,

Bing

0 Kudos
JamesWhite5
New Contributor III

Thank you for all of the responses and I will try then when I get back into my office next week.  Life happens, right? 

0 Kudos
JamesWhite5
New Contributor III

Ok, I did get it to work using the r'C:\...' method, but it does give me this error:

arcpy._mp.Layer object at 0x000001A9BFDFEC88

I tried searching for that error but didn't return anything specific as to what might be causing it.

Again, the layers are being added to the current project though.

Thanks,

James


					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am not seeing an error?  All I see is an object reference.

0 Kudos
JamesWhite5
New Contributor III

Oh, you are absolutely correct!  I am used to that being an error popping up right there.  Sorry about that.  Then I guess this is fixed! 

Just out of curiosity though, why does putting an "r" before the path name make a difference?  I thought they were both the same thing either with r'C:/.....' or just going straight to 'C:/.....'

0 Kudos