Complete newbie here looking for some assistance. I need to convert a shapefile into a KML file and as i do this often was looking at Python to automate it. The shapefile has a join to another database to keep it dynamic so i am having to use the "export data" to make a shapefile which i then amend the field name (alias's) and convert to KML. I have tried the featureclasstofeatureclass_conversion command but this renames the fieldnames? I have then tried makefeaturelayermanagement command but this doesnt bring across the joined fields?
did you try
Copy Features—Help | ArcGIS for Desktop
Feature Class to Feature Class—Help | ArcGIS for Desktop but look again at (fixed link I hope)
but you can control field mapping with the options in FC2FC
Thanks Dan,
I did try to use the help before asking but to be honest as my first bit of python code, it was a bit above me 😞
if you get stuck, it is easier to comment on your code, so feel free to post it
I have just taken bits from archelp so its probably all wrong
import arcpy
arcpy.env.workspace="C:\Users\ME\Docume~1\ArcGIS\Default.gdb"
arcpy.SelectLayerByAttribute_management ("Areamap","NEW_SELECTION"," [Type_Of_area] = 'busy')
This does say that it works but nothing is selected
Probably using a Shapefile as the intermediate format is not the best choice... Have a look a the example below:
I have a featureclass in a fgdb called "Building_lines" with the following field names:
>>> for fld in arcpy.ListFields("Building_lines"): ... print fld.name
OBJECTID Shape LEFT_FID RIGHT_FID Shape_Length Left_Height Right_Height Left_Parcel Right_Parcel Height_dif Wall_Surface Wall_Situation Final_Parcel
If I create a join in the session of ArcMap to another featureclass called "Building_height_sel" and list the fields again, it will show the joined fields too identified by the featureclass name prefix. If I list the fields again it will show:
>>> for fld in arcpy.ListFields("Building_lines"): ... print fld.name
Building_lines.OBJECTID Building_lines.Shape Building_lines.LEFT_FID Building_lines.RIGHT_FID Building_lines.Shape_Length Building_lines.Left_Height Building_lines.Right_Height Building_lines.Left_Parcel Building_lines.Right_Parcel Building_lines.Height_dif Building_lines.Wall_Surface Building_lines.Wall_Situation Building_lines.Final_Parcel Building_height_sel.OBJECTID_1 Building_height_sel.OBJECTID Building_height_sel.HEIGHT Building_height_sel.Shape_Length Building_height_sel.Shape_Area Building_height_sel.BuildingID
As Dan Patterson mentioned you can use the Copy Features tool to export the result and it will honor the joined fields.
>>> arcpy.CopyFeatures_management("Building_lines", r'D:\Temp\test.shp') <Result 'D:\\Temp\\test.shp'>
Listing the fields of this result:
>>> for fld in arcpy.ListFields("D:\\Temp\\test.shp"): ... print fld.name
Will reveal this beautiful list of (not very useful) field names:
FID Shape Building_l Building_1 Building_2 Building_3 Building_4 Building_5 Building_6 Building_7 Building_8 Building_9 Buildin_10 Building_h Buildin_11 Buildin_12 Buildin_13 Buildin_14 Buildin_15
However, if you copy the features to a featureclass in a file geodatabase.
>>> arcpy.CopyFeatures_management("Building_lines", r'D:\Temp\Test.gdb\test') <Result 'D:\\Temp\\Test.gdb\\test'>
... and list the fields of the result:
>>> for fld in arcpy.ListFields("D:\\Temp\\Test.gdb\\test"): ... print fld.name
It will have more useful names:
OBJECTID Shape Building_lines_LEFT_FID Building_lines_RIGHT_FID Building_lines_Left_Height Building_lines_Right_Height Building_lines_Left_Parcel Building_lines_Right_Parcel Building_lines_Height_dif Building_lines_Wall_Surface Building_lines_Wall_Situation Building_lines_Final_Parcel Building_height_sel_OBJECTID_1 Building_height_sel_OBJECTID Building_height_sel_HEIGHT Building_height_sel_BuildingID Shape_Length
Xander,
Thanks for you comprehensive response. I have tried what you suggested but i am still coming up with errors
It is coming up with
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 2335, in CopyFeatures raise e ExecuteError: ERROR 000210: Cannot create output C:\Users\Phiben\Documents\ArcGIS | esti.shp Failed to execute (CopyFeatures). |
I really am starting to feel out of my depth !
btw what tool have you used to run your code.
I am just using the python window
😕
This particular error is due to the file naming. In my code examples I used two forms of the paths to featureclass.
One is using the paths with single slashes like:
r'D:\Temp\test.shp'
Please note the "r" in front of the path. This is to create a raw notation. Python will interpret a string and convert a "\t" into a <TAB>. Hence the error message you received:
"Cannot create output C:\Users\Phiben\Documents\ArcGIS<TAB>esti.shp Failed to execute..."
...when you probably specified "C:\Users\Phiben\Documents\ArcGIS\testi.shp"
The other I used is the double slashes like "D:\\Temp\\Test.gdb\\test"
You may be interested in reading this blog: Filenames and file paths in Python by Dan Patterson.
The code I wrote in my Python windows, inside a session of ArcMap 10.4.
yes...paths can be bad... how many people use r"c:\temp\" or r"c:\temp" as paths
>>> print("c:\temp") c: emp >>> print(r"c:\temp\") Traceback ( File "<interactive input>", line 1 print(r"c:\temp\") ^ SyntaxError: EOL while scanning string literal >>> print(r"c:\temp") c:\temp >>>
more surprises lurk
Dan, your Feature Class to Feature Class—Help | ArcGIS for Desktop is a bad link.