Getting vertices from object

5322
14
Jump to solution
06-10-2015 04:54 AM
ExEFan
by
New Contributor

Hi, i am trying to write a code in python that will get me all the vertices from one polygon object. Normally that wouldn't be a problem, but this object comes from Geoview 6, and this polygon object has a hole in the center. In ArcGis 10.2, when you load polygon object, it looks normal. Sketch properties shows all vertices. But when i write a code to print vertices, or to get them in any way, it returns only the outer vertices. It doesn't show (print) 4 vertices inside. Can someone help, and write a code that will print all 19 vertices that this polygon has?

Most importantly, do not change the polygon in any way. When you modify the polygon and save changes, it works. But when i have 1000 polygons, i don't have time to modify them all.

Thank you.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Interesting, If I execute this code:

def main():
    import arcpy
    import json
    fc = r"D:\Xander\GeoNet\Vertices\Sample.shp"

    row_cnt= 0
    with arcpy.da.SearchCursor(fc, ("SHAPE@")) as curs:
        for row in curs:
            row_cnt += 1
            print "feature: {0}".format(row_cnt)
            polygon = row[0]
            print "feature part count: {0}".format(polygon.partCount)
            print "feature point count: {0}".format(polygon.pointCount)
            part_cnt = 0
            for part in polygon:
                part_cnt += 1
                print " - Part: {0}".format(part_cnt)
                pnt_cnt = 0
                for pnt in part:
                    pnt_cnt += 1
                    print "   - pnt_cnt: {0}, {1}".format(pnt_cnt, pnt)

if __name__ == '__main__':
    main()

... it lists:

feature: 1
feature part count: 1
feature point count: 21
 - Part: 1
   - pnt_cnt: 1, 572300,2334 4796865,7241 196,371199999994 0
   - pnt_cnt: 2, 572294,1433 4796862,6476 196,477299999999 0
   - pnt_cnt: 3, 572297,146 4796855,1305 196,995599999995 0
   - pnt_cnt: 4, 572291,4095 4796851,9345 196,8024 0
   - pnt_cnt: 5, 572294,1533 4796846,8767 195,8027 0
   - pnt_cnt: 6, 572300,2434 4796850,1324 195,477599999998 0
   - pnt_cnt: 7, 572303,0768 4796845,6122 195,526299999998 0
   - pnt_cnt: 8, 572298,3959 4796841,5202 191,434299999994 0
   - pnt_cnt: 9, 572292,52 4796839,1306 195,599499999997 0
   - pnt_cnt: 10, 572289,6965 4796844,3179 198,8995 0
   - pnt_cnt: 11, 572283,8255 4796841,2414 197,298299999995 0
   - pnt_cnt: 12, 572277,5213 4796853,9258 196,550499999998 0
   - pnt_cnt: 13, 572280,8427 4796856,7733 198,663799999995 0
   - pnt_cnt: 14, 572274,3592 4796864,141 196,664300000004 0
   - pnt_cnt: 15, 572297,1012 4796873,5896 196,257899999997 0
   - pnt_cnt: 16, 572300,2334 4796865,7241 196,371199999994 0

But, if I access the JSON property of the polygon I do see the two rings:

def main():
    import arcpy
    import json
    fc = r"D:\Xander\GeoNet\Vertices\Sample.shp"

    with arcpy.da.SearchCursor(fc, ("SHAPE@")) as curs:
        for row in curs:
            polygon = row[0]
            jsn = polygon.JSON
            dct = json.loads(jsn)
            rings = dct["rings"]
            rng_cnt = 0
            for r in rings:
                rng_cnt += 1
                print "ring: {0}".format(rng_cnt)
                p_cnt = 0
                for p in r:
                    p_cnt += 1
                    print " - pnt: {0}, {1}".format(p_cnt, p)
            
if __name__ == '__main__':
    main()

... it lists the two rings and all the points:

ring: 1
 - pnt: 1, [572289.7662000004, 4796863.0558, 198.3188999999984, 0]
 - pnt: 2, [572283.2280000001, 4796859.4516, 197.790599999993, 0]
 - pnt: 3, [572288.4415999996, 4796853.5474, 199.08299999999872, 0]
 - pnt: 4, [572293.2719, 4796857.818700001, 199.1073000000033, 0]
 - pnt: 5, [572289.7662000004, 4796863.0558, 198.3188999999984, 0]
