Tuesday 5 April 2016

cursor - Alternative to an arcpy script for points to lines to polygons?


I'm relatively new to arcpy and python.


I have a script that creates triangular polygons given two point shapefiles of centroid points and vertexes around them. I need to run this over ~170k points, which would take more than a week at my available processing power, especially if Murphy's Law strikes during the script execution. I haven't had much luck digging through the Arcpy docs and coming up with anything that could give me the same results in less time.


Can anyone point me in the direction of a different tool chain for this if they can think of one?


Or notice anything in the code that would hamper performance?


with centroidCursor as cecursor:
for record in cecursor:
whereClause = "IN_FID = " + str(record[0])
selection = arcpy.SelectLayerByAttribute_management(nearTableView, "NEW_SELECTION", whereClause) # get the all the records that have that centroid ID from the near table


with arcpy.da.SearchCursor(selection, "NEAR_FID") as selcursor: # then append the vertex IDs to a list
tempList = [row[0]] # create temporary list
for selrow in selcursor:
tempList.append(selrow[0]) # iterate though rows appending vertex IDs to list

idNum = record[0]
whereClause = "OBJECTID = {0} OR OBJECTID = {1} OR OBJECTID = {2}".format(tempList[0],tempList[1],tempList[2])
selected = arcpy.SelectLayerByAttribute_management(cornerpoints, "NEW_SELECTION", whereClause) # select the vertexes from the list
lines = arcpy.PointsToLine_management(selected, "in_memory", Close_Line="CLOSE") # convert points to a temporary lines
poly = arcpy.FeatureToPolygon_management(lines, workspace + "/mesh_polys/polygons{0}".format(idNum)) # then convert lines to polygons stored in a dataset




I'm going to first try going straight from points to polygons with the Geometry class. From the discussion in the comments the follow up will be one of two methods:


A - Using a layer definition query inside the nested searchCursors (I'm unfamiliar with this and it's throwing a bunch of different errors as I'm working with it after FelixIP pointed me in it's direction).


B - creating a custom table with some kind of relevant information tied to the cursor_ID (vertex geometry, perhaps?).




I now understand what the definition queries do and they are absolutely faster.


 whereClause = "OBJECTID IN ({0} , {1} , {2})".format(tempList[0],tempList[1],tempList[2])
cornerpoints.definitionQuery = whereClause
selection = cornerpoints

lines = arcpy.PointsToLine_management(selection, workspace + "/mesh_lines/line{0}".format(row[0]), Close_Line="CLOSE")# convert points to a temporary lines

However, I'm now getting this error when I try to run the script:


ExecuteError: IOError: "GPL0" does not exist

There's very little about what can cause this error that I can find. Anyone know what I'm missing?



Answer



Having looked at your code where is row[] being created, I guess we are not seeing the full picture?


I've seen this "GPL0" error before. It's indicating that the first parameter is invalid. If you look at the syntax section of the help for this tool it states the input features are of Data Type FeatureLayer. As we are not seeing the full code we have no idea how you are creating cornerpoints, is it a FeatureLayer?


I know this is easier said than done, but if it were me I would have abandon the approach you have taken and built the geometries manually. You want to avoid running intensive processing where you are constantly calling a tool within a loop. As I've understood it there is always a cost when calling a tool. Have a look at this page it shows how to build geometries using arcpy.



The first part of your code that builds tempList could be jigged around to read once into a dictionary which would take about 2-3 seconds, then use that to get the points that build the triangles. Again you might want to consider reading those into a dictionary first and then its just reading from dictionaries which will be blisteringly fast!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...