shifting features

4884
18
Jump to solution
01-15-2015 09:05 AM
WendyRolston
New Contributor

I am totally new to script writing and am trying to get the Shifting Features code to work from ArcPy Café.  I can't see what I am doing wrong.

0 Kudos
18 Replies
WendyRolston
New Contributor

Sorry, I am just not getting it.  I have two fields.  One is called x_shift and one is called y_shift.  Both float fields.  Each currently have a value of 200.  I keep getting an error.

import arcpy
in_features = r"C:/Temp/CP_C06.shp"
def shift_features (in_features):
    
    with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY','x_shift','y_shift']) as cursor:
        for row in cursor:
            cursor.updateRow([[row[0][0] + (row[1] or 0), row[0][1] + (row[2] or 0)]])
            
    return
shift_features(in_features)
0 Kudos
DanPatterson_Retired
MVP Emeritus

EDIT

Never mind...need the error message please

0 Kudos
WendyRolston
New Contributor

Traceback (most recent call last):

  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript

    exec codeObject in __main__.__dict__

  File "C:\Student\Script_fieldname.py", line 13, in <module>

    shift_features(in_features)

  File "C:\Student\Script_fieldname.py", line 9, in shift_features

    cursor.updateRow([[row[0][0] + (row[1] or 0), row[0][1] + (row[2] or 0)]])

TypeError: sequence size must match size of the row

>>>

I'm working on a project that, in the past, I ran a move calculation [SHAPE].Move([fieldname],0) in arc3.3.  I work in ArcGIS 10, but use the oldie for a couple of tasks.  We have upgraded and now don't have the use of arc3.3 anymore.  I need to find a (modern) way to shift polygon features way out of their original spatial placement and then back again.  Arcpy Café "shifting features" coding looked promising, but way out of my wheelhouse.  So I am here begging for help.  I did some Python tutorials, but am just not getting it.

Wendy

0 Kudos
AngelaBlanco
New Contributor

Hi Dan,

I am trying to shift points using the function shift_features, but i am getting this error:

Runtime error
Traceback (most recent call last):
  File "<string>", line 10, in <module>
  File "<string>", line 5, in shift_features
AttributeError: 'module' object has no attribute 'da_UpdateCursor'

import arcpy  
in_features =r"B14_20140416_Mornings_UTM32"  
 
def shift_features(in_features):  
  with arcpy.da_UpdateCursor(in_features, ['SHAPE@XY','NEAR_X','NEAR_Y']) as cursor:  
    for row in cursor:  
      cursor.updateRow([[row[0][0] + (row[1] or 0), row[0][1] + (row[2] or 0)]])  
  return  
 
shift_features(in_features)

I am working on a FileGDB with ArcGIS 10.3

NEAR_X and NEAR_Y are attributes of B14_20140416_Mornings_UTM32"  

Thanks for the help

0 Kudos
AngelaBlanco
New Contributor

I got a new error:

Runtime error
Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "<string>", line 4, in shift_features
TypeError: sequence size must match size of the row

0 Kudos
DanPatterson_Retired
MVP Emeritus

Angela, it is working from a table in arcmap and not a file on disc and you need the associated other fields in the table besides the shapefield.  You would provide the function with the layer's name in the shift_features call,

0 Kudos
AngelaBlanco
New Contributor

Thank Dan for the quick answer. Could you provide me an example what I should include?

in_features ="B14_20140416_Mornings_UTM32" is a FeatureClass with points. It contains the Attributes NEAR_X and NEAR_Y.

Best regards,

Angela

0 Kudos
DanPatterson_Retired
MVP Emeritus

Angela... to be on the safe side, I hate messing with existing geometry...

Since your data are in a table already, then it would be far safer to add 2 new fields New_X and New_Y (double) and simply use the field calculator to calculate new values for the point in those fields then use File,Add data, add XY data just like you would for any event theme. (ie New_X would equal !SHAPE! + !Your_X_Shift_Field! ... and similarly for Y

There is a field calculator example which could be modified, if you want to experiment... on a copy... NOTE, totally untested so work on a copy of the file... making my original suggestion even better... but give it a try later when you have time or want to experiment.

Let me know if it works, It will be some time before I am on an Arc* machine.

# code block... python parser

def shape_shifter(shape, shift_x=0, shift_y=0):
    """provide the shift_x and shift_y fields"""
    point = shape.getPart(0)
    point.X += shift_X
    point.Y += shift_Y
    return point

# expression box...
# shape_shifter(!SHAPE!, !X_shift_fld!, !Y_shift_fld!)
# presumably on the Shape field in the field calculator

that can be modified

0 Kudos
AngelaBlanco
New Contributor

Thanks Dan,

I created the new fields how you mencioned and the calculated the new position for every point. Then I used arcpy.MakeXYEventLayer_management and saved it in a new feature.

Now it works.

Best regards

Ángela

0 Kudos