How to create a point using python and ArcObjects?

418
1
12-18-2010 08:04 AM
karlkliparchuk
New Contributor
Hi.

I have a comma delimited ascii txt file.  It has a sample value, lat, long value in it.  I am trying to convert this text file into a shape file.  I have verified that the data values are being read in correctly, but when I display the shapefile, all the points are on top of each other at the south pole. Which is of course NOT correct.  They should be over Vancouver, BC.  Here is the code.  Can someone point out what they think i am doing wrong?  Thanks in advance.  I've been bashing at this for a few days without success! (Note. for some reason the indenting does not show up for the FOR loop, but it is there.)

Karl

import sys, os, string, math
import arcpy

arcpy.env.workspace = "C:/temp"
outputGDBpath = "C:/temp"
outputGDBname = "particulate"
fc = "MyData"

# Open a new text file to read the coordinates
inFile = open("C:\\temp\\particulate_data.csv", "r")

arcpy.CreateFeatureclass_management(outputGDBpath, "partSamples2.shp", "POINT","","","","C:/temp/VancouverLLwgs84.shp")

outputFC = "partSamples2.shp"

# Process: Add Fields
arcpy.AddField_management(outputFC, "sample", "LONG", 9, "", "", "", "NULLABLE","NON_REQUIRED","")
arcpy.AddField_management(outputFC, "latitude", "DOUBLE", "", "", "", "", "NULLABLE")
arcpy.AddField_management(outputFC, "longitude", "DOUBLE", "", "", "", "", "NULLABLE")

i=1
rows = arcpy.InsertCursor (outputFC)
pnt = arcpy.Point()

# Loop through the coordinate values
for line in inFile.readlines():
   values = string.split(line, ",")  #split up the comma delimited line
   pnt.id= int(i)
   pnt.x= float(values[2])
   pnt.y= float(values[1])
#   arcpy.AddMessage ("Long: "+str(pnt.x)+ " Lat:" +str(pnt.y))
   row = rows.newRow()  # New row into table/cursor
   row.sample = values[0]
   row.latitude = values[1]
   row.longitude = values[2]
   row.shape= pnt  # Assign pnt object property to shape
   rows.insertRow(row)  # Save the row/feature pnt
   i += 1

del row
del rows
inFile.close ()

=== end of code.

Sample data:
1829,49.292669,-123.135382
3109,49.292669,-123.135382
965,49.292669,-123.135382
3973,49.292669,-123.135382
2477,49.292281,-123.136013
0 Kudos
1 Reply
DanielBaril
New Contributor
Hello,

I just can't believe that this kind of script is NOT PROVIDED by ESRI as a basic script for importing csv/test files !....

After many searches here is the bug: the XY fields needs to be in UPPERCASE..... Now it works fine....

Just need to use this:

[INDENT]pnt.X= float(values[2])
pnt.Y= float(values[1])
[/INDENT]
instead of :

[INDENT]pnt.x= float(values[2])
pnt.y= float(values[1])[/INDENT]

Daniel
0 Kudos