@@ -346,9 +346,24 @@ public void initialize() throws OperatorException {
346346 ProductUtils .copyTiePointGrids (masterProduct , targetProduct );
347347
348348 // Add master bands
349+ ProductNodeGroup <FlagCoding > flagCodingGroup = targetProduct .getFlagCodingGroup ();
350+ ProductNodeGroup <IndexCoding > indexCodingGroup = targetProduct .getIndexCodingGroup ();
349351 for (Band sourceBand : masterProduct .getBands ()) {
350352 Band targetBand = ProductUtils .copyBand (sourceBand .getName (), masterProduct , targetProduct , true );
351- handleSampleCodings (sourceBand , targetBand , renameMasterComponents , masterComponentPattern );
353+ FlagCoding flagCoding = targetBand .getFlagCoding ();
354+ if (flagCoding != null ) {
355+ // first remove FlagCoding potentially added by copyBand() then handle the FlagCoding
356+ flagCodingGroup .remove (flagCoding );
357+ targetBand .setSampleCoding (null );
358+ handleFlagCoding (sourceBand , targetBand , renameMasterComponents , masterComponentPattern );
359+ }
360+ IndexCoding indexCoding = targetBand .getIndexCoding ();
361+ if (indexCoding != null ) {
362+ // first remove IndexCoding potentially added by copyBand() then handle the IndexCoding
363+ indexCodingGroup .remove (indexCoding );
364+ targetBand .setSampleCoding (null );
365+ handleIndexCoding (sourceBand , targetBand , renameMasterComponents , masterComponentPattern );
366+ }
352367 sourceRasterMap .put (targetBand , sourceBand );
353368 if (renameMasterComponents ) {
354369 targetBand .setName (masterComponentPattern .replace (SOURCE_NAME_REFERENCE , sourceBand .getName ()));
@@ -383,8 +398,12 @@ public void initialize() throws OperatorException {
383398 // Add slave bands
384399 for (Band sourceBand : slaveProduct .getBands ()) {
385400 String targetBandName = getTargetBandName (sourceBand , i );
386- Band targetBand = targetProduct .addBand (targetBandName , sourceBand .getDataType ());
401+ // first creating the band, then copying the properties and then adding it to the target product
402+ // if the
403+ Band targetBand = new Band (targetBandName , sourceBand .getDataType (),
404+ targetProduct .getSceneRasterWidth (), targetProduct .getSceneRasterHeight ());
387405 ProductUtils .copyRasterDataNodeProperties (sourceBand , targetBand );
406+ targetProduct .addBand (targetBand );
388407 handleSampleCodings (sourceBand , targetBand , renameSlaveComponents , slaveComponentPattern );
389408 sourceRasterMap .put (targetBand , sourceBand );
390409 originalSlaveNames .put (targetBand .getName (), sourceBand .getName ());
@@ -449,6 +468,7 @@ public void initialize() throws OperatorException {
449468 }
450469 }
451470
471+
452472 private String getTargetBandName (RasterDataNode rasterDataNode , int productIndex ) {
453473 String rasterDataNodeName = rasterDataNode .getName ();
454474 if (renameSlaveComponents ) {
@@ -457,7 +477,7 @@ private String getTargetBandName(RasterDataNode rasterDataNode, int productIndex
457477 if (StringUtils .isNullOrEmpty (slaveComponentPattern )) {
458478 throw new OperatorException (format (
459479 "Target product already contains a raster data node with name ''{0}''. " +
460- "Parameter 'slaveComponentPattern' must be set." ,
480+ "Parameter 'slaveComponentPattern' must be set." ,
461481 rasterDataNodeName ));
462482 }
463483 return rename (rasterDataNodeName , productIndex );
@@ -654,7 +674,7 @@ private void collocateSourceBand(RasterDataNode sourceBand, Rectangle sourceRect
654674
655675 if (sourcePixelPos != null ) {
656676 resampling .computeIndex (sourcePixelPos .x , sourcePixelPos .y ,
657- sourceRasterWidth , sourceRasterHeight , resamplingIndex );
677+ sourceRasterWidth , sourceRasterHeight , resamplingIndex );
658678 double sample ;
659679 if (resampling == Resampling .NEAREST_NEIGHBOUR ) {
660680 sample = sourceTile .getSampleDouble ((int ) resamplingIndex .i0 , (int ) resamplingIndex .j0 );
@@ -717,9 +737,9 @@ private void copyMasks(Product sourceProduct, boolean rename, String pattern, Ma
717737 if (!targetProduct .getMaskGroup ().contains (sourceMask .getName ())) {
718738 Mask .ImageType imageType = sourceMask .getImageType ();
719739 Mask targetMask = new Mask (sourceMask .getName (),
720- targetProduct .getSceneRasterWidth (),
721- targetProduct .getSceneRasterHeight (),
722- imageType );
740+ targetProduct .getSceneRasterWidth (),
741+ targetProduct .getSceneRasterHeight (),
742+ imageType );
723743 targetMask .setDescription (sourceMask .getDescription ());
724744 for (Property property : sourceMask .getImageConfig ().getProperties ()) {
725745 targetMask .getImageConfig ().setValue (property .getDescriptor ().getName (), property .getValue ());
@@ -746,53 +766,46 @@ private void copyMasks(Product sourceProduct, boolean rename, String pattern, Ma
746766 }
747767
748768 private void handleSampleCodings (Band sourceBand , Band targetBand , boolean renameComponents , String renamePattern ) {
749- handleFlagCoding (sourceBand , targetBand , renameComponents , renamePattern );
750- handleIndexCoding (sourceBand , targetBand , renameComponents , renamePattern );
769+ if (sourceBand .getFlagCoding () != null ) {
770+ handleFlagCoding (sourceBand , targetBand , renameComponents , renamePattern );
771+ }
772+ if (sourceBand .getIndexCoding () != null ) {
773+ handleIndexCoding (sourceBand , targetBand , renameComponents , renamePattern );
774+ }
751775 }
752776
753777 private void handleFlagCoding (Band sourceBand , Band targetBand , boolean renameComponents , String renamePattern ) {
754- if (sourceBand .getFlagCoding () != null ) {
755- targetBand .getProduct ().getFlagCodingGroup ().remove (targetBand .getFlagCoding ());
756- targetBand .setSampleCoding (null );
757- }
758778 setFlagCoding (targetBand , sourceBand .getFlagCoding (), renameComponents , renamePattern );
759779 }
760780
761781 private void handleIndexCoding (Band sourceBand , Band targetBand , boolean renameComponents , String renamePattern ) {
762- if (sourceBand .getIndexCoding () != null ) {
763- targetBand .getProduct ().getIndexCodingGroup ().remove (targetBand .getIndexCoding ());
764- targetBand .setSampleCoding (null );
765- }
766782 setIndexCoding (targetBand , sourceBand .getIndexCoding (), renameComponents , renamePattern );
767783 }
768784
769785 private void setFlagCoding (Band band , FlagCoding flagCoding , boolean rename , String pattern ) {
770- if (flagCoding != null ) {
771- String flagCodingName = flagCoding .getName ();
772- if (rename ) {
773- if (slaveProducts .length == 1 ) {
774- flagCodingName = pattern .replace (SOURCE_NAME_REFERENCE , flagCodingName ).replace (SLAVE_NUMBER_ID_REFERENCE , "" );
775- } else {
776- int id = -1 ;
777- for (int i = 0 ; i < slaveProducts .length ; i ++) {
778- if (band .getProduct ().getName ().equals (slaveProducts [i ].getName ())) {
779- id = i ;
780- break ;
781- }
786+ String flagCodingName = flagCoding .getName ();
787+ if (rename ) {
788+ if (slaveProducts .length == 1 ) {
789+ flagCodingName = pattern .replace (SOURCE_NAME_REFERENCE , flagCodingName ).replace (SLAVE_NUMBER_ID_REFERENCE , "" );
790+ } else {
791+ int id = -1 ;
792+ for (int i = 0 ; i < slaveProducts .length ; i ++) {
793+ if (band .getProduct ().getName ().equals (slaveProducts [i ].getName ())) {
794+ id = i ;
795+ break ;
782796 }
783- flagCodingName = pattern .replace (SOURCE_NAME_REFERENCE , flagCodingName ).replace (SLAVE_NUMBER_ID_REFERENCE , String .valueOf (id ));
784797 }
798+ flagCodingName = pattern .replace (SOURCE_NAME_REFERENCE , flagCodingName ).replace (SLAVE_NUMBER_ID_REFERENCE , String .valueOf (id ));
785799 }
786- final Product product = band .getProduct ();
787- if (!product .getFlagCodingGroup ().contains (flagCodingName )) {
788- addFlagCoding (product , flagCoding , flagCodingName );
789- }
790- band .setSampleCoding (product .getFlagCodingGroup ().get (flagCodingName ));
791800 }
801+ final Product product = band .getProduct ();
802+ if (!product .getFlagCodingGroup ().contains (flagCodingName )) {
803+ addFlagCoding (product , flagCoding , flagCodingName );
804+ }
805+ band .setSampleCoding (product .getFlagCodingGroup ().get (flagCodingName ));
792806 }
793807
794808 private void setIndexCoding (Band band , IndexCoding indexCoding , boolean rename , String pattern ) {
795- if (indexCoding != null ) {
796809 String indexCodingName = indexCoding .getName ();
797810 if (rename ) {
798811 if (slaveProducts .length == 1 ) {
@@ -813,7 +826,6 @@ private void setIndexCoding(Band band, IndexCoding indexCoding, boolean rename,
813826 addIndexCoding (product , indexCoding , indexCodingName );
814827 }
815828 band .setSampleCoding (product .getIndexCodingGroup ().get (indexCodingName ));
816- }
817829 }
818830
819831 private static void addFlagCoding (Product product , FlagCoding flagCoding , String flagCodingName ) {
0 commit comments