'Trying to create point from X,Y, coordinate

I have used a formula to create an X and Y coordinate that I would like to create a point from. They are held in the attribute table, and I don't know how to use those points for the geometry. Here's the code:

cursor01 = arcpy.da.InsertCursor(OutPutCent,["SHAPE@X", "SHAPE@Y","Xcoord","Ycoord","totpop", "NAME","STATE_NAME","POLY_ID", "OBJECTID", "STATE_FIPS", "CNTY_FIPS", "FIPS", "FIPSnum","FIPS_NUMER" ])
#if row[0] >= 5.2:
cursor01.insertRow([XPoint,YPoint,centroid_X1,centroid_Y1,TotalPop1,thecntyName1,TheStateName1,idpoly1, idobject1, stateFIPS1, countyFIPS1, fips1, fipSnum1, fipsNumer1])

Any suggestions would be much appreciated I need to resolve this asap! Best



Solution 1:[1]

This should probably be in gis.stackexchange.com.

Hard to tell what you're aiming for with that code, but I'll have a guess anyway...If you have existing points you want to relocate based on updated coordinates in an attribute table, try one of the following.

The simple no-scripting solution is this:

  • export your attribute table to csv
  • use Add XY Data to import the csv as a new layer

During the import process you can set the geometry to your Xcoord, Ycoord attribute fields, so the new dataset will have updated locations. Then just prune any records you don't want - looks like you're filtering on some value? Select by Attributes and delete rows that don't meet your condition.

Alternatively, a structurally correct template to update existing geometry with new coordinates from the attibute table is:

with arcpy.da.UpdateCursor(dataset, ['SHAPE@XY', 'point_unique_id_or_name', 'conditional_field', 'X_coord', 'Y_coord']) as cursor:
    update_count = 0
    for row in cursor:
        if row[conditional_field_index] meets condition:
            updated_point = (row[X_coord_index], row[Y_coord_index])
            row[SHAPE@XY_index] = updated_point
            cursor.updateRow(row)
            print "{} location updated".format(row[point_unique_id_or_name_index])
            update_count += 1

print "{} point locations updated".format(update_count)

You only need to reference relevant fields in the UpdateCursor - if they don't get referenced inside the for loop, don't include them.

Strongly advise you practice on a copy of your original dataset.

Solution 2:[2]

you need to import QPoint from QtCore:

from PyQt5.QtCore import QPoint

Then create a qpoint object:

_qpoint = QPoint()

Then add your desired x and y coordinates with the setX, and setY methods:

_qpoint.setX()

_qpoint.setY()

example:

_qpoint.setX(500)

_qpoint.setY(700)

beware of the capital X and Y in the 'set' method of the point object.

For more details, refer to this link.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Reza SA