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 @ 40559
History | View | Annotate | Download (5.51 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.shp.utils; |
25 |
|
26 |
import java.awt.geom.PathIterator; |
27 |
import java.nio.ByteBuffer; |
28 |
import java.nio.MappedByteBuffer; |
29 |
|
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryLocator; |
32 |
import org.gvsig.fmap.geom.GeometryManager; |
33 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
34 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
35 |
import org.gvsig.fmap.geom.primitive.Point; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
|
40 |
/**
|
41 |
* DOCUMENT ME!
|
42 |
*
|
43 |
* @author Vicente Caballero Navarro
|
44 |
*/
|
45 |
public class SHPPoint implements SHPShape { |
46 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
47 |
private static final Logger logger = LoggerFactory.getLogger(SHPPoint.class); |
48 |
private int m_type; |
49 |
private Point point; |
50 |
private double z; |
51 |
|
52 |
/**
|
53 |
* Crea un nuevo SHPPoint.
|
54 |
*
|
55 |
* @param type DOCUMENT ME!
|
56 |
*
|
57 |
* @throws ShapefileException DOCUMENT ME!
|
58 |
*/
|
59 |
public SHPPoint(int type) { |
60 |
if ((type != SHP.POINT2D) &&
|
61 |
(type != SHP.POINTM) && |
62 |
(type != SHP.POINT3D)) { // 2d, 2d+m, 3d+m
|
63 |
// throw new ShapefileException("No es un punto 1,11 ni 21");
|
64 |
} |
65 |
|
66 |
m_type = type; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Crea un nuevo SHPPoint.
|
71 |
*/
|
72 |
public SHPPoint() {
|
73 |
m_type = SHP.POINT2D; //2d
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
|
78 |
*/
|
79 |
public int getShapeType() { |
80 |
return m_type;
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
|
85 |
*/
|
86 |
public Geometry read(MappedByteBuffer buffer, int type) { |
87 |
double x = buffer.getDouble();
|
88 |
double y = buffer.getDouble();
|
89 |
double z = Double.NaN; |
90 |
|
91 |
if (m_type == SHP.POINTM) {
|
92 |
buffer.getDouble(); |
93 |
} |
94 |
|
95 |
if (m_type == SHP.POINT3D) {
|
96 |
z = buffer.getDouble(); |
97 |
Point point;
|
98 |
try {
|
99 |
point = geomManager.createPoint(x, y, SUBTYPES.GEOM3D); |
100 |
point.setCoordinateAt(2, z);
|
101 |
return point;
|
102 |
//FIXME que hacems con esto
|
103 |
} catch (CreateGeometryException e) {
|
104 |
logger.error("Error creating a point", e);
|
105 |
} |
106 |
} |
107 |
try {
|
108 |
return geomManager.createPoint(x, y, SUBTYPES.GEOM2D);
|
109 |
} catch (CreateGeometryException e) {
|
110 |
logger.error("Error creating a point", e);
|
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
|
117 |
*/
|
118 |
public void write(ByteBuffer buffer, Geometry geometry) { |
119 |
buffer.putDouble(point.getX()); |
120 |
buffer.putDouble(point.getY()); |
121 |
|
122 |
if ((m_type == SHP.POINT3D) || (m_type == SHP.POINTM)){
|
123 |
if (Double.isNaN(z)) { // nan means not defined |
124 |
buffer.putDouble(0.0);
|
125 |
} else {
|
126 |
buffer.putDouble(z); |
127 |
} |
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
|
133 |
*/
|
134 |
public int getLength(Geometry fgeometry) { |
135 |
int length;
|
136 |
|
137 |
if (m_type == SHP.POINT2D) {
|
138 |
length = 20;
|
139 |
} else if (m_type == SHP.POINTM || m_type == SHP.POINT3D) { |
140 |
length = 28;
|
141 |
} else {
|
142 |
throw new IllegalStateException("Expected ShapeType of Point, got" + |
143 |
m_type); |
144 |
} |
145 |
|
146 |
return length;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* @see com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
|
151 |
*/
|
152 |
public void obtainsPoints(Geometry g) { |
153 |
if (m_type == SHP.POINTM || m_type == SHP.POINT3D){
|
154 |
z = ((Point)g).getCoordinateAt(2); |
155 |
} |
156 |
PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness); |
157 |
double[] theData = new double[6]; |
158 |
|
159 |
while (!theIterator.isDone()) {
|
160 |
//while not done
|
161 |
// int theType = theIterator.currentSegment(theData);
|
162 |
theIterator.currentSegment(theData); |
163 |
|
164 |
try {
|
165 |
point = geomManager.createPoint(theData[0], theData[1], SUBTYPES.GEOM2D); |
166 |
} catch (CreateGeometryException e) {
|
167 |
logger.error("Error creating a point", e);
|
168 |
} |
169 |
|
170 |
theIterator.next(); |
171 |
} //end while loop
|
172 |
} |
173 |
// public void setFlatness(double flatness) {
|
174 |
// // this.flatness=flatness;
|
175 |
// }
|
176 |
} |