0

How can I take a shapefile that has road segments and read it into a data structure in Python that I can use to get begin and end points with?

For example x, y to x2, y2.

It's not a large shapefile, just a one with roads for my county. I've previously worked with argisscripting at ArcGIS 9.3 but that was a couple years ago and now I'm rusty and using ArcGIS 10.1.

import arcpy

infc = "C:\\Users\\Jeff\\Downloads\\tl_2013_30063_edges\\tl_2013_30063_edges.shp"

for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current line ID

    print("Feature {0}:".format(row[0]))

    #Set start point
    startpt = row[1].firstPoint

    #Set Start coordinates
    startx = startpt.X
    starty = startpt.Y

    #Set end point
    endpt = row[1].lastPoint

    #Set End coordinates
    endx = endpt.X
    endy = endpt.Y

This is what one of the features looks like.

FID 16992

Shape Polyline

STATEFP 30

COUNTYFP 063

TLID 205238472

TFIDL 217915003

TFIDR 217914215

MTFCC S1400

FULLNAME S 9th St W

5
  • I'm using 10.1.
    – Jeff
    Commented Nov 14, 2013 at 6:19
  • if I'm not trying to use the command line, what would I put in the infc variable to get it to read a shape file? I'm getting an error when I try and just put the path to the .shp file.
    – Jeff
    Commented Nov 14, 2013 at 6:46
  • 1
    It will be something like infc = r"C:\temp\test.shp" - the example code is from when writing a Python script tool that takes the input feature class as a parameter from the tool dialog.
    – PolyGeo
    Commented Nov 14, 2013 at 6:53
  • will I need to change the OID or other things to match the fields in my shp file?
    – Jeff
    Commented Nov 14, 2013 at 7:12
  • I don't think so but I cannot see your code so please edit your question to show us where you are up to. From there it should be easy to advise because I imagine that code to be not much more than 10 lines (excluding any comments).
    – PolyGeo
    Commented Nov 14, 2013 at 7:58

1 Answer 1

1

I think what you're asking for could be rather easily accomplished by accessing each feature's geometry object that defines it's shape/geometry. You can read the documentation about it at http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Reading_geometries/002z0000001t000000/

That's for desktop 10.0, but I'm pretty sure 10.1 works the same way.

Anyway, it's got some pretty straight forward examples on that page that I think should be able to walk you through everything (See the example at the bottom of the page for probably the most relevant for your task), but if you still have questions on implementing this for your particular situation, feel free to say something and I'm sure me or someone else on here can help out. All you should have to do is access the geometry object for each feature in arcpy and access the first record and last record in the resulting array (unless you have multi part features, then you just have to add another nested for loop).

3
  • 2
    It's fairly different in 10.1, where you get access to the data access module for cursors.
    – Paul
    Commented Nov 14, 2013 at 6:40
  • 2
    @Paul Ok, sorry, you're right, there are some differences. Instead refer to documentation at resources.arcgis.com/en/help/main/10.1/index.html#//… It looks like you have to change the syntax slightly on the search cursor and add a [1] in one spot (compared to the 10.0 examples). Good catch I'd not tried that since I upgraded to 10.1, just remembered it from 10.0 work.
    – John
    Commented Nov 14, 2013 at 6:49
  • 1
    No need to apologize, was just pointing it out. Here is a good breakdown between the differences. One thing that should be changed is the use of a with statement for 10.1.
    – Paul
    Commented Nov 14, 2013 at 7:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.