Project

General

Profile

1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5
package eu.dnetlib.espas.util;
6

    
7
import static eu.dnetlib.espas.util.MetadataHandler.serializeNode;
8
import java.io.IOException;
9
import java.util.Collection;
10
import javax.xml.bind.JAXBException;
11
import javax.xml.bind.Unmarshaller;
12
import org.apache.log4j.Logger;
13
import org.apache.log4j.Priority;
14
import org.geotoolkit.gml.xml.v321.VerticalCRSType;
15
import org.geotoolkit.metadata.iso.extent.AbstractGeographicExtent;
16
import org.geotoolkit.metadata.iso.extent.DefaultExtent;
17
import org.geotoolkit.metadata.iso.extent.DefaultGeographicBoundingBox;
18
import org.geotoolkit.metadata.iso.extent.DefaultVerticalExtent;
19
import org.geotoolkit.xml.MarshallerPool;
20
import org.opengis.metadata.extent.BoundingPolygon;
21
import org.opengis.metadata.extent.Extent;
22
import org.opengis.metadata.extent.GeographicBoundingBox;
23
import org.opengis.metadata.extent.VerticalExtent;
24
import org.w3c.dom.Element;
25
import org.w3c.dom.Node;
26
import org.w3c.dom.NodeList;
27

    
28

    
29
/**
30
 *  The <code>GeometryMetadataHandler</code> class provides static methods for managing the manipulation of OGC metadata elements that are used 
31
 * in the description of ESPAS information. The provided methods are manage the stripping of geometry related information out of the OGC surrounding 
32
 * elements. In most of the provided implementations important is the role of the associated CRS system specified in the given elements as this defines 
33
 * the transformation of the provided input to the one expected by the ESPAS db.
34
 * 
35
 * @author georgeathanasopoulos
36
 */
