-1

My assignment is to insert (using insertCursor) a new city at 0.02 decimal degrees east and 0.03 decimal degrees south of another city. The cities shapfile did not originally have x-y coordinate fields; I added them manually and only for the latter city. However, I'm still not able to use SHAPE@XY or to even print the x-y coordinates on which the new point will be based.

enter image description here

I've been calling the old city based on its FID (1942), but I'm utterly lost on how to use it to add the new city.

Also, in my code, are the mxd, df, and ll objects necessary, or is it enough to call the shapefile using the featureclass object?

closed as off-topic by BERA, PolyGeo Mar 27 at 8:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – BERA, PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Please paste code as text, it makes it easier for us to copy and modify rather than retyping the whole thing. Everything before featureclass= is superfluous, it's not being used but will result in repetition. It's probably not working because you need to add 'r' in front of your feature class full path or double the backslashes (backslash is the escape char). – Michael Stimson Mar 27 at 2:24
  • In your file paths, use / instead of \. Python will interpret that correctly, even though Windows file paths actually use \. The reason is that certain combinations of \ and other characters (e.g., \n) mean special things to Python, and instances of those will cause your paths to be read incorrectly. – Paulo Raposo Mar 27 at 8:00
1

Consider something like this, using the where clause of the search cursor to limit the cursor to just the one row:

featureclass = r'e:\Schoolwork\GEOG\GEOG5590\Homework\Homework3\CITIES.shp'
# search cursor with just the FID you want, btw it's bad to rely on FIDs 
# in a shapefile as they have a habit of changing
with arcpy.da.SeachCursor(featureclass,'SHAPE@XY','FID = 1942') as SCur:
    for row in SCur:
        XY = row[0] # xy is a tuple (X,Y) to use later
        break      # done the first one, now break out

# BIG assumption that the data is geographic CRS
with arcpy.da.UpdateCursor(featureclass,'SHAPE@XY') as ICur:
    # adjust the values
    newX = XY[0] + 0.02
    newY = XY[1] - 0.03

    ICur.insertRow(((newX,newY))) # insert the tuple of the new X and Y

This assumes you want to insert your new city into the same feature class you sourced the original from and that your data is already geographical. The SHAPE@XY token returns a tuple (X,Y) but you could also use ['SHAPE@X','SHAPE@Y'] which would make your insertRow((newX,newY)) as you're inserting the row as a tuple of values.

Lists can be used instead of tuples, see the examples, as it tends to make it easier to understand whether you're closing the statement, row or tuple:

ICur.insertRow([(newX,newY)]) # insert the tuple of the new X and Y
  • Thank you, this is certainly helpful. To what does the row[0] of the XY = row[0] refer? – andm Mar 27 at 3:09
  • row is the row object, [0] refers to the first (and only) attribute within the cursor.. one important thing to note, fields that aren't specified in the declaration of the cursor don't exist as far as the row is concerned; you can specify '*' as the fields but then you need to get the index of the shape, which will be a geometry object, to access it. – Michael Stimson Mar 27 at 3:27

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