Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_894 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gml / FMAPGeometryFactory.java @ 10309

History | View | Annotate | Download (7.69 KB)

1 6723 jorpiell
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 9916 jorpiell
import java.util.Iterator;
7
import java.util.LinkedHashMap;
8
import java.util.Map;
9
import java.util.Set;
10 6723 jorpiell
11
import org.apache.log4j.Logger;
12
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
13 9916 jorpiell
import org.gvsig.remoteClient.gml.types.IXMLType;
14 6723 jorpiell
15 8765 jjdelcerro
import com.hardcode.gdbms.engine.values.ComplexValue;
16 6723 jorpiell
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.IFeature;
20
import com.iver.cit.gvsig.fmap.core.IGeometry;
21
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
22
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
23
import com.vividsolutions.jts.geom.Geometry;
24
import com.vividsolutions.jts.geom.GeometryFactory;
25
import com.vividsolutions.jts.io.gml2.GMLReader;
26
27
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
28
 *
29
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
44
 *
45
 * For more information, contact:
46
 *
47
 *  Generalitat Valenciana
48
 *   Conselleria d'Infraestructures i Transport
49
 *   Av. Blasco Ib??ez, 50
50
 *   46010 VALENCIA
51
 *   SPAIN
52
 *
53
 *      +34 963862235
54
 *   gvsig@gva.es
55
 *      www.gvsig.gva.es
56
 *
57
 *    or
58
 *
59
 *   IVER T.I. S.A
60
 *   Salamanca 50
61
 *   46005 Valencia
62
 *   Spain
63
 *
64
 *   +34 963163400
65
 *   dac@iver.es
66
 */
67
/* CVS MESSAGES:
68
 *
69
 * $Id$
70
 * $Log$
71 9916 jorpiell
 * Revision 1.1.4.2  2007-01-25 16:11:35  jorpiell
72
 * Se han cambiado los imports que hac?an referencia a remoteServices. Esto es as?, porque se han renombrado las clases del driver de GML
73 8765 jjdelcerro
 *
74 9916 jorpiell
 * Revision 1.4  2007/01/08 12:12:03  csanchez
75
 * Corregida visualizaci?n de Tablas simples
76
 *
77
 * Revision 1.3  2006/12/29 17:12:10  jorpiell
78
 * Se tienen en cuenta los simpleTypes y los choices, adem?s de los atributos multiples
79
 *
80 8765 jjdelcerro
 * Revision 1.2  2006/10/10 12:55:27  jorpiell
81
 * Se ha a?adido el soporte de features complejas
82
 *
83
 * Revision 1.1  2006/08/10 12:03:43  jorpiell
84 6723 jorpiell
 * Se usa el nuevo driver de GML de remoteServices
85
 *
86
 *
87
 */
88
/**
89
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
90
 */
