The code below will generate the following result:
Change the input and output featureclasses on line 5, 6 and 7. The output featureclass (shapefile) will be created.
On line 10 you can play with the precision (stepsize). In this case I specified 1 (m).
def main():
import arcpy
# 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\lines02.shp"
# precision (m)
stepsize = 1 # extract point on boundary polygon every 1 meter
sr = arcpy.Describe(fc_lin).spatialReference
# create list of line geometries
lst_lines = [r[0] for r in arcpy.da.SearchCursor(fc_lin, ("SHAPE@"))]
# loop through polygons
lst_res = []
with arcpy.da.SearchCursor(fc_pol, ("SHAPE@")) as curs:
for row in curs:
polygon = row[0]
boundary = polygon.boundary()
dist_min = None
for i in range(0, int(boundary.length), stepsize):
point_pol = boundary.positionAlongLine(i, False)
for line in lst_lines:
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)
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)
line_out = arcpy.Polyline(arcpy.Array([pnt_lin_found, pnt_pol_found]), sr)
lst_res.append(line_out)
arcpy.CopyFeatures_management(lst_res, fc_out)
if __name__ == '__main__':
main()
Kind regards, Xander