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 java.io.File;
032import java.io.IOException;
033import java.nio.file.Files;
034import java.nio.file.Paths;
035
036import org.apache.commons.io.FilenameUtils;
037import org.kepler.gis.util.GISUtilities;
038import org.kepler.gis.util.RasterUtilities;
039import org.kepler.gis.util.RasterUtilities.RasterInfo;
040import org.opengis.referencing.crs.CoordinateReferenceSystem;
041
042import ptolemy.actor.TypedAtomicActor;
043import ptolemy.actor.TypedIOPort;
044import ptolemy.data.StringToken;
045import ptolemy.data.Token;
046import ptolemy.data.type.BaseType;
047import ptolemy.kernel.CompositeEntity;
048import ptolemy.kernel.util.IllegalActionException;
049import ptolemy.kernel.util.NameDuplicationException;
050
051/** Get the projection for a file.
052 * 
053 *  @author Daniel Crawl
054 *  @version $Id: GetProjection.java 34523 2016-08-28 02:19:15Z crawl $
055 */
056public class GetProjection extends TypedAtomicActor {
057
058
059    public GetProjection(CompositeEntity container, String name) throws IllegalActionException, NameDuplicationException {
060        super(container, name);
061        
062        file = new TypedIOPort(this, "file", true, false);
063        file.setTypeEquals(BaseType.STRING);
064        
065        code = new TypedIOPort(this, "code", false, true);
066        code.setTypeEquals(BaseType.STRING);
067        
068    }
069    
070    @Override
071    public void fire() throws IllegalActionException {
072        
073        super.fire();
074        
075        Token token = file.get(0);
076        if(token == null) {
077            throw new IllegalActionException(this, "No input file name.");
078        }
079        String fileName = ((StringToken)token).stringValue();
080        
081        if(fileName.trim().isEmpty()) {
082            throw new IllegalActionException("Input file name is empty.");
083        }
084        
085        String dirName = FilenameUtils.getFullPath(fileName);
086        String prjFileName = FilenameUtils.getBaseName(fileName) + ".prj";
087        File prjFile = new File(dirName, prjFileName);
088
089        String wktStr;
090
091        if(prjFile.exists()) {
092            try {
093                wktStr = new String(Files.readAllBytes(Paths.get(prjFile.getAbsolutePath())));
094            } catch(IOException e) {
095                throw new IllegalActionException(this, e, "Error reading " + prjFile);
096            }
097        } else {
098            
099            // see if gdal can get the projection
100            RasterInfo info = RasterUtilities.getInfo(new File(fileName));
101            if(info == null) {
102                throw new IllegalActionException(this, "No .prj file and not a raster: " + fileName);
103            }
104            CoordinateReferenceSystem crs = info.getCRS();
105            if(crs == null) {
106              throw new IllegalActionException(this, "CRS is null for " + fileName);  
107            }
108            wktStr = crs.toWKT();            
109        }
110
111        Integer codeVal = GISUtilities.getEPSGCode(wktStr);
112        
113        //System.out.println(codeVal + " for " + fileName);
114        
115        code.broadcast(new StringToken("EPSG:" + String.valueOf(codeVal)));        
116    }
117    
118    /** The input file. */
119    public TypedIOPort file;
120    
121    /** The EPSG code. */
122    public TypedIOPort code;
123    
124
125}