2

Using ArcGis 10.2

I have a script converting the nodes of a net into polygons. The polygons should be either triangles or rectangles but the script doesn't build polygon as rectangle, skipping the fourth vertice. Being not fluent in python, I can't spot the mistake: it looks like the Array stores or restitute only 3 values instead of 4. Could you please help me to fix this?

enter image description here

Here the code:

import sys
sys.setdefaultencoding('UTF8')
import arcpy
import os
outfileGDB =arcpy.env.workspace= arcpy.GetParameterAsText (0)
arcpy.env.overwriteOutput = True
polygonsFile = arcpy.GetParameterAsText (1)
verticesFile = arcpy.GetParameterAsText (2)
srtxt = arcpy.GetParameterAsText(3)
sr = arcpy.SpatialReference()
sr.loadFromString(srtxt)
Output = arcpy.GetParameterAsText(4)
polygontable = arcpy.TableToTable_conversion(in_rows=polygonsFile,
                                  out_path= outfileGDB,
                                  out_name="polygons_table",
                                  where_clause="")
vertextable = arcpy.TableToTable_conversion(in_rows=verticesFile,
                                  out_path= outfileGDB,
                                  out_name="vertices_tables",
                                  where_clause="")
outfc =arcpy.CreateFeatureclass_management (outfileGDB,Output,"POLYGON","#","DISABLED","ENABLED",sr)
arcpy.AddField_management (outfc,"ID_el", "LONG", "", "", "", "", "NULLABLE")
with arcpy.da.SearchCursor(polygontable,["OID@","Eltyp","ID_el","IDpt1","IDpt2","IDpt3","IDpt4"]) as poly_cur:
    print "polygons"
    if "ELtyp"== "E3T":    
        poly_dict = {x[2]: x[3:5] for x in poly_cur}
    else:
        poly_dict = {x[2]: x[3:6] for x in poly_cur}
with arcpy.da.SearchCursor(vertextable,"*") as vertex_cur:
    print "vertices"
    vertex_dict = {x[1]: x[2:] for x in vertex_cur}
polyArray = {}
for polykey in poly_dict:
    polynodes = poly_dict[polykey]
    coordsList = []
    for polynode in polynodes:
        coords = (v for k,v in vertex_dict.iteritems() if k == polynode)
        for coord in coords:
            coordsList.append(coord)
    polyArray[polykey] = coordsList
features = []
for key,values in polyArray.iteritems():
    features.append((key,arcpy.Polygon(arcpy.Array([arcpy.Point(*value) for value in values]), sr,
                                      True))) #True - for has_z parameter
for feature in features:
    with arcpy.da.InsertCursor(outfc,["ID_el","SHAPE@"]) as cur:
        cur.insertRow(feature)
arcpy.JoinField_management(outfc,"ID_el",polygontable,"ID_el",["IDpt1", "IDpt2", "IDpt3", "IDpt4", "Mat"])      
  • To close your polygons with four vertices there should be five coordinates i.e. the fifth one should be the same as the first. – PolyGeo Oct 21 '15 at 6:54
  • @PolyGeo: it Looks like the mistake was not in this script but in the one used to produce the "polygonsFile" , which I modified to implement the last Point. Your comment put me on the right path. Problem fixed, thanks! – PMessager Oct 21 '15 at 8:39
3

Without delving into your code whenever I see triangles instead of the expected rectangles my suspicion is that only four instead of five coordinates are being used to form them.

For a rectangle you would expect to see five coordinates with the first and last (fifth) being the same, in order to close the polygon.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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