diff --git a/src/ac/ed/lurg/landuse/LandCoverReader.java b/src/ac/ed/lurg/landuse/LandCoverReader.java index dbe86eb27c52d138026c6b037c4bb1140c1591d7..eee229fe49451310e13f0dd778b6a6134572bbb3 100644 --- a/src/ac/ed/lurg/landuse/LandCoverReader.java +++ b/src/ac/ed/lurg/landuse/LandCoverReader.java @@ -1,5 +1,7 @@ package ac.ed.lurg.landuse; +import java.util.Map; + import ac.ed.lurg.types.LandCoverType; import ac.sac.raster.AbstractTabularRasterReader; import ac.sac.raster.RasterHeaderDetails; @@ -9,10 +11,6 @@ import ac.sac.raster.RasterSet; public class LandCoverReader extends AbstractTabularRasterReader<LandCoverItem> { private static final int MIN_COLS = 6; - private int croplandCol = 2; - private int pastureCol = 3; - private int naturalCol = 4; - private int barrenCol = 5; public LandCoverReader (RasterHeaderDetails rasterProj) { super(" +", MIN_COLS); @@ -26,20 +24,12 @@ public class LandCoverReader extends AbstractTabularRasterReader<LandCoverItem> }; } - protected void handleHeader(String[] headertokens) { - handleHeader(headertokens); - croplandCol = findColIndex(headertokens, "CROPLAND"); - pastureCol = findColIndex(headertokens, "PASTURE"); - naturalCol = findColIndex(headertokens, "NATURAL"); - barrenCol = findColIndex(headertokens, "BARREN"); - } - @Override - protected void setData(RasterKey key, LandCoverItem lcData, double[] colValues) { + protected void setData(RasterKey key, LandCoverItem lcData, Map<String, Double> rowValues) { lcData.setTotalArea(dataset.getAreaMha(key)); - lcData.setLandCoverFract(LandCoverType.CROPLAND, colValues[croplandCol]); - lcData.setLandCoverFract(LandCoverType.PASTURE, colValues[pastureCol]); - lcData.setLandCoverFract(LandCoverType.OTHER_NATURAL, colValues[naturalCol]); - lcData.setLandCoverFract(LandCoverType.BARREN, colValues[barrenCol]); + lcData.setLandCoverFract(LandCoverType.CROPLAND, getValueForCol(rowValues, "cropland")); + lcData.setLandCoverFract(LandCoverType.PASTURE, getValueForCol(rowValues, "pasture")); + lcData.setLandCoverFract(LandCoverType.OTHER_NATURAL, getValueForCol(rowValues, "natural")); + lcData.setLandCoverFract(LandCoverType.BARREN, getValueForCol(rowValues, "barren")); } } diff --git a/src/ac/sac/raster/AbstractTabularRasterReader.java b/src/ac/sac/raster/AbstractTabularRasterReader.java index 7abbc23c625f9d49afdc1a4fd63674786a82570a..927b562cb12690082b387e4eb96d9d72b1aa0fa4 100644 --- a/src/ac/sac/raster/AbstractTabularRasterReader.java +++ b/src/ac/sac/raster/AbstractTabularRasterReader.java @@ -3,9 +3,12 @@ package ac.sac.raster; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import ac.ed.lurg.utils.LogWriter; +/** Assumes first two columns are x/y and rest are doubles */ public abstract class AbstractTabularRasterReader<D extends RasterItem> { private static int X_COL = 0; @@ -24,22 +27,20 @@ public abstract class AbstractTabularRasterReader<D extends RasterItem> { return line.trim().split(delimiterRegex); } - private void handleHeader(String[] headertokens) { + private String[] handleHeader(String[] headertokens) { if (headertokens.length < minColNum) { LogWriter.printlnError("Too few columns"); } - } - - protected int findColIndex(String[] headertokens, String colName) { + // List<String> colNames = new ArrayList<String>(Arrays.asList(headertokens)); + for (int i=0; i<headertokens.length; i++) { - if (headertokens[i].toUpperCase().equals(colName)) { - return i; - } + headertokens[i] = headertokens[i].toLowerCase(); } - throw new RuntimeException("Can't find colName " + colName); + return headertokens; } + public RasterSet<D> getRasterDataFromFile(String filename) { long startTime = System.currentTimeMillis(); @@ -47,26 +48,29 @@ public abstract class AbstractTabularRasterReader<D extends RasterItem> { try { in = new BufferedReader(new FileReader(filename)); String line = in.readLine(); // read header - handleHeader(parseLine(line)); + String[] dataColNames = handleHeader(parseLine(line)); while ((line=in.readLine()) != null) { String[] tokens = parseLine(line); - - double[] colValues = new double[tokens.length]; - for (int i=0; i<tokens.length; i++) { + + double x = Double.parseDouble(tokens[X_COL]); + double y = Double.parseDouble(tokens[Y_COL]); + + Map<String, Double> rowValues = new HashMap<String, Double>(dataColNames.length); + for (int i=2; i<tokens.length; i++) { double d = Double.parseDouble(tokens[i]); - colValues[i] = d; + rowValues.put(dataColNames[i], d); } - D item = dataset.getFromCoordinates(colValues[X_COL], colValues[Y_COL]); - RasterKey key = dataset.getKeyFromCoordinates(colValues[X_COL], colValues[Y_COL]); - setData(key, item, colValues); + D item = dataset.getFromCoordinates(x, y); + RasterKey key = dataset.getKeyFromCoordinates(x, y); + setData(key, item, rowValues); } in.close(); } catch (Exception e) { - LogWriter.printlnError("Problem reading initial land cover data"); + LogWriter.printlnError("Problem reading data file"); e.printStackTrace(); throw new RuntimeException(e); } @@ -83,6 +87,16 @@ public abstract class AbstractTabularRasterReader<D extends RasterItem> { return dataset; } + + protected double getValueForCol(Map<String, Double> rowValues, String colName) { + Double d = rowValues.get(colName); + if (d == null) { + LogWriter.printlnError("Can't find column name " + colName); + throw new RuntimeException("Can't find column name " + colName); + } + + return d; + } - abstract protected void setData(RasterKey key, D item, double[] colValues); + abstract protected void setData(RasterKey key, D item, Map<String, Double> rowValues); }