Statistics
| Revision:

root / branches / piloto3d / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gml / FMAPGeometryFactory.java @ 9524

History | View | Annotate | Download (6.25 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

    
7
import org.apache.log4j.Logger;
8
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
9

    
10
import com.hardcode.gdbms.engine.values.ComplexValue;
11
import com.hardcode.gdbms.engine.values.Value;
12
import com.hardcode.gdbms.engine.values.ValueFactory;
13
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
14
import com.iver.cit.gvsig.fmap.core.IFeature;
15
import com.iver.cit.gvsig.fmap.core.IGeometry;
16
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
17
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
18
import com.vividsolutions.jts.geom.Geometry;
19
import com.vividsolutions.jts.geom.GeometryFactory;
20
import com.vividsolutions.jts.io.gml2.GMLReader;
21

    
22
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
23
 *
24
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
25
 *
26
 * This program is free software; you can redistribute it and/or
27
 * modify it under the terms of the GNU General Public License
28
 * as published by the Free Software Foundation; either version 2
29
 * of the License, or (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
39
 *
40
 * For more information, contact:
41
 *
42
 *  Generalitat Valenciana
43
 *   Conselleria d'Infraestructures i Transport
44
 *   Av. Blasco Ib??ez, 50
45
 *   46010 VALENCIA
46
 *   SPAIN
47
 *
48
 *      +34 963862235
49
 *   gvsig@gva.es
50
 *      www.gvsig.gva.es
51
 *
52
 *    or
53
 *
54
 *   IVER T.I. S.A
55
 *   Salamanca 50
56
 *   46005 Valencia
57
 *   Spain
58
 *
59
 *   +34 963163400
60
 *   dac@iver.es
61
 */
62
/* CVS MESSAGES:
63
 *
64
 * $Id: FMAPGeometryFactory.java 9524 2007-01-03 15:14:45Z sbayarri $
65
 * $Log$
66
 * Revision 1.1.2.1  2007-01-03 15:12:33  sbayarri
67
 * v10 synch & 3D
68
 *
69
 * Revision 1.1.4.1  2006/11/15 00:08:23  jjdelcerro
70
 * *** empty log message ***
71
 *
72
 * Revision 1.2  2006/10/10 12:55:27  jorpiell
73
 * Se ha a?adido el soporte de features complejas
74
 *
75
 * Revision 1.1  2006/08/10 12:03:43  jorpiell
76
 * Se usa el nuevo driver de GML de remoteServices
77
 *
78
 *
79
 */
80
/**
81
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
82
 */
83
public class FMAPGeometryFactory implements IGeometriesFactory{
84
        private static Logger logger = Logger.getLogger(FMAPGeometryFactory.class);
85
        
86
        public Object createPoint2D(double x, double y){
87
                return ShapeFactory.createPoint2D(x,y);
88
        }
89

    
90
        public Object createPoint2D(Point2D point2D) {
91
                return createPoint2D(point2D.getX(),point2D.getY());
92
        }
93
        
94
        public Object createMultipoint2D(double[] x, double[] y){
95
                return ShapeFactory.createMultipoint2D(x,y);
96
        }
97
        
98
        public Object createSimpleFeature(String element, Object geometry, ArrayList params, ArrayList values) {
99
                ArrayList fmapParams = getParams((ArrayList)params.get(1));
100
                Value[] fmapValues = getValues((ArrayList)params.get(1),(ArrayList)values.get(1));
101
                IFeature feature = new DefaultFeature((IGeometry)geometry,fmapValues);
102
                return new FeatureWithAttributes(feature,fmapParams,fmapValues);
103
        }
104

    
105
        public Object createGeometry(String gmlGeometry) {
106
                GMLReader reader = new GMLReader();
107
                try {
108
                        Geometry geom = reader.read(gmlGeometry,new GeometryFactory());
109
                        return ShapeFactory.createGeometry(FConverter.jts_to_java2d(geom));
110
                } catch (Exception e){
111
                        logger.error("Can't parse: " + gmlGeometry,e);
112
                }
113
                return null;
114
        }
115
        
116
        private ArrayList getParams(ArrayList params){
117
                ArrayList fmapParams = new ArrayList();
118
                for (int i=0 ; i<params.size() ; i++){
119
                        if (!(params.get(i) instanceof ArrayList)){
120
                                 fmapParams.add(params.get(i));
121
                        }
122
                }
123
                return fmapParams;
124
        }
125
        
126
        private Value[] getValues(ArrayList params,ArrayList values){
127
                int i = 0;
128
                ArrayList fmapValues = new ArrayList();
129
                while(i<params.size()){
130
                        if ((i+1 < params.size()) && (params.get(i+1) instanceof ArrayList)){
131
                                ComplexValue value = ValueFactory.createComplexValue((String)params.get(i));
132
                                ArrayList subParams = getParams((ArrayList)params.get(i+1));
133
                                Value[] subValues = getValues((ArrayList)params.get(i+1),(ArrayList)values.get(i+1));
134
                                for (int j=0 ; j<subParams.size() ; j++){
135
                                        value.put(subParams.get(j),subValues[j]);
136
                                }
137
                                fmapValues.add(value);
138
                                i = i + 2;
139
                        }else{
140
                                fmapValues.add(getValue(values.get(i)));
141
                                i++;
142
                        }                                
143
                }
144
                Value[] returnValues = new Value[fmapValues.size()];
145
                for (int k=0 ; k<fmapValues.size() ; k++){
146
                        returnValues[k] = (Value)fmapValues.get(k);
147
                }
148
                return returnValues;
149
        }
150
        
151
        /**
152
         * Gets the attributes
153
         * @param attr
154
         * @return
155
         */
156
        private Value getValue(Object attr) {        
157
                if (attr instanceof Double){
158
                        return ValueFactory.createValue(((Double)attr).doubleValue());
159
                }else if (attr instanceof String){
160
                        return ValueFactory.createValue(String.valueOf(attr));
161
                }else if (attr instanceof Long){
162
                        return ValueFactory.createValue(((Long)attr).longValue());
163
                }else if (attr instanceof Integer){
164
                        return ValueFactory.createValue(((Integer)attr).intValue());
165
                }else if (attr instanceof Float){
166
                        return ValueFactory.createValue(((Float)attr).floatValue());
167
                }else if (attr instanceof Short){
168
                        return ValueFactory.createValue(((Short)attr).shortValue());
169
                }else if (attr instanceof Boolean){
170
                        return ValueFactory.createValue(((Boolean)attr).booleanValue());
171
                }else if (attr instanceof Date){
172
                        return ValueFactory.createValue(((Date)attr));
173
                }
174
                return ValueFactory.createValue(((String)""));
175
        }
176
        
177
        public class FeatureWithAttributes{
178
                private IFeature feature;
179
                private ArrayList attributeName;
180
                private Value[] attributeValue;
181
                
182
                public FeatureWithAttributes(IFeature feature, ArrayList attributeName, Value[] attributeValue) {
183
                        super();                
184
                        this.feature = feature;
185
                        this.attributeName = attributeName;
186
                        this.attributeValue = attributeValue;
187
                }
188

    
189
                /**
190
                 * @return Returns the attributeName.
191
                 */
192
                public ArrayList getAttributeName() {
193
                        return attributeName;
194
                }
195

    
196
                /**
197
                 * @return Returns the feature.
198
                 */
199
                public IFeature getFeature() {
200
                        return feature;
201
                }
202

    
203
                /**
204
                 * @return Returns the attributeValue.
205
                 */
206
                public Value[] getAttributeValue() {
207
                        return attributeValue;
208
                }
209
                
210
        }
211

    
212
}