001/*
002 * Copyright (c) 2016 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2018-05-02 05:42:57 +0000 (Wed, 02 May 2018) $' 
007 * '$Revision: 34690 $'
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.data;
030
031import java.io.IOException;
032
033import org.geotools.data.simple.SimpleFeatureCollection;
034import org.geotools.referencing.CRS;
035import org.geotools.referencing.crs.DefaultGeographicCRS;
036import org.json.JSONException;
037import org.kepler.gis.util.GISUtilities;
038import org.kepler.gis.util.VectorUtilities;
039import org.kepler.webview.data.Converter;
040import org.opengis.feature.simple.SimpleFeatureType;
041import org.opengis.referencing.FactoryException;
042import org.opengis.referencing.crs.CoordinateReferenceSystem;
043
044import ptolemy.data.Token;
045import ptolemy.data.type.Type;
046import ptolemy.kernel.util.IllegalActionException;
047
048/** Class to convert between VectorTokens and JSON strings.
049 * 
050 * @author crawl
051 * @version $Id: WebViewConverter.java 34690 2018-05-02 05:42:57Z crawl $
052 */
053public class WebViewConverter implements Converter {
054
055    /** Convert from a token to a (geo)json string. */
056    @Override
057    public Object fromToken(Token token) throws IllegalActionException {
058        
059        Type type = token.getType();
060        
061        if(type == VectorToken.VECTOR) {
062
063            SimpleFeatureCollection features = ((VectorToken)token).getVectors();
064
065            CoordinateReferenceSystem currentCRS = null;
066            SimpleFeatureType currentSchema = features.getSchema();
067            if(currentSchema != null) {
068                currentCRS = currentSchema.getCoordinateReferenceSystem();
069                if(currentCRS == null) {
070                    try {
071                        // FIXME
072                        System.out.println("WARNING: web view gis converter assuming EPSG:26911");
073                        currentCRS = CRS.decode("EPSG:26911");
074                    } catch (FactoryException e) {
075                        // TODO Auto-generated catch block
076                        e.printStackTrace();
077                    }
078                }
079                if(!GISUtilities.isCRSWGS84(currentCRS)) {
080                    features = 
081                        VectorUtilities.reproject(features, DefaultGeographicCRS.WGS84);
082                }
083            }
084            return VectorUtilities.toGeoJSONString(features);
085        }
086        
087        return null;
088    }
089
090    /** Converts from a geojson string to a token. */
091    @Override
092    public Token toToken(String data, Type type) throws IllegalActionException {
093
094        if(type == VectorToken.VECTOR) {            
095            try {
096                return VectorUtilities.readGeoJSONString(data, CRS.decode("EPSG:4326"));
097            } catch (IOException | JSONException e) {
098                throw new IllegalActionException("Error reading GeoJSON: " +
099                    e.getMessage());
100            } catch (FactoryException e) {
101                throw new IllegalActionException("Error decoding CRS EPSG:4326");
102            }
103        }
104        
105        return null;
106    }
107
108}