Using text file to create Shapefile


Well, it seems as though all of the things we learn, are learned due to necessity.  I have always worked with either layers or database tables in my Python Geoprocessing scripts.  It has always worked so there has not been a need to try something different, until last week.

Working on a Python tool for importing a file I requested a .dbf table.  I kept running into issues with the formatting or the header names or what have you.  I was asked if I can take a .csv for an input file.  I assumed this was possible so off I went to solve my new issue.  After a couple of quick Google searches I found what I needed and the ESRI thread that I found was answered by a friend of mine named R.D. and if you spend ANY time on the forums, you will see his name.

CODE


import sys, os, glob
import arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create()

# Create the geoprocessor object

## System arguments
# Input Customer .csv
Stops = sys.argv[1]
# Output Stops <a class="zem_slink" title="Shapefile" rel="wikipedia" href="http://en.wikipedia.org/wiki/Shapefile">shapefile</a>
Stops_output = sys.argv[2]

# Set the workspace for the processing files
gp.workspace = os.path.dirname(sys.argv[3])

import arcgisscripting, os
gp = arcgisscripting.create()
gp.AddMessage("\nCreating processing .dbf from .csv...")
gp.TableToDbase_conversion(Stops, os.path.dirname(sys.argv[3]))            #1
gp.AddMessage("\nCreating Event Layer")
gp.MakeXYEventLayer_management(Stops[:-4]+".dbf", "X", "Y", Stops[:-4])            #2

# Create a prj file for Stops_output.shp            #3
coordsys = "Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj"
gp.defineprojection(Stops[:-4], coordsys)

Obviously that is just a snippet of the code but you should get the idea.  #1 shows using the TableToDbase_conversion geoprocessing function.  I have found very little info on this function so I do not know what possible arguments that can be used.  What I do know is that you will get a .dbf file with the exact same name as the original file and the .dbf extension.  #2 Then takes the resulting .dbf from #1 by using the input Stops[:-4] to take the original name and remove the .csv (4 spaces back) and then concatenated it together with “.dbf”.  This then uses the X and Y coordinates to create the new layer and assigns it to the original Stops input name.

In order for the new resulting shapefile to work, you will need to define the projection to the correct coordinate system as seen in #4.

Hope this helps 🙂

BR

%d bloggers like this: