001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2016-04-21 05:38:24 +0000 (Thu, 21 Apr 2016) $' 
007 * '$Revision: 34475 $'
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;
030
031import org.geotools.referencing.CRS;
032import org.opengis.referencing.FactoryException;
033import org.opengis.referencing.crs.CoordinateReferenceSystem;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.parameters.PortParameter;
037import ptolemy.data.BooleanToken;
038import ptolemy.data.StringToken;
039import ptolemy.data.Token;
040import ptolemy.data.expr.Parameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.Attribute;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046import ptolemy.kernel.util.Workspace;
047
048/** Base class for a GIS actor containing a CRS parameter.
049 * 
050 *  @author Daniel Crawl
051 *  @version $Id: CRSActor.java 34475 2016-04-21 05:38:24Z crawl $
052 */
053public abstract class CRSActor extends TypedAtomicActor {
054
055    public CRSActor(CompositeEntity container, String name) throws IllegalActionException, NameDuplicationException {
056        super(container, name);
057
058        crs = new PortParameter(this, "crs");
059        crs.setTypeEquals(BaseType.STRING);
060        crs.setStringMode(true);
061        crs.addChoice("EPSG:4326");
062        crs.addChoice("EPSG:26911");
063        crs.getPort().setTypeEquals(BaseType.STRING);
064        new Attribute(crs.getPort(), "_showName");
065        
066        longitudeFirst = new Parameter(this, "longitudeFirst");
067        longitudeFirst.setTypeEquals(BaseType.BOOLEAN);
068        longitudeFirst.setToken(BooleanToken.FALSE);
069    }
070
071    @Override
072    public void attributeChanged(Attribute attribute) throws IllegalActionException {
073        
074        boolean decode = false;
075        
076        if(attribute == crs) {
077            _crs = null;
078            Token token = crs.getToken();
079            if(token != null) {
080                String crsStr = ((StringToken)token).stringValue().trim();
081                if(!crsStr.isEmpty()) {
082                    decode = true;
083                }
084            }
085        } else if(attribute == longitudeFirst) {
086            decode = true;
087        } else {
088            super.attributeChanged(attribute);
089        }
090        
091        if(decode) {
092            boolean longitudeFirstVal = ((BooleanToken)longitudeFirst.getToken()).booleanValue();
093            Token token = crs.getToken();
094            if(token != null) {
095                String crsStr = ((StringToken)token).stringValue().trim();
096                if(!crsStr.isEmpty()) {
097                    try {
098                        _crs = CRS.decode(crsStr, longitudeFirstVal);
099                    } catch (FactoryException e) {
100                        throw new IllegalActionException(this, e, "Error decoding " + crsStr);
101                    }
102                }
103            }
104        }
105    }
106    
107    /** Clone the actor into the specified workspace. */
108    @Override
109    public Object clone(Workspace workspace) throws CloneNotSupportedException  {
110        CRSActor newObject = (CRSActor) super.clone(workspace);
111        newObject._crs = null;
112        return newObject;
113    }
114    
115    @Override
116    public void fire() throws IllegalActionException {
117        super.fire();
118        crs.update();
119    }
120    
121    /** The coordinate reference system. */
122    public PortParameter crs;
123
124    /** If true, longitude is first in axis. */
125    public Parameter longitudeFirst;
126    
127    /** The coordinate reference system object. */
128    protected CoordinateReferenceSystem _crs;
129}