Switch places of X and Y coordinates in polygon feature dataset

6384
6
07-26-2010 04:35 AM
AndreyLabodin
New Contributor III
Hello.
I need to transform coordinate represetnation of polygon feature dataset. This shapefile was created with some unknown software and its data represented like mirror-transformed.

Please help.. Thank you 🙂
0 Kudos
6 Replies
JamesHood
Occasional Contributor
Hello,

There are "Georeferencing" Tools in the Georeferencing toolbar that might help. 


View / Toolbars / Georeferencing

In the drop down list you have options to  flip horizontally or vertically.  I don't know if this will work if you have multiple polygons, but I imagine it would work with a single polygon. 

I hope this helps!
0 Kudos
AndreyLabodin
New Contributor III
Hello,

There are "Georeferencing" Tools in the Georeferencing toolbar that might help. 


View / Toolbars / Georeferencing

In the drop down list you have options to  flip horizontally or vertically.  I don't know if this will work if you have multiple polygons, but I imagine it would work with a single polygon. 

I hope this helps!


Thank you, but It is not a solution.
The problem is that ex-unknown software, called InGeo, uses different order of coordinates (YX).
That`s why exported data looks like morror-transformed around axis, going through 0,0 and 10000,10000 for example. I can use mirror task from task list and set a mirroring axis by snapping it`s vertices to pre-drawed line with coordinates 0,0 - 10000,10000, then x becomes y and y becomes x.
Everything is great, but it is a manual method. This method is appropriate when layer count les then 10 and summary count of features and vertices in them les then 10000000. Otherwise, this is takes a lot of precious time

1 hour to select features
5 hours to wait for "mirror features" operation ends
20 min to delete selected (old) features
3 hours to save edits

I think, the best way is writing some script to
1. collect each vertex
2. switch places x and y (Xnew = y, Ynew = x, or something like that)
3. put converted vertices back in feature
4. be happy 🙂

In this case i can use geoprocessing tools, automate this process

