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?
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.
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.
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
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
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
😕
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 🙂
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:
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
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
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.
Dan, your Feature Class to Feature Class—Help | ArcGIS for Desktop is a bad link.
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
if you get stuck, it is easier to comment on your code, so feel free to post it
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 😞
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
FieldMappings—Help | ArcGIS for Desktop
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.