Select to view content in your preferred language

ArcPy: Polyline Geomtry

3393
6
08-03-2013 02:21 PM
PeterWilson
Regular Contributor
I'm busy writing a script to generate long-sections from a polyline. I've written the following based on an example from an ESRI video. The problem that I have is that the first and last position is not returned.

The following works but doesn't return the first and last position along the polyline:

>>> pts = []
>>> with arcpy.da.SearchCursor("Hatsamas_Bloukrans_3D_Albers", "SHAPE@")as rows:
...     for row in rows:
...         i = 100
...         while i < row[0].length:
...             pts.append(row[0].positionAlongLine(i))
...             i += 100
... arcpy.CopyFeatures_management(pts, r"in_memory\ptsalongline")


I've tried the following but returns an error:

>>> pts = []
>>> with arcpy.da.SearchCursor("Hatsamas_Bloukrans_3D_Albers", "SHAPE@") as rows:
...     for row in rows:
...         pts.append(row[0].firstPoint)
...         i = 100
...         while i < row[0].length:
...             pts.append(row[0].positionAlongLine(i))
...             i += 100
...         pts.append(row[0].lastPoint)
... arcpy.CopyFeatures_management(pts, r"in_memory\Ptsalongline")


Any help will be appreciated.

Regards
Tags (2)
0 Kudos
6 Replies
T__WayneWhitley
Frequent Contributor
hmmm, I'm not sure why that doesn't work.  However, you should be able to get the first/last points using the positionAlongLine method you already have set up and simply append that to the first and last position in the point list.

Didn't test this, but here's the gist I think will do it, modifying your 1st piece of code:
>>> pts = []
>>> with arcpy.da.SearchCursor("Hatsamas_Bloukrans_3D_Albers", "SHAPE@")as rows:
...     for row in rows:
...         i = 0
...         while i < row[0].length:
...             pts.append(row[0].positionAlongLine(i))
...             i += 100
...         pts.append(row[0].positionAlongLine(row[0].length))
... arcpy.CopyFeatures_management(pts, r"in_memory\ptsalongline")


Enjoy,
Wayne
0 Kudos
PeterWilson
Regular Contributor
Hi Wayne

Thanks for the following, but unfortunately it only returns the lastPoint and not the firstPoint. Any suggestion why the following isn't working:

pts.append(row[0].firstPoint)


Regards
0 Kudos
PeterWilson
Regular Contributor
I've gotten closer to what I'm looking for, but my script is currently creating the firstPoint multiple times until it steps outside of the while loop and then generates the lastPoint once. I'm not sure why suddenly the positionAlongLine is only reading the firstPoint and not the entire geometry of the polyline. Any advice in how to resolve this would be appreciated.

>>> pts = []
>>> with arcpy.da.SearchCursor("Hatsamas_Bloukrans_3D_Albers","SHAPE@") as rows:
...     for row in rows:
...         pts.append(arcpy.PointGeometry(row[0].firstPoint))
...         i = 100
...         while i < row[0].length:
...             pts.append(row[0].positionAlongLine(i))
...             i += 100
...         pts.append(arcpy.PointGeometry(row[0].lastPoint))
... arcpy.CopyFeatures_management(pts, r"in_memory\ptsalongline")
... del pts


Regards
0 Kudos
T__WayneWhitley
Frequent Contributor
Have you tried a projecting your data in a different coordinate system than Hatsamas_Bloukrans_3D_Albers?  (EDIT - ah, never mind, must be the name of your dataset.)
I'm not in your part of the world, so I confess I have no idea what projection this is or what your data looks like - is there any way you can attach a small sample dataset, just a few features to run test code on?

Also, can you describe carefully what exactly you are trying to do? -- I ask this because your loop is feeding in 100 unit increments at a time (starting at 100), so what is this really doing and is it necessary?  Verify what the units are...doesn't look like you're doing any error-trapping whatsoever, so how can you determine what is 'behaving badly'?

Attach a small shapefile or something in your native projection, and someone may more likely be able to help you.

Wayne
0 Kudos
PeterWilson
Regular Contributor
Hi Wayne

Thanks for getting back to me, your assistance is greatly appreciated. The purpose of the following script is to extract points along a polyline based on a defined user interval i.e. 100m. The polyline in this instance is a proposed pipeline (3D Polyline). I want to extract (generate) a point feature class starting at the begning of the line then thereafter every 100m in this instance ending witht the last vertex of the polyline. The newly generated (in_memory) point feature class represents the longsection of my proposed pipeline. I then extract the elevation for each point feature and export the attribute table for futher processing. What I eventually landup with is a longsection profile of my pipeline based on a user defined interval. In other words the next time round I could extract my longsection every 500m, but I need the first and last point each time. Also I can have multiple features within my feature class representing multiple pipelines that I need to generate longsection for. Please note that the longsection are plotted using a scatter graph within excel.

Regards

I've attached print screens of my polylines representing my proposed pipelines.

Just a note. If I run the firstPoint and lastPoint on its own it works as expected, but as soon as I combined it with positionAlongLine it seems to get confused.
0 Kudos
PeterWilson
Regular Contributor
I have come up with a solution. I hope this helps anyone else that I trying to generate a means of extacting longsection from 3D Polylines for engineering purposes.

I'm certain that there's a bug with ArcGIS when using firstPoint, lastPoint and positionAlongLine together within a for loop as the following works perfectly and is virtually the same as using firtPoint and lastPoint to derive the first and last position along a line.

pts = []
with arcpy.da.SearchCursor("Hatsamas_Bloukrans_3D_Albers","SHAPE@") as rows:
    for row in rows:
        pts.append(row[0].positionAlongLine(0))
        i = 100
        while i < row[0].length:
            pts.append(row[0].positionAlongLine(i))
            i += 100
        leng = row[0].length
        pts.append(row[0].positionAlongLine(leng))
arcpy.CopyFeatures_management(pts, r"in_memory\ptsalongline")
del pts


Regards
0 Kudos