root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / kml / KmlFMapDriver.java @ 10627
History | View | Annotate | Download (9.29 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.v02.FConstant; |
34 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
35 |
import com.iver.cit.gvsig.fmap.drivers.ConcreteMemoryDriver; |
36 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
37 |
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
38 |
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory; |
39 |
import com.iver.cit.gvsig.fmap.drivers.gml.FMAPGeometryFactory.FeatureWithAttributes; |
40 |
import com.iver.cit.gvsig.fmap.rendering.Legend; |
41 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
42 |
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend; |
43 |
|
44 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
45 |
*
|
46 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
47 |
*
|
48 |
* This program is free software; you can redistribute it and/or
|
49 |
* modify it under the terms of the GNU General Public License
|
50 |
* as published by the Free Software Foundation; either version 2
|
51 |
* of the License, or (at your option) any later version.
|
52 |
*
|
53 |
* This program is distributed in the hope that it will be useful,
|
54 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
55 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
56 |
* GNU General Public License for more details.
|
57 |
*
|
58 |
* You should have received a copy of the GNU General Public License
|
59 |
* along with this program; if not, write to the Free Software
|
60 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
61 |
*
|
62 |
* For more information, contact:
|
63 |
*
|
64 |
* Generalitat Valenciana
|
65 |
* Conselleria d'Infraestructures i Transport
|
66 |
* Av. Blasco Ib??ez, 50
|
67 |
* 46010 VALENCIA
|
68 |
* SPAIN
|
69 |
*
|
70 |
* +34 963862235
|
71 |
* gvsig@gva.es
|
72 |
* www.gvsig.gva.es
|
73 |
*
|
74 |
* or
|
75 |
*
|
76 |
* IVER T.I. S.A
|
77 |
* Salamanca 50
|
78 |
* 46005 Valencia
|
79 |
* Spain
|
80 |
*
|
81 |
* +34 963163400
|
82 |
* dac@iver.es
|
83 |
*/
|
84 |
/* CVS MESSAGES:
|
85 |
*
|
86 |
* $Id: KmlFMapDriver.java 10627 2007-03-06 17:10:21Z caballero $
|
87 |
* $Log$
|
88 |
* Revision 1.2 2007-03-06 17:09:00 caballero
|
89 |
* Exceptions
|
90 |
*
|
91 |
* Revision 1.1 2007/02/12 13:50:08 jorpiell
|
92 |
* A?adido el driver de KML
|
93 |
*
|
94 |
*
|
95 |
*/
|
96 |
/**
|
97 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
98 |
*/
|
99 |
public class KmlFMapDriver extends ConcreteMemoryDriver implements VectorialFileDriver,WithDefaultLegend{ |
100 |
private static Logger logger = Logger.getLogger(KmlDriver.class.getName()); |
101 |
private File m_Fich; |
102 |
private VectorialUniqueValueLegend defaultLegend = null; |
103 |
private FSymbol defaultSymbol = null; |
104 |
private IGeometriesFactory factory = null; |
105 |
private KmlDriver driver = null; |
106 |
private IFeaturesIterator iterator = null; |
107 |
private static final float heightText = 10; |
108 |
private Random r = null; |
109 |
//it contains the position for the folder name.
|
110 |
private int FOLDER_NAME_POSITION = 0; |
111 |
|
112 |
/*
|
113 |
* (non-Javadoc)
|
114 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
|
115 |
*/
|
116 |
public void open(File f) { |
117 |
m_Fich = f; |
118 |
factory = new FMAPGeometryFactory();
|
119 |
r = new Random(); |
120 |
} |
121 |
|
122 |
/*
|
123 |
* (non-Javadoc)
|
124 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
|
125 |
*/
|
126 |
public void close() { |
127 |
// TODO Auto-generated method stub
|
128 |
|
129 |
} |
130 |
|
131 |
/*
|
132 |
* (non-Javadoc)
|
133 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
|
134 |
*/
|
135 |
public void initialize() throws InitializeDriverException { |
136 |
try {
|
137 |
driver = new KmlDriver(createInputStream(),factory);
|
138 |
iterator = driver.getFeaturesIterator(); |
139 |
} catch (BaseException e) {
|
140 |
logger.error("Error openning the kml file",e);
|
141 |
throw new InitializeDriverException(getName(),e); |
142 |
} |
143 |
|
144 |
int index = 0; |
145 |
|
146 |
IntValue clave; |
147 |
|
148 |
boolean setModel = false; |
149 |
FeatureWithAttributes feature; |
150 |
try {
|
151 |
while (iterator.hasNext()) {
|
152 |
try{
|
153 |
feature = (FeatureWithAttributes)iterator.next(); |
154 |
//There are features without geometry
|
155 |
if (feature.getFeature().getGeometry() != null){ |
156 |
if (!setModel){
|
157 |
getTableModel().setColumnIdentifiers(feature.getAttributeName()); |
158 |
setModel = true;
|
159 |
} |
160 |
clave = ValueFactory.createValue(index); |
161 |
IGeometry geom = feature.getFeature().getGeometry(); |
162 |
if (geom != null){ |
163 |
FSymbol theSymbol = null;
|
164 |
theSymbol = getDefaultSymbol(); |
165 |
theSymbol.setDescription(clave.toString()); |
166 |
addGeometry(geom, feature.getAttributeValue()); |
167 |
} |
168 |
} |
169 |
index++; |
170 |
}catch(GMLException e){
|
171 |
logger.error(e.getMessage(),e); |
172 |
e.printStackTrace(); |
173 |
} |
174 |
} |
175 |
}catch (BaseException e) {
|
176 |
logger.error("Error retrieving the geometries",e);
|
177 |
e.printStackTrace(); |
178 |
} |
179 |
|
180 |
try {
|
181 |
createLegend(); |
182 |
} catch (ReadDriverException e) {
|
183 |
throw new InitializeDriverException(getName(),e); |
184 |
} |
185 |
} |
186 |
|
187 |
/**
|
188 |
* Creates the legend. The unique value is the field
|
189 |
* that is specified in the KmlTags.FOLDER_NAME constant
|
190 |
* @throws IOException
|
191 |
*/
|
192 |
private void createLegend() throws ReadDriverException{ |
193 |
ObjectDriver rs = this;
|
194 |
StringValue clave; |
195 |
FSymbol theSymbol = null;
|
196 |
for (long j = 0; j < rs.getRowCount(); j++) { |
197 |
clave = (StringValue) rs.getFieldValue(j, FOLDER_NAME_POSITION); |
198 |
if (((VectorialUniqueValueLegend)getDefaultLegend()).getSymbolByValue(clave) == null) { |
199 |
theSymbol = new FSymbol(getShapeType());
|
200 |
theSymbol.setDescription(clave.toString()); |
201 |
Color c = getRandomColor();
|
202 |
theSymbol.setColor(c); |
203 |
theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID); |
204 |
theSymbol.setSize(3);
|
205 |
theSymbol.setSizeInPixels(true);
|
206 |
defaultLegend.addSymbol(clave, theSymbol); |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
/*
|
212 |
* (non-Javadoc)
|
213 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
|
214 |
*/
|
215 |
public boolean accept(File f) { |
216 |
if ((f.getName().toUpperCase().endsWith("KML")) || |
217 |
(f.getName().toUpperCase().endsWith("KMZ"))){
|
218 |
return true; |
219 |
} |
220 |
return false; |
221 |
} |
222 |
|
223 |
/*
|
224 |
* (non-Javadoc)
|
225 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFile()
|
226 |
*/
|
227 |
public File getFile() { |
228 |
return m_Fich;
|
229 |
} |
230 |
|
231 |
|
232 |
/*
|
233 |
* (non-Javadoc)
|
234 |
* @see com.hardcode.driverManager.Driver#getName()
|
235 |
*/
|
236 |
public String getName() { |
237 |
return "gvSIG KML Memory Driver"; |
238 |
} |
239 |
|
240 |
/*
|
241 |
* (non-Javadoc)
|
242 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
|
243 |
*/
|
244 |
public int getShapeType() { |
245 |
return FShape.MULTI;
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* It returns a random color
|
250 |
* @return
|
251 |
*/
|
252 |
private Color getRandomColor(){ |
253 |
return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)); |
254 |
} |
255 |
|
256 |
/**
|
257 |
* Creates the default symbol
|
258 |
* @return
|
259 |
*/
|
260 |
private FSymbol getDefaultSymbol(){
|
261 |
if (defaultSymbol == null){ |
262 |
defaultSymbol = new FSymbol(getShapeType());
|
263 |
defaultSymbol.setShapeVisible(false);
|
264 |
defaultSymbol.setFontSizeInPixels(false);
|
265 |
defaultSymbol.setFont(new Font("SansSerif",Font.PLAIN, 9)); |
266 |
defaultSymbol.setFontColor(Color.BLACK);
|
267 |
defaultSymbol.setFontSize(heightText); |
268 |
defaultSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_SOLID); |
269 |
defaultSymbol.setSize(3);
|
270 |
defaultSymbol.setSizeInPixels(true);
|
271 |
} |
272 |
return defaultSymbol;
|
273 |
} |
274 |
|
275 |
/*
|
276 |
* (non-Javadoc)
|
277 |
* @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
|
278 |
*/
|
279 |
public Legend getDefaultLegend() {
|
280 |
if (defaultLegend == null){ |
281 |
defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType()); |
282 |
defaultLegend.setFieldName(KmlTags.FOLDER_NAME); |
283 |
defaultLegend.setLabelField(null);
|
284 |
defaultLegend.setLabelHeightField(null);
|
285 |
defaultLegend.setLabelRotationField(null);
|
286 |
defaultLegend.setDefaultSymbol(getDefaultSymbol()); |
287 |
} |
288 |
return defaultLegend;
|
289 |
} |
290 |
|
291 |
/**
|
292 |
* It creates an InputStream. The Kml file can have the
|
293 |
* KML or the KMZ extension
|
294 |
* @return
|
295 |
* @throws KmlException
|
296 |
*/
|
297 |
private InputStream createInputStream() throws KmlException{ |
298 |
try {
|
299 |
if (getFile().getName().toUpperCase().endsWith("KML")){ |
300 |
return new FileInputStream(getFile()); |
301 |
}else if(getFile().getName().toUpperCase().endsWith("KMZ")){ |
302 |
FileInputStream fis = new FileInputStream(getFile()); |
303 |
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); |
304 |
ZipEntry entry = null; |
305 |
while((entry = zis.getNextEntry()) != null){ |
306 |
if (!entry.isDirectory()){
|
307 |
ZipFile fz = new ZipFile(getFile()); |
308 |
return fz.getInputStream(entry);
|
309 |
} |
310 |
} |
311 |
}else{
|
312 |
throw new KmlException(getFile()); |
313 |
} |
314 |
} catch (FileNotFoundException e) { |
315 |
throw new KmlException(getFile(),e); |
316 |
} catch (IOException e) { |
317 |
throw new KmlException(getFile(),e); |
318 |
} |
319 |
return null; |
320 |
} |
321 |
} |