-1

I am trying to convert several polygon shapefiles into point shapefiles. I can get it to do it one at a time but when I try to get it to go all of them it will just list the files and then give me an error.

One file at a time:

import arcpy
from arcpy import env
import os
import glob

env.workspace = "F:/Thesis/Aridisol/"

inFeatures="F:/Thesis/Aridisol/1_Percent_Random/Polygon/Band4A_900_1.shp"
#  Set local variables

outFeatureClass = "F:/Thesis/Aridisol/1_Percent_Random/Point/Band4A_900_1.shp"

# Use FeatureToPoint function to find a point inside 
arcpy.FeatureToPoint_management(inFeatures, outFeatureClass, "INSIDE")

print "Processing Complete"

Multiple files:

import arcpy
from arcpy import env
import os
import glob

env.workspace = "F:/Thesis/Aridisol/"
outWorkspace= "F:/Thesis/Aridisol/3_Percent_Random/Point/"

featureclasses=glob.glob("F:/Thesis/Aridisol/3_Percent_Random/Polygon/*.shp")

# Get a list of ascii
for fc in featureclasses:
    #Print list
    print fc
    outFeatureClass=fc 

# Use FeatureToPoint function to find a point inside
arcpy.FeatureToPoint_management(featureclasses, outFeatureClass, "INSIDE")

print "Processing Complete"
  • 4
    Your feature to point isn't inside your loop, you still need to do them still one at a time instead of trying to supply a list of strings. If you want the output merged then use merge or append. Instead of using glob use arcpy.ListFeatureClasses('*.shp') resources.arcgis.com/en/help/main/10.2/index.html#//… ... what error do you get? I bet it's something to do with the feature class already existing as out is the same as in, you really should do something about giving the output a different name or path, perhaps outFeatureClass=os.path.join(outWorkspace,fc). – Michael Stimson Apr 21 '17 at 2:43
  • 3
    Welcome to GIS SE. As a new user, please take the Tour. All questions should always contain the exact version of software in use, and should always include the exact error, not just "then give me an error" – Vince Apr 21 '17 at 2:49
0

You need to include your arcpy.FeatureToPoint_management() inside your loop, otherwise it will only run on the last shapefile. Personally I'd also use arcpy.ListFeatureClasses() instead of glob, filtering on Polygon to limit your search to polygon shapefiles.

import arcpy
import os

arcpy.env.workspace = "F:/Thesis/Aridisol/3_Percent_Random/Polygon"
outWorkspace = "F:/Thesis/Aridisol/3_Percent_Random/Point/"

featureclasses = arcpy.ListFeatureClasses("*.SHP", "Polygon") 

# Loop through polygon shapefiles
for fc in featureclasses:
    print fc
    # Give new shapefile name by appending '_point'
    newfc = "{}_point.shp".format(os.path.splitext(fc)[0])
    # Output filepath
    outFeatureClass = os.path.join(outWorkspace, newfc)

    # Use FeatureToPoint function to find a point inside 
    # and save to output filepath
    arcpy.FeatureToPoint_management(fc, outFeatureClass, "INSIDE")

print "Processing Complete"
  • That worked! Thank you! This was much easier than I was making it out to be. – user3325912 May 7 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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