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.java_gis;
031
032import java.io.File;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.data.IntToken;
037import ptolemy.data.StringToken;
038import ptolemy.data.expr.StringParameter;
039import ptolemy.data.type.BaseType;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043
044/**
045 * <p>
046 * The purpose of this actor is to take a set of (x, y) points and return the
047 * points that define the convex hull around the input. The convex hull can be
048 * thought of as the region defined by a 'rubber band' placed around the
049 * original data set. It is a sort of smallest polygon surrounding the input.
050 * The ConvexHull routine is implemented in Java and is thus portable to any
051 * java-enabled system. This actor is designed to have the same functionality as
052 * the JNI basedGISHullActor
053 * </p>
054 * <p>
055 *'pointFileName' is a tab delimited text file with x,y input points<br/>
056 *'hullFileName' is the name to be given to the hull point list file<br/>
057 *'numHullPoint' is the number of x,y pairs in the hull file<br/>
058 *'hullFileResult' is the output hull file name (same value as the
059 * 'hullFileName' but used as a trigger for output
060 * </p>
061 * <p>
062 * There is also a 'scaleFactorParameter'. This is the scale factor for an
063 * AffineTransformation of the shape created by the ConvexHull. The convexHull
064 * shape is scaled by this factor (linearly), centered on the center of the
065 * convexhull bounding rectangle. The scale by area, set the scalefactor to the
066 * square root of the area scaling factor (i.e. to make a shape with twice the
067 * area, set the scale factor to SQRT(2) )
068 * </p>
069 * <p>
070 * Note: if the scaleFactorParameter is empty or not a number, no scaling will
071 * be done.
072 * </p>
073 */
074
075public class GISHullActorJ extends TypedAtomicActor {
076        // input ports
077        /**
078         * 'pointFileName' is a tab delimited text file with x,y input points
079         */
080        public TypedIOPort pointFileName = new TypedIOPort(this, "pointFileName",
081                        true, false);
082        /**
083         * 'hullFileName' is the name to be given to the hull point list file
084         */
085        public TypedIOPort hullFileName = new TypedIOPort(this, "hullFileName",
086                        true, false);
087        /**
088         * 'numHullPoint' is the number of x,y pairs in the hull file<br/>
089         */
090        public TypedIOPort numHullPoint = new TypedIOPort(this, "numHullPoint",
091                        false, true);
092        /**
093         * 'hullFileResult' is the output hull file name (same value as the
094         * 'hullFileName' but used as a trigger for output
095         */
096        public TypedIOPort hullFileResult = new TypedIOPort(this, "hullFileResult",
097                        false, true);
098
099        /**
100         * This is the scale factor for an AffineTransformation of the shape created
101         * by the ConvexHull. The convexHull shape is scaled by this factor
102         * (linearly), centered on the center of the convexhull bounding rectangle.
103         * The scale by area, set the scalefactor to the square root of the area
104         * scaling factor (i.e. to make a shape with twice the area, set the scale
105         * factor to SQRT(2) )
106         */
107        public StringParameter scaleFactorParameter;
108
109        public GISHullActorJ(CompositeEntity container, String name)
110                        throws NameDuplicationException, IllegalActionException {
111                super(container, name);
112
113                scaleFactorParameter = new StringParameter(this, "scaleFactorParameter");
114                scaleFactorParameter.setDisplayName("Scale Factor");
115
116                pointFileName.setTypeEquals(BaseType.STRING);
117                hullFileName.setTypeEquals(BaseType.STRING);
118                numHullPoint.setTypeEquals(BaseType.INT);
119                hullFileResult.setTypeEquals(BaseType.STRING);
120        }
121
122        /**
123   *
124   */
125
126        public void initialize() throws IllegalActionException {
127
128        }
129
130        /**
131   *
132   */
133        public boolean prefire() throws IllegalActionException {
134                return super.prefire();
135        }
136
137        /**
138   *
139   */
140        public void fire() throws IllegalActionException {
141                super.fire();
142
143                String scaleFactorString = "";
144                double scaleFactor = 1.0;
145                scaleFactorString = scaleFactorParameter.stringValue();
146                try {
147                        scaleFactor = (new Double(scaleFactorString)).doubleValue();
148                        // System.out.println("Scale factor: "+scaleFactor);
149                } catch (Exception w) {
150                        scaleFactorString = "";
151                        scaleFactor = 1.0;
152                }
153                StringToken inputFileToken = (StringToken) pointFileName.get(0);
154                String inputFileNameStr = inputFileToken.stringValue();
155                StringToken outputFileToken = (StringToken) hullFileName.get(0);
156                String outFileStr = outputFileToken.stringValue();
157
158                File pointFile = new File(inputFileToken.stringValue());
159                ConvexHull ch = new ConvexHull(pointFile);
160                if (scaleFactorString.equals("")) {
161                        ch.cvHullToFile(outFileStr);
162                } else {
163                        ch.cvScaledHullToFile(scaleFactor, outFileStr);
164                }
165
166                int num_HullPoint = ch.getCHListSize();
167                // note: scaled convex hull will have same number of points
168
169                numHullPoint.broadcast(new IntToken(num_HullPoint));
170                hullFileResult.broadcast(new StringToken(outFileStr));
171        }
172
173}