Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / ring / Ring2DM.java @ 47432

History | View | Annotate | Download (8.22 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.ring;
24

    
25
import com.vividsolutions.jts.geom.Coordinate;
26
import com.vividsolutions.jts.geom.CoordinateSequence;
27
import com.vividsolutions.jts.geom.GeometryFactory;
28
import com.vividsolutions.jts.geom.LinearRing;
29
import com.vividsolutions.jts.geomgraph.Position;
30
import com.vividsolutions.jts.operation.buffer.BufferParameters;
31
import com.vividsolutions.jts.operation.buffer.OffsetCurveBuilder;
32
import org.cresques.cts.ICoordTrans;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.GeometryException;
35
import org.gvsig.fmap.geom.aggregate.MultiLine;
36
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
37
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2DM;
38
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2DM;
39
import org.gvsig.fmap.geom.jts.mgeom.MCoordinate;
40
import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine2DM;
41
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM;
42
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2DM;
43
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
44
import org.gvsig.fmap.geom.jts.util.JTSUtils;
45
import org.gvsig.fmap.geom.operation.GeometryOperationException;
46
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
47
import org.gvsig.fmap.geom.primitive.Ring;
48

    
49
/**
50
 * @author fdiaz
51
 *
52
 */
53
public class Ring2DM extends BaseLine2DM implements Ring {
54

    
55
    /**
56
     * @param subtype
57
     */
58
    public Ring2DM() {
59
        super(Geometry.TYPES.RING);
60
    }
61

    
62
    /**
63
     *
64
     */
65
    public Ring2DM(Coordinate[] coordinates) {
66
        super(Geometry.TYPES.RING, coordinates);
67
        closePrimitive();
68
    }
69
    
70
    public Ring2DM(ArrayListCoordinateSequence coordinates) {
71
        super(Geometry.TYPES.RING, coordinates);
72
    }
73

    
74

    
75
    /* (non-Javadoc)
76
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
77
     */
78
    @Override
79
    public Geometry cloneGeometry() {
80
        Ring2DM clone = new Ring2DM(cloneCoordinates().toCoordinateArray());
81
        clone.setProjection(this.getProjection());
82
        return clone;
83
    }
84

    
85
    /* (non-Javadoc)
86
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
87
     */
88
    public MultiLine toLines() throws GeometryException {
89
        MultiLine multiLine = new MultiLine2DM();
90
        multiLine.addPrimitive(new Line2DM(coordinates.toCoordinateArray()));
91
        return multiLine;
92
    }
93

    
94
    /* (non-Javadoc)
95
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
96
     */
97
    public MultiPolygon toPolygons() throws GeometryException {
98
        MultiPolygon multiPolygon = new MultiPolygon2DM();
99
        multiPolygon.addPrimitive(new Polygon2DM(coordinates.toCoordinateArray()));
100
        return multiPolygon;
101
    }
102

    
103
    /*
104
     * (non-Javadoc)
105
     *
106
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
107
     */
108
    public com.vividsolutions.jts.geom.Geometry getJTS() {
109
        return JTSUtils.createJTSLinearRing(coordinates);
110
    }
111

    
112
    /* (non-Javadoc)
113
    * @see org.gvsig.fmap.geom.Geometry#offset(double)
114
    */
115
      public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
116
          com.vividsolutions.jts.geom.LinearRing jtsRing = (LinearRing) getJTS();
117
          GeometryFactory factory = jtsRing.getFactory();
118
          BufferParameters bufParams = JTSUtils.getBufferParameters();
119

    
120
          OffsetCurveBuilder ocb = new OffsetCurveBuilder(factory.getPrecisionModel(), bufParams);
121

    
122
          Coordinate[] coordinates = jtsRing.getCoordinates();
123
          Coordinate[] coords = ocb.getRingCurve(coordinates, Position.LEFT, distance); // .getOffsetCurve(coordinates,
124

    
125
          return new Ring2DM(coords);
126
      }
127

    
128
      public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
129
          com.vividsolutions.jts.geom.LinearRing jtsRing = (LinearRing) getJTS();
130
          GeometryFactory factory = jtsRing.getFactory();
131
          BufferParameters bufParams = JTSUtils.getBufferParameters(joinStyle, BufferParameters.CAP_FLAT);
132

    
133
          OffsetCurveBuilder ocb = new OffsetCurveBuilder(factory.getPrecisionModel(), bufParams);
134

    
135
          Coordinate[] coordinates = jtsRing.getCoordinates();
136
          Coordinate[] coords = ocb.getRingCurve(coordinates, Position.LEFT, distance); // .getOffsetCurve(coordinates,
137

    
138
          return new Ring2DM(coords);
139
      }
140

    
141

    
142
      /* (non-Javadoc)
143
       * @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#reProject(org.cresques.cts.ICoordTrans)
144
       */
145
      @Override
146
      public void reProject(ICoordTrans ct) {
147
          super.reProject(ct);
148
          if (coordinates.size()>=2 && !isClosed()) {
149
              closePrimitive();
150
          }
151
      }
152

    
153
      @Override
154
      public boolean equals(Object obj) {
155
          boolean res = super.equals(obj);
156
          if(res && obj instanceof Ring2DM){
157
              Ring2DM other = (Ring2DM)obj;
158
              if(this.getNumVertices() != other.getNumVertices()){
159
                  return false;
160
              }
161
              for(int i=0; i < this.getNumVertices(); i++){
162
                  if(other.coordinates.get(i).getOrdinate(CoordinateSequence.M)!=this.coordinates.get(i).getOrdinate(CoordinateSequence.M)){
163
                      return false;
164
                  };
165
              }
166
              return true;
167
          } else {
168
              return false;
169
          }
170
      }
171
      
172
    @Override
173
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
174
        ArrayListCoordinateSequence coordinates2D = new ArrayListCoordinateSequence(coordinates.size());
175
        for (Coordinate coordinate : this.coordinates) {
176
            coordinates2D.add(new Coordinate(coordinate.x, coordinate.y));
177
        }
178
        Ring2D ring = new Ring2D(coordinates2D);
179
        ring.setProjection(this.getProjection());
180
        return ring;
181
    }
182

    
183
    @Override
184
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
185
        ArrayListCoordinateSequence coordinates2DM = new ArrayListCoordinateSequence(coordinates.size());
186
        for (Coordinate coordinate : this.coordinates) {
187
            coordinates2DM.add(MCoordinate.create2dWithMeasure(coordinate.x, coordinate.y, 0));
188
        }
189
        Ring2DM ring = new Ring2DM(coordinates2DM);
190
        ring.setProjection(this.getProjection());
191
        return ring;
192
    }
193

    
194
    @Override
195
    public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException {
196
        ArrayListCoordinateSequence coordinates3D = new ArrayListCoordinateSequence(coordinates.size());
197
        for (Coordinate coordinate : this.coordinates) {
198
            coordinates3D.add(new Coordinate(coordinate.x, coordinate.y, 0));
199
        }
200
        Ring3D ring = new Ring3D(coordinates3D);
201
        ring.setProjection(this.getProjection());
202
        return ring;
203
    }
204

    
205
    @Override
206
    public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
207
        ArrayListCoordinateSequence coordinates3DM = new ArrayListCoordinateSequence(coordinates.size());
208
        for (Coordinate coordinate : this.coordinates) {
209
            coordinates3DM.add(MCoordinate.create3dWithMeasure(coordinate.x, coordinate.y, 0, 0));
210
        }
211
        Ring3DM ring = new Ring3DM(coordinates3DM);
212
        ring.setProjection(this.getProjection());
213
        return ring;
214
    }
215
      
216
}