ring: 2
 - pnt: 1, [572300.2334000003, 4796865.724099999, 196.37119999999413, 0]
 - pnt: 2, [572294.1432999996, 4796862.647600001, 196.47729999999865, 0]
 - pnt: 3, [572297.1459999997, 4796855.1305, 196.99559999999474, 0]
 - pnt: 4, [572291.4095000001, 4796851.9344999995, 196.8024000000005, 0]
 - pnt: 5, [572294.1533000004, 4796846.876700001, 195.8027000000002, 0]
 - pnt: 6, [572300.2434, 4796850.1324000005, 195.47759999999835, 0]
 - pnt: 7, [572303.0767999999, 4796845.612199999, 195.52629999999772, 0]
 - pnt: 8, [572298.3958999999, 4796841.520199999, 191.43429999999353, 0]
 - pnt: 9, [572292.5199999996, 4796839.1306, 195.59949999999662, 0]
 - pnt: 10, [572289.6964999996, 4796844.3179, 198.89949999999953, 0]
 - pnt: 11, [572283.8255000003, 4796841.2414, 197.29829999999492, 0]
 - pnt: 12, [572277.5213000001, 4796853.925799999, 196.55049999999756, 0]
 - pnt: 13, [572280.8426999999, 4796856.7733, 198.6637999999948, 0]
 - pnt: 14, [572274.3591999998, 4796864.141000001, 196.664300000004, 0]
 - pnt: 15, [572297.1012000004, 4796873.5896000005, 196.25789999999688, 0]
 - pnt: 16, [572300.2334000003, 4796865.724099999, 196.37119999999413, 0]

View solution in original post

0 Kudos
14 Replies
KenBuja
MVP Esteemed Contributor

Please note that the GeoNet Help​ space is for the discussions about GeoNet, not specific applications. I've moved this to the Python​ space, but feel free to move it to a more appropriate space if necessary.

Also, you should add tags to your discussion. This helps others in searching on the topic.

ExEFan
by
New Contributor

Thank you.

0 Kudos
DuncanHornby
MVP Notable Contributor

I've just downloaded your data and ran it through the Feature vertices to Points tool which is an Advance licensed tool and this extracted out to a new dataset all vertices, including those of the inner ring. So it works for me...

Capture.PNG

0 Kudos
ExEFan
by
New Contributor

Thank you, but, as i have said, ArcGis recognizes the polygon, and it's functions work. I am interested in python solution. How to extract vertices from code, without ArcGis functions.

0 Kudos
NeilAyres
MVP Alum

Have you looked at the help "Reading Geometries",

here : http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001t000000

The example is exactly what you need.

ExEFan
by
New Contributor

Yes i have looked there, and this is familiar to me. Like i have said, this would have worked with normal ArcGis polygon. Don't know why isn't working with those polygons created in Geoview 6. I was surprised when i tried in python to get the number of vertices. Function is pointCount. It got all the vertices. When i tried isMultipart, returned True. So it sees all vertices and part0 and part1. But i need to extract their coordinates. And every time i get only the exterior points.

0 Kudos
DuncanHornby
MVP Notable Contributor

If you intend to use a python approach in ArcGIS and you look at Neil's idea you still need to import arcpy to use the cursors. If you are importing arcpy then why make lots of work for yourself when you can simply call the tool to do it! The only reason you would want to do it using cursors and accessing geometries is if you do not have an Advance license. You should state what your license level is in any question as this dictates what sort of solution you will get.

0 Kudos
ExEFan
by
New Contributor

Yes, i want to use arcpy and cursors. I said that i want python solution. The problem is that i can't extract them all from python.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Interesting, If I execute this code:

def main():
    import arcpy
    import json
    fc = r"D:\Xander\GeoNet\Vertices\Sample.shp"

    row_cnt= 0
    with arcpy.da.SearchCursor(fc, ("SHAPE@")) as curs:
        for row in curs:
            row_cnt += 1
            print "feature: {0}".format(row_cnt)
            polygon = row[0]
            print "feature part count: {0}".format(polygon.partCount)
            print "feature point count: {0}".format(polygon.pointCount)
            part_cnt = 0
            for part in polygon:
                part_cnt += 1
                print " - Part: {0}".format(part_cnt)
                pnt_cnt = 0
                for pnt in part:
                    pnt_cnt += 1
                    print "   - pnt_cnt: {0}, {1}".format(pnt_cnt, pnt)

if __name__ == '__main__':
    main()

