1

I have some shapefiles that were produced programatically, and everything came out okay EXCEPT that the geometry information was not written correctly. The coordinates for each feature are null. Luckily it also made a duplicate x,y,z column with the coordinate information in there.

How can I rewrite the coordinates for each feature to pull from the attribute data? Particularly in Python/ArcPy so that I could script it (Java/GeoTools can also be an option).

Edit: These are point data (which makes sense as one x-column, one y-column, one z-column).

6
  • 1
    Would you be able to edit your question to include the feature type (points, lines or polygons) of your shapefiles, as well as some details about how your shapefiles "were produced programatically", please?
    – PolyGeo
    Commented Nov 12, 2014 at 22:57
  • Do you have access to a 3D Analyst extension license?
    – Chris W
    Commented Nov 13, 2014 at 1:28
  • If you are doing this as a one off, export the table and then right click in catalog - select Create Feature Class > From XY Table which will allow you to select the 'z' coordinate for 3d data. It would be much faster than writing code updating the Z. If you're looking to fix your original code you need to do shape@x, shape@y and shape@z on your insert cursor, not just shape@xy. If you want to fix your original code please post some so we can see where you're not including the Z. Commented Nov 13, 2014 at 1:38
  • @ChrisW yup have the license. Commented Nov 13, 2014 at 14:29
  • 1
    There are two possible routes to go. Identify the problem in the original code, which needs more information and an edit to the question, per the comments. Or deal with just fixing your current data. To do that you'll need tables anyway - your shapefiles aren't really valid with no valid geometry, so essentially they already are tables. You might be able to use them (or their dbfs) directly as inputs, but ultimately the quickest route is the Make XY Event Layer (Data Management) tool.
    – Chris W
    Commented Nov 14, 2014 at 21:40

1 Answer 1

2

First you will need an iterator to go through your shape files, there are two methods I employ:

Method One: a folder full of shape files:

import arcpy, sys

InF = sys.argv[1]

arcpy.env.workspace=InF
for fc in arcpy.ListFeatureClasses():

Method Two: a whole tree full of shape files:

import sys, os, arcpy
InFolder = sys.argv[1]

for (path, dirs, files) in os.walk(InFolder):
    for ThisFile in files:
        fName,fExt = os.path.splitext(ThisFile)
        if fExt.upper() == ".SHP":
            fc = path + "\\" + ThisFile

Next, decide where your output is going, for this example I'm using the same folder and calling the new shapefile with the old name appended with _WithZ:

fileName, fileExt = os.path.splitext(fc) # separates file name and extension
outShape = fileName + "_WithZ.shp"

This works for either of the two file iteration methods.

If you already have files that have the output name this may cause problems if overwrite is off so set overwrite to on using arcpy.env arcpy.env.overwriteOutput = True

Then as ChrisW indicated the quickest way to do this is with Make XY Event layer - this is a layer, not a shape file so will have to be copied to such using Copy Features:

# get the existing spatial reference, assuming the X,Y & Z coordinates are in the same system
desc = arcpy.Describe(fc)
spatialRef = desc.spatialReference

# make and export the Event Layer
arcpy.MakeXYEventLayer_management (fc, "POINT_X", "POINT_Y", "Event Layer", spatialRef, "POINT_Z")
arcpy.CopyFeatures_management("Event Layer",outShape)
arcpy.Delete_management("Event Layer") # clear the layer now we're finished with it

There are other ways to make the layer into a shape file; without debating on which one is better or more appropriate in this case using copy features works but feel free to substitute any other method that does essentially the same thing.

That should be enough code to string together a working script to fix the existing problem but it would be better to fix the original script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.