private static CoordinateSequence makeSequenceValid ( CoordinateSequence sequence , boolean preserveDuplicateCoord , boolean close ) { int dim = sequence . getDimension ( ) ; // we add 1 to the sequence size for the case where we have to close the linear ring double [ ] array = new double [ ( sequence . size ( ) + 1 ) * sequence . getDimension ( ) ] ; boolean modified = false ; int count = 0 ; // Iterate through coordinates, skip points with x=NaN, y=NaN or duplicate for ( int i = 0 ; i < sequence . size ( ) ; i ++ ) { if ( Double . isNaN ( sequence . getOrdinate ( i , 0 ) ) || Double . isNaN ( sequence . getOrdinate ( i , 1 ) ) ) { modified = true ; continue ; } if ( ! preserveDuplicateCoord && count > 0 && sequence . getCoordinate ( i ) . equals ( sequence . getCoordinate ( i - 1 ) ) ) { modified = true ; continue ; } for ( int j = 0 ; j < dim ; j ++ ) { array [ count * dim + j ] = sequence . getOrdinate ( i , j ) ; if ( j == dim - 1 ) { count ++ ; } } } // Close the sequence if it is not closed and there is already 3 distinct coordinates if ( close && count > 2 && ( array [ 0 ] != array [ ( count - 1 ) * dim ] || array [ 1 ] != array [ ( count - 1 ) * dim + 1 ] ) ) { System . arraycopy ( array , 0 , array , count * dim , dim ) ; modified = true ; count ++ ; } // Close z, m dimension if needed if ( close && count > 3 && dim > 2 ) { for ( int d = 2 ; d < dim ; d ++ ) { if ( array [ ( count - 1 ) * dim + d ] != array [ d ] ) { modified = true ; } array [ ( count - 1 ) * dim + d ] = array [ d ] ; } } if ( modified ) { double [ ] shrinkedArray = new double [ count * dim ] ; System . arraycopy ( array , 0 , shrinkedArray , 0 , count * dim ) ; return PackedCoordinateSequenceFactory . DOUBLE_FACTORY . create ( shrinkedArray , dim ) ; } else { return sequence ; } }
Returns a coordinateSequence free of Coordinates with X or Y NaN value and if desired free of duplicated coordinates . makeSequenceValid keeps the original dimension of input sequence .