Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / kml / KmlFMapDriver.java @ 11558

History | View | Annotate | Download (11.2 KB)

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

    
3
import java.awt.Color;
4
import java.awt.Font;
5
import java.io.BufferedInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.util.Random;
12
import java.util.zip.ZipEntry;
13
import java.util.zip.ZipFile;
14
import java.util.zip.ZipInputStream;
15

    
16
import org.apache.log4j.Logger;
17
import org.gvsig.exceptions.BaseException;
18
import org.gvsig.remoteClient.gml.engine.IFeaturesIterator;
19
import org.gvsig.remoteClient.gml.exceptions.GMLException;
20
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
21
import org.gvsig.remoteClient.kml.KmlDriver;
22
import org.gvsig.remoteClient.kml.KmlTags;
23
import org.gvsig.remoteClient.kml.exceptions.KmlException;
24

    
25
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
26
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
27
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
28
import com.hardcode.gdbms.engine.values.IntValue;
29
import com.hardcode.gdbms.engine.values.StringValue;
30
import com.hardcode.gdbms.engine.values.ValueFactory;
31
import com.iver.cit.gvsig.fmap.core.FShape;
32
import com.iver.cit.gvsig.fmap.core.IGeometry;
33
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
34
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
35
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
36
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
37
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
38
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
39
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
40
import com.iver.cit.gvsig.fmap.drivers.ConcreteMemoryDriver;
41
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
42
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
43
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory;
44
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes;
45
import com.iver.cit.gvsig.fmap.rendering.ILegend;
46
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
47
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
48
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
49

    
50
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
51
 *
52
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
53
 *
54
 * This program is free software; you can redistribute it and/or
55
 * modify it under the terms of the GNU General Public License
56
 * as published by the Free Software Foundation; either version 2
57
 * of the License, or (at your option) any later version.
58
 *
59
 * This program is distributed in the hope that it will be useful,
60
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62
 * GNU General Public License for more details.
63
 *
64
 * You should have received a copy of the GNU General Public License
65
 * along with this program; if not, write to the Free Software
66
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
67
 *
68
 * For more information, contact:
69
 *
70
 *  Generalitat Valenciana
71
 *   Conselleria d'Infraestructures i Transport
72
 *   Av. Blasco Ib??ez, 50
73
 *   46010 VALENCIA
74
 *   SPAIN
75
 *
76
 *      +34 963862235
77
 *   gvsig@gva.es
78
 *      www.gvsig.gva.es
79
 *
80
 *    or
81
 *
82
 *   IVER T.I. S.A
83
 *   Salamanca 50
84
 *   46005 Valencia
85
 *   Spain
86
 *
87
 *   +34 963163400
88
 *   dac@iver.es
89
 */
90
/* CVS MESSAGES:
91
 *
92
 * $Id: KmlFMapDriver.java 11558 2007-05-10 09:46:45Z jaume $
93
 * $Log$
94
 * Revision 1.4  2007-05-10 09:44:09  jaume
95
 * Refactored legend interface names
96
 *
97
 * Revision 1.3  2007/03/20 16:09:53  jaume
98
 * refactored to use ISymbol instead of FSymbol
99
 *
100
 * Revision 1.2  2007/03/06 17:09:00  caballero
101
 * Exceptions
102
 *
103
 * Revision 1.1  2007/02/12 13:50:08  jorpiell
104
 * A?adido el driver de KML
105
 *
106
 *
107
 */
108
/**
109
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
110
 */
111
public class KmlFMapDriver extends ConcreteMemoryDriver implements VectorialFileDriver,WithDefaultLegend{
112
        private static Logger logger = Logger.getLogger(KmlDriver.class.getName());
113
        private File m_Fich;
114
        private VectorialUniqueValueLegend defaultLegend = null;
115
        private ISymbol defaultSymbol = null;
116
        private IGeometriesFactory factory = null;
117
        private KmlDriver driver = null;
118
        private IFeaturesIterator iterator = null;
119
        private static final float heightText = 10;
120
        private Random r = null;
121
        //it contains the position for the folder name.
122
        private int FOLDER_NAME_POSITION = 0;
123
        private ILabelingStrategy labeler;
124
        /*
125
         *  (non-Javadoc)
126
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
127
         */
128
        public void open(File f) {
129
                m_Fich = f;
130
                factory = new FMAPGeometryFactory();
131
                r = new Random();
132
        }
133

    
134
        /*
135
         *  (non-Javadoc)
136
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
137
         */
138
        public void close() {
139
                // TODO Auto-generated method stub
140

    
141
        }
142

    
143
        /*
144
         *  (non-Javadoc)
145
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
146
         */
147
        public void initialize() throws InitializeDriverException {
148
                try {
149
                        driver = new KmlDriver(createInputStream(),factory);
150
                        iterator =         driver.getFeaturesIterator();
151
                } catch (BaseException e) {
152
                        logger.error("Error openning the kml file",e);
153
                        throw new InitializeDriverException(getName(),e);
154
                }
155

    
156
                int index = 0;
157

    
158
                IntValue clave;
159

    
160
                boolean setModel = false;
161
                FeatureWithAttributes feature;
162
                try {
163
                        while (iterator.hasNext()) {
164
                                try{
165
                                        feature = (FeatureWithAttributes)iterator.next();
166
                                        //There are features without geometry
167
                                        if (feature.getFeature().getGeometry() != null){
168
                                                if (!setModel){
169
                                                        getTableModel().setColumnIdentifiers(feature.getAttributeName());
170
                                                        setModel = true;
171
                                                }
172
                                                clave = ValueFactory.createValue(index);
173
                                                IGeometry geom = feature.getFeature().getGeometry();
174
                                                if (geom != null){
175
                                                        ISymbol theSymbol = null;
176
                                                        theSymbol = getDefaultSymbol();
177
                                                        theSymbol.setDescription(clave.toString());
178
                                                        addGeometry(geom, feature.getAttributeValue());
179
                                                }
180
                                        }
181
                                        index++;
182
                                }catch(GMLException e){
183
                                        logger.error(e.getMessage(),e);
184
                                        e.printStackTrace();
185
                                }
186
                        }
187
                }catch (BaseException e) {
188
                        logger.error("Error retrieving the geometries",e);
189
                        e.printStackTrace();
190
                }
191

    
192
                try {
193
                        createLegend();
194
                } catch (ReadDriverException e) {
195
                        throw new InitializeDriverException(getName(),e);
196
                }
197
        }
198

    
199
        /**
200
         * Creates the legend. The unique value is the field
201
         * that is specified in the KmlTags.FOLDER_NAME constant
202
         * @throws IOException
203
         */
204
        private void createLegend() throws ReadDriverException{
205
                ObjectDriver rs = this;
206
                StringValue clave;
207
                ISymbol theSymbol = null;
208
                for (long j = 0; j < rs.getRowCount(); j++) {
209
                        clave = (StringValue) rs.getFieldValue(j, FOLDER_NAME_POSITION);
210
                        if (((VectorialUniqueValueLegend)getDefaultLegend()).getSymbolByValue(clave) == null) {
211
                                /*
212
                                 * jaume (moved to ISymbol)
213
                                theSymbol = new FSymbol(getShapeType());
214
                                theSymbol.setDescription(clave.toString());
215
                                Color c = getRandomColor();
216
                                theSymbol.setColor(c);
217
                                theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
218
                                theSymbol.setSize(3);
219
                                theSymbol.setSizeInPixels(true);
220
                                */
221
                                theSymbol = SymbologyFactory.
222
                                        createDefaultSymbolByShapeType(getShapeType());
223
                                theSymbol.setDescription(clave.toString());
224
                                Color c = getRandomColor();
225
                                if (theSymbol instanceof IMarkerSymbol) {
226
                                        ((IMarkerSymbol) theSymbol).setColor(c);
227
                                        ((IMarkerSymbol) theSymbol).setSize(3);
228

    
229
                                }
230
                                if (theSymbol instanceof ILineSymbol) {
231
                                        ((ILineSymbol) theSymbol).setLineColor(c);
232
                                        ((ILineSymbol) theSymbol).setLineWidth(3);
233
                                }
234
                                if (theSymbol instanceof IFillSymbol) {
235
                                        ((IFillSymbol) theSymbol).setFillColor(c);
236
                                        ((IFillSymbol) theSymbol).getOutline().setLineWidth(3);
237
                                }
238

    
239
                                defaultLegend.addSymbol(clave, theSymbol);
240
                        }
241
                }
242
        }
243

    
244
        /*
245
         *  (non-Javadoc)
246
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
247
         */
248
        public boolean accept(File f) {
249
                if ((f.getName().toUpperCase().endsWith("KML")) ||
250
                                (f.getName().toUpperCase().endsWith("KMZ"))){
251
                        return true;
252
                }
253
                return false;
254
        }
255

    
256
        /*
257
         *  (non-Javadoc)
258
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
259
         */
260
        public File getFile() {
261
                return m_Fich;
262
        }
263

    
264

    
265
        /*
266
         *  (non-Javadoc)
267
         * @see com.hardcode.driverManager.Driver#getName()
268
         */
269
        public String getName() {
270
                return "gvSIG KML Memory Driver";
271
        }
272

    
273
        /*
274
         *  (non-Javadoc)
275
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
276
         */
277
        public int getShapeType() {
278
                return FShape.MULTI;
279
        }
280

    
281
        /**
282
         * It returns a random color
283
         * @return
284
         */
285
        private Color getRandomColor(){
286
                return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
287
        }
288

    
289
        /**
290
         * Creates the default symbol
291
         * @return
292
         */
293
        private ISymbol getDefaultSymbol(){
294
                if (defaultSymbol == null){
295
                        /* jaume (moved to ISymbol)
296
                         * defaultSymbol = new FSymbol(getShapeType());
297
                        defaultSymbol.setShapeVisible(false);
298
                        defaultSymbol.setFontSizeInPixels(false);
299
                        defaultSymbol.setFont(new Font("SansSerif",Font.PLAIN, 9));
300
                        defaultSymbol.setFontColor(Color.BLACK);
301
                        defaultSymbol.setFontSize(heightText);
302
                        defaultSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID);
303
                        defaultSymbol.setSize(3);
304
                        defaultSymbol.setSizeInPixels(true);
305
                        */
306
                        defaultSymbol = SymbologyFactory.
307
                        createDefaultSymbolByShapeType(getShapeType());
308

    
309
                        Color c = getRandomColor();
310
                        if (defaultSymbol instanceof IMarkerSymbol) {
311
                                ((IMarkerSymbol) defaultSymbol).setColor(c);
312
                                ((IMarkerSymbol) defaultSymbol).setSize(3);
313

    
314
                        }
315
                        if (defaultSymbol instanceof ILineSymbol) {
316
                                ((ILineSymbol) defaultSymbol).setLineColor(c);
317
                                ((ILineSymbol) defaultSymbol).setLineWidth(3);
318
                        }
319
                        if (defaultSymbol instanceof IFillSymbol) {
320
                                ((IFillSymbol) defaultSymbol).setFillColor(c);
321
                                ((IFillSymbol) defaultSymbol).getOutline().setLineWidth(3);
322
                        }
323
                }
324
                return defaultSymbol;
325
        }
326

    
327
        /*
328
         *  (non-Javadoc)
329
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
330
         */
331
        public ILegend getDefaultLegend() {
332
                if (defaultLegend == null){
333
                        defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
334
                        defaultLegend.setFieldName(KmlTags.FOLDER_NAME);
335
                        defaultLegend.setLabelField(null);
336
                        defaultLegend.setLabelHeightField(null);
337
                        defaultLegend.setLabelRotationField(null);
338
                        defaultLegend.setDefaultSymbol(getDefaultSymbol());
339
                }
340
                return defaultLegend;
341
        }
342

    
343
        /**
344
         * It creates an InputStream. The Kml file can have the
345
         * KML or the KMZ extension
346
         * @return
347
         * @throws KmlException
348
         */
349
        private InputStream createInputStream() throws KmlException{
350
                try {
351
                        if (getFile().getName().toUpperCase().endsWith("KML")){
352
                                return new FileInputStream(getFile());
353
                        }else if(getFile().getName().toUpperCase().endsWith("KMZ")){
354
                                FileInputStream fis = new FileInputStream(getFile());
355
                                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
356
                                ZipEntry entry = null;
357
                                while((entry = zis.getNextEntry()) != null){
358
                                        if (!entry.isDirectory()){
359
                                                ZipFile fz = new ZipFile(getFile());
360
                                                return fz.getInputStream(entry);
361
                                        }
362
                                }
363
                        }else{
364
                                throw new KmlException(getFile());
365
                        }
366
                } catch (FileNotFoundException e) {
367
                        throw new KmlException(getFile(),e);
368
                } catch (IOException e) {
369
                        throw new KmlException(getFile(),e);
370
                }
371
                return null;
372
        }
373

    
374
        public ILabelingStrategy getDefaultLabelingStrategy() {
375
                return labeler;
376
        }
377
}