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 ptolemy.actor.TypedAtomicActor;
033import ptolemy.actor.TypedIOPort;
034import ptolemy.data.DoubleToken;
035import ptolemy.data.IntToken;
036import ptolemy.data.StringToken;
037import ptolemy.data.type.BaseType;
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041
042/**
043 * This actor crease a spatial raster grid usng the ConvexHull polygon. Add
044 * raster points within the ConvexHull are set to a value of 1. Points outside
045 * have a value of 0. The raster is thus a 'mask'.
046 * 
047 * 'hullFileName' is a text file with the (x,y) values of the convex hull (one
048 * pair per line, space delimited) 'numHullPoint' is the number of Hull points
049 * 'rasterFileName' is the name to be given to the raster output 'numRasterRows'
050 * is the number of rows for the raster 'numRasterCols' is the number of columns
051 * for the raster 'xmin' is the minumum x value (if -1 set to the minimum x in
052 * the convexHull) 'xmax' is the minumum x value (if -1 set to the minimum x in
053 * the convexHull) 'ymin' is the minumum y value (if -1 set to the minimum y in
054 * the convexHull) 'xmax' is the minumum y value (if -1 set to the minimum y in
055 * the convexHull)
056 * 
057 */
058public class GISRasterActor extends TypedAtomicActor {
059        // input ports
060        public TypedIOPort hullFileName = new TypedIOPort(this, "hullFileName",
061                        true, false);
062        public TypedIOPort numHullPoint = new TypedIOPort(this, "numHullPoint",
063                        true, false);
064        public TypedIOPort rasterFileName = new TypedIOPort(this, "rasterFileName",
065                        true, false);
066        public TypedIOPort numRasterRows = new TypedIOPort(this, "numRasterRows",
067                        true, false);
068        public TypedIOPort numRasterCols = new TypedIOPort(this, "numRasterCols",
069                        true, false);
070
071        public TypedIOPort xmin = new TypedIOPort(this, "xmin", true, false);
072        public TypedIOPort ymin = new TypedIOPort(this, "ymin", true, false);
073        public TypedIOPort xmax = new TypedIOPort(this, "xmax", true, false);
074        public TypedIOPort ymax = new TypedIOPort(this, "ymax", true, false);
075
076        public TypedIOPort rasterFileResult = new TypedIOPort(this,
077                        "rasterFileResult", false, true);
078
079        /**
080   *
081   */
082        public GISRasterActor(CompositeEntity container, String name)
083                        throws NameDuplicationException, IllegalActionException {
084                super(container, name);
085                hullFileName.setTypeEquals(BaseType.STRING);
086                numHullPoint.setTypeEquals(BaseType.INT);
087                rasterFileName.setTypeEquals(BaseType.STRING);
088                numRasterRows.setTypeEquals(BaseType.INT);
089                numRasterCols.setTypeEquals(BaseType.INT);
090                xmin.setTypeEquals(BaseType.DOUBLE);
091                ymin.setTypeEquals(BaseType.DOUBLE);
092                xmax.setTypeEquals(BaseType.DOUBLE);
093                ymax.setTypeEquals(BaseType.DOUBLE);
094                rasterFileResult.setTypeEquals(BaseType.STRING);
095        }
096
097        /**
098   *
099   */
100        public void initialize() throws IllegalActionException {
101        }
102
103        /**
104   *
105   */
106        public boolean prefire() throws IllegalActionException {
107                return super.prefire();
108        }
109
110        /**
111   *
112   */
113        public void fire() throws IllegalActionException {
114                // System.out.println("firing GISRasterActor");
115                super.fire();
116
117                StringToken inputFiletToken = (StringToken) hullFileName.get(0);
118                String inputFiletNameStr = inputFiletToken.stringValue();
119
120                IntToken numHullPointToken = (IntToken) numHullPoint.get(0);
121                int num_HullPoint = numHullPointToken.intValue();
122
123                StringToken outputFileToken = (StringToken) rasterFileName.get(0);
124                String outFileStr = outputFileToken.stringValue();
125
126                IntToken numRasterRowsToken = (IntToken) numRasterRows.get(0);
127                int num_RasterRows = numRasterRowsToken.intValue();
128
129                IntToken numRasterColsToken = (IntToken) numRasterCols.get(0);
130                int num_RasterCols = numRasterColsToken.intValue();
131
132                double x_min = ((DoubleToken) (xmin.get(0))).doubleValue();
133                double y_min = ((DoubleToken) (ymin.get(0))).doubleValue();
134                double x_max = ((DoubleToken) (xmax.get(0))).doubleValue();
135                double y_max = ((DoubleToken) (ymax.get(0))).doubleValue();
136
137                // System.out.println("Running GISRaster JNI code");
138                RasterJniGlue g = new RasterJniGlue();
139
140                g.ReadInput(inputFiletNameStr, num_HullPoint);
141                g.SetGridSize(num_RasterRows, num_RasterCols);
142                double[] boundary = g.GetBoundary();
143                System.out.println(boundary[0] + " " + boundary[1] + " " + boundary[2]
144                                + " " + boundary[3]);
145                if (x_min == -1)
146                        x_min = boundary[0];
147                if (y_min == -1)
148                        y_min = boundary[1];
149                if (x_max == -1)
150                        x_max = boundary[2];
151                if (y_max == -1)
152                        y_max = boundary[3];
153
154                g.SetBoundary(x_min, y_min, x_max, y_max);
155                g.PerformRasterization();
156                g.WriteOutput(outFileStr);
157
158                rasterFileResult.broadcast(new StringToken(outFileStr));
159        }
160}