BUT, i am not a programmer. And I need your help, because it seems like i will get same dumb files (updated) every 3 months until the end of time (2012 🙂
0 Kudos
JamesHood
Occasional Contributor
Apparently the vertices of a polygon can be read ad outputted to a .txt file.  using the:
Samples / Conversion / Data management  / Write Features to Data File tool.  The reverse can be done here too.  See: create feature from text file. 

http://forums.esri.com/Thread.asp?c=93&f=992&t=301240

So In theory a script could be written to read the poly,  Output the txt file,  perform the math
(+100000x, -100000y) , and then rewrite polygon with .txt file.  I'll see if I can slap a sample together for you.
0 Kudos
StevenLambert
New Contributor
Paying it forward.....Try this.....

# -------------------------------------------------------------------------
#Tool Name:            Polygon_Mirror
#Source Name:          Polygon_mirror.py
# ESRI SW Version:     ArcGIS 10.0
# Application Version  1.0
#Author:               Steven R. Lambert, ESRI, Inc.
#
#Revision(s):          Version 1.0  Date:   August 03, 2010
#
# Input: Polygon feature class whise X,Y coordinates are to be reversed
#        (Supports multipart polygon features)
# Output: Polygon featureclass with X,Y coordinates reversed
#
# -------------------------------------------------------------------------

# Import system modules

import arcpy
import sys
import os
import traceback
import string

def Reverse_XY(infc, outfc):

# Reverses the X,Y coordianates of a polygon feature class (mirrors)

# Test to see in infc & outfc exist

    if not arcpy.Exists(infc):
        AddMsgAndPrint("Input feature class not found")
        return

    if not arcpy.Exists(outfc):
        AddMsgAndPrint("Output feature class does not exists")
        return

# Define cursors for input & output

    mrows = arcpy.SearchCursor(infc)
    mrow = mrows.next()
   
    fcrows = arcpy.InsertCursor(outfc)
    fcrow = fcrows.next()

    AddMsgAndPrint("Cursors Created...")

# Update progress info in dialog box

    arcpy.SetProgressor("default")
    result = arcpy.GetCount_management(infc)
    count = int(result.getOutput(0))
    fcount = 0

    AddMsgAndPrint("Progressor Created...")

# Create temporary geometry objects

    aPoly_Array = arcpy.Array()
    aPt = arcpy.Point()
    rPt = arcpy.Point()

# For each discrete feature in the source feature set

    shapefieldname = arcpy.gp.Describe(infc).ShapeFieldName

    while mrow:
        fcount += 1
        partnum = 0
        aPoly = mrow.getValue(shapefieldname)
        partcount = aPoly.partCount

# Update progressor message

        progressMessage = "Processing source feature: " + str(fcount) + " of " + str(count)
        arcpy.SetProgressorLabel(progressMessage)
       
        while partnum < partcount:
            part = aPoly.getPart(partnum)
            aPt = part.next()
            while aPt:
                rPt.X, rPt.Y = aPt.Y, aPt.X
                aPoly_Array.append(rPt)
                aPt = part.next()
            rPoly = fcrows.newRow()
            rPoly.Shape = aPoly_Array
            fcrows.insertRow(rPoly)
            partnum += 1
            aPoly_Array.removeAll()
       
        mrow = mrows.next()     
   
    return

def AddMsgAndPrint(message):
    arcpy.AddMessage(message)
    print message
    return 0

#
#+-------------------------------------------------------------------------------------+
#                                      Main Process Loop
#+-------------------------------------------------------------------------------------+

# Create the Geoprocessor object and set the overwrite setting

arcpy.OverWriteOutput = True

try:

#
# Input Parameters
#
# infc  -   input polygon feature class
# outfc -   output feature class

    infc = arcpy.GetParameterAsText(0)
    outfc = arcpy.GetParameterAsText(1)

    Reverse_XY(infc, outfc)

    AddMsgAndPrint("Polygon mirroring completed...")
 
# Done

except arcpy.ExecuteError:

# Get the geoprocessing error messages

    msgs = arcpy.GetMessage(0)
    msgs += arcpy.GetMessages(2)

# Return gp error messages for use with a script tool

    arcpy.AddError(msgs)

# Print gp error messages for use in Python/PythonWin

    print msgs

   
except:

# Get the traceback object
    #
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]

    # Concatenate information together concerning the error into a
    #   message string
    #
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)

    # Return python error messages for use with a script tool
    #
    arcpy.AddError(pymsg)

    # Print Python error messages for use in Python/PythonWin
    #
    print pymsg



Add this script to a toolbox, specify 1st parameter as "infc" a Feature Class and 2nd parameter as "outfc" a Feature Class.  Progressor will count down polygons as it reverses......

Let me know if this is what you needed.....Good luck
0 Kudos
StevenLambert
New Contributor
This may be easier...

# -------------------------------------------------------------------------
#Tool Name:            Polygon_Mirror
#Source Name:          Polygon_mirror.py
# ESRI SW Version:     ArcGIS 10.0
# Application Version  1.0
#Author:               Steven R. Lambert, ESRI, Inc.
#
#Revision(s):          Version 1.0  Date:   August 03, 2010
#
# Input: Polygon feature class whise X,Y coordinates are to be reversed
#        (Supports multipart polygon features)
# Output: Polygon featureclass with X,Y coordinates reversed
#
# -------------------------------------------------------------------------

# Import system modules

import arcpy
import sys
import os
import traceback
import string

def Reverse_XY(infc, outfc):

# Reverses the X,Y coordianates of a polygon feature class (mirrors)

# Test to see in infc & outfc exist

    if not arcpy.Exists(infc):
        AddMsgAndPrint("Input feature class not found")
        return 

    if not arcpy.Exists(outfc):
        AddMsgAndPrint("Output feature class does not exists")
        return 

# Define cursors for input & output

    mrows = arcpy.SearchCursor(infc)
    mrow = mrows.next()
    
    fcrows = arcpy.InsertCursor(outfc)
    fcrow = fcrows.next()

    AddMsgAndPrint("Cursors Created...")

