I am interested in converting a polygon featureclass created via Create Fishnet (Data Management) from singleparts to multipart. QGIS has a very handy tool called singleparts to multipart that does the job, however, I need to stay within the arcpy/Python realm. The ArcGIS equivalent appears to be Dissolve, yet the tool does not preserve individual feature geometry when the polygons share a border.
The following is an example of the type of fishnet polygon data I am working with, which was generated from the below script.
import arcpy
# Create a fishnet with 9 columns and 9 rows
# with origin at (1, 1) and output geometry is set to default (POLYGON)
arcpy.CreateFishnet_management("C:/temp/fishnet2.shp", "1 1", "1 9", "1", "1", "9", "9", "#", "NO_LABELS", geometry_type = "POLYGON")
How can I convert a fishnet polygon from singleparts to multipart using a scripting approach?
*Note. There is a similar question at GIS SE (Create a single connected polygon from multiple polygons), although it appears to be limited to desktop tools. I am looking for any Python or arcpy-based approaches.
Answer
I am not sure if the following will not do the same thing as dissolve, but if I'm correct, it should not.
You can use a SearchCursor()
to loop through the polygons, get each polygon's geometry, add these as parts on a new polygon geometry object, and use an InsertCursor()
to insert this new record.
sc = arcpy.SearchCursor("c:/temp/fishnet2.shp")
ic = arcpy.InsertCursor("yournewfeatureclass.shp")
newRow = ic.newRow()
array = arcpy.Array()
for row in sc:
geom = row.getValue("shape").getPart(0)
array.add(geom)
polygon = arcpy.Polygon(array)
newRow.setValue("shape", polygon)
ic.insertRow(newRow)
del sc, ic
Note: this inserts all of the polygons if your initial feature class as one multipart polygon in the output feature class. You can use MakeFeatureLayer_management()
and SelectLayerByAttribute_management()
to select the right polygons to be added together.
Okay, so I didn't test the above and it didn't work, unfortunately (anyone know how to strikethrough text?). Below is the result of my test. Curiously, the effect wasn't the same for an FGDB feature class and a shapefile. The polygons in the searchcursor are sorted by Objectid, the bottomleft being the first, traveling first upward along colums, then along rows.
No comments:
Post a Comment