[Schmitzm-commits] r2313 - in trunk/schmitzm-core/src/main/java/de/schmitzm: data lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri May 24 16:35:01 CEST 2013


Author: mojays
Date: 2013-05-24 16:35:01 +0200 (Fri, 24 May 2013)
New Revision: 2313

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/DataUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
DataUtil: new method to writer ASCII raster to file
LangUtil: generic sort(.) method extended with "ignoreCase" parameter


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/DataUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/DataUtil.java	2013-05-23 13:40:29 UTC (rev 2312)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/DataUtil.java	2013-05-24 14:35:01 UTC (rev 2313)
@@ -29,8 +29,14 @@
  ******************************************************************************/
 package de.schmitzm.data;
 
+import java.awt.image.Raster;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.Locale;
 
+import de.schmitzm.io.IOUtil;
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.lang.ResourceProvider;
 
@@ -46,4 +52,60 @@
    *  in properties-Datein unter {@code schmitzm.data.resource.locales}
    *  hinterlegt. */
   public static ResourceProvider RESOURCE = ResourceProvider.newInstance( LangUtil.extendPackagePath(DataUtil.class,"resource.locales.DataResourceBundle"), Locale.ENGLISH );
+  
+
+  /**
+   * Exports a {@link Raster} to ASCII file.
+   * @param raster raster
+   * @param destFile destination file
+   * @param delim delimiter to separate the columns with (if {@code null} a white-space is used, which is
+   *        the usual ascii raster format; other values, e.g. semicolon, can be used, if ascii raster should
+   *        be processed in Excel) 
+   * @param inclHeader indicates whether to generate ascii raster header (NCOLS, NROWS, XLLCORNER, ...)
+   * @throws IOException
+   */
+  public static void writeRasterToAsciiFile(Raster raster, File destFile, Character delim, boolean inclHeader) throws IOException {
+    if ( delim == null )
+      delim = ' ';
+        
+    BufferedWriter out = null;
+    try {
+      out = new BufferedWriter( new FileWriter(destFile) );
+      int    wCells  = raster.getWidth();
+      int    hCells  = raster.getHeight();
+      int    minX    = raster.getMinX();
+      int    minY    = raster.getMinY();
+      double xBounds = raster.getBounds().getX();
+      double yBounds = raster.getBounds().getY();
+      double wBounds = raster.getBounds().getWidth();
+      // treat UR corner as 0/0
+      // -> to display raster in geographical visualisation (map)
+      //    the Y coordinates are negative in south direction!
+      double xBoundsLL = xBounds;
+      double yBoundsLL = -(yBounds+hCells);
+      
+      // Header
+      if ( inclHeader ) {
+        out.write("NCOLS"+delim+wCells+"\n");
+        out.write("NROWS"+delim+hCells+"\n");
+        out.write("XLLCORNER"+delim+xBoundsLL+"\n");
+        out.write("YLLCORNER"+delim+yBoundsLL+"\n");
+        out.write("CELLSIZE"+delim+wBounds/wCells+"\n");
+      }
+      // Data
+      for (int r=0; r<hCells; r++) {
+        for (int c=0; c<wCells; c++) {
+          double cellValue = raster.getSampleDouble(minX+c, minY+r, 0);
+          if ( c > 0 )
+            out.write(delim);
+          out.write(""+cellValue);
+        }
+        out.newLine();
+      }
+    } finally {
+      IOUtil.closeWriter(out);
+    }
+  }
+  
+  
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-05-23 13:40:29 UTC (rev 2312)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-05-24 14:35:01 UTC (rev 2313)
@@ -1881,14 +1881,30 @@
 	 *            Objekte
 	 */
 	public static void sort(Object[] objects) {
-		Arrays.sort(objects, new Comparator<Object>() {
-			public int compare(Object c1, Object c2) {
-				return c1.toString().compareTo(c2.toString());
-			}
-		});
+	  sort(objects,false);
 	}
 
-	/**
+    /**
+     * Sortiert einen Array aus beliebigen Objekten nach der natuerlichen Sortierung ihrer {@code toString()}-Werte.
+     * 
+     * @param objects
+     *            Objekte
+     */
+    public static void sort(Object[] objects, final boolean ignoreCase) {
+        Arrays.sort(objects, new Comparator<Object>() {
+            public int compare(Object c1, Object c2) {
+                String s1 = c1.toString();
+                String s2 = c2.toString();
+                if ( ignoreCase ) {
+                  s1 = s1.toLowerCase();
+                  s2 = s2.toLowerCase();
+                }
+                return s1.compareTo(s2);
+            }
+        });
+    }
+
+    /**
 	 * Returns the maximum value of some integer collections.
 	 * 
 	 * @param defMaxValue



More information about the Schmitzm-commits mailing list