Using Python to replicate the Export Data command

5952
17
05-24-2016 05:53 AM
philbennison
New Contributor

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?

0 Kudos
17 Replies
DanPatterson_Retired
MVP Emeritus

I worked earlier... geonet is burping again... I will check later

in text form with gaps...   http:  //  desktop.arcgis.com/en/arcmap/latest/tools/conversion-toolbox/feature-class-to-feature-class. htm

0 Kudos
WesMiller
Regular Contributor III

Have you tried doing these steps in model builder. If you are new to python it may be simpler to work out the process in model builder then ease into python.

What is ModelBuilder?—Help | ArcGIS for Desktop

Add Join—Help | ArcGIS for Desktop

FC2FC that Dan mentions

Layer To KML—Help | ArcGIS for Desktop

RebeccaStrauch__GISP
MVP Emeritus

Also, don't overlook getting it to work "manually" thru the tool, then open the Tools-Geoprocessing-Python window, find the successful tool and "copy as snippet" or just "save" it to a file.  That helps in seeing how the command have it laid out, and it's then relatively easy to convert this to a python script/tool.  In any case, as Dan mentioned, it would give others a look at what you are trying to do, which make help with more direct feedback.

philbennison
New Contributor

Hi Rebecca,

Not sure if don't have a badly installed copy or am just missing stuff.

I Don't have a tool menu. I have a geoprocessing menu and a python window but when i open this it is blank (This is where i have been entering my code)

If there is a feature when i can copy the code that has been generated by my manual operations that would be awesome

Did i mention that i am very new to Python and fairly new to arcGIS 🙂

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Sorry Phil...was typing from memory which can be stuck in older programs.  The Geoprocessing toolbox is where you will find the Python window option....but what I REALLY wanted to point you to is the RESULTs window under Geoprocessing.   The "save as" is if you ran it in the

After you run a script, and it is successful, you can right click on the name of the tool in the Results window and "Copy as python snippet".

This may look intimidating sometimes...but see the snippet, and what you really need (minimum) to get it to run...

#  from the copy from snippet...
arcpy.FeatureClassToFeatureClass_conversion(in_features="D:/___111136Cbu/WorkSpace/2004-05Hunts.mdb/bison_region_hunts", out_path="C:/__temp/test.gdb", out_name="test", where_clause="", field_mapping="""HUNTNO "HUNTNO" true true false 5 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,HUNTNO,-1,-1;RELATED "RELATED" true true false 50 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,RELATED,-1,-1;HUNTLABEL1 "LABEL1" true true false 25 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,HUNTLABEL1,-1,-1;HUNTLABEL2 "LABEL2" true true false 25 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,HUNTLABEL2,-1,-1;UNIT "UNIT" true true false 2 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,UNIT,-1,-1;SUBUNIT "SUBUNIT" true true false 1 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,SUBUNIT,-1,-1;REGION "REGION" true true false 2 Short 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,REGION,-1,-1;HUNTTYPE "HUNTTYPE" true true false 1 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,HUNTTYPE,-1,-1;DESC_ "DESCRIPTION" true true false 50 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,DESC_,-1,-1;COMMENTS "COMMENTS" true true false 40 Text 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,COMMENTS,-1,-1;SHAPE_Length "SHAPE_Length" false true true 8 Double 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,SHAPE_Length,-1,-1;SHAPE_Area "SHAPE_Area" false true true 8 Double 0 0 ,First,#,D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts,SHAPE_Area,-1,-1""", config_keyword="")

# what you really need from this is only required items...unless you are changing some mapping
arcpy.FeatureClassToFeatureClass_conversion(r'D:\___111136Cbu\WorkSpace\2004-05Hunts.mdb\bison_region_hunts', r'C:\__temp\test.gdb', "testout2")

The SaveAs is from the python window, if you are running from that, and will have a cleaner look, similar to the last line in the above.

Again, my apologies for confusing you...hope this makes more sense.

0 Kudos
NeilAyres
MVP Alum

Gee Rebecca,

I would be intimidated by that as well

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Lol.  Ya Neil....drives me nuts that it has to list all of the field mappings and other arguments, even if the don't change or are default.  First thing I do is strip down to the minimum....or if I DO need things like the field mapping, pushing it off to a variable that I can use.....the full snippet is almost unreadable and probably does scare many off.  Too bad too....it's a great tool.

0 Kudos
ChristianPfeifer
New Contributor

A general solution to this problem can be found here: https://desktop.arcgis.com/en/arcmap/10.7/tools/environments/qualified-field-names.htm

simply add: 

arcpy.env.qualifiedFieldNames = False

at the beginning of your script and the joined field names will not be renamed when exporting the data using e.g. arcpy.CopyFeatures_management.

 

0 Kudos