From f2d5b6322518ea55320015f418acf6caf6afca1e Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Thu, 16 Jun 2016 14:20:17 +0100
Subject: [PATCH] Handle crop fraction when interpolating to zero cropland
 cover

---
 src/ac/ed/lurg/landuse/LandUseItem.java | 45 ++++++++++++++++---------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/src/ac/ed/lurg/landuse/LandUseItem.java b/src/ac/ed/lurg/landuse/LandUseItem.java
index 94c2d60d..3f6b6b05 100644
--- a/src/ac/ed/lurg/landuse/LandUseItem.java
+++ b/src/ac/ed/lurg/landuse/LandUseItem.java
@@ -220,22 +220,35 @@ public class LandUseItem implements InterpolatingRasterItem<LandUseItem> {
 		cropFractions = new HashMap<CropType, Double>();
 		landCoverAreas = new HashMap<LandCoverType, Double>();
 
-		for (CropType crop : CropType.values()) {
-			Double from = fromItem.cropFractions.get(crop);
-			Double to = toItem.cropFractions.get(crop);
-			Double d = Interpolator.interpolate(from, to, factor);
-			cropFractions.put(crop, d);
-			
-			Intensity fromIntensity = fromItem.intensityMap.get(crop);
-			Intensity toIntensity = toItem.intensityMap.get(crop);
-			Intensity interpolateIntensity = toIntensity;  // might still be null
-			
-			if (fromIntensity != null && toIntensity != null)
-				interpolateIntensity = new Intensity(fromIntensity, toIntensity, factor);  // both non-null really interpolate
-			else if (fromIntensity != null)
-				interpolateIntensity = fromIntensity; // just fromIntensity non-null
-			
-			intensityMap.put(crop, interpolateIntensity);
+		Double fromCropCover = fromItem.landCoverAreas.get(LandCoverType.CROPLAND);
+		Double toCropCover = toItem.landCoverAreas.get(LandCoverType.CROPLAND);
+
+		if (fromCropCover != 0 && toCropCover == 0) { // if start with crop but end with none, take starting crop fractions
+			cropFractions.putAll(fromItem.cropFractions);
+			intensityMap.putAll(fromItem.intensityMap);
+		}
+		else if (fromCropCover == 0 && toCropCover != 0) { // if start with no crop but end with some, take end crop fractions
+			cropFractions.putAll(toItem.cropFractions);
+			intensityMap.putAll(toItem.intensityMap);
+		}
+		else { // otherwise we need to interpolate crop fractions
+			for (CropType crop : CropType.values()) {
+				Double from = fromItem.cropFractions.get(crop);
+				Double to = toItem.cropFractions.get(crop);
+				Double d = Interpolator.interpolate(from, to, factor);
+				cropFractions.put(crop, d);
+				
+				Intensity fromIntensity = fromItem.intensityMap.get(crop);
+				Intensity toIntensity = toItem.intensityMap.get(crop);
+				Intensity interpolateIntensity = toIntensity;  // might still be null
+				
+				if (fromIntensity != null && toIntensity != null)
+					interpolateIntensity = new Intensity(fromIntensity, toIntensity, factor);  // both non-null really interpolate
+				else if (fromIntensity != null)
+					interpolateIntensity = fromIntensity; // just fromIntensity non-null
+				
+				intensityMap.put(crop, interpolateIntensity);
+			}
 		}
 		
 		for (LandCoverType landCover : LandCoverType.values()) {
-- 
GitLab