More

Swapping Lat and Lon coordinates of shapefile?

Swapping Lat and Lon coordinates of shapefile?


I have a shapefile that consists of one polygon whose boundary is defined by a large number of points. Whoever originally made the file inverted the latitude and longitude coordinates, so the data displays at 38 degrees east of Greenwich and 122 degrees south of the equator, which is well south of the south pole, or off in space, or somewhere on the imaginary number line, or something. How can I fix this? I could make another shapefile and manually enter the correct coordinates, but this particular polygon is defined by several hundered vertices so I would rather not do it that way. Is there a python script or GP tool that can help me?


You can do it easily with Python. You can do the following in ArcMap's Python Window:

with arcpy.da.UpdateCursor("your_layer_name", ['[email protected]']) as rows: for r in rows: rows.updateRow((r[0][::-1],))

You'll need to refresh the active view afterwards. You can do that with the refresh button orarcpy.RefreshActiveView(). Then you should see the points shift.

EDIT:

Just reread your question again. If you want to skip the point adjustment, and reverse the coordinates for all the vertices of the polygons themselves, the code becomes slightly more complicated but retains the same general principal:

with arcpy.da.UpdateCursor("your_layer_name", ['[email protected]']) as rows: for r in rows: # reverse points within polygon (supports multipart polygons as well) rows.updateRow((arcpy.Polygon(arcpy.Array(arcpy.Array([arcpy.Point(p.Y, p.X) for p in part]) for part in r[0])),))

Does the attribute information hold x and y points? If not you could try Feature to Point to create them and then switch the X and Y.


Export your shapefile as a table, right click the table and choose "Display XY Data". When you enter in the latitude/longitude fields, switch the order of your fields, and now will you have a correct X/Y event layer, which you can export into a gdb as a feature class


You can create new add two fields columns to the shapefile and then calcuate them from the old field column such as X coordinate and Y coordinate to the new field. Then you can delete the old ones from the shapefile.


Watch the video: Python GIS - Open and Display a Shapefile with Geopandas