How can I create points from attributes?

2283
4
06-20-2013 09:48 AM
SeanWray
New Contributor
I have created a shp file in ArcCatalog and prepopulted my x y data. Now I want to calculate the Shape field based on my x y values. I used to use the code below, but now it doesn't work.

(I know how to add as a table, display XY, and then convert. It seems to me there should be a shorter way to do this)

Is there a python replacement?

Dim pPoint As IPoint
Set pPoint = [Shape]
pPoint.X = [Long]
pPoint.Y = [Lat]

Enter the following in the box under 'Shape = ':

pPoint
Tags (2)
0 Kudos
4 Replies
by Anonymous User
Not applicable
How did you get your coordinates?  You could use arcpy point" rel="nofollow" target="_blank">http://resources.arcgis.com/en/help/main/10.1/index.html#//018... geometry or an insert" rel="nofollow" target="_blank">http://resources.arcgis.com/en/help/main/10.1/index.html#//01... cursor to build your point feature class.  Here is an example of how you can do this by reading x,y coords from a csv and making a point feature class.

This is what the csv looks like:

Line_ID,X_Coord,Y_Coord,Length,NAME1,JOIN_FID,Type
1,2317355.49962,681731.016618,1315.40287879,SPICER AVENUE,1,Start
2,2317485.73951,676450.010776,5316.04898921,SPICER AVENUE,2,Start
3,2317627.9246,671135.874095,5294.36015714,190TH STREET,3,Start
4,2317355.49962,681731.016618,5376.75305576,170TH STREET,4,Start

I just split this list by commas to get the 2nd and third columns which are the x and y coordinates and created a tuple for the shape field.  Here is how it can be done in 10.1.

import arcpy
arcpy.env.overwriteOutput = True


# Coords stored in a csv
csv = r'G:\PROJECTS\Cedar\GeneralScripts\PyLibrary\temp_end_coords.csv'
gdb = r'C:\Testing\test2.gdb'
SR = 3418 # Factory code for state plane Iowa South

# Get coords list as a tuple
coords = []
with open(csv, 'r') as r:
    for line in r.readlines()[1:]: # skip headers
        coords.append(tuple(float(i) for i in line.split(',')[1:3]))

# Create new feature class
fc = arcpy.CreateFeatureclass_management(gdb, 'Points_test','POINT',spatial_reference=SR)
with arcpy.da.InsertCursor(fc, ['SHAPE@']) as irows:
    for cord in coords:
        irows.insertRow((cord,))
print 'Done'
0 Kudos
curtvprice
MVP Esteemed Contributor

Sean, here's how you do it using the Calculate Field tool in 10.x.

Field: Shape

Expression:

pointMove(!SHAPE@!, !XNEW!, !YNEW!)

Code Block:

def pointMove(shape, x_value, y_value):

  point = shape.getPart(0)

  point.X = x_value

  point.Y = y_value

  return point

Parser: PYTHON_9.3

0 Kudos
SeanWray
New Contributor
I manually entered the coords in the attribute table.


How did you get your coordinates?  You could use arcpy point geometry or an insert cursor to build your point feature class.  Here is an example of how you can do this by reading x,y coords from a csv and making a point feature class.

This is what the csv looks like:

Line_ID,X_Coord,Y_Coord,Length,NAME1,JOIN_FID,Type
1,2317355.49962,681731.016618,1315.40287879,SPICER AVENUE,1,Start
2,2317485.73951,676450.010776,5316.04898921,SPICER AVENUE,2,Start
3,2317627.9246,671135.874095,5294.36015714,190TH STREET,3,Start
4,2317355.49962,681731.016618,5376.75305576,170TH STREET,4,Start

I just split this list by commas to get the 2nd and third columns which are the x and y coordinates and created a tuple for the shape field.  Here is how it can be done in 10.1.

import arcpy
arcpy.env.overwriteOutput = True


# Coords stored in a csv
csv = r'G:\PROJECTS\Cedar\GeneralScripts\PyLibrary\temp_end_coords.csv'
gdb = r'C:\Testing\test2.gdb'
SR = 3418 # Factory code for state plane Iowa South

# Get coords list as a tuple
coords = []
with open(csv, 'r') as r:
    for line in r.readlines()[1:]: # skip headers
        coords.append(tuple(float(i) for i in line.split(',')[1:3]))

# Create new feature class
fc = arcpy.CreateFeatureclass_management(gdb, 'Points_test','POINT',spatial_reference=SR)
with arcpy.da.InsertCursor(fc, ['SHAPE@']) as irows:
    for cord in coords:
        irows.insertRow((cord,))
print 'Done'
0 Kudos
by Anonymous User
Not applicable

I manually entered the coords in the attribute table.


Well that doesn't sound like fun....Ok, since you already have the points in that table, you can read them into point geometry objects to create a new point feature class.  I did not know if you are using ArcGIS 10 or 10.1 so this would be the 10.0 version to be safe.

import arcpy, os
arcpy.env.overwriteOutput = True

def CreatePoints(in_table, x_field, y_field, output, SR):

    # Create new feature class
    if arcpy.Exists(output):
        arcpy.Delete_management(output)
    arcpy.CreateFeatureclass_management(os.path.dirname(output),
                                        os.path.basename(output),
                                        'POINT',spatial_reference=SR)
    # Get coords list 
    irows = arcpy.InsertCursor(output)
    rows = arcpy.SearchCursor(in_table)
    for srow in rows:
        x = srow.getValue(x_field)
        y = srow.getValue(y_field)
        point = arcpy.Point(x,y)

        # Insert point objects into shape field
        row = irows.newRow()
        row.Shape = point
        irows.insertRow(row)
    print 'Done'

if __name__ == '__main__':

    in_table = r'C:\Testing\test2.gdb\Roads_Points2'
    x_field = 'X_Coord'
    y_field = 'Y_Coord'
    output = r'C:\Testing\test2.gdb\Roads_Points3'
    SR = 3418 # Factory code for state plane Iowa South

    CreatePoints(in_table, x_field, y_field, output, SR)


You will need to change the variables under the if name = main part.  If you do have ArcGIS 10.0 you will need to find the full path to the .prj file for the SR parameter.  Otherwise, if using 10.1 you can just supply a factory code like I did.  Obviously the output would be the new point feature class.

Of course, you could always skip all this and do the make xy event table, but your original question was how to do something like this in Python instead so the above code should work for you.  Good luck!
0 Kudos