001/*
002 * Copyright (c) 2016 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2016-08-28 02:19:15 +0000 (Sun, 28 Aug 2016) $' 
007 * '$Revision: 34523 $'
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 */
029package org.kepler.gis.actor.proj;
030
031import org.geotools.geometry.jts.ReferencedEnvelope;
032import org.kepler.gis.actor.GetBoundingBox;
033import org.kepler.gis.data.GISToken;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.data.IntToken;
038import ptolemy.data.Token;
039import ptolemy.data.type.BaseType;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043
044/** Get the UTM Zone for a gis dataset.
045 * 
046 *  @author Daniel Crawl
047 *  @version $Id: GetUTMZone.java 34523 2016-08-28 02:19:15Z crawl $
048 */
049public class GetUTMZone extends TypedAtomicActor {
050
051    public GetUTMZone(CompositeEntity container, String name) throws IllegalActionException, NameDuplicationException {
052        super(container, name);
053        
054        input = new TypedIOPort(this, "input", true, false);
055        input.setTypeAtMost(GISToken.GIS);
056
057        longitudeZone = new TypedIOPort(this, "longitudeZone", false, true);
058        longitudeZone.setTypeEquals(BaseType.INT);
059        
060    }
061    
062    @Override
063    public void fire() throws IllegalActionException {
064
065        super.fire();
066        
067        Token token = input.get(0);
068        ReferencedEnvelope envelope = GetBoundingBox.getBoundingBox(token, null);
069        
070        double longitude = (envelope.getMaxX() + envelope.getMinX()) / 2;
071
072        //System.out.println("longitude is " + longitude);
073
074        double longZone = 0;
075        if (longitude < 0.0) {
076          longZone = ((180.0 + longitude) / 6) + 1;
077        } else {
078          longZone = (longitude / 6) + 31;
079        }        
080        
081        //System.out.println("longitude zone = " + longZone);
082        
083        longitudeZone.broadcast(new IntToken(Double.valueOf(longZone).intValue()));
084
085    }
086
087    /** The input file. */
088    public TypedIOPort input;
089    
090    /** The UTM zone for the longitude. */
091    public TypedIOPort longitudeZone;
092}