001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.ecoinformatics.seek.gis.grass; 031 032import java.io.BufferedReader; 033import java.io.InputStreamReader; 034 035import ptolemy.actor.TypedAtomicActor; 036import ptolemy.actor.TypedIOPort; 037import ptolemy.data.StringToken; 038import ptolemy.data.expr.Parameter; 039import ptolemy.data.type.BaseType; 040import ptolemy.kernel.CompositeEntity; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043 044/** 045 * This actor imports an ARC file into the GRASS database. it does not produce 046 * any tangible output, rather it just writes the info the the GRASS DB and 047 * fires a trigger to say that it's done. 048 */ 049 050public class GrassImportActor extends TypedAtomicActor { 051 // input ports 052 public TypedIOPort inFileName = new TypedIOPort(this, "inFileName", true, 053 false); 054 public TypedIOPort dataType = new TypedIOPort(this, "dataType", true, false); 055 public TypedIOPort objectName = new TypedIOPort(this, "objectName", true, 056 false); 057 public TypedIOPort trigger = new TypedIOPort(this, "trigger", false, true); 058 059 public Parameter grass_gisbase = new Parameter(this, "GRASS_GISBASE"); 060 public Parameter location_name = new Parameter(this, "LOCATION_NAME"); 061 public Parameter gisdbase = new Parameter(this, "GISDBASE"); 062 public Parameter gisrc = new Parameter(this, "GISRC"); 063 064 private String GRASS_GISBASE; 065 private String LOCATION_NAME; 066 private String GISDBASE; 067 private String GISRC; 068 069 /** 070 * Grass Import Actor. imports an arc file into the grass database. 071 */ 072 public GrassImportActor(CompositeEntity container, String name) 073 throws NameDuplicationException, IllegalActionException { 074 super(container, name); 075 inFileName.setTypeEquals(BaseType.STRING); 076 dataType.setTypeEquals(BaseType.STRING); 077 objectName.setTypeEquals(BaseType.STRING); 078 trigger.setTypeEquals(BaseType.STRING); 079 } 080 081 /** 082 * 083 */ 084 public void initialize() throws IllegalActionException { 085 if (grass_gisbase.getToken() != null) { 086 GRASS_GISBASE = ((StringToken) grass_gisbase.getToken()).toString(); 087 // get rid of the quotes 088 GRASS_GISBASE = GRASS_GISBASE.substring(1, 089 GRASS_GISBASE.length() - 1); 090 System.out.println("GRASS_GISBASE: " + GRASS_GISBASE); 091 } 092 if (location_name.getToken() != null) { 093 LOCATION_NAME = ((StringToken) location_name.getToken()).toString(); 094 // get rid of the quotes 095 LOCATION_NAME = LOCATION_NAME.substring(1, 096 LOCATION_NAME.length() - 1); 097 System.out.println("LOCATION_NAME: " + LOCATION_NAME); 098 } 099 if (gisdbase.getToken() != null) { 100 GISDBASE = ((StringToken) gisdbase.getToken()).toString(); 101 // get rid of the quotes 102 GISDBASE = GISDBASE.substring(1, GISDBASE.length() - 1); 103 System.out.println("GISDBASE: " + GISDBASE); 104 } 105 if (gisrc.getToken() != null) { 106 GISRC = ((StringToken) gisrc.getToken()).toString(); 107 // get rid of the quotes 108 GISRC = GISRC.substring(1, GISRC.length() - 1); 109 System.out.println("GISRC: " + GISRC); 110 } 111 112 if (GISRC == null || GISDBASE == null || LOCATION_NAME == null 113 || GRASS_GISBASE == null) { 114 throw new IllegalActionException( 115 "The parameters GISRC, GISBASE, " 116 + "LOCATION_NAME and GRASS_GISBASE must have valid values. GISRC is " 117 + "the path to your .grassrc file. GISBASE is the path to your grass5" 118 + "installation directory. LOCATION_NAME is the name of the database " 119 + "location within GRASS. GRASS_GISBASE is the base directory for " 120 + "your GRASS database. Please provide these paths and re-execute."); 121 } 122 } 123 124 /** 125 * 126 */ 127 public boolean prefire() throws IllegalActionException { 128 return super.prefire(); 129 } 130 131 /** 132 * 133 */ 134 public void fire() throws IllegalActionException { 135 System.out.println("firing GrassImportActor"); 136 super.fire(); 137 138 StringToken inFileToken = (StringToken) inFileName.get(0); 139 String inFileStr = inFileToken.stringValue(); 140 141 StringToken dataTypeToken = (StringToken) dataType.get(0); 142 String dataTypeStr = dataTypeToken.stringValue(); 143 144 StringToken objectNameToken = (StringToken) objectName.get(0); 145 String objectNameTypeStr = objectNameToken.stringValue(); 146 147 String errorout = ""; 148 // /////////////////////////// IMPL CODE 149 // ////////////////////////////////// 150 151 try { 152 final Process listener; 153 String importCommand = null; 154 155 if (dataTypeStr.equalsIgnoreCase("asc")) { 156 importCommand = "r.in.arc"; 157 } else if (dataTypeStr.equalsIgnoreCase("shp")) { 158 importCommand = "r.in.shape"; 159 } else { 160 importCommand = "r.in.arc"; // default 161 } 162 163 String args[] = { this.GRASS_GISBASE + "/bin/" + importCommand, 164 "input=" + inFileStr, "output=" + objectNameTypeStr }; 165 166 String envvars[] = { "GISBASE=" + this.GRASS_GISBASE, 167 "LOCATION_NAME=" + this.LOCATION_NAME, 168 "GISDBASE=" + this.GISDBASE, "GISRC=" + this.GISRC }; 169 170 listener = Runtime.getRuntime().exec(args, envvars); 171 172 /* 173 * new Thread(new Runnable() { public void run() { try { 174 * BufferedReader br_in = new BufferedReader( new 175 * InputStreamReader(listener.getInputStream())); BufferedReader 176 * br_err_in = new BufferedReader( new 177 * InputStreamReader(listener.getErrorStream())); String buff = 178 * null; while ((br_in != null && (buff = br_in.readLine()) != 179 * null)) { System.out.println("Process out: " + buff); try 180 * {Thread.sleep(1); } catch(Exception e) {} } br_in.close(); while 181 * ((br_err_in != null && (buff = br_err_in.readLine()) != null)) { 182 * System.out.println("Process error out: " + buff); try 183 * {Thread.sleep(1); } catch(Exception e) {} } br_err_in.close(); } 184 * catch (IOException ioe) { 185 * System.out.println("Exception caught printing result"); 186 * ioe.printStackTrace(); } } }).start(); 187 */ 188 189 BufferedReader br_err_in = new BufferedReader( 190 new InputStreamReader(listener.getErrorStream())); 191 String buff = null; 192 while ((br_err_in != null && (buff = br_err_in.readLine()) != null)) { 193 errorout += buff + "\n"; 194 } 195 br_err_in.close(); 196 197 System.out.println("****" + errorout + "****"); 198 199 // thread to wait for grass process to terminate 200 new Thread(new Runnable() { 201 public void run() { 202 try { 203 System.out 204 .println("*Thread waiting for Process to exit"); 205 listener.waitFor(); 206 System.out.println("*Thread detected Process exiting"); 207 } catch (InterruptedException ie) { 208 System.out 209 .println("InterruptedException caught whilst waiting " 210 + "for Mozilla Process to exit: " + ie); 211 ie.printStackTrace(); 212 } 213 } 214 }).start(); 215 } catch (Exception e) { 216 e.printStackTrace(); 217 throw new IllegalActionException("Unexpected error: " 218 + e.getMessage()); 219 } 220 221 // //////////////////////////////////////////////////////////////////////// 222 223 if (errorout.indexOf("CREATING SUPPORT FILES FOR") != -1) { 224 trigger.broadcast(new StringToken("SUCCESS")); 225 System.out.println("Grass Import Action done"); 226 } else { 227 throw new IllegalActionException("There was a problem running the " 228 + "GRASS script: " + errorout); 229 } 230 } 231}