Issues creating a polygon by reading its geometry

1075
8
01-19-2017 08:23 AM
ErnestoCarreras3
Occasional Contributor

Need to print the polygon coordinates as one single line text string instead of multi-line. This output will be used as a variable to generate the polygon later in the code. NOTE: The geometry in the searchCursor is a polygon.

for row in arcpy.da.SearchCursor("countAddrPnts_lyr", ["OID@", "SHAPE@", "SITUS_STREET_NUMBER_END2", "SITUS_STREET_NUMBER2"]):x = 0for part in row[1]:    for pnt in part:        if pnt:            pntArray = ("arcpy.Point(" + "{}, {}".format(pnt.X, pnt.Y) + "),")            print pntArray     x += 1

The current output looks like this:

arcpy.Point(907781.079669, 628629.095669),arcpy.Point(907782.765669, 628579.116669),arcpy.Point(907649.596669, 628574.910669),arcpy.Point(907647.068669, 628649.878669),arcpy.Point(907780.236669, 628654.092669),arcpy.Point(907781.079669, 628629.095669),

I need it to look as follows:

arcpy.Point(907781.079669, 628629.095669), arcpy.Point(907782.765669, 628579.116669), arcpy.Point(907649.596669, 628574.910669), arcpy.Point(907647.068669, 628649.878669), arcpy.Point(907780.236669, 628654.092669), arcpy.Point(907781.079669, 628629.095669)

Any suggestions will be appreciated.

arcpy - Issues creating a polygon by reading its geometry - Geographic Information Systems Stack Exc... 

Tags (3)
0 Kudos
8 Replies
MicahBabinski
Regular Contributor

Heya Ernesto,

How about this:

pntArray = []

for row in arcpy.da.SearchCursor("countAddrPnts_lyr", ["OID@", "SHAPE@", "SITUS_STREET_NUMBER_END2", "SITUS_STREET_NUMBER2"]):
    for part in row[1]:
        for pnt in part:
            if pnt:
                pntArray.append("arcpy.Point({}, {})".format(pnt.X, pnt.Y))

print(", ".join(pntArray))‍‍‍‍‍‍‍‍

Would that work for you?

Micah

JoshuaBixby
MVP Esteemed Contributor

Stepping back from the specific question, why do you want to output a geometry as text to simply regenerate the same geometry from text later on?  Are you wanting to dump/export for processing by another program or script?

For dumping or exporting geometries, it is best to use one of the data exchange types of formats like JSON/GeoJSON, WKT/WKB.  You can use FromWKT/FromWKB or AsShape to convert those strings back into geometries. 

ErnestoCarreras3
Occasional Contributor

Ok, basically I want to grab the geometry properties (X, Y) of a polygon input FC and copy it into an empty polygon schema of the same FC. I don’t want to use the Copy Management tool, nor Append because it does not fit all my requirements for the final output. Instead, I am attempting to use an Insert cursor to insert the rows. After following Esri’s documentation on how to use the insert Cursor, I was able to add the rows but the FC had no geometry properties, that is, the attribute table for the FC was populated but no features were displayed because the geometry info was missing. After trying various ways, I found out that by passing the coordinates as suggested in my first post inside shapeArray = arcpy.Array([pntArray]), I was able to achieve my desired result. I hard-coded the values to test it and it worked but can’t format the text the way I need it. Maybe my approach is not the correct one, so any suggestions will be welcome.

BTW, I am not a programmer per se therefore, my code will not be the prettiest but it is doing the job.

Here is the other piece that follows...

for x in xrange((row[2] - 1), row[3]):
    fieldlist = ["SHAPE@", number_Field, full_addr]
   
with arcpy.da.InsertCursor(featureClassCopy, fieldlist) as cursor:
        shapeArray = arcpy.Array([pntArray])
        polygonGeo = arcpy.Polygon(shapeArray)
        cursor.insertRow([polygonGeo
, (str(x + 1)), "test"])
    print x + 1

row = rows.next()

0 Kudos
MicahBabinski
Regular Contributor

Hi Ernesto,

Good on you for diving into Python! It sounds like you are on the right track and learning to use DA insert, update, and search cursors is incredibly valuable on it's own. Still, I am curious to know why the Append tool wouldn't work for what you are describing. What were you not happy with about the results of an Append?

Micah

0 Kudos
ErnestoCarreras3
Occasional Contributor

I shared the data and script to the post.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Can you post the entire relevant part of the code?

It sounds like you're completely deconstructing and reconstructing the exact same geometry, when you could just reference the original geometry (e.g. row[1] in your first search cursor).

0 Kudos
ErnestoCarreras3
Occasional Contributor

I shared the data and script to the post.

0 Kudos
ErnestoCarreras3
Occasional Contributor

I am including the python script and the GDB with the two relevant FC. In this way, you can run the code and see more clearly what I am doing. Still, there are some steps that have not been worked but I will reuse what I achieve here to complete the whole process I need for the final output.

The reason I am not using the append is because I was not able to find a way of updating the number_Field variable, that is, "SITUS_STREET_NUMBER" in the attribute table the way the cursor allows me to do it, plus it generated the output way faster too. I am working with thousands of records. Pay attention to this to understand what I am doing in the table: cursor.insertRow([polygonGeo, (str(x + 1)), "test"])

I really appreciate your assistance!

UPDATE: Uploading the correct script.py file. The first uploaded py file was not the correct one.

0 Kudos