... it lists:

feature: 1
feature part count: 1
feature point count: 21
 - Part: 1
   - pnt_cnt: 1, 572300,2334 4796865,7241 196,371199999994 0
   - pnt_cnt: 2, 572294,1433 4796862,6476 196,477299999999 0
   - pnt_cnt: 3, 572297,146 4796855,1305 196,995599999995 0
   - pnt_cnt: 4, 572291,4095 4796851,9345 196,8024 0
   - pnt_cnt: 5, 572294,1533 4796846,8767 195,8027 0
   - pnt_cnt: 6, 572300,2434 4796850,1324 195,477599999998 0
   - pnt_cnt: 7, 572303,0768 4796845,6122 195,526299999998 0
   - pnt_cnt: 8, 572298,3959 4796841,5202 191,434299999994 0
   - pnt_cnt: 9, 572292,52 4796839,1306 195,599499999997 0
   - pnt_cnt: 10, 572289,6965 4796844,3179 198,8995 0
   - pnt_cnt: 11, 572283,8255 4796841,2414 197,298299999995 0
   - pnt_cnt: 12, 572277,5213 4796853,9258 196,550499999998 0
   - pnt_cnt: 13, 572280,8427 4796856,7733 198,663799999995 0
   - pnt_cnt: 14, 572274,3592 4796864,141 196,664300000004 0
   - pnt_cnt: 15, 572297,1012 4796873,5896 196,257899999997 0
   - pnt_cnt: 16, 572300,2334 4796865,7241 196,371199999994 0

But, if I access the JSON property of the polygon I do see the two rings:

def main():
    import arcpy
    import json
    fc = r"D:\Xander\GeoNet\Vertices\Sample.shp"

    with arcpy.da.SearchCursor(fc, ("SHAPE@")) as curs:
        for row in curs:
            polygon = row[0]
            jsn = polygon.JSON
            dct = json.loads(jsn)
            rings = dct["rings"]
            rng_cnt = 0
            for r in rings:
                rng_cnt += 1
                print "ring: {0}".format(rng_cnt)
                p_cnt = 0
                for p in r:
                    p_cnt += 1
                    print " - pnt: {0}, {1}".format(p_cnt, p)
            
if __name__ == '__main__':
    main()

... it lists the two rings and all the points:

ring: 1
 - pnt: 1, [572289.7662000004, 4796863.0558, 198.3188999999984, 0]
 - pnt: 2, [572283.2280000001, 4796859.4516, 197.790599999993, 0]
 - pnt: 3, [572288.4415999996, 4796853.5474, 199.08299999999872, 0]
 - pnt: 4, [572293.2719, 4796857.818700001, 199.1073000000033, 0]
 - pnt: 5, [572289.7662000004, 4796863.0558, 198.3188999999984, 0]
ring: 2
 - pnt: 1, [572300.2334000003, 4796865.724099999, 196.37119999999413, 0]
 - pnt: 2, [572294.1432999996, 4796862.647600001, 196.47729999999865, 0]
 - pnt: 3, [572297.1459999997, 4796855.1305, 196.99559999999474, 0]
 - pnt: 4, [572291.4095000001, 4796851.9344999995, 196.8024000000005, 0]
 - pnt: 5, [572294.1533000004, 4796846.876700001, 195.8027000000002, 0]
 - pnt: 6, [572300.2434, 4796850.1324000005, 195.47759999999835, 0]
 - pnt: 7, [572303.0767999999, 4796845.612199999, 195.52629999999772, 0]
 - pnt: 8, [572298.3958999999, 4796841.520199999, 191.43429999999353, 0]
 - pnt: 9, [572292.5199999996, 4796839.1306, 195.59949999999662, 0]
 - pnt: 10, [572289.6964999996, 4796844.3179, 198.89949999999953, 0]
 - pnt: 11, [572283.8255000003, 4796841.2414, 197.29829999999492, 0]
 - pnt: 12, [572277.5213000001, 4796853.925799999, 196.55049999999756, 0]
 - pnt: 13, [572280.8426999999, 4796856.7733, 198.6637999999948, 0]
 - pnt: 14, [572274.3591999998, 4796864.141000001, 196.664300000004, 0]
 - pnt: 15, [572297.1012000004, 4796873.5896000005, 196.25789999999688, 0]
 - pnt: 16, [572300.2334000003, 4796865.724099999, 196.37119999999413, 0]
0 Kudos