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 @ 45762

History | View | Annotate | Download (6.1 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.GeometryFactory;
27
import com.vividsolutions.jts.geom.LinearRing;
28
import com.vividsolutions.jts.geomgraph.Position;
29
import com.vividsolutions.jts.operation.buffer.BufferParameters;
30
import com.vividsolutions.jts.operation.buffer.OffsetCurveBuilder;
31

    
32
import org.cresques.cts.ICoordTrans;
33

    
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryException;
36
import org.gvsig.fmap.geom.aggregate.MultiLine;
37
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
38
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D;
39
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2DM;
40
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D;
41
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2DM;
42
import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine2D;
43
import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine2DM;
44
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
45
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM;
46
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
47
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2DM;
48
import org.gvsig.fmap.geom.jts.util.JTSUtils;
49
import org.gvsig.fmap.geom.operation.GeometryOperationException;
50
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
51
import org.gvsig.fmap.geom.primitive.Ring;
52

    
53
/**
54
 * @author fdiaz
55
 *
56
 */
57
public class Ring2DM extends BaseLine2DM implements Ring {
58

    
59
    /**
60
     * @param subtype
61
     */
62
    public Ring2DM() {
63
        super(Geometry.TYPES.RING);
64
    }
65

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

    
74
    /* (non-Javadoc)
75
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
76
     */
77
    public Geometry cloneGeometry() {
78
        return new Ring2DM(cloneCoordinates().toCoordinateArray());
79
    }
80

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

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

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

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

    
116
          OffsetCurveBuilder ocb = new OffsetCurveBuilder(factory.getPrecisionModel(), bufParams);
117

    
118
          Coordinate[] coordinates = jtsRing.getCoordinates();
119
          Coordinate[] coords = ocb.getRingCurve(coordinates, Position.LEFT, distance); // .getOffsetCurve(coordinates,
120

    
121
          return new Ring2DM(coords);
122
      }
123

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

    
129
          OffsetCurveBuilder ocb = new OffsetCurveBuilder(factory.getPrecisionModel(), bufParams);
130

    
131
          Coordinate[] coordinates = jtsRing.getCoordinates();
132
          Coordinate[] coords = ocb.getRingCurve(coordinates, Position.LEFT, distance); // .getOffsetCurve(coordinates,
133

    
134
          return new Ring2DM(coords);
135
      }
136

    
137

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

    
149
      @Override
150
      public boolean equals(Object obj) {
151
          boolean res = super.equals(obj);
152
          if(res && obj instanceof Ring2DM){
153
              Ring2DM other = (Ring2DM)obj;
154
              if(this.getNumVertices() != other.getNumVertices()){
155
                  return false;
156
              }
157
              for(int i=0; i < this.getNumVertices(); i++){
158
                  if(other.coordinates.get(i).getOrdinate(2)!=this.coordinates.get(i).getOrdinate(2)){
159
                      return false;
160
                  };
161
              }
162
              return true;
163
          } else {
164
              return false;
165
          }
166
      }
167
}