Some basic Python help

536
4
03-14-2013 07:31 PM
DavidPenton
New Contributor
Hello,

I am a beginner when it comes to python and I need to create a script that

1. Loads (copy I guess)  file geodatabase feature classes and shapefiles from a workspace to a separate file geodatabase.
2. Only loads points and polygons, no lines.

I am someone with no experience in scripting or programming languages so any help is appreciated. 

Thanks
Tags (2)
0 Kudos
4 Replies
markdenil
Occasional Contributor III
Very broadly speaking:

to get a list of data sets to copy, use
ListFeatureClasses ({wild_card}, {feature_type}, {feature_dataset})

... with the the feature_type argument:
Polygon (�??Only polygon feature classes are returned.)

then run it again to make another list using:
Point (�??Only point feature classes are returned. )

Then append these lists into one for that source workspace

ListFeatureClasses only reports on the current workspace, so
run ListFeatureClasses to get lists for each source workspace (gdb (for FCs) or directory (for shapefiles))

loop through each list and use CopyFeatures_management
to copy each input to your target gdb
-- make sure your input path is the appropriate one for that particular input list --
0 Kudos
LucasDanzinger
Esri Frequent Contributor
I agree with Mark. You could also do this in ModelBuilder using an iterator, if you feel more comfortable in that environment. This feature class iterator will go through a workspace, grab all the feature classes (you can specify geometry type), then you can just use the Feature Class to Feature Class tool to export to another gdb.
0 Kudos
DavidPenton
New Contributor
Okay so I looked into the suggestions but I am really not up to speed on scripting yet as I am still a beginner.  I then got other suggestions and looked into ArcGIS Help.  I came up with this from trying to look at other examples.  I get a string error when I run it but I am sure that I am probably not really close but here goes.  Is this on the right track?


#Import system modules
import arcpy
from arcpy import env
import os

#Set the workspace.  List all of the polygon and point features#

env.workspace = "C:\Users\marvell2k\ARC\P_Assignment_6\Miramonte\shapefile"

outWorkspace = "C:\Users\marvell2k\ARC\P_Assignment_6\MiramonteFGDB.gdb"

#Use ListFeatureClasses to generate a list of shapefiles in the workspace shown above"

fcList = arcpy.ListFeatureClasses ("polygon", "point")

#Execute CopyFeatures for each input shapefile for shapefile in fcList"
#Determine the new output feature class path and name#

outFeatureClass = os.path.join(outWorkspace, shapefile.strip (".shp"))
arcpy.CopyFeatures_management (shapefile, outFeatureClass)
0 Kudos
TarunJaiswal
Esri Contributor
Okay so I looked into the suggestions but I am really not up to speed on scripting yet as I am still a beginner.  I then got other suggestions and looked into ArcGIS Help.  I came up with this from trying to look at other examples.  I get a string error when I run it but I am sure that I am probably not really close but here goes.  Is this on the right track?


#Import system modules
import arcpy
from arcpy import env
import os

#Set the workspace.  List all of the polygon and point features#

env.workspace = "C:\Users\marvell2k\ARC\P_Assignment_6\Miramonte\shapefile"

outWorkspace = "C:\Users\marvell2k\ARC\P_Assignment_6\MiramonteFGDB.gdb


#Use ListFeatureClasses to generate a list of shapefiles in the workspace shown above"

fcList = arcpy.ListFeatureClasses ("polygon", "point")

#Execute CopyFeatures for each input shapefile for shapefile in fcList"
#Determine the new output feature class path and name#

outFeatureClass = os.path.join(outWorkspace, shapefile.strip (".shp"))
arcpy.CopyFeatures_management (shapefile, outFeatureClass)


Greetings!

Here are some of the suggestions:

1) Please use a string literal r in your env.workspace. Please refer to the help on Setting paths to data in Python
for eg = r"C:\Users\marvell2k\ARC\P_Assignment_6\Miramonte\shapefile"

2) As suggested earlier, create separate list for point and polygons:
for eg: fcList = arcpy.ListFeatureClasses ("* ", "Point")
fcList2 = arcpy.ListFeatureClasses ("*", "Polygon")

3) Loop through each list
for eg: for fc in fcList:
                    # do something
Please refer to the sample code at ListFeatureClasses (arcpy)

Hope this helps.

Thank you!
0 Kudos