# Update progress info in dialog box

    arcpy.SetProgressor("default")
    result = arcpy.GetCount_management(infc)
    count = int(result.getOutput(0))
    fcount = 0

    AddMsgAndPrint("Progressor Created...")

# Create temporary geometry objects

    aPoly_Array = arcpy.Array()
    aPt = arcpy.Point()
    rPt = arcpy.Point()

# For each discrete feature in the source feature set

    shapefieldname = arcpy.gp.Describe(infc).ShapeFieldName

    while mrow:
        fcount += 1
        partnum = 0
        aPoly = mrow.getValue(shapefieldname)
        partcount = aPoly.partCount

# Update progressor message 

        progressMessage = "Processing source feature: " + str(fcount) + " of " + str(count)
        arcpy.SetProgressorLabel(progressMessage)
        
        while partnum < partcount:
            part = aPoly.getPart(partnum)
            aPt = part.next()
            while aPt:
                rPt.X, rPt.Y = aPt.Y, aPt.X
                aPoly_Array.append(rPt)
                aPt = part.next()
            rPoly = fcrows.newRow()
            rPoly.Shape = aPoly_Array
            fcrows.insertRow(rPoly)
            partnum += 1
            aPoly_Array.removeAll()
        
        mrow = mrows.next()      
    
    return

def AddMsgAndPrint(message):
    arcpy.AddMessage(message)
    print message
    return 0

#
#+-------------------------------------------------------------------------------------+
#                                      Main Process Loop
#+-------------------------------------------------------------------------------------+

# Create the Geoprocessor object and set the overwrite setting

arcpy.OverWriteOutput = True

try:

#
# Input Parameters
#
# infc  -   input polygon feature class
# outfc -   output feature class

    infc = arcpy.GetParameterAsText(0)
    outfc = arcpy.GetParameterAsText(1)

    Reverse_XY(infc, outfc)

    AddMsgAndPrint("Polygon mirroring completed...")
  
# Done

except arcpy.ExecuteError:

# Get the geoprocessing error messages

    msgs = arcpy.GetMessage(0)
    msgs += arcpy.GetMessages(2)

# Return gp error messages for use with a script tool

    arcpy.AddError(msgs)

# Print gp error messages for use in Python/PythonWin

    print msgs

    
except:

# Get the traceback object
    #
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]

    # Concatenate information together concerning the error into a 
    #   message string
    #
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)

    # Return python error messages for use with a script tool
    #
    arcpy.AddError(pymsg)

    # Print Python error messages for use in Python/PythonWin
    #
    print pymsg
0 Kudos
JamesHood
Occasional Contributor
In the event that you don't have ArcGIS 10.0,  I have your code + tool bar tool I promised. 

I asked a friend if he would help me in figuring out some code,  and he  just created it for me so it all works out in the end! 

Attached is a .zip file called "JIMCODE"... 
Inside there is a file  JIM.mxd.
Open the .mxd.
Load your backwards Polygon file. 

Go to the toolbox / Samples / Conversion / Data management / Write Features to Data File tool.

Input your polygon and save the text to your workspace.



THEN,  click on the JIM tool at the bottom of your standard ESRI toolbar...  (Thats why you had to load this mxd,  because I don't know how to make it a free standing tool.  My friend did all the work)

Using the JIM tool, input your newly created txt file, and the tool will output an output text file into the same workspace directory. 

Finally,  go back to the toolbox, and use the reverse  tool : "create features from text file" and select the newly created text file. 

I don't know how to load the JIM Tool into a model,  but if you could,  that would be the slickest coordinate switcher ever. 

Also,  I apologize that every thing is named JIM,  I should have specified to my friend, what we were doing with this thing... plus I never asked him to write it but since I don't know VBA,  I couldn't say no after the fact. 

So now you have  lots of help.  Hopefully one of them works!

Let us know!

- JIM
0 Kudos