Skip to main content
6 of 8
added 99 characters in body
Rikudo Pain
  • 428
  • 5
  • 17

Polygon Between Contour Line

I have a contour generated from SRTM,
enter image description here

How to fill the space between that contour with polygon (for all part) but it generates field in attribute table automaticaly, example in image above between contour 6 and 7 then the polygon generated should have custom field "nilai" with value "6-7" in attribute table.

I've tried polyline to polygon but it's not working I should enter the value of polygon manually, there are so many to be edited one by one.

1st Method Using Spatial Analyst License (you can zoom in youre browser using "ctrl and +")

  1. Created Polygon (per 1m for 1m - 30m using feature to polygon tool) from contour created from dtm using contour list tool. https://i.sstatic.net/OdBXL.png enter image description here
  2. This is my DTM raster https://i.sstatic.net/mez5C.png enter image description here
  3. Creating Zonal using Zonal Statistic as Table tool https://i.sstatic.net/ieJlz.png enter image description here
  4. Zonal result attribute table https://i.sstatic.net/8w8iG.png enter image description here
  5. Join polygon with zonal result https://i.sstatic.net/LDhA6.png enter image description here
  6. Attribute Table of joined result https://i.sstatic.net/b8Ve5.png enter image description here
  7. Checking the result after i round it, from that result maximum and minimum value are not match with the contour line https://i.sstatic.net/94Gdb.png enter image description here

2nd Method Using Spatial Analyst License I use this script provide by Michael

    >>> import sys, os, arcpy

InPolygons = sys.argv[1]
NearContour = sys.argv[2]
OutPolygons = sys.argv[3]

ElevationFieldName = "contour" # change this to match your field name

InFolder = os.path.dirname(InPolygons) # the folder the input is in
Working = InFolder + "\\Working.gdb"
WorkingContour = Working+"\\Contour"
WorkingPoly = Working+"\\Poly"

arcpy.CreateFileGDB_management(InFolder,"Working.gdb") # create a working space
arcpy.AddMessage("Copy 1")
arcpy.CopyFeatures_management(NearContour,WorkingContour) # need to do this to take charge of names
arcpy.AddMessage("Copy 2")
arcpy.CopyFeatures_management(InPolygons,WorkingPoly) 

# remove 'bad' edges, tagged with elevation = -9999 to show they are a boundary
arcpy.MakeFeatureLayer_management(WorkingContour,"Remove",ElevationFieldName + " = -9999")
arcpy.DeleteFeatures_management("Remove")
arcpy.Delete_management("Remove") # relax, it's just removing the layer, just to keep it clean

# Create a near table
arcpy.AddMessage("Near table")
arcpy.GenerateNearTable_analysis(WorkingPoly,WorkingContour,Working+"\\NearTable","0",closest="ALL")
arcpy.AddField_management(Working+"\\NearTable","Elev","DOUBLE")

try:
    arcpy.AddIndex_management(Working+"\\NearTable","NEAR_FID","NEAR_FID")
except:
    arcpy.AddWarning("Unable to index")

# join and copy elevations
arcpy.AddMessage("Join & calc")
arcpy.MakeTableView_management(Working+"\\NearTable","View")
arcpy.AddJoin_management("View","NEAR_FID",WorkingContour,"OBJECTID")
arcpy.CalculateField_management("View","NearTable.Elev","!Contour."+ElevationFieldName+"!","PYTHON")
arcpy.Delete_management("View") # relax, it's just removing the table view, just to keep it clean

# remove join and summary statistics
arcpy.AddMessage("Make stats")
#arcpy.RemoveJoin_management("View")
arcpy.Statistics_analysis(Working+"\\NearTable",Working+"\\NearStats",[["Elev","MIN"],["Elev","Max"]],"IN_FID")

# copy the min and max to the working contours
arcpy.AddMessage("Copy min/max")
arcpy.AddField_management(WorkingPoly,"MinElev","DOUBLE")
arcpy.AddField_management(WorkingPoly,"MaxElev","DOUBLE")
arcpy.MakeFeatureLayer_management(WorkingPoly,"Layer")
arcpy.AddJoin_management("Layer","OBJECTID",Working+"\\NearStats","IN_FID")
arcpy.CalculateField_management("Layer","Poly.MinElev","!NearStats.Min_Elev!","PYTHON")
arcpy.CalculateField_management("Layer","Poly.MaxElev","!NearStats.Max_Elev!","PYTHON")
arcpy.RemoveJoin_management("Layer")
arcpy.Delete_management("Layer") # relax, it's just removing the layer, just to keep it clean

# check for field
arcpy.AddMessage("Checking for field")
Fields = arcpy.ListFields(WorkingPoly,"nilai")
if not Fields:
    arcpy.AddMessage("-Adding field")
    arcpy.AddField_management(WorkingPoly,"nilai","TEXT",field_length="10")

# format the field with the min and max,
# this could be done with a field calculation
# but in this case I want a special case for isolations
arcpy.AddMessage("Cursoring")
with arcpy.da.UpdateCursor(WorkingPoly,("nilai","MinElev","MaxElev")) as UpCur:
    for Feat in UpCur:
        if (Feat[1]==Feat[2]):
            # values are the same = an isolation
            Feat[0] = str(int(Feat[1]))
        else:
            Feat[0] = "%d - %d" % (Feat[1],Feat[2])
        UpCur.updateRow(Feat)

arcpy.AddMessage("Copy out")
arcpy.CopyFeatures_management(WorkingPoly,OutPolygons) 

Processing : enter image description here

Result http://i.imgur.com/yUS55ZC.png enter image description here

My Polygon and attribute table are shown in the printscreen. Please help, any sugestion using python or else. Thanks.

Rikudo Pain
  • 428
  • 5
  • 17