Select to view content in your preferred language

Strange arcpy polyline geometry behavior. (mixins._passthrough object)

1027
1
12-12-2018 02:31 AM
AdrianCroitor1
New Contributor

Hello guys,

is there someone who were annoyed by this kind of problem?

So, first of all I want you to notice that the feature class Roads has a POLYLINE geometry type, and there are no multipart features in it.

import arcpy

for i in arcpy.da.SearchCursor(r'C:\Users\...\Administrative_area.gdb\Roads', ["SHAPE@"]):
    print(i[0])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Normally, the output for this code will be something like this:

And it works.

Now the problem appears when I put this little piece of code in the main script:

    def get_near_data(self):
        with arcpy.da.SearchCursor(r'C:\Users\...\Administrative_area.gdb\Roads', ["SHAPE@"]) as reader:
            for f in reader:
                print(f[0])‍‍‍‍‍‍‍‍‍‍‍‍‍‍

And the output is not the same as in the first case. This is not geometry.

I've noticed that if I change the path for feature class Roads to another feature class which has a polygon geometry type, it works just fine again, take a look:

    def get_near_data(self):

        with arcpy.da.SearchCursor(r"C:\Users\...\ArcGIS\Administrative_area.gdb\CountyBoundary", ["SHAPE@"]) as reader:
            for f in reader:
                print(f[0])‍‍‍‍‍‍‍‍‍‍

And the output is going to be like this:

So, what is going on here? Why geometries of polyline feature classes aren't returned while geometries of polygons are?

1 Reply
DanPatterson_Retired
MVP Emeritus

Not sure what you are looking for, but there are simpler ways to confirm geometry types

import arcpy

in_fc1 = r'C:\Arc_projects\profile_maker\profiler.gdb\transect_split'

cur = arcpy.da.SearchCursor(in_fc1, "SHAPE@")

cur.next()
(<Polyline object at 0x1fe0ea8cb38[0x1fe11929b70]>,)

# ---- or even

cur.__getitem__(0)
<Polyline object at 0x1fe0ea8cb38[0x1fe11929b70]>

cur._dtype
dtype([('SHAPE@', 'O')])

but it can't be converted using _as_narray since  SHAPE@ is used which returns a geometry object rather than the alternatives which allow you to return coordinates and convert directly to something useable

cur = arcpy.da.SearchCursor(in_fc1, "SHAPE@XY")

cur._as_narray()  # ---- option 1 return coordinates as a tuple of a list
  
array([([ 340936.21594756, 5046043.74849692],),
       ([ 340859.41938063, 5046107.73866551],),
       ([ 340781.86449519, 5046170.67589448],),
       ([ 340711.39750497, 5046241.13812473],),
       ([ 340657.89948473, 5046324.76720457],),
       ([ 340614.92821842, 5046414.92588379],),
       ([ 340560.40635839, 5046498.12427651],),
       ([ 340492.34102822, 5046571.17846557],),
       ([ 340422.96741265, 5046643.0664908 ],),
       ([ 340359.31866013, 5046720.00262745],),
       ([ 340287.12978867, 5046788.80373807],),
       ([ 340208.32592782, 5046850.21868976],),
       ([ 340125.05206674, 5046905.49227955],),
       ([ 340042.75438658, 5046962.04556696],),
       ([ 340005.82725   , 5046995.80015   ],)],
      dtype=[('SHAPE@XY', '<f8', (2,))])

cur = arcpy.da.SearchCursor(in_fc1, ["SHAPE@X", "SHAPE@Y"])

cur._as_narray()  # ---- option 2, return the array as two separate fields
Out[38]: 
array([(340936.21594756, 5046043.74849692),
       (340859.41938063, 5046107.73866551),
       (340781.86449519, 5046170.67589448),
       (340711.39750497, 5046241.13812473),
       (340657.89948473, 5046324.76720457),
       (340614.92821842, 5046414.92588379),
       (340560.40635839, 5046498.12427651),
       (340492.34102822, 5046571.17846557),
       (340422.96741265, 5046643.0664908 ),
       (340359.31866013, 5046720.00262745),
       (340287.12978867, 5046788.80373807),
       (340208.32592782, 5046850.21868976),
       (340125.05206674, 5046905.49227955),
       (340042.75438658, 5046962.04556696),
       (340005.82725   , 5046995.80015   )],
      dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8')])