37

    
38
public class GeometryMetadataHandler extends MetadataHandler {
39

    
40
    private static Logger _logger = Logger.getLogger(GeometryMetadataHandler.class);
41
    
42
    private static GMLGeometryTransformationHandler gmlHandler;
43

    
44
  //       static initializer used for setting the context of the required JAXB classes used through out the list of provided operations
45
    static {
46
        gmlHandler = new GMLGeometryTransformationHandler();
47
        
48
//      marshaller pool used for handling input metadata 
49
        try {
50

    
51
            metadataMarshPool = new MarshallerPool(MarshallerPool.defaultClassesToBeBound());
52
        } catch (JAXBException ex) {
53
            _logger.log(Priority.ERROR, null, ex);
54
        }
55
    }
56

    
57
    public static boolean hasGeoExtent(Node extentContent) throws IOException, JAXBException {
58
        _logger.log(Priority.INFO, "hasGeoExtent");
59

    
60

    
61
        String strCont = serializeNode(extentContent);
62
        _logger.log(Priority.INFO, "Content is: \n" + strCont);
63

    
64
        Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
65
        Object extent = unMarshaller.unmarshal(extentContent);
66
        metadataMarshPool.release(unMarshaller);
67

    
68
        if (extent != null && (extent instanceof AbstractGeographicExtent))
69
            return true;
70

    
71
        return false;
72
    }
73

    
74
    public static boolean isGeoExtent(Node extentContent) throws IOException, JAXBException {
75
        _logger.log(Priority.INFO, "hasGeoExtent");
76

    
77

    
78
            String strCont = serializeNode(extentContent);
79
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
80

    
81
            Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
82
            Object extent = unMarshaller.unmarshal(extentContent);
83
            metadataMarshPool.release(unMarshaller);
84
            
85
            if(extent==null)
86
                return false;
87

    
88
            if(extent instanceof BoundingPolygon)
89
                return true;
90
            else if(extent instanceof GeographicBoundingBox)
91
                return true;
92

    
93

    
94
        return false;
95
    }
96
    
97
/** Calls the GMLTransformationHander for the extraction of the correct GML instance representation.
98
 */    
99
    public static String getGeoLocation(Node geomLocationChild) throws JAXBException{
100
        _logger.log(Priority.INFO, "Processing espas:GeometryLocation node \n");
101
            initCRSAuthorityFactory();
102

    
103
            String result = gmlHandler.getGeometryRepresentation(geomLocationChild);
104
            _logger.log(Priority.INFO, "GML geography postgis statement is: \n"+result);
105
            return result;
106
            
107
    }
108
    
109

    
110
    public static String getGeoSrsName(Node geometryLocation) {
111
        _logger.log(Priority.INFO, "Processing espas:GeometryLocation node \n");
112
        try {
113
            String result = gmlHandler.getGeometrySrsName(geometryLocation);
114
            return result;
115
            
116
        } catch (JAXBException ex) {
117
            _logger.log(Priority.ERROR, null, ex);
118
            return "";
119
        }
120
    }
121

    
122

    
123
    
124
    public static String getGeoExtent(Node geographicExtentNode) throws IOException, JAXBException {
125
        _logger.log(Priority.INFO, "getGeoExtent");
126
        initCRSAuthorityFactory();
127

    
128
        String output = "";
129
    
130
        if(geographicExtentNode==null)
131
            return output;
132
        
133
        String strCont = serializeNode(geographicExtentNode);
134
        _logger.log(Priority.INFO, "Content is: \n" + strCont);
135

    
136
        Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
137
        Object defGeoExt = unMarshaller.unmarshal(geographicExtentNode);
138
        metadataMarshPool.release(unMarshaller);
139

    
140
//            handle internal gml representations
141
        if (defGeoExt != null && defGeoExt instanceof DefaultGeographicBoundingBox)
142
            output = ((DefaultGeographicBoundingBox) defGeoExt).toString();
143
        else if (defGeoExt != null && defGeoExt instanceof BoundingPolygon) {
144
            NodeList polygons = ((Element) geographicExtentNode).getElementsByTagNameNS(GMD_NAMESPACE, "polygon");
145

    
146
//                if(polygons.getLength()>1){
147
                Node[] gmlElements = new Node[polygons.getLength()];
148
                for (int i = 0; i < polygons.getLength(); i++) {
149
                    gmlElements[i]=((Element) polygons.item(i)).getElementsByTagNameNS(GML32_NAMESPACE, "*").item(0);
150
                    strCont = serializeNode(gmlElements[i]);
151
                    _logger.debug("Polygon Content is: \n" + strCont);
152
                    }
153
                output = gmlHandler.getBoundingPolygonRepresentation(gmlElements);
154
//                    }
155
//                @todo: this should be changed so as to return polygons even in cases where single points are provided. 
156
//                else
157
//                    for (int i = 0; i < polygons.getLength(); i++) {
158
//                    strCont = serializeNode(((Element) polygons.item(i)).getFirstChild());
159
//                    _logger.log(Priority.INFO, "Polygon Content is: \n" + strCont);
160
//                    output = gmlHandler.getGeometryRepresentation(((Element) polygons.item(i)).getElementsByTagNameNS(GML32_NAMESPACE, "*").item(0));
161
//                }
162

    
163
        } else
164
            throw new IOException("GeoExtent extent cannot be initialized!");
165

    
166
        return output;
167

    
168
    }
169

    
170
    /**
171
     * Asserts whether the provided extent node has vertical extensions.
172
     */
173
    public static boolean hasVerticalExtent(Node extentContent) {
174
        _logger.log(Priority.INFO, "hasVerticalExtent");
175
        if (extentContent == null)
176
            return false;
177
        try {
178

    
179
            String strCont = serializeNode(extentContent);
180
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
181

    
182
            Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
183
            Object extent = unMarshaller.unmarshal(extentContent);
184
            metadataMarshPool.release(unMarshaller);
185

    
186
            Collection<VerticalExtent> vertElements = DefaultExtent.castOrCopy((Extent)extent).getVerticalElements();
187
            if (vertElements != null && !vertElements.isEmpty())
188
                return true;
189

    
190
        } catch (JAXBException ex) {
191
            _logger.log(Priority.ERROR, null, ex);
192
        } catch (IOException ex) {
193
            _logger.log(Priority.ERROR, null, ex);
194
        }
195

    
196
        return false;
197
    }
198
    
199
    
200
    public static String getVerticalExtentMin(Node verticalExtent) throws IOException, JAXBException {
201
        return getVerticalExtentMin(verticalExtent, null);
202
    }
203

    
204
    public static String getVerticalExtentMin(Node verticalExtent, String vSrsName) throws IOException, JAXBException {
205
        _logger.log(Priority.INFO, "getVerticalExtentMin");
206
        String output = "";
207

    
208
        if(verticalExtent==null)
209
            return output;
210

    
211
            String strCont = serializeNode(verticalExtent);
212
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
213

    
214
            Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
215
            DefaultVerticalExtent defTempExt = (DefaultVerticalExtent) unMarshaller.unmarshal(verticalExtent);
216
            metadataMarshPool.release(unMarshaller);
217

    
218
            if (defTempExt != null){
219
                if(defTempExt.getMinimumValue()==null)
220
                    output= ((Element)((Element)verticalExtent).getElementsByTagNameNS(GMD_NAMESPACE,"minimumValue").item(0)).getElementsByTagNameNS(GCO_NAMESPACE,"Real").item(0).getFirstChild().getTextContent();
221
                else
222
                    output= defTempExt.getMinimumValue().toString();
223
            }
224
            else
225
                throw new IOException("GeoExtent extent cannot be initialized!");
226

    
227
            return output;
228

    
229
    }
230
    
231
    
232
    public static String getVerticalExtentMax(Node verticalExtent) throws IOException, JAXBException {
233
        return getVerticalExtentMax(verticalExtent,null);
234
    }
235
    
236
    
237
    public static String getVerticalExtentMax(Node verticalExtent, String vSrsName) throws IOException, JAXBException {
238
        _logger.log(Priority.INFO, "getVerticalExtentMax");
239
        String output = "";
240

    
241
        if(verticalExtent==null)
242
            return output;
243
            
244
            String strCont = serializeNode(verticalExtent);
245
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
246

    
247
            Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
248
            DefaultVerticalExtent defTempExt = (DefaultVerticalExtent) unMarshaller.unmarshal(verticalExtent);
249
            metadataMarshPool.release(unMarshaller);
250

    
251
            if (defTempExt != null){
252
                if(defTempExt.getMaximumValue()==null){
253
                    NodeList list = ((Element)verticalExtent).getElementsByTagNameNS(GMD_NAMESPACE,"maximumValue");
254
                    list= ((Element)list.item(0)).getElementsByTagNameNS(GCO_NAMESPACE,"Real");
255
                    output= ((Element)list.item(0)).getFirstChild().getTextContent();
256
                }
257
                else
258
                    output= defTempExt.getMaximumValue().toString();
259
                }
260
            else
261
                throw new IOException("GeoExtent extent cannot be initialized!");
262

    
263
            return output;
264

    
265
    }
266
    
267
    /** Retrieves the vertical CRS assigned to a gmd:verticalExtent element. The vertical crs can be provided either as xlink:href reference to the verticalCRS element or 
268
     * as an embedded gml:VerticalCRS description.
269
     */
270
    public static String getVerticalSRSName(Node verticalExtent){
271
        _logger.log(Priority.INFO, "getVerticalSRSName");
272
        String output = "";
273
        try {
274
            
275
            String strCont = serializeNode(verticalExtent);
276
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
277

    
278
            
279
            Unmarshaller unMarshaller = metadataMarshPool.acquireUnmarshaller();
280
            DefaultVerticalExtent defTempExt = (DefaultVerticalExtent) unMarshaller.unmarshal(verticalExtent);
281

    
282
            metadataMarshPool.release(unMarshaller);
283
            if (defTempExt != null && defTempExt.getVerticalCRS()!=null){
284
                    NodeList list = ((Element)verticalExtent).getElementsByTagNameNS(GMD_NAMESPACE,"verticalCRS");
285
                    output = ((Element)list.item(0)).getAttributeNS(XLINK1999_NAMESPACE,"href");
286
            }
287
//            in the case where there is no href attribute pointing to the VCRS system then this should be embeded in the description
288
            else{
289
                NodeList list = ((Element)verticalExtent).getElementsByTagNameNS(GML32_NAMESPACE,"VerticalCRS");
290
//                if no gml:VerticalCRS element is embeded then this is an invalid document
291
                if(list.getLength()==0)
292
                    throw new IOException("GeoExtent extent cannot be initialized!");
293

    
294
                unMarshaller = metadataMarshPool.acquireUnmarshaller();
295
                VerticalCRSType vCRSType = (VerticalCRSType) unMarshaller.unmarshal(list.item(0));
296
                output  = vCRSType.getIdentifier();
297
            }
298
             
299

    
300
        } catch (JAXBException ex) {
301
            _logger.log(Priority.ERROR, null, ex);
302
        } catch (IOException ex) {
303
            _logger.log(Priority.ERROR, null, ex);
304
        }
305
        finally {
306
            return output;
307
        }
308
    }
309

    
310
    /**
311
     * Returns the specified geometry location in terms of position. This transformation depends on the use of GeneralDirectPosition class of the
312
     * GeoToolkit.
313
     */
314
    
315
    @Deprecated
316
    public static String getGeometryLocation(Node geometryLocation) {
317
        _logger.log(Priority.INFO, "getGeometryLocation");
318
        String output = "";
319
        try {
320

    
321
            String strCont = serializeNode(geometryLocation);
322
            _logger.log(Priority.INFO, "Content is: \n" + strCont);
323

    
324
            output= gmlHandler.getGeometryRepresentation(geometryLocation);
325
            }
326
        
327
        finally {
328
            return output;
329
        }
330

    
331

    
332
    }
333
}
(4-4/9)