3

I'm attempting to convert CSV files to a group of shapefiles, then re-project them to a different coordinate system.

The first conversion works, but I'm having issues with the projection piece.

import arcpy
import os
arcpy.env.overwriteOutput = True
from os import listdir
from os.path import isfile, join, basename

csvpath = arcpy.GetParameterAsText(0)
outshapefolder = r'U:\skelley\Python\FWD_GPR_import\SampleCSVout2'

files = [join(csvpath,f) for f in listdir(csvpath) if isfile(join(csvpath, f))]

for f in files:
    arcpy.MakeTableView_management(in_table=f, out_view='tempview')
    arcpy.MakeXYEventLayer_management(table='tempview', in_x_field='Lat', in_y_field='Lon',
                                 out_layer='tempevent', spatial_reference=4326)
    arcpy.CopyFeatures_management(in_features='tempevent', 
   out_feature_class=join(outshapefolder,basename(f).replace('.csv','.shp')))

#set new projection
outCS = arcpy.GetParameterAsText(2)
SR = arcpy.SpatialReference()  #an empty spatial reference object
SR.loadFromString(outCS)

inshape = r'U:\skelley\Python\FWD_GPR_import\SampleCSVout2'
outshape = arcpy.GetParameterAsText(1)

#loop through new shapefiles to project
for x in listdir(inshape):
    if x.endswith('.shp'):

        #Determine the new output feature class path and name
        infc = os.path.join(inshape, x)
        outfc = os.path.join(outshape, x)

        #Run project tool
        arcpy.Project_management(infc, outfc, outCS, 'NAD_1983_HARN_To_WGS_1984_2')
  • 1
    In your filestoo = [join(inshape,x) for x in listdir(inshape) if isfile(join(inshape, x))] you need to only keep the shapefiles, not all the other files. Try arcpy.ListFeatureClasses resources.arcgis.com/en/help/main/10.2/index.html#//… (set your arcpy.env.workspace = inshape) instead of os.listdir to get only the shape files. The error message is telling you that your file (.cpg) isn't something that it understands as a feature class to be projected not that the file doesn't exist, the error message is a little ambiguous. – Michael Stimson Dec 11 '18 at 22:51
  • 1
    Or, if you prefer: glob.glob(os.path.join(inshape, "*.shp")) – Aaron Dec 11 '18 at 22:54
  • @MichaelStimson - so when you say "only keep the shapefiles" - does that mean literally only something ending in .shp in the file location? I don't really know how this works, but I've noticed that shapefiles appear as one object in the Arcmap catalog, and several in the actual file location. – SarahKelley Dec 11 '18 at 23:17
  • Yes, there are at least 3 files that make a shapefile: .shp, .shx and .dbf see en.wikipedia.org/wiki/Shapefile for more info. If you iterate with listfiles you need to filter out everything but the .shp (you can use os.path.splitext which returns a tuple of file name and extension), arcpy.ListFeatureClasses returns only the shape files, like you see in Catalog. – Michael Stimson Dec 11 '18 at 23:22
  • 1
    Which line of code produces the error? – Emil Brundage Dec 11 '18 at 23:48
3

I guess Michael Stimson is suggesting to do something like:

for x in filestoo:

        if x.endswith('shp'):
            # Determine the new output feature class path and name
            outfc = os.path.join(outshape, x)

            # run project tool
            arcpy.Project_management(x, outfc, outCS)
  • Sort of, it's more like arcpy.env.workspace = r'U:\skelley\Python\FWD_GPR_import\SampleCSVout2' then filestoo = [os.path.join(inshape,x) for x in arcpy.ListFeatureClasses('*.SHP')] which will give you only the feature classes in the workspace; as the workspace is a folder then feature classes are most likely to be shapefiles but can also be CAD files and other, rarer, formats. Specifying the wildcard '*.shp' (not case sensitive) will return a list of only the shapefiles in that folder. – Michael Stimson Dec 13 '18 at 2:33
  • @MichaelStimson I've edited the code above to reflect the current state. It now seems to work until arcpy.Project_management(), then it kicks out this: "ERROR 000670: output Output Dataset or Feature Class is same as input Input Dataset or Feature Class." Does this mean it doesn't like the fact that the output file names are the same as input? I thought it wouldn't matter, since I'm putting them in a different file location. – SarahKelley Jan 7 at 22:02
  • That should only happen if parameter 3 (output workspace) is U:\skelley\Python\FWD_GPR_import\SampleCSVout2, you can reuse the same name but not in the same folder, the input is locked and can't be overwritten, but you can project to os.environ.get('TEMP') and then use arcpy.Copy_management to do the overwriting - a simple two-step change but I would caution against that, it's usually best to keep your original files (or a backup of such) just in case the process does something you didn't anticipate and destroys your data. – Michael Stimson Jan 7 at 22:53
  • @MichaelStimson My output workspace isn't U:\skelley\Python\FWD_GPR_import\SampleCSVout2; that's the weird part. Any theories? – SarahKelley Jan 7 at 23:36
  • Take the join out of filestoo = [join(inshape,x) for x in listdir(inshape) if x.endswith('.shp')].. at this point only the names of the feature classes are required then in arcpy.Project_management(x, outfc, outCS, 'NAD_1983_HARN_To_WGS_1984_2') make the first parameter join(inshape,x) and that should (might) fix the problem. – Michael Stimson Jan 7 at 23:40

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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