@@ -2,24 +2,25 @@ namespace Alidade.Osm.Handlers.Editing;
22
33/// <inheritdoc />
44public sealed class GridifyWay ( EditBufferStateService editBufferState )
5- : IRequestHandler < GridifyWay . Command , CommandResult >
5+ : IRequestHandler < GridifyWay . Query , QueryResult < IReadOnlyList < OsmElementRef > > >
66{
7- static GridifyWay ( ) => UndoDescriptions . Register < Command > ( "Gridify" ) ;
7+ static GridifyWay ( ) => UndoDescriptions . Register < Query > ( "Gridify" ) ;
88
99 /// <summary>
1010 /// Splits a closed way into a grid of equal rectangular sub-areas.
1111 /// The grid is rotated by <see cref="RotationDeg"/> degrees (clockwise from east in
12- /// the local flat-Earth projection). The original way is replaced by the new cell ways.
12+ /// the local flat-Earth projection). The original way is replaced by the new cell ways,
13+ /// whose refs are returned on success.
1314 /// </summary>
1415 /// <param name="WayId">The ID of the closed way to gridify.</param>
1516 /// <param name="Rows">Number of rows in the output grid.</param>
1617 /// <param name="Cols">Number of columns in the output grid.</param>
1718 /// <param name="RotationDeg">Grid rotation in degrees.</param>
18- public record Command ( long WayId , int Rows , int Cols , double RotationDeg )
19- : IRequest < CommandResult > , IUndoableCommand ;
19+ public record Query ( long WayId , int Rows , int Cols , double RotationDeg )
20+ : IRequest < QueryResult < IReadOnlyList < OsmElementRef > > > , IUndoableCommand ;
2021
2122 /// <inheritdoc />
22- public Task < CommandResult > Handle ( Command request , CancellationToken cancellationToken )
23+ public Task < QueryResult < IReadOnlyList < OsmElementRef > > > Handle ( Query request , CancellationToken cancellationToken )
2324 {
2425 GridifyResult result = GeometryService . Gridify (
2526 request . WayId ,
@@ -31,13 +32,13 @@ public Task<CommandResult> Handle(Command request, CancellationToken cancellatio
3132
3233 if ( result . CellNodeRefs . Count == 0 )
3334 {
34- return Task . FromResult ( CommandResult . Fail ( "The selected way cannot be gridified. Select a single closed way." ) ) ;
35+ return Task . FromResult ( QueryResult < IReadOnlyList < OsmElementRef > > . Fail ( "The selected way cannot be gridified. Select a single closed way." ) ) ;
3536 }
3637
3738 EditBufferState state = editBufferState . State ;
3839 if ( ! state . Ways . TryGetValue ( request . WayId , out OsmWay ? originalWay ) )
3940 {
40- return Task . FromResult ( CommandResult . Pass ( ) ) ;
41+ return Task . FromResult ( QueryResult < IReadOnlyList < OsmElementRef > > . Fail ( ) ) ;
4142 }
4243
4344 ImmutableDictionary < long , OsmNode > nodeDict = state . Nodes ;
@@ -57,13 +58,15 @@ public Task<CommandResult> Handle(Command request, CancellationToken cancellatio
5758 }
5859
5960 IReadOnlyDictionary < string , string > tags = originalWay . Tags ;
60- foreach ( IReadOnlyList < GridifyNodeRef > cellRefs in result . CellNodeRefs )
61+ List < OsmElementRef > cellRefs = [ ] ;
62+ foreach ( IReadOnlyList < GridifyNodeRef > cellNodeRefs in result . CellNodeRefs )
6163 {
62- long [ ] cellNodeIds = [ .. cellRefs . Select ( r =>
64+ long [ ] cellNodeIds = [ .. cellNodeRefs . Select ( r =>
6365 r . IsExisting ? r . ExistingNodeId : newNodeIds [ r . NewNodeIndex ] ) ] ;
6466 OsmWay cellWay = new ( nextId , 1 , null , null , null , cellNodeIds , tags . ToImmutableDictionary ( ) ) ;
6567 wayDict = wayDict . SetItem ( nextId , cellWay ) ;
6668 editStates = editStates . SetItem ( cellWay . Ref , EditState . Created ) ;
69+ cellRefs . Add ( cellWay . Ref ) ;
6770 nextId -- ;
6871 }
6972
@@ -86,6 +89,7 @@ public Task<CommandResult> Handle(Command request, CancellationToken cancellatio
8689 EditStates = editStates ,
8790 NextNegativeId = nextId
8891 } ) ;
89- return Task . FromResult ( CommandResult . Pass ( ) ) ;
92+
93+ return Task . FromResult < QueryResult < IReadOnlyList < OsmElementRef > > > ( cellRefs ) ;
9094 }
9195}
0 commit comments