Statistics
| Revision:

root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / writer / v21 / geometries / CoordinatesWriter.java @ 20221

History | View | Annotate | Download (3.62 KB)

1
package org.gvsig.gpe.kml.writer.v21.geometries;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.gpe.kml.utils.KmlTags;
6
import org.gvsig.gpe.kml.writer.GPEKmlWriterHandlerImplementor;
7
import org.gvsig.gpe.parser.ICoordinateIterator;
8
import org.gvsig.gpe.xml.writer.IWriter;
9

    
10
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
11
 *
12
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
13
 *
14
 * This program is free software; you can redistribute it and/or
15
 * modify it under the terms of the GNU General Public License
16
 * as published by the Free Software Foundation; either version 2
17
 * of the License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
27
 *
28
 * For more information, contact:
29
 *
30
 *  Generalitat Valenciana
31
 *   Conselleria d'Infraestructures i Transport
32
 *   Av. Blasco Ib??ez, 50
33
 *   46010 VALENCIA
34
 *   SPAIN
35
 *
36
 *      +34 963862235
37
 *   gvsig@gva.es
38
 *      www.gvsig.gva.es
39
 *
40
 *    or
41
 *
42
 *   IVER T.I. S.A
43
 *   Salamanca 50
44
 *   46005 Valencia
45
 *   Spain
46
 *
47
 *   +34 963163400
48
 *   dac@iver.es
49
 */
50
/* CVS MESSAGES:
51
 *
52
 * $Id: CoordinatesWriter.java 361 2008-01-10 08:41:21Z jpiera $
53
 * $Log$
54
 * Revision 1.3  2007/05/16 09:30:09  jorpiell
55
 * the writting methods has to have the errorHandler argument
56
 *
57
 * Revision 1.2  2007/05/08 07:53:08  jorpiell
58
 * Add comments to the writers
59
 *
60
 * Revision 1.1  2007/04/14 16:08:07  jorpiell
61
 * Kml writing support added
62
 *
63
 *
64
 */
65
/**
66
 * This class writes a coordinates Kml tag. Example:
67
 * <p>
68
 * <pre>
69
 * <code>
70
 * &lt;coordinates&gt;60.0,60.0 60.0,90.0 90.0,90.0&lt;/coordinates&gt;
71
 * </code>
72
 * </pre>
73
 * </p> 
74
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
75
 * @see http://code.google.com/apis/kml/documentation/kml_tags_21.html#coordinates
76
 */
77
public class CoordinatesWriter {
78
        
79
        /**
80
         * It writes an array of coordinates written using
81
         * the kml coordinates tag
82
         * @param writer
83
         * Writer to write the labels
84
         * @param handler
85
         * The writer handler implementor
86
         * @param coords
87
         * A coordinates iterator. 
88
         * @throws IOException
89
         */
90
        public void write(IWriter writer, GPEKmlWriterHandlerImplementor handler,
91
                        ICoordinateIterator coords) throws IOException{
92
                writer.write("\n");
93
                writer.write("<" + KmlTags.COORDINATES.getLocalPart() + ">");
94
                double[] buffer = new double[coords.getDimension()];
95
                //The first coordinate
96
                if (coords.hasNext()){
97
                        coords.next(buffer);                
98
                        writeOneCoordinate(writer,handler,buffer);                        
99
                }
100
                //The other coordinates
101
                while (coords.hasNext()){
102
                        coords.next(buffer);                
103
                        writer.write(KmlTags.TUPLES_SEPARATOR);
104
                        writeOneCoordinate(writer,handler,buffer);                        
105
                }                
106
                writer.write("</" + KmlTags.COORDINATES.getLocalPart() + ">");
107
        }
108
        
109
        /**
110
         * Writes the content of a coordinates kml tag
111
         * @param writer
112
         * Writer to write the labels
113
         * @param handler
114
         * The writer handler implementor
115
         * @param buffer
116
         * An array with the coordinates
117
         * @throws IOException
118
         */
119
        private void writeOneCoordinate(IWriter writer, GPEKmlWriterHandlerImplementor handler,
120
                        double[] buffer) throws IOException{        
121
                for (int i=0 ; i<buffer.length ; i++){
122
                        handler.getProfile().getDoubleWriter().write(writer,handler,buffer[i]);
123
                        if (i<buffer.length -1){
124
                                writer.write(KmlTags.COORDINATES_SEPARATOR);
125
                        }
126
                }
127
        }
128
}