4

I'm working in ArcMap 10.3, and I have a script that searches through all the folder and files in a directory using arcpy.da.Walk, grabs any file containing a key word, and copies it over to new a gdb based on whether it is a point, line, or polygon.

In this case I'm only grabbing vector files, either shapefiles or feature classes.

The problem is that it gets held up when it encounters non-spatial files, like a .jpg, because I'm using desc.shapeType on each file. What I'd like to know is, is there a way to quickly determine if a file is spatial or not, besides adding in some statements about its file extension (i.e.if fc.endswith(".jpg"):?

3
  • Sounds like the naïve solution is a try except else clause. Dec 30, 2015 at 0:36
  • 1
    if hasattr(desc, 'ShapeType'): # continue processing
    – klewis
    Dec 30, 2015 at 23:07
  • maybe use a searchcursor to open up each file and check if it has geometry?...
    – ziggy
    Apr 17, 2017 at 13:28

1 Answer 1

6

You can use the arcpy.ListFeatureClasses to accomplish this there is a type option. Documentation below

http://resources.arcgis.com/en/help/main/10.1%20/index.html#//002z00000011000000

I'm assuming your question is vague for a reason and not poorly written. There are going to have to be some assumptions on my end and the answer can only be psuedo code, because you have not stated what the file types meet your criteria for conversion, and if there are multiple data types. If there are you will have to expand this answer with if/else clause in order to ultimately perform Do something or provide your script in the OP. The da.walk method is a hybrid of os.walk and includes a data type.

I think this may be a misunderstanding of what a feature classes is. A more detailed discussion on the topic can be found here on another thread but the basics:

The definition of feature class is a "homogeneous collections of common features, each having the same spatial representation, such as points, lines, or polygons, and a common set of attribute columns, for example, a line feature class for representing road centerlines."

Feature classes are stored in a container be it a shapefile, DWG, or DB. For high level thinking a shapefile can be thought of as a single feature class while a gdb is many features classes. You script is going to have to target the container that holds the feature class (shapefile or some other container it does not matter) and perform the action on the contents. You can use arcpy.da.Walk to target those containers that meet a certain datatype criteria (provided its an ESRI compatible container).

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  topdown=True,
                                                  datatype="featureclass"):
    for filename in filenames:
        arrcpy.whatever()

Another Example:

As per the ESRI documentation you can query based on dataypes and filetype

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
    datatype='FeatureClass', type=['Polygon', 'Polyline']):  

Here is an example of using da.walk on a data tree

''' 
Created on Aug 25, 2014 
@author: PeterW 
'''  

# Import system modules  
import arcpy  
import os  

# Set current workspace  
workspace = r'E:\Projects\Projects14\H103342\test2.gdb'  
arcpy.env.workspace = workspace  

# Set environment settings  
arcpy.env.overwriteOutput = True  

# Create list of Feature Classes in each Feature Dataset  
fcs = []  

# Iterate through each Feature Dataset within the File Geodatabase  
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype = "FeatureDataset"):  
    for filename in filenames:  
        print filename   
10
  • After following you link, it looks like it may be good to include datatype = FeatureClass in the arguments. It didn't see where it said how to use arcpy.ListFeatureClasses with da.Walk. But then, I didn't add that in my question. (I will now).
    – CSB
    Dec 29, 2015 at 21:51
  • I don't understand why you don't want to target .shp in order to solve this. It's real easy to evaluate to true and solve. To solve this I would need to see the code or have a better explanation ... Can you include it?
    – risail
    Dec 29, 2015 at 22:48
  • I'm not having any issues with .shapefiles, only with non-spatial files since they have no method .shapeType. So far the script has no issues until it encounters a non-spatial file.
    – CSB
    Dec 29, 2015 at 22:59
  • 1
    You could perform a walk and do if == .shp do x. and then perform some function on that list. But you said that's not an option for you.
    – risail
    Dec 29, 2015 at 23:18
  • 1
    Hey @Dan, after seeing how you edited your answer, it looks like I somehow offended you. Wasn't trying to be snotty in my replies, so my apologies if that was the case. I wasn't trying to be vague in my question, either. I thought that when I wrote that I was looking for either points, lines, or polygons it was clear that I was looking for vector data, and from the title of the question, that it had to be spatial - i.e. either shapefiles or feature classes. I edited my question to include that, and I'll try to be clearer in the future.
    – CSB
    Dec 30, 2015 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.