1

Basically, I am trying to generate polygon geometries based on the selection of another polygon layer. These polygons will be copied into a new empty schema. I avoiding using the Append and Copy operations since they won't meet all the requirements needed for my output. I also found the cursors help me achieving the desired result way faster. As a result, I 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.

for row in arcpy.da.SearchCursor("countAddrPnts_lyr", ["OID@", "SHAPE@", "SITUS_STREET_NUMBER_END2", "SITUS_STREET_NUMBER2"]):
x = 0
for 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)

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.

In this other post you can download the data in GDB format, as well as the py script.

  • BTW, I tested the suggested output in my code and it works as expected... I just can't achieve the desired result via the code. – Ernesto CD Jan 19 '17 at 15:40
  • a few remarks 1. what geometry type is the current searchcursor on? point,polygon? 2. if it is a polygon you can simply do something like this to create a polygon for each record: arcpy.MakeFeatureLayer(row[1],outputname) and you do not have to mess around with dismantling the SHAPE@ for the layer – ziggy Jan 19 '17 at 15:47
  • The geometry in the searchCursor is a polygon. I have tried already your suggestion and although I was able to successfully append the polygons into the new output polygon layer, the process is more complex than that and all the requirements were not met. The code I am using is the one Esri has in their resources for reading X, Y coordinates from layers but I want to modify the way they output it by achieving what I described above – Ernesto CD Jan 19 '17 at 16:00
  • Please edit the question in response to requests for clarification. It is not fair to those who would answer your question to need to mine the comments for critical information. Even though you've been here for years, you have not yet taken the Tour, which explains that questions should be edited to improve their clarity. – Vince Jan 20 '17 at 12:22
1

A little list comprehension and the .join function will do it.

pntString = ", ".join (["arcpy.Point ({}, {})".format (xy [0], xy [1]) for xy
                     in arcpy.da.SearchCursor ("countAddrPnts_lyr", "SHAPE@XY")])
1

What you're calling "your output" is actually one piece of output per point. You need to store it all in the same variable, appending to it each time (untested):

for row in arcpy.da.SearchCursor("countAddrPnts_lyr", ["OID@", "SHAPE@", "SITUS_STREET_NUMBER_END2", "SITUS_STREET_NUMBER2"]):
    x = 0
    poly_text = ""
    for part in row[1]:
        for pnt in part:
            if pnt:
                poly_text += "arcpy.Point(" + "{}, {}".format(pnt.X, pnt.Y) + "),"
    print poly_text # should contain all points' text
    x += 1

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.