Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / utils / SHPPoint.java @ 40435

History | View | Annotate | Download (5.78 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.dal.store.shp.utils;
42

    
43
import java.awt.geom.PathIterator;
44
import java.nio.ByteBuffer;
45
import java.nio.MappedByteBuffer;
46

    
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.exception.CreateGeometryException;
52
import org.gvsig.fmap.geom.primitive.Point;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56

    
57
/**
58
 * DOCUMENT ME!
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SHPPoint implements SHPShape {
63
    private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
64
    private static final Logger logger = LoggerFactory.getLogger(SHPPoint.class);
65
    private int m_type;
66
    private Point point;
67
    private double z;
68

    
69
    /**
70
     * Crea un nuevo SHPPoint.
71
     *
72
     * @param type DOCUMENT ME!
73
     *
74
     * @throws ShapefileException DOCUMENT ME!
75
     */
76
    public SHPPoint(int type)  {
77
        if ((type != SHP.POINT2D) &&
78
            (type != SHP.POINTM) &&
79
            (type != SHP.POINT3D)) { // 2d, 2d+m, 3d+m
80
            //                        throw new ShapefileException("No es un punto 1,11 ni 21");
81
        }
82

    
83
        m_type = type;
84
    }
85

    
86
    /**
87
     * Crea un nuevo SHPPoint.
88
     */
89
    public SHPPoint() {
90
        m_type = SHP.POINT2D; //2d
91
    }
92

    
93
    /**
94
     * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
95
     */
96
    public int getShapeType() {
97
        return m_type;
98
    }
99

    
100
    /**
101
     * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
102
     */
103
    public Geometry read(MappedByteBuffer buffer, int type) {
104
        double x = buffer.getDouble();
105
        double y = buffer.getDouble();
106
        double z = Double.NaN;
107

    
108
        if (m_type == SHP.POINTM) {
109
            buffer.getDouble();
110
        }
111

    
112
        if (m_type == SHP.POINT3D) {
113
            z = buffer.getDouble();
114
            Point point;
115
            try {
116
                point = geomManager.createPoint(x, y, SUBTYPES.GEOM3D);
117
                point.setCoordinateAt(2, z);
118
                return point;
119
                //FIXME que hacems con esto
120
            } catch (CreateGeometryException e) {
121
                logger.error("Error creating a point", e);
122
            }                        
123
        }
124
        try {
125
            return geomManager.createPoint(x, y, SUBTYPES.GEOM2D);
126
        } catch (CreateGeometryException e) {
127
            logger.error("Error creating a point", e);
128
        }
129
        return null;
130
    }
131

    
132
    /**
133
     * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
134
     */
135
    public void write(ByteBuffer buffer, Geometry geometry) {
136
        buffer.putDouble(point.getX());
137
        buffer.putDouble(point.getY());
138

    
139
        if ((m_type == SHP.POINT3D) ||  (m_type == SHP.POINTM)){
140
            if (Double.isNaN(z)) { // nan means not defined
141
                buffer.putDouble(0.0);
142
            } else {
143
                buffer.putDouble(z);
144
            }
145
        }
146
    }
147

    
148
    /**
149
     * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
150
     */
151
    public int getLength(Geometry fgeometry) {
152
        int length;
153

    
154
        if (m_type == SHP.POINT2D) {
155
            length = 20;
156
        } else if (m_type == SHP.POINTM || m_type == SHP.POINT3D) {
157
            length = 28;
158
        } else {
159
            throw new IllegalStateException("Expected ShapeType of Point, got" +
160
                m_type);
161
        }
162

    
163
        return length;
164
    }
165

    
166
    /**
167
     * @see com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
168
     */
169
    public void obtainsPoints(Geometry g) {
170
        if (m_type == SHP.POINTM || m_type == SHP.POINT3D){
171
            z = ((Point)g).getCoordinateAt(2);
172
        }
173
        PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness);
174
        double[] theData = new double[6];
175

    
176
        while (!theIterator.isDone()) {
177
            //while not done
178
            //                        int theType = theIterator.currentSegment(theData);
179
            theIterator.currentSegment(theData);
180

    
181
            try {
182
                point = geomManager.createPoint(theData[0], theData[1], SUBTYPES.GEOM2D);
183
            } catch (CreateGeometryException e) {
184
                logger.error("Error creating a point", e);
185
            }
186

    
187
            theIterator.next();
188
        } //end while loop
189
    }
190
    //        public void setFlatness(double flatness) {
191
    //        //        this.flatness=flatness;
192
    //        }
193
}