Shifting X coordinates using SHAPE@X

840
4
Jump to solution
07-04-2018 02:57 AM
anTonialcaraz
Occasional Contributor II

Hi,

I need to shift by 360° points that are offset. It is just 7 columns of points in the West on the X axis that need to be shifted (see pictures  and script below).  I'm getting this error: TypeError: 'float' object has no attribute '__getitem__'

Also, I'm not sure if I'm selecting/identifying the 7 columns of points I want correctly on my script.

Any help will be much appreciated.

import arcpy
import string
import math


FC = r"H:\NETcdf_TEST\TEST.gdb\rast2point"

x_shift = float(360)

with arcpy.da.UpdateCursor(FC, ['SHAPE@X']) as cursor:

    for row in cursor:

        cursor.updateRow([[row[0][7] + x_shift]])

    del cursor

0 Kudos
1 Solution

Accepted Solutions
anTonialcaraz
Occasional Contributor II

Thanks for all the suggestions. I did it selecting first the points I wanted to move. Maybe not very elegant but it works.

import arcpy
from arcpy import env
from arcpy.sa import *



FC1 = r"H:\NETcdf_TEST\TEST.gdb\rast2point" # This is the point FC after resampling (0.25/bilinear) and converting the shifted netcdf
FC2 = r"H:\NETcdf_TEST\TEST.gdb\rast2point2"
FC1new = r"H:\NETcdf_TEST\TEST.gdb\rast2point_new1"
FC2new = r"H:\NETcdf_TEST\TEST.gdb\rast2point2_new2"
FCfinal = r"H:\NETcdf_TEST\TEST.gdb\rast2pointFinal"


# Add XY coordinate fields and calculate field geometry

arcpy.AddXY_management(FC1)


# Make a copy of FC1

arcpy.Copy_management(FC1,FC2)


# Make layers from FCs

arcpy.MakeFeatureLayer_management(FC1, "FC1_lyr")
arcpy.MakeFeatureLayer_management(FC2, "FC2_lyr")


# Select point values <-180 (FC1) and point values >-180 (FC2)

arcpy.SelectLayerByAttribute_management("FC1_lyr", "NEW_SELECTION", '"POINT_X" <-180')
arcpy.SelectLayerByAttribute_management("FC2_lyr", "NEW_SELECTION", '"POINT_X" >-180')


# Write the selected features to a new featureclass

arcpy.CopyFeatures_management("FC1_lyr", FC1new)
arcpy.CopyFeatures_management("FC2_lyr", FC2new)


# Perform the move of FC1new

xOffset = 360.000
yOffset = 0.000

with arcpy.da.UpdateCursor(FC1new, ["SHAPE@XY"]) as cursor:
    for row in cursor:
        cursor.updateRow([[row[0][0] + xOffset,row[0][1] + yOffset]])


# Refresh GDB

arcpy.RefreshCatalog(r"H:\NETcdf_TEST\TEST.gdb")


# Merge FC1new and FC2new into FCfinal

arcpy.Merge_management([FC1new, FC2new], FCfinal)

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

try [:7] instead of [7]

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you selecting first only the points you want to shift?  If you have a feature class with lots of points and only want to shift some, you either have to understand how the points are ordered or only select those you want to shift; otherwise, you won't have any way to know whether a point needs to be shifted or not.

In your code above, row will be a list with a single X value, the X value of the "first" point, whatever that point is depending on the ordering of the cursor.  Your code is failing because row[0] returned a float, and floats don't support indexing, slicing, or other similar functions that sequences support.

curtvprice
MVP Esteemed Contributor

Your points look like a raster that has been converted to points.

Do you want to shift a raster?  This can be done using the Shift tool in Desktop and Pro.

(Thought it was worth mentioning.)

anTonialcaraz
Occasional Contributor II

Thanks for all the suggestions. I did it selecting first the points I wanted to move. Maybe not very elegant but it works.

import arcpy
from arcpy import env
from arcpy.sa import *



FC1 = r"H:\NETcdf_TEST\TEST.gdb\rast2point" # This is the point FC after resampling (0.25/bilinear) and converting the shifted netcdf
FC2 = r"H:\NETcdf_TEST\TEST.gdb\rast2point2"
FC1new = r"H:\NETcdf_TEST\TEST.gdb\rast2point_new1"
FC2new = r"H:\NETcdf_TEST\TEST.gdb\rast2point2_new2"
FCfinal = r"H:\NETcdf_TEST\TEST.gdb\rast2pointFinal"


# Add XY coordinate fields and calculate field geometry

arcpy.AddXY_management(FC1)


# Make a copy of FC1

arcpy.Copy_management(FC1,FC2)


# Make layers from FCs

arcpy.MakeFeatureLayer_management(FC1, "FC1_lyr")
arcpy.MakeFeatureLayer_management(FC2, "FC2_lyr")


# Select point values <-180 (FC1) and point values >-180 (FC2)

arcpy.SelectLayerByAttribute_management("FC1_lyr", "NEW_SELECTION", '"POINT_X" <-180')
arcpy.SelectLayerByAttribute_management("FC2_lyr", "NEW_SELECTION", '"POINT_X" >-180')


# Write the selected features to a new featureclass

arcpy.CopyFeatures_management("FC1_lyr", FC1new)
arcpy.CopyFeatures_management("FC2_lyr", FC2new)


# Perform the move of FC1new

xOffset = 360.000
yOffset = 0.000

with arcpy.da.UpdateCursor(FC1new, ["SHAPE@XY"]) as cursor:
    for row in cursor:
        cursor.updateRow([[row[0][0] + xOffset,row[0][1] + yOffset]])


# Refresh GDB

arcpy.RefreshCatalog(r"H:\NETcdf_TEST\TEST.gdb")


# Merge FC1new and FC2new into FCfinal

arcpy.Merge_management([FC1new, FC2new], FCfinal)
0 Kudos