2

I have a polyline (subrivers) and polygon (subshed) shapefile with both of them are shown below along with tables. Their FID index don't overlap spatially, e.g. surivers second row attribute doesn't overlap with second row attribute of subshed. I want to re-arrange the order of attributes in subshed as per in subrivers such that FID of 'subshed' should read like 1,3,4,2,5 (FROM_NODE attributes) with GRIDCODE values also shuffled accordingly. Any suggestions to do it would be very much appreciative.

fc = "C:/temp/spatial_join.shp"
sc = arcpy.da.SearchCursor(fc, ("FID","SHAPE@))
poly = "C:/temp/polygon.shp"
fc = arcpy.da.InsertCursor(poly, ("FID","SHAPE@"))
row1 = sc.next()
for i in sc:
    fc.insertRow(row1)

del row1
Ibe
  • 1,231
  • 3
  • 20
  • 37

1 Answers1

1

you cannot update your FID field, so you need to create a new feature class where you insert the polygons in the order that you want. However, this is not necesseraly a good idea because the FID could change in case of an edit session. Anyway, the first step is a spatial join to get the attribute of the lines that are inside your polygons. In order to avoid ambiguous spatial relationship, I recommend to do this spatial join using the center point ("feature vertices to point" with "MID" option), but this is not necessary.

In Python, you can use arcpy.da.searchCursor(subshed, ("joint_subriver_FID_FIELD" , "SHAPE@"))

You then loop on the cursor to create a Python tuple

You sort this tuple based on its first element

You create un insertcursor on an empty shapefile

You make a second loop to insert your rows in the order that you want

EDIT :

First step : assign the FID of the polylines to the polygons. Create points to avoid ambiguities between lines and polygon when joining. Create a copy of the line FID field to make sure that it will be transmitted to the points.

arcpy.featureVerticesToPoints_management("lines.shp", "points.shp", "MID") arcpy.SpatialJoin_analysis("polygon.shp", "points.shp", "joinpolygon.shp")

Now you have a new polygon layer with the attributes of the lines. You then need to create a new shapefile of type "polygon" that will store the new geometries. The shapefile should also contain the fields that you want to keep. Based on these two shp (jointpolygon.shp and newpolygon.shp), you can use the following Python script.

fc = "C:/jointpolygon.shp"
sc = arcpy.da.SearchCursor(fc, ("FID_from_lines", "otherfield","SHAPE@)) 

storage = []
for row in cursor:
        storage.append(row)
sortedlist = sorted(storage, key=lambda x: x[0]) #sort based on line FID

poly = "C:/temp/newpolygon.shp"
fcout = arcpy.da.InsertCursor(poly, ("FID_from_lines","otherfield","SHAPE@"))

for i in sortedlist:
    fcout.insertRow(i)

del fcout, sc
radouxju
  • 49,636
  • 2
  • 71
  • 144
  • I get geometry error, while using FID from polyline and inserting them into a polygon. Do you think that writing from polyline to polygon could be the problem? – Ibe Jan 14 '14 at 09:58
  • sorry, this wasn't clear enough. Yous search and update cursor must be for the same geometry. (your empty shp should be polygon type). "Line_FID_Field" is a field that comes from your spatial join of the lines with your polygons. – radouxju Jan 14 '14 at 10:36
  • I have added a snippet where I have "SHAPE@" queried over spatially joined shapefile. This geometry is "polyline" and target geometry is "polygon" -- gives error. If i only use "FID" then get "PolygonM" under shape, which I don't understand. There is nothing in display as well. – Ibe Jan 16 '14 at 19:14
  • I've added some code. HTH – radouxju Jan 16 '14 at 20:19
  • I have ArcView license level so I had to get mid-point values otherwise. This link was helpful. Following thread explains how to insert obtained XY mid-points into a point feature class. Then I used your posted code, and it worked fine. – Ibe Jan 17 '14 at 12:35