Projecting polygons into polyline

Solution for QGIS

Let's assume there are two layers 'squares' (green) and 'lines' (blue) with its corresponding attribute tables accordingly, see image below.

input

Step 1. Proceed with 'Extract vertices' (Apply additionally 'Delete duplicate geometries' or 'Remove duplicate vertices' if needed)

step_1

Step 2. By means of a 'Virtual Layer' through Layer > Add Layer > Add/Edit Virtual Layer... use a query query with ST_ClosestPoint()

SELECT v.id, sv.Name, ST_ClosestPoint(l.geometry, v.geometry) AS geom
FROM "vertices" AS v, lines AS l

step_2

Step 3. Proceed with 'Points to path'. Do not forget about "Order field"

step_3

Step 4. Proceed with 'Buffer'. Do not forget about 'End cap style' and 'Join Style'.

step_4

Step 5. Proceed with 'Difference' or as was mentioned in comments apply 'Intersection'.

step_5


References:

  • Where/How can I enter the code for a Virtual Layer?

Solution for ArcGIS using ArcPy

You can

  • Generate Near Table to find coordinates to nearest line from each polygon

  • Move each polygon to that coordinate with da.UpdateCursor

  • Clip the line using polygons as clip features

import arcpy

arcpy.env.workspace = r'C:\GIS\Somedatabase.gdb' #Change
lines = 'polyline' #Change
polygons = 'squares' #Change. WILL BE ALTERED SO BACKUP YOUR DATA BEFORE BEFORE EXECUTING CODE
output_lines = 'newlines123' #Change

arcpy.GenerateNearTable_analysis(in_features=polygons, near_features=lines, out_table='neartable', location=True, closest=True) #Find nearest line coordinates
d = {oid:[x,y] for oid,x,y in arcpy.da.SearchCursor('neartable',['IN_FID','NEAR_X','NEAR_Y'])} #Store in dictionary of OID:coordinates

with arcpy.da.UpdateCursor(polygons,['OID@','SHAPE@X','SHAPE@Y']) as cursor: #Move each polygon
    for row in cursor:
        row[1], row[2] = d[row[0]]
        cursor.updateRow(row)

arcpy.Clip_analysis(in_features=lines, clip_features=polygons, out_feature_class=output_lines)

enter image description here