91
public class FMAPGeometryFactory implements IGeometriesFactory{
92
        private static Logger logger = Logger.getLogger(FMAPGeometryFactory.class);
93
94
        public Object createPoint2D(double x, double y){
95
                return ShapeFactory.createPoint2D(x,y);
96
        }
97
98
        public Object createPoint2D(Point2D point2D) {
99
                return createPoint2D(point2D.getX(),point2D.getY());
100
        }
101
102
        public Object createMultipoint2D(double[] x, double[] y){
103
                return ShapeFactory.createMultipoint2D(x,y);
104
        }
105
106 9916 jorpiell
107
        public Object createSimpleFeature(String element, IXMLType type,Map values,Object geometry) {
108
                Object[] fmapParams = values.keySet().toArray();
109
                Value[] fmapValues = getValues(values);
110 8765 jjdelcerro
                IFeature feature = new DefaultFeature((IGeometry)geometry,fmapValues);
111
                return new FeatureWithAttributes(feature,fmapParams,fmapValues);
112 6723 jorpiell
        }
113
114
        public Object createGeometry(String gmlGeometry) {
115
                GMLReader reader = new GMLReader();
116
                try {
117
                        Geometry geom = reader.read(gmlGeometry,new GeometryFactory());
118
                        return ShapeFactory.createGeometry(FConverter.jts_to_java2d(geom));
119
                } catch (Exception e){
120
                        logger.error("Can't parse: " + gmlGeometry,e);
121
                }
122
                return null;
123
        }
124
125 9916 jorpiell
        private Value[] getValues(Map values){
126
                int i = 0;
127
                Set keys = values.keySet();
128
                Iterator it = keys.iterator();
129
                Value[] returnValues = new Value[keys.size()];
130
                while(it.hasNext()){
131
                        String key = (String)it.next();
132
                        Object obj = values.get(key);
133
                        /**************
134
                            * <MULTIPLE> *
135
                            **************/
136
                        if (obj instanceof ArrayList){
137
                                returnValues[i] = getMultipleValue(key,(ArrayList)obj);
138 8765 jjdelcerro
                        }
139 9916 jorpiell
                        /*************
140
                            * <COMPLEX> *
141
                            *************/
142
                        else if(obj instanceof LinkedHashMap){
143
                                returnValues[i]  = getComplexValue(key,(LinkedHashMap)obj);
144
                        }
145
                        /************
146
                            * <SIMPLE> *
147
                            ************/
148
                        else{
149
                                returnValues[i] = getValue(obj);
150
                        }
151
                        i++;
152 8765 jjdelcerro
                }
153 9916 jorpiell
                return returnValues;
154 8765 jjdelcerro
        }
155
156 9916 jorpiell
        private Value getComplexValue(String name,Map hash){
157
                ComplexValue value = ValueFactory.createComplexValue(name);
158
                Set keys = hash.keySet();
159
                Iterator it = keys.iterator();
160
                while (it.hasNext()){
161
                        String key = (String)it.next();
162
                        Object obj = hash.get(key);
163
                        if (obj instanceof ArrayList){
164
                                value.put(key,getMultipleValue(key,(ArrayList)obj));
165
                        }else if(obj instanceof LinkedHashMap){
166
                                value.put(key,getComplexValue(key,(LinkedHashMap)obj));
167 8765 jjdelcerro
                        }else{
168 9916 jorpiell
                                value.put(key,getValue(obj));
169
                        }
170 8765 jjdelcerro
                }
171 9916 jorpiell
                return value;
172
        }
173
174
        private Value getMultipleValue(String name,ArrayList al){
175
                if (al.size()==0){
176
                        return null;
177 8765 jjdelcerro
                }
178 9916 jorpiell
                else if ((al.size()==1)&&(!((al.get(0) instanceof ArrayList)||(al.get(0) instanceof LinkedHashMap)))){
179
                        return getValue(al.get(0));
180
                }
181
                else{
182
                        ComplexValue value = ValueFactory.createComplexValue(name);
183
                        for (int i=0 ; i<al.size() ; i++){
184
                                String attName = name + "_" + i;
185
                                Object obj = al.get(i);
186
                                /**************
187
                                    * <MULTIPLE> *
188
                                    **************/
189
                                if (obj instanceof ArrayList){
190
                                        value.put(attName,getMultipleValue(attName,(ArrayList)obj));
191
                                }
192
                                /*************
193
                                    * <COMPLEX> *
194
                                    *************/
195
                                else if(obj instanceof LinkedHashMap){
196
                                        value.put(attName,getComplexValue(attName,(LinkedHashMap)obj));
197
                                }
198
                                /************
199
                                    * <SIMPLE> *
200
                                    ************/
201
                                else{
202
                                        value.put(attName,getValue(obj));
203
                                }
204
                        }
205
                        return value;
206
                }
207 8765 jjdelcerro
        }
208
209 6723 jorpiell
        /**
210
         * Gets the attributes
211
         * @param attr
212
         * @return
213
         */
214 8765 jjdelcerro
        private Value getValue(Object attr) {
215
                if (attr instanceof Double){
216
                        return ValueFactory.createValue(((Double)attr).doubleValue());
217
                }else if (attr instanceof String){
218
                        return ValueFactory.createValue(String.valueOf(attr));
219
                }else if (attr instanceof Long){
220
                        return ValueFactory.createValue(((Long)attr).longValue());
221
                }else if (attr instanceof Integer){
222
                        return ValueFactory.createValue(((Integer)attr).intValue());
223
                }else if (attr instanceof Float){
224
                        return ValueFactory.createValue(((Float)attr).floatValue());
225
                }else if (attr instanceof Short){
226
                        return ValueFactory.createValue(((Short)attr).shortValue());
227
                }else if (attr instanceof Boolean){
228
                        return ValueFactory.createValue(((Boolean)attr).booleanValue());
229
                }else if (attr instanceof Date){
230
                        return ValueFactory.createValue(((Date)attr));
231 6723 jorpiell
                }
232 8765 jjdelcerro
                return ValueFactory.createValue(((String)""));
233 6723 jorpiell
        }
234
235
        public class FeatureWithAttributes{
236
                private IFeature feature;
237 9916 jorpiell
                private Object[] attributeName;
238 6723 jorpiell
                private Value[] attributeValue;
239
240 9916 jorpiell
                public FeatureWithAttributes(IFeature feature, Object[] attributeName, Value[] attributeValue) {
241 6723 jorpiell
                        super();
242
                        this.feature = feature;
243
                        this.attributeName = attributeName;
244
                        this.attributeValue = attributeValue;
245
                }
246
247
                /**
248
                 * @return Returns the attributeName.
249
                 */
250 9916 jorpiell
                public Object[] getAttributeName() {
251 6723 jorpiell
                        return attributeName;
252
                }
253
254
                /**
255
                 * @return Returns the feature.
256
                 */
257
                public IFeature getFeature() {
258
                        return feature;
259
                }
260
261
                /**
262
                 * @return Returns the attributeValue.
263
                 */
264
                public Value[] getAttributeValue() {
265
                        return attributeValue;
266
                }
267
268
        }
269
270 9916 jorpiell
271 6723 jorpiell
}