In version 9.3 there is a toolbox called Create Features from Text File, which can do what you require. The help files are quite detailed and are found here.
The following code sample should be simple enough to adapt to reflect your dataset:
# Create geoprocessing dispatch object
import arcgisscripting
gp = arcgisscripting.create()
# Set up inputs to tool
inTxt = r"C:\temp\StreamPoints.txt"
inSep = "." #the symbol used to indicate a decimal point
output = r"C:\temp\Streams.shp"
# Run tool
gp.CreateFeaturesFromTextFile(inTxt, inSep, output, "#")
UPDATE:
If you require numerous attributes of a range of types to be written to a feature dataset from a text file or csv file, it my be easiest to use pyshp. I wrote up an answer to a similar question about pyshp a few days ago and will reproduce the relevant parts of this answer below.
The pyshp/xbase datatyping has always been tricky for me until I found this user guide for the xbase format and as a result of this question I have written a small note on my blog regarding the relevant pyshp datatypes, part of which I have pasted below:
- C is ASCII characters
- N is a double precision integer limited to around 18 characters in length
- D is for dates in the YYYYMMDD format, with no spaces or hyphens between the sections.
- F is for floating point numbers with the same length limits as N
- L is for logical data which is stored in the shapefile's attribute table as a short integer as a 1 (true) or a 0 (false). The values it can receive are 1, 0, y, n, Y, N, T, F or the python builtins True and False
There is a DateTime type as well which should allow you to store correctly formatted time data as well using the same syntax as the above datatypes.
Here is an example python script showing the use of the various pyshp datatypes being read in from lists, which could be populated with your data using the csv module:
import shapefile as shp
#Input parameters
out_file = 'GPS_Pts.shp'
#Set up blank lists for data
x,y,id_no=[1.0,2.4,3.9],[4.3,5.5,6.1],[55,16,10]
date,notes = ['20121012','20121012','20121011'],['This','is a','string']
logic = [True,False,True]
#Set up shapefile writer and create empty fields
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('X','F',10,5)
w.field('Y','F',10,5) #float - needed for coordinates
w.field('Date','D') #date
w.field('Notes','C',50) #string
w.field('ID_N','N',12) #double
w.field('Logic','L',1)#boolean
#loop through the data and write the shapefile
for j,k in enumerate(x):
w.point(k,y[j]) #write the geometry
w.record(k,y[j],date[j], notes[j], id_no[j], logic[j]) #write the attributes
#Save shapefile
w.save(out_file)
You combine whereClause3 and whereClause4 with the AND operator.
As gm70560 wrote, the variables workforceField, workforceindex are not defined. That's why, first you have to define them. I guess workforceindex is a double or at least a number datatype like farmfieldindex. So you have to Change:
whereClause3 = "\"%s\" = '%s'" % (workforceField, workforceindex)
to:
whereClause3 = "\"%s\" = %s" % (workforceField, workforceindex)
The whereClause4 is not defined (commented out). You have to change
whereClause = "\"%s\" = '%s'" % (farmField, farmfieldindex)
to:
whereClause4 = "\"%s\" = %s" % (farmField, farmfieldindex)
Is that right? (I removed the single quotes for the farmfieldindex)
Another reason for getting an error should be the following:
whereClause3 + "AND" + whereClause4
Is the same like: "\"%s\" = %s" + "AND" + "\"%s\" = %s"
Is the same like: "\"%s\" = %sAND\"%s\" = %s"
Thus, you have no blank between the value (workforceindex) of the first part of the where clause and the AND Operator. Change the Select Layer by Attribute Function to the following and it should work:
arcpy.SelectLayerByAttribute_management(countiesL, "NEW_SELECTION", whereClause3 + " AND " + whereClause4)
If it still not working check the delimiters in the where clause (have a look at the AddFieldDelimiters function)
Best Answer
Using ogr2ogr
The simplest way to do this is probably to just use ogr2ogr, no need for code. Something like this:-
Replace XXXX with the CRS of your shapefile.
In Python
If you want to use Python, you can do this using shapely / fiona / pyproj. This will be slower than ogr2ogr, but more flexible.
This code will reproject a polygon shapefile and write the results as geoJSON. It doesn't work with points at the moment.
This holds the whole geojson in memory, so you may need to refactor it for very large shapefiles.
In GeoJSON, coordinates are always in X,Y format (lon,lat) rather than Y,X format (lat,lon).