Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gml / FMAPGeometryFactory.java @ 10109

History | View | Annotate | Download (8.68 KB)

1
package com.iver.cit.gvsig.fmap.drivers.gml;
2

    
3
import java.awt.geom.Point2D;
4
import java.util.ArrayList;
5
import java.util.Date;
6
import java.util.Iterator;
7
import java.util.LinkedHashMap;
8
import java.util.Map;
9
import java.util.Set;
10

    
11
import org.apache.log4j.Logger;
12
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
13
import org.gvsig.remoteClient.gml.types.IXMLType;
14

    
15
import com.hardcode.gdbms.engine.values.ComplexValue;
16
import com.hardcode.gdbms.engine.values.Value;
17
import com.hardcode.gdbms.engine.values.ValueFactory;
18
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
19
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
20
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
21
import com.iver.cit.gvsig.fmap.core.IFeature;
22
import com.iver.cit.gvsig.fmap.core.IGeometry;
23
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
24
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
25
import com.vividsolutions.jts.geom.Geometry;
26
import com.vividsolutions.jts.geom.GeometryFactory;
27
import com.vividsolutions.jts.io.gml2.GMLReader;
28

    
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69
/* CVS MESSAGES:
70
 *
71
 * $Id: FMAPGeometryFactory.java 10109 2007-02-05 13:20:21Z jorpiell $
72
 * $Log$
73
 * Revision 1.5  2007-02-05 13:20:21  jorpiell
74
 * A?adidos nuevos m?todos en la factoria.
75
 *
76
 * Revision 1.4  2007/01/08 12:12:03  csanchez
77
 * Corregida visualizaci?n de Tablas simples
78
 *
79
 * Revision 1.3  2006/12/29 17:12:10  jorpiell
80
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
81
 *
82
 * Revision 1.2  2006/10/10 12:55:27  jorpiell
83
 * Se ha a?adido el soporte de features complejas
84
 *
85
 * Revision 1.1  2006/08/10 12:03:43  jorpiell
86
 * Se usa el nuevo driver de GML de remoteServices
87
 *
88
 *
89
 */
90
/**
91
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
92
 */
93
public class FMAPGeometryFactory implements IGeometriesFactory{
94
        private static Logger logger = Logger.getLogger(FMAPGeometryFactory.class);
95
        
96
        /*
97
         *  (non-Javadoc)
98
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(double, double)
99
         */
100
        public Object createPoint2D(double x, double y){
101
                return ShapeFactory.createPoint2D(x,y);
102
        }
103

    
104
        /*
105
         *  (non-Javadoc)
106
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(java.awt.geom.Point2D)
107
         */
108
        public Object createPoint2D(Point2D point2D) {
109
                return createPoint2D(point2D.getX(),point2D.getY());
110
        }
111
        
112
        /*
113
         *  (non-Javadoc)
114
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createMultipoint2D(double[], double[])
115
         */
116
        public Object createMultipoint2D(double[] x, double[] y){
117
                return ShapeFactory.createMultipoint2D(x,y);
118
        }
119

    
120
        /*
121
         *  (non-Javadoc)
122
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPolygon2D(double[], double[])
123
         */
124
        public Object createPolygon2D(double[] x, double[] y) {
125
                GeneralPathX path = new GeneralPathX();
126
                if ((x.length > 0) && (y.length > 0)){
127
                        path.moveTo(x[0],y[0]);
128
                        for (int i=0 ; i<x.length ; i++){
129
                                path.lineTo(x[i],y[i]);
130
                        }
131
                }                
132
                return ShapeFactory.createPolygon2D(path);
133
        }
134
        
135
        /*
136
         *  (non-Javadoc)
137
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createSimpleFeature(java.lang.String, org.gvsig.remoteClient.gml.types.IXMLType, java.util.Map, java.lang.Object)
138
         */
139
        public Object createSimpleFeature(String element, IXMLType type,Map values,Object geometry) {
140
                Object[] fmapParams = values.keySet().toArray();
141
                Value[] fmapValues = getValues(values);
142
                IFeature feature = new DefaultFeature((IGeometry)geometry,fmapValues);
143
                return new FeatureWithAttributes(feature,fmapParams,fmapValues);
144
        }
145

    
146
        public Object createGeometry(String gmlGeometry) {
147
                GMLReader reader = new GMLReader();
148
                try {
149
                        Geometry geom = reader.read(gmlGeometry,new GeometryFactory());
150
                        return ShapeFactory.createGeometry(FConverter.jts_to_java2d(geom));
151
                } catch (Exception e){
152
                        logger.error("Can't parse: " + gmlGeometry,e);
153
                }
154
                return null;
155
        }
156
        
157
        private Value[] getValues(Map values){
158
                int i = 0;        
159
                Set keys = values.keySet();
160
                Iterator it = keys.iterator();
161
                Value[] returnValues = new Value[keys.size()];
162
                while(it.hasNext()){
163
                        String key = (String)it.next();
164
                        Object obj = values.get(key);
165
                        /**************        
166
                            * <MULTIPLE> * 
167
                            **************/        
168
                        if (obj instanceof ArrayList){
169
                                returnValues[i] = getMultipleValue(key,(ArrayList)obj);
170
                        }
171
                        /*************        
172
                            * <COMPLEX> *  
173
                            *************/        
174
                        else if(obj instanceof LinkedHashMap){
175
                                returnValues[i]  = getComplexValue(key,(LinkedHashMap)obj);
176
                        }
177
                        /************        
178
                            * <SIMPLE> *  
179
                            ************/
180
                        else{
181
                                returnValues[i] = getValue(obj);
182
                        }        
183
                        i++;
184
                }
185
                return returnValues;
186
        }
187
        
188
        private Value getComplexValue(String name,Map hash){
189
                ComplexValue value = ValueFactory.createComplexValue(name);
190
                Set keys = hash.keySet();
191
                Iterator it = keys.iterator();
192
                while (it.hasNext()){
193
                        String key = (String)it.next();
194
                        Object obj = hash.get(key);                
195
                        if (obj instanceof ArrayList){
196
                                value.put(key,getMultipleValue(key,(ArrayList)obj));
197
                        }else if(obj instanceof LinkedHashMap){
198
                                value.put(key,getComplexValue(key,(LinkedHashMap)obj));
199
                        }else{
200
                                value.put(key,getValue(obj));
201
                        }
202
                }
203
                return value;
204
        }
205
        
206
        private Value getMultipleValue(String name,ArrayList al){
207
                if (al.size()==0){
208
                        return null;
209
                }
210
                else if ((al.size()==1)&&(!((al.get(0) instanceof ArrayList)||(al.get(0) instanceof LinkedHashMap)))){
211
                        return getValue(al.get(0));
212
                }
213
                else{
214
                        ComplexValue value = ValueFactory.createComplexValue(name);
215
                        for (int i=0 ; i<al.size() ; i++){
216
                                String attName = name + "_" + i;
217
                                Object obj = al.get(i);
218
                                /**************        
219
                                    * <MULTIPLE> * 
220
                                    **************/        
221
                                if (obj instanceof ArrayList){
222
                                        value.put(attName,getMultipleValue(attName,(ArrayList)obj));
223
                                }
224
                                /*************        
225
                                    * <COMPLEX> *  
226
                                    *************/        
227
                                else if(obj instanceof LinkedHashMap){
228
                                        value.put(attName,getComplexValue(attName,(LinkedHashMap)obj));
229
                                }
230
                                /************        
231
                                    * <SIMPLE> *  
232
                                    ************/        
233
                                else{
234
                                        value.put(attName,getValue(obj));
235
                                }
236
                        }
237
                        return value;
238
                }
239
        }
240
        
241
        /**
242
         * Gets the attributes
243
         * @param attr
244
         * @return
245
         */
246
        private Value getValue(Object attr) {        
247
                if (attr instanceof Double){
248
                        return ValueFactory.createValue(((Double)attr).doubleValue());
249
                }else if (attr instanceof String){
250
                        return ValueFactory.createValue(String.valueOf(attr));
251
                }else if (attr instanceof Long){
252
                        return ValueFactory.createValue(((Long)attr).longValue());
253
                }else if (attr instanceof Integer){
254
                        return ValueFactory.createValue(((Integer)attr).intValue());
255
                }else if (attr instanceof Float){
256
                        return ValueFactory.createValue(((Float)attr).floatValue());
257
                }else if (attr instanceof Short){
258
                        return ValueFactory.createValue(((Short)attr).shortValue());
259
                }else if (attr instanceof Boolean){
260
                        return ValueFactory.createValue(((Boolean)attr).booleanValue());
261
                }else if (attr instanceof Date){
262
                        return ValueFactory.createValue(((Date)attr));
263
                }
264
                return ValueFactory.createValue(((String)""));
265
        }
266
        
267
        public class FeatureWithAttributes{
268
                private IFeature feature;
269
                private Object[] attributeName;
270
                private Value[] attributeValue;
271
                
272
                public FeatureWithAttributes(IFeature feature, Object[] attributeName, Value[] attributeValue) {
273
                        super();                
274
                        this.feature = feature;
275
                        this.attributeName = attributeName;
276
                        this.attributeValue = attributeValue;
277
                }
278

    
279
                /**
280
                 * @return Returns the attributeName.
281
                 */
282
                public Object[] getAttributeName() {
283
                        return attributeName;
284
                }
285

    
286
                /**
287
                 * @return Returns the feature.
288
                 */
289
                public IFeature getFeature() {
290
                        return feature;
291
                }
292

    
293
                /**
294
                 * @return Returns the attributeValue.
295
                 */
296
                public Value[] getAttributeValue() {
297
                        return attributeValue;
298
                }
299
                
300
        }
301
}