M.Engelsma

Creating shapefile from ASCII file

Discussion created by M.Engelsma on May 29, 2013
Latest reply on Jun 3, 2013 by rzufelt
Hi all,
I'm trying to convert an ASCII file to a point feature class. I've been busy with this little project on and off for a few weeks now but it seems I've run out of ideas finally :).

The idea behind the script below is that it will be implemented as a tool eventually and it has to take other ASCII files as input as well although they have the same structure as the example in the attachment. At least one of them is in the order of 185 MB, so quite large.

The code I have sofar works without errors, it creates a shapefile with the correct name, it adds the correct fieldnames and it creates the correct number of points from the ASCII file in the right location.
There are still two critical things lacking though before I can turn it into a tool:

1) While I figured out how to add the fields I can't seem to get the fieldtype right. Some fields should be of type String but others should be Double or Long. Somebody gave me the tip to use a try and except on the data to test if it can be made into an integer, if not it's text, otherwise it's a Double. I understand the logic behind it, I'm just struggling to implement it. How do you link the result for the try and except to the fields that are created?

2) The script now creates the correct points but the only data that is attached to them is the X,Y data in the shape attributes. Short of *manually* letting the script write each item in the list of the current line to the correct field (in the same way I write the X,Y geometry) is there a quick way of getting all the data from the ASCII to the table correctly? I feel there should be a way but I was not able to find it yet or come up with one.

Thanks in advance for any answers, I hope someone can help me out here.

Cheers,
Marc

#Script settings en voorbereiding
#Check out any necessary licenses

import arcpy, string, os, sys, linecache

arcpy.env.overwriteOutput =1

#Inputs
File = r"S:\GEOINFO\01_Basis_info\12_BAG\Adresseerbare object special\ADRESOBJECT_SPS_20130131.UVA2"

InName =  os.path.splitext(os.path.basename(File))[0]
print InName

OutDatabase = r"S:\GEOINFO\01_Basis_info\12_BAG\BAG.gdb\Adresseerbare_Object_Special"
print OutDatabase

OutShape = str(OutDatabase) + "\\" + str(InName)
print OutShape

InputASCII = file(File, "r")

# Create shapefile
arcpy.CreateFeatureclass_management(r"S:\GEOINFO\01_Basis_info\12_BAG\BAG.gdb\Adresseerbare_Object_Special", InName, "Point", "", "", "", "PROJCS['RD_New',GEOGCS['GCS_Amersfoort',DATUM['D_Amersfoort',SPHEROID['Bessel_1841',6377397.155,299.1528128]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Double_Stereographic'],PARAMETER['False_Easting',155000.0],PARAMETER['False_Northing',463000.0],PARAMETER['Central_Meridian',5.38763888888889],PARAMETER['Scale_Factor',0.9999079],PARAMETER['Latitude_Of_Origin',52.15616055555555],UNIT['Meter',1.0]]")

print "Shapefile created"

#Read and create columns from ASCII
Headerline = linecache.getline (File,4)

TextList = Headerline.split(';')
for Column in TextList:
    arcpy.AddField_management(OutShape, Column, "TEXT")

print "Field names added"

#Create points from ASCII
Icur = arcpy.InsertCursor(OutShape)
LineCorrect = 0
LineError = 0

for Line in InputASCII.readlines ()[4:]:
    try:
       Row = Icur.newRow()

       List = Line.split(";")
       Point = arcpy.Point((List[20]), (List[21]))
       print Point.X
       print Point.Y

       Row.setValue("Shape", Point)
       Icur.insertRow(Row)

       LineCorrect = LineCorrect + 1
       del Row

    except:
       LineError = LineError + 1
       del Row

del Icur

print "LineCorrect = " + str(LineCorrect)
print "LineError = " + str(LineError)

#-------------------------------------------------------------------------------
#Close files
InputASCII.close

Outcomes