@@ -5,6 +5,7 @@ import { utils } from '@rjsf/core';
5
5
import lineSplit from '@turf/line-split' ;
6
6
import fnLength from '@turf/length' ;
7
7
import { EditorEntity } from 'types/editor' ;
8
+ import { removeInvalidRanges } from 'applications/editor/tools/trackEdition/utils' ;
8
9
import { LinearMetadataItem , OperationalPoint } from './types' ;
9
10
10
11
export const LINEAR_METADATA_FIELDS = [ 'slopes' , 'curves' ] ;
@@ -219,17 +220,17 @@ export function mergeIn<T>(
219
220
* - if empty it generate one
220
221
* - if there is a gap at begin/end or inside, it is created
221
222
* - if there is an overlaps, remove it
222
- * @param value The linear metadata
223
+ * @param items The linear metadata
223
224
* @param lineLength The full length of the linearmetadata (should be computed from the LineString or given by the user)
224
225
* @param opts If defined, it allows the function to fill gaps with default field value
225
226
*/
226
227
export function fixLinearMetadataItems < T > (
227
- value : Array < LinearMetadataItem < T > > | undefined ,
228
+ items : Array < LinearMetadataItem < T > > | undefined ,
228
229
lineLength : number ,
229
230
opts ?: { fieldName : string ; defaultValue : unknown }
230
231
) : Array < LinearMetadataItem < T > > {
231
232
// simple scenario
232
- if ( ! value || value . length === 0 ) {
233
+ if ( ! items || items . length === 0 ) {
233
234
return [
234
235
{
235
236
begin : 0 ,
@@ -238,6 +239,7 @@ export function fixLinearMetadataItems<T>(
238
239
} as LinearMetadataItem < T > ,
239
240
] ;
240
241
}
242
+ const filteredItems = removeInvalidRanges ( items , lineLength ) ;
241
243
242
244
function haveAdditionalKeys ( item : LinearMetadataItem , itemToCompare : LinearMetadataItem ) {
243
245
const keys = Object . keys ( item ) ;
@@ -249,7 +251,7 @@ export function fixLinearMetadataItems<T>(
249
251
}
250
252
251
253
// merge empty adjacent items
252
- let fixedLinearMetadata : Array < LinearMetadataItem < T > > = sortBy ( value , [ 'begin' ] ) ;
254
+ let fixedLinearMetadata : Array < LinearMetadataItem < T > > = sortBy ( filteredItems , [ 'begin' ] ) ;
253
255
254
256
// Order the array and fix it by filling gaps if there are some
255
257
fixedLinearMetadata = fixedLinearMetadata . flatMap ( ( item , index , array ) => {
@@ -362,7 +364,6 @@ export function update<T>(
362
364
linearMetadata : Array < LinearMetadataItem < T > >
363
365
) : Array < LinearMetadataItem < T > > {
364
366
if ( linearMetadata . length === 0 ) return [ ] ;
365
-
366
367
// Compute the source coordinates of the changed point
367
368
// by doing
368
369
// - a diff between source & target for change
@@ -660,9 +661,12 @@ export function getClosestOperationalPoint(
660
661
* @param sourceLine The original LineString (before the change)
661
662
* @returns The entity modified in adquation
662
663
*/
664
+
663
665
export function entityDoUpdate < T extends EditorEntity > ( entity : T , sourceLine : LineString ) : T {
664
- if ( entity . geometry . type === 'LineString' && ! isNil ( entity . properties ) ) {
665
- const newProps : EditorEntity [ 'properties' ] = { id : entity . properties . id } ;
666
+ const newProps : EditorEntity [ 'properties' ] = { id : entity . properties . id } ;
667
+ // The modification of the linestring modifies the entity real properties only during initialization.
668
+ const isInitialization = sourceLine . coordinates . length === 0 ;
669
+ if ( entity . geometry . type === 'LineString' && ! isNil ( entity . properties ) && isInitialization ) {
666
670
Object . keys ( entity . properties ) . forEach ( ( name ) => {
667
671
const value = ( entity . properties as { [ key : string ] : unknown } ) [ name ] ;
668
672
// is a LM ?
@@ -672,9 +676,7 @@ export function entityDoUpdate<T extends EditorEntity>(entity: T, sourceLine: Li
672
676
newProps [ name ] = value ;
673
677
}
674
678
} ) ;
675
- // eslint-disable-next-line dot-notation
676
- newProps [ 'length' ] = getLineStringDistance ( entity . geometry as LineString ) ;
677
-
679
+ newProps . length = getLineStringDistance ( entity . geometry as LineString ) ;
678
680
return { ...entity , properties : newProps } ;
679
681
}
680
682
return entity ;
0 commit comments