001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/crs/configuration/gml/GMLFileResource.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.crs.configuration.gml;
038    
039    import java.io.IOException;
040    import java.util.ArrayList;
041    import java.util.Arrays;
042    import java.util.HashMap;
043    import java.util.List;
044    import java.util.Map;
045    import java.util.Properties;
046    
047    import org.deegree.crs.configuration.resources.XMLFileResource;
048    import org.deegree.crs.coordinatesystems.CompoundCRS;
049    import org.deegree.crs.coordinatesystems.CoordinateSystem;
050    import org.deegree.crs.coordinatesystems.GeographicCRS;
051    import org.deegree.crs.coordinatesystems.ProjectedCRS;
052    import org.deegree.crs.transformations.Transformation;
053    import org.deegree.crs.transformations.coordinate.CRSTransformation;
054    import org.deegree.crs.transformations.coordinate.ConcatenatedTransform;
055    import org.deegree.crs.transformations.helmert.Helmert;
056    import org.deegree.framework.log.ILogger;
057    import org.deegree.framework.log.LoggerFactory;
058    import org.deegree.framework.xml.XMLParsingException;
059    import org.deegree.framework.xml.XMLTools;
060    import org.deegree.ogcbase.CommonNamespaces;
061    import org.w3c.dom.Element;
062    
063    /**
064     * The <code>GMLFileResource</code> provides easy access to a gml3.2. dictionary file, which can be used together with
065     * the {@link GMLCRSProvider}.
066     *
067     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
068     *
069     * @author last edited by: $Author: mschneider $
070     *
071     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
072     *
073     */
074    public class GMLFileResource extends XMLFileResource {
075    
076        /**
077         *
078         */
079        private static final long serialVersionUID = -4389365894942107300L;
080    
081        private static ILogger LOG = LoggerFactory.getLogger( GMLFileResource.class );
082    
083        private static final String PRE = CommonNamespaces.GML3_2_PREFIX + ":";
084    
085        private static final String ID_XPATH = "//" + PRE + "dictionaryEntry/*[" + PRE + "identifier='";
086    
087        private static final String OR_CONTAINS_NAMES = " or " + PRE + "name='";
088    
089        private static final String TRANSFORM_XPATH = "/" + PRE + "Dictionary/" + PRE + "dictionaryEntry/" + PRE
090                                                      + "Transformation";
091    
092        private List<Element> transformations;
093    
094        private Map<CoordinateSystem, Helmert> cachedWGS84Transformations;
095    
096        /**
097         * @param provider
098         * @param properties
099         */
100        public GMLFileResource( GMLCRSProvider provider, Properties properties ) {
101            super( provider, properties, "Dictionary", CommonNamespaces.GML3_2_NS.toASCIIString() );
102            try {
103                transformations = XMLTools.getElements( getRootElement(), TRANSFORM_XPATH, nsContext );
104    
105            } catch ( XMLParsingException e ) {
106                LOG.logError( e.getLocalizedMessage(), e );
107            }
108            cachedWGS84Transformations = new HashMap<CoordinateSystem, Helmert>();
109        }
110    
111        public Helmert getWGS84Transformation( GeographicCRS sourceCRS ) {
112            if ( sourceCRS == null ) {
113                return null;
114            }
115            if ( LOG.isDebug() ) {
116                LOG.logDebug( "Searching for wgs84 transformation for given sourceCRS: "
117                              + Arrays.toString( sourceCRS.getIdentifiers() ) );
118            }
119            Helmert result = cachedWGS84Transformations.get( sourceCRS );
120            if ( result == null ) {
121                Transformation parsedTransformation = getTransformation( sourceCRS, null );
122                if ( parsedTransformation instanceof Helmert ) {
123                    LOG.logDebug( "Found an helmert transformation for sourceCRS: "
124                                  + Arrays.toString( sourceCRS.getIdentifiers() ) );
125                    result = (Helmert) parsedTransformation;
126                } else {
127                    if ( parsedTransformation instanceof CRSTransformation ) {
128                        CoordinateSystem target = ( (CRSTransformation) parsedTransformation ).getTargetCRS();
129                        GeographicCRS t = null;
130                        if ( LOG.isDebug() ) {
131                            LOG.logDebug( "Found crstransformation for sourceCRS: "
132                                          + Arrays.toString( sourceCRS.getIdentifiers() ) + " and targetCRS: "
133                                          + Arrays.toString( target.getIdentifiers() )
134                                          + " will now use the targetCRS to find a Helmert transformation." );
135                        }
136                        if ( target.getType() == CoordinateSystem.COMPOUND_CRS ) {
137                            if ( ( (CompoundCRS) target ).getUnderlyingCRS().getType() == CoordinateSystem.PROJECTED_CRS ) {
138                                t = ( (ProjectedCRS) ( (CompoundCRS) target ).getUnderlyingCRS() ).getGeographicCRS();
139                            } else if ( ( (CompoundCRS) target ).getUnderlyingCRS().getType() == CoordinateSystem.GEOGRAPHIC_CRS ) {
140                                t = (GeographicCRS) target;
141                            } else {
142                                LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains." );
143                            }
144                        } else if ( target.getType() == CoordinateSystem.PROJECTED_CRS ) {
145                            t = ( (ProjectedCRS) target ).getGeographicCRS();
146                        } else if ( target.getType() == CoordinateSystem.GEOGRAPHIC_CRS ) {
147                            t = (GeographicCRS) target;
148                        } else {
149                            LOG.logWarning( "Wgs84 Transformation lookup is currently only supported for GeographicCRS-chains." );
150                        }
151                        if ( t != null ) {
152                            if ( LOG.isDebug() ) {
153                                LOG.logDebug( "Trying to resolve target to find a wgs84transformation for the 'targetCRS': "
154                                              + Arrays.toString( t.getIdentifiers() ) );
155                            }
156                            result = getWGS84Transformation( t );
157                        }
158                    } else {
159                        LOG.logWarning( "The transformation is not an instance of CRSTransformation nor a Helmert, ignoring it." );
160                    }
161                }
162            }
163    
164            if ( result != null ) {
165                if ( LOG.isDebug() ) {
166                    LOG.logDebug( "For the given crs: " + sourceCRS.getIdentifier()
167                                  + " following helmert transformation was found:\n" + result );
168    
169                }
170    
171                cachedWGS84Transformations.put( sourceCRS, result );
172            } else {
173                LOG.logInfo( "No helmert transformation found for the given crs: " + sourceCRS.getIdentifier() );
174            }
175            return result;
176        }
177    
178        public Transformation getTransformation( CoordinateSystem sourceCRS, CoordinateSystem targetCRS ) {
179            if ( sourceCRS == null ) {
180                return null;
181            }
182            List<Element> toBeRemoved = new ArrayList<Element>( transformations.size() );
183            List<String> sourceIDs = Arrays.asList( sourceCRS.getIdentifiers() );
184            List<String> targetIDs = null;
185            if ( targetCRS != null ) {
186                targetIDs = Arrays.asList( targetCRS.getIdentifiers() );
187            } else {
188                targetIDs = new ArrayList<String>();
189            }
190            Transformation result = null;
191            for ( int i = 0; i < transformations.size() && result == null; ++i ) {
192                Element transElem = transformations.get( i );
193                if ( transElem != null ) {
194                    try {
195                        Element sourceCRSProp = XMLTools.getRequiredElement( transElem, PRE + "sourceCRS", nsContext );
196                        String transformSourceID = null;
197                        String transformTargetID = null;
198                        if ( sourceCRSProp != null ) {
199                            transformSourceID = sourceCRSProp.getAttributeNS( CommonNamespaces.XLNNS.toASCIIString(),
200                                                                              "href" );
201                            if ( "".equals( transformSourceID ) ) {
202                                transformSourceID = XMLTools.getRequiredNodeAsString( sourceCRSProp, "*[1]/" + PRE
203                                                                                                     + "identifier",
204                                                                                      nsContext );
205                            }
206                        }
207                        if ( targetCRS != null ) {
208                            Element targetCRSProp = XMLTools.getRequiredElement( transElem, PRE + "targetCRS", nsContext );
209                            if ( targetCRSProp != null ) {
210    
211                                transformTargetID = targetCRSProp.getAttributeNS( CommonNamespaces.XLNNS.toASCIIString(),
212                                                                                  "href" );
213                                if ( "".equals( transformTargetID ) ) {
214                                    transformTargetID = XMLTools.getRequiredNodeAsString( targetCRSProp, "*[1]/" + PRE
215                                                                                                         + "identifier",
216                                                                                          nsContext );
217                                }
218                            }
219                        }
220    
221                        if ( sourceIDs.contains( transformSourceID ) ) {
222                            result = getProvider().parseTransformation( transElem );
223                            if ( targetCRS == null ) {
224                                // Trying to find a helmert transformation
225                                LOG.logDebug( "Resolving a possible transformation." );
226                                if ( result != null && !( result instanceof Helmert ) ) {
227                                    result = getTransformation( result.getTargetCRS(), null );
228                                }
229                            } else {
230                                if ( !targetIDs.contains( transformTargetID ) ) {
231                                    LOG.logDebug( "Found a transformation with gml:id: "
232                                                  + transElem.getAttributeNS( CommonNamespaces.GML3_2_NS.toASCIIString(),
233                                                                              "id" )
234                                                  + ", but the target does not match the source crs, trying to build transformation chain." );
235                                    Transformation second = getTransformation( result.getTargetCRS(), targetCRS );
236                                    if ( second != null ) {
237                                        result = new ConcatenatedTransform( result, second );
238                                    } else {
239                                        LOG.logDebug( "The transformation with gml:id: "
240                                                      + transElem.getAttributeNS(
241                                                                                  CommonNamespaces.GML3_2_NS.toASCIIString(),
242                                                                                  "id" )
243                                                      + " is not the start of transformation chain, discarding it. " );
244                                        result = null;
245                                    }
246                                }
247                            }
248                        }
249    
250                    } catch ( XMLParsingException e ) {
251                        toBeRemoved.add( transElem );
252                        LOG.logWarning( "No source CRS id could be found in this transformation(gml:id): "
253                                        + transElem.getAttributeNS( CommonNamespaces.GML3_2_NS.toASCIIString(), "id" )
254                                        + " this is not correct, removing transformation from cache." );
255                        LOG.logWarning( e.getMessage() );
256                    }
257                }
258                if ( toBeRemoved.size() > 0 ) {
259                    transformations.removeAll( toBeRemoved );
260                }
261            }
262            return result;
263        }
264    
265        public Element getURIAsType( String uri )
266                                throws IOException {
267            Element result = null;
268            try {
269                result = XMLTools.getElement( getRootElement(), ID_XPATH + uri + "'" + OR_CONTAINS_NAMES + uri + "']",
270                                              nsContext );
271            } catch ( XMLParsingException e ) {
272                LOG.logError( e );
273            }
274            return result;
275        }
276    }