An equivalent shift tool for vectors as the one available for rasters,

2647
14
07-14-2019 10:10 AM
JamalNUMAN
Legendary Contributor

An equivalent shift tool for vectors as the one available for rasters,

 

I couldn’t figure out if there is an equivalent shift tool for vectors as the one available for rasters. I’m aware of the “move” tool available in the editor toolbar, but what I need here is a tool that can be added to the model builder

 

Thank you

 

Jamal

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
14 Replies
LanceCole
MVP Regular Contributor

Jamal, 

You can do this using a python script with an update cursor and modifying the Shape@XY parameter.  Here is a link to a code sample Shifting features.  Another example Move Features can be found here on GeoNet posted by Mathew Coyle.  You can create a new script tool using python then use the tool in Desktop or even Model Builder.  See a quick tour of creating tools with Python and its related topics.

On a side note, are your rasters and vector data on the same coordinate systems?  If you are noticing a uniform shift in your data, you may be dealing with multiple coordinate systems and need to set the correct real-time transformations between the systems or Project some of your data to a different system.  

MajdoleenO_A__Awadallah
Occasional Contributor III

Dear Lancw,

May you please guide me how to create tbx model tool for the Shifting features. I am not familiar with the python script.

Thank you in advance.

Best

Majdoleen

MajdoleenO_A__Awadallah
Occasional Contributor III

Please note that I have created the attached script/ toolbox, and despite the fact that the job completed successfully, the feature remains on its place.

here the script:

import arcpy
 
 
Feature = arcpy.GetParameterAsText(0)

X_Value = arcpy.GetParameterAsText(1)

Y_Value = arcpy.GetParameterAsText(2)

def shift_features(Feature, X_Value=None, Y_Value=None):
    """
    Shifts features by an x and/or y value. The shift values are in
    the units of the in_features coordinate system.
 
    Parameters:
    in_features: string
        An existing feature class or feature layer.  If using a
        feature layer with a selection, only the selected features
        will be modified.
 
    X_Value: float
        The distance the x coordinates will be shifted.
 
    Y_Value: float
        The distance the y coordinates will be shifted.
    """
 
    with arcpy.da.UpdateCursor(Feature, ['SHAPE@XY']) as cursor:
        for row in cursor:
            cursor.updateRow([[row[0][0] + (X_Value or 0),
                               row[0][1] + (Y_Value or 0)]])
 
    return

ps: I don't have any experience in script

AhmadSALEH1
Occasional Contributor III

Hi Majdoleen Awadallah

I see that you have one error in the script which is something normal since you are learning python scripting. 

 -You created a function called shift_features but you didn't call it in your script. the function is there sitting in your script but the tool will not execute it because it's not called.

here is a small edit that will make the tool works:

import arcpy
Feature = arcpy.GetParameterAsText(0)

X_Value = arcpy.GetParameterAsText(1)

Y_Value = arcpy.GetParameterAsText(2)

def shift_features(Feature, X_Value=None, Y_Value=None):
    """
    Shifts features by an x and/or y value. The shift values are in
    the units of the in_features coordinate system.

    Parameters:
    in_features: string
        An existing feature class or feature layer.  If using a
        feature layer with a selection, only the selected features
        will be modified.

    X_Value: float
        The distance the x coordinates will be shifted.

    Y_Value: float
        The distance the y coordinates will be shifted.
    """

    with arcpy.da.UpdateCursor(Feature, ['SHAPE@XY']) as cursor:
        for row in cursor:
            cursor.updateRow([[row[0][0] + (X_Value or 0),
                               row[0][1] + (Y_Value or 0)]])

            


    return


shift_features(Feature,X_Value,Y_Value)

Thanks,

MajdoleenO_A__Awadallah
Occasional Contributor III

Thank you Ahmad so much for your prompt help, I tried to use your python but unfortunately I end with the error shown below:

What might be the issue here?

Best

Majdoleen

DanPatterson_Retired
MVP Emeritus

Majdoleen Awadallah‌ it looks like you have a space before 'return'

Code formatting would help rather than screen grabs

/blogs/dan_patterson/2016/08/14/script-formatting 

MajdoleenO_A__Awadallah
Occasional Contributor III

Here's the code which is written by Ahmad before, and as Ahmad mentioned, it worked fine with Ahmad! also I used the python online checker :https://extendsclass.com/python-tester.html

which indicate that their is no error, but ends with an error at the level of Arcmap!!

What do you think?

import arcpy
Feature = arcpy.GetParameterAsText(0)

X_Value = arcpy.GetParameterAsText(1)

Y_Value = arcpy.GetParameterAsText(2)


def shift_features(Feature, X_Value=None, Y_Value=None):
    """
    Shifts features by an x and/or y value. The shift values are in
    the units of the in_features coordinate system.

    Parameters:
    in_features: string
        An existing feature class or feature layer.  If using a
        feature layer with a selection, only the selected features
        will be modified.

    X_Value: float
        The distance the x coordinates will be shifted.

    Y_Value: float
        The distance the y coordinates will be shifted.
    """

    with arcpy.da.UpdateCursor(Feature, ['SHAPE@XY']) as cursor:
        for row in cursor:
            cursor.updateRow([[row[0][0] + (X_Value or 0),
                               row[0][1] + (Y_Value or 0)]])        
    return


shift_features(Feature, X_Value, Y_Value) 
DanPatterson_Retired
MVP Emeritus

An online code checker would fail on the first line since it can't import arcpy.

You should be using a Python IDE like Spyder

/blogs/dan_patterson/2018/12/13/spyder 

that does real time code checking

MajdoleenO_A__Awadallah
Occasional Contributor III

Thank you Dan, |I will try to use it.