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.util.StringTokenizer;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.data.IntToken;
037import ptolemy.data.StringToken;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043/**
044 *
045 */
046public class GISBufferActor extends TypedAtomicActor {
047        public TypedIOPort rasterFileName = new TypedIOPort(this, "rasterFileName",
048                        true, false);
049        public TypedIOPort numRasterRows = new TypedIOPort(this, "numRasterRows",
050                        true, false);
051        public TypedIOPort numRasterCols = new TypedIOPort(this, "numRasterCols",
052                        true, false);
053        public TypedIOPort bufferFileName = new TypedIOPort(this, "bufferFileName",
054                        true, false);
055        public TypedIOPort numDistances = new TypedIOPort(this, "numDistances",
056                        true, false);
057        public TypedIOPort valDistances = new TypedIOPort(this, "valDistances",
058                        true, false);
059        public TypedIOPort bufferFileResult = new TypedIOPort(this,
060                        "bufferFileResult", false, true);
061
062        /**
063         * his actor takes a raster grid and assigns a 'buffed regoin around the
064         * cells with values of 1. This allows one to take a grid created using a
065         * convexHull and expand the masked region about its outer boundary.
066         * 
067         * inputs describe the grid and set the number of buffered regions to add
068         * and the number of cells to add for each buffered region.
069         */
070        public GISBufferActor(CompositeEntity container, String name)
071                        throws NameDuplicationException, IllegalActionException {
072                super(container, name);
073                rasterFileName.setTypeEquals(BaseType.STRING);
074                numRasterRows.setTypeEquals(BaseType.INT);
075                numRasterCols.setTypeEquals(BaseType.INT);
076                bufferFileName.setTypeEquals(BaseType.STRING);
077                numDistances.setTypeEquals(BaseType.INT);
078                valDistances.setTypeEquals(BaseType.STRING);
079                bufferFileResult.setTypeEquals(BaseType.STRING);
080        }
081
082        /**
083   *
084   */
085        public void initialize() throws IllegalActionException {
086        }
087
088        /**
089   *
090   */
091        public boolean prefire() throws IllegalActionException {
092                return super.prefire();
093        }
094
095        /**
096   *
097   */
098        public void fire() throws IllegalActionException {
099                // System.out.println("firing GISBufferActor");
100                super.fire();
101
102                StringToken inputFiletToken = (StringToken) rasterFileName.get(0);
103                String inputFiletNameStr = inputFiletToken.stringValue();
104
105                IntToken numRasterRowsToken = (IntToken) numRasterRows.get(0);
106                int num_RasterRows = numRasterRowsToken.intValue();
107
108                IntToken numRasterColsToken = (IntToken) numRasterCols.get(0);
109                int num_RasterCols = numRasterColsToken.intValue();
110
111                StringToken outputFileToken = (StringToken) bufferFileName.get(0);
112                String outFileStr = outputFileToken.stringValue();
113
114                IntToken numDistancesToken = (IntToken) numDistances.get(0);
115                int num_distances = numDistancesToken.intValue();
116
117                StringToken valDistancesToken = (StringToken) valDistances.get(0);
118                StringTokenizer st = new StringTokenizer(valDistancesToken
119                                .stringValue());
120                int count = st.countTokens();
121                float[] distances = new float[count];
122                int seq = 0;
123                while (st.hasMoreElements()) {
124                        distances[seq++] = (new Float(st.nextToken())).floatValue();
125                }
126
127                // System.out.println("Running GISBuffer JNI code");
128                BufferJniGlue g = new BufferJniGlue();
129                int ret = g.GISBuffer(inputFiletNameStr, num_RasterRows,
130                                num_RasterCols, outFileStr, num_distances, distances);
131                bufferFileResult.broadcast(new StringToken(outFileStr));
132        }
133}