Generate Near Table

6641
13
Jump to solution
02-20-2015 08:48 AM
SukhvirRehal
New Contributor III

Hi Guys,

 

hope everyone is well?

 

I'm hoping someone can help me here, is there a way to generate something like a 'near table' (http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00080000001n000000) with a basic level user licence?

 

Are there any work around's you can suggest?

 

Thanks in advance for any help.

 

I'm using ArcGIS 10.2

0 Kudos
13 Replies
XanderBakker
Esri Esteemed Contributor

and in case you want to keep a reference to the input data, you can write the input OID of the polygons and lines to the output featureclass like this:

def main():
    import arcpy
    import os

    # inputs and output
    fc_pol = r"D:\Xander\GeoNet\NearLines\Polygons.shp"
    fc_lin = r"D:\Xander\GeoNet\NearLines\Road_Boundary.shp"
    fc_out = r"D:\Xander\GeoNet\NearLines\lines04.shp"

    # output fields
    fld_oid_polygon = "OIDpolygon"
    fld_oid_line = "OIDline"
    flds_add = [fld_oid_polygon, fld_oid_line]

    # precision (m)
    stepsize = 1 # extract point on boundary polygon every 1 meter

    sr = arcpy.Describe(fc_lin).spatialReference

    # create empty output fc
    fc_ws, fc_name = os.path.split(fc_out)
    arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYLINE", spatial_reference=sr)

    # add fields
    for fldname in flds_add:
        arcpy.AddField_management(fc_out, fldname, "LONG")

    # create dict of oid vs line geometries
    dct_lines = {r[0]: r[1] for r in arcpy.da.SearchCursor(fc_lin, ("OID@", "SHAPE@"))}

    # insert cursor
    flds = ["SHAPE@"]
    flds.extend(flds_add)
    with arcpy.da.InsertCursor(fc_out, flds) as curs_out:

        # loop through polygons
        lst_res = []
        with arcpy.da.SearchCursor(fc_pol, ("SHAPE@", "OID@")) as curs:
            for row in curs:
                polygon = row[0]
                oid_polygon = row[1]
                boundary = polygon.boundary()
                dist_min = None

                for i in range(0, int(boundary.length), stepsize):
                    point_pol = boundary.positionAlongLine(i, False)

                    for oid_line, line in dct_lines.items():
                        pntg, val, dist, right_side = line.queryPointAndDistance(point_pol, False)
                        if dist_min is None:
                            dist_min = dist
                            pnt_lin_found = arcpy.Point(pntg.firstPoint.X, pntg.firstPoint.Y)
                            pnt_pol_found = arcpy.Point(point_pol.firstPoint.X, point_pol.firstPoint.Y)
                            oid_line_found = oid_line
                        else:
                            if dist < dist_min:
                                dist_min = dist
                                pnt_lin_found = arcpy.Point(pntg.firstPoint.X, pntg.firstPoint.Y)
                                pnt_pol_found = arcpy.Point(point_pol.firstPoint.X, point_pol.firstPoint.Y)
                                oid_line_found = oid_line

                polyline = arcpy.Polyline(arcpy.Array([pnt_lin_found, pnt_pol_found]), sr)
                row_out = (polyline, oid_polygon, oid_line_found, )
                curs_out.insertRow(row_out)

if __name__ == '__main__':
    main()
SukhvirRehal
New Contributor III

Thank you Xander,

your screen shot is exactly what I wanted.

Do I simply paste this into the code window? I will try to create a toolbox.

0 Kudos
XanderBakker
Esri Esteemed Contributor

I normally run the code from a stand alone IDE (I use PyScripter), but you can run the code from the python window (it will not add the result to the TOC, though). Before you run it, make sure you change the paths to your input data and provide a valid output name/path.

You can also create a toolbox from it. Use the arcpy.GetParameterAsText()  to collect the inputs

    fc_pol = arcpy.GetParameterAsText(0) # featurelayer, filter on polygon, direction is input
    fc_lin = arcpy.GetParameterAsText(1) # featurelayer, filter on polyline, direction is input
    fc_out = arcpy.GetParameterAsText(2) # featureclass, direction is output

If you have problems, post them back and I can have a look at it.

If it works, please mark the thread as answered (at the post that answered your question). If a post was helpful you can mark the post as such using the links below the post Helpful Yes | No

0 Kudos
StanaforthHopkins
New Contributor II

You offer hope for the hobbyist. If python routines can substitute for geoprocesswing nd perhaps more, a $10K professional licene my not be needed.

0 Kudos