Revision 42378

View differences:

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/operation/fromwkb/WKBParser3.java
38 38
import org.gvsig.fmap.geom.Geometry.TYPES;
39 39
import org.gvsig.fmap.geom.aggregate.MultiPoint;
40 40
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
41
import org.gvsig.fmap.geom.aggregate.MultiSurface;
42 41
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43
import org.gvsig.fmap.geom.primitive.Curve;
44 42
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
45 43
import org.gvsig.fmap.geom.primitive.Point;
46 44
import org.gvsig.fmap.geom.primitive.Primitive;
47
import org.gvsig.fmap.geom.primitive.Surface;
48 45
import org.gvsig.fmap.geom.type.GeometryType;
49
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
50
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
51 46
import org.slf4j.Logger;
52 47
import org.slf4j.LoggerFactory;
53 48

  
54 49
import com.vividsolutions.jts.io.WKBConstants;
50
import org.gvsig.fmap.geom.primitive.Line;
51
import org.gvsig.fmap.geom.primitive.Polygon;
55 52

  
56 53
/**
57 54
 * Parse binary representation of geometries. Currently, only text rep (hexed)
......
72 69

  
73 70
    private boolean gHaveM, gHaveZ, gHaveS; // M, Z y SRID
74 71

  
75
    private static final Logger LOG = LoggerFactory.getLogger(WKBParser2.class);
76
    private GeometryManager geomManager = GeometryLocator.getGeometryManager();
77
    private GeometryType[] pointGeometryTypes;
72
    private static final Logger LOG = LoggerFactory.getLogger(WKBParser3.class);
73
    private final GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
    private final GeometryType[] pointGeometryTypes;
78 75

  
79
    /**
80
     * @throws GeometryTypeNotValidException
81
     * @throws GeometryTypeNotSupportedException
82
     *
83
     */
84 76
    public WKBParser3() {
85 77
        pointGeometryTypes
86 78
                = new GeometryType[]{
......
90 82
                    loadPointGeometryType(Geometry.SUBTYPES.GEOM3DM, "3DM")};
91 83
    }
92 84

  
85
    @SuppressWarnings("UseSpecificCatch")
93 86
    private GeometryType loadPointGeometryType(int subtype, String subTypeName) {
94 87
        try {
95 88
            return geomManager.getGeometryType(Geometry.TYPES.POINT, subtype);
......
106 99
     * Is synchronized to protect offset counter. (Unfortunately, Java does not
107 100
     * have neither call by reference nor multiple return values.)
108 101
     *
102
     * @param value
103
     * @return 
109 104
     * @throws CreateGeometryException
110 105
     */
111 106
    public synchronized Geometry parse(byte[] value) throws CreateGeometryException {
......
117 112
    /**
118 113
     * Parse a geometry starting at offset.
119 114
     *
115
     * @param data
116
     * @return 
120 117
     * @throws CreateGeometryException
121 118
     */
122 119
    protected Geometry parseGeometry(ByteBuffer data) throws CreateGeometryException {
......
200 197
                /*point.setCoordinateAt(Geometry.DIMENSIONS.Z + 1,
201 198
                 data.getDouble());*/
202 199
                data.getDouble();
203
            }
200
        }
204 201
        } else {
205
            if (haveM) {
202
        if (haveM) {
206 203
                /*point.setCoordinateAt(Geometry.DIMENSIONS.Y + 1,
207 204
                 data.getDouble());*/
208 205
                data.getDouble();
209
            }
210 206
        }
207
        }
211 208

  
212 209
        return point;
213 210
    }
......
224 221
         ? Geometry.SUBTYPES.GEOM2DM : Geometry.SUBTYPES.GEOM2D);*/
225 222
        //TODO: No hay soporte para M
226 223
        int subtype = haveZ ? Geometry.SUBTYPES.GEOM3D : Geometry.SUBTYPES.GEOM2D;
227
        return subtype;
224
       return subtype;
228 225
    }
229 226

  
230
    private Curve parseMultiLineString(ByteBuffer data) throws CreateGeometryException {
231
        Curve curve = (Curve) geomManager.create(TYPES.CURVE, getSubType(gHaveZ, gHaveM));
232
        fillOrientablePrimitive(data, curve);
233
        return curve;
234
    }
227
    private MultiPrimitive parseMultiLineString(ByteBuffer data) throws CreateGeometryException {
228
        int subType = getSubType(gHaveZ, gHaveM);
229
        MultiPrimitive multiline = geomManager.createMultiLine(subType);
235 230

  
236
    /**
237
     * @param data
238
     * @return
239
     * @throws CreateGeometryException
240
     */
241
    private void fillOrientablePrimitive(ByteBuffer data, OrientablePrimitive orientablePrimitive)
242
            throws CreateGeometryException {
243 231
        int count = data.getInt();
244 232

  
245 233
        for (int i = 0; i < count; i++) {
246 234
            parseTypeAndSRID(data);
247 235
            Point[] points = parsePointArray(data, gHaveZ, gHaveM);
248

  
249
            orientablePrimitive.addMoveToVertex(points[0]);
250
            for (int j = 1; j < points.length; j++) {
251
                orientablePrimitive.addVertex(points[j]);
236
            Line line = geomManager.createLine(getSubType(gHaveZ, gHaveM));
237
            line.ensureCapacity(points.length);
238
            for (Point point : points) {
239
                line.addVertex(point);
252 240
            }
241
            multiline.addPrimitive(line);
253 242
        }
243
        return multiline;
254 244
    }
255 245

  
256
    private MultiSurface parseMultiPolygon(ByteBuffer data)
246
    private MultiPrimitive parseMultiPolygon(ByteBuffer data)
257 247
            throws CreateGeometryException {
258 248
        int count = data.getInt();
259 249

  
260 250
        int subType = getSubType(gHaveZ, gHaveM);
261
        MultiSurface multiSurface = (MultiSurface) geomManager.create(TYPES.MULTISURFACE, subType);
251
        MultiPrimitive multipolygon = geomManager.createMultiPolygon(subType);
262 252

  
263 253
        Point point;
264 254
        for (int i = 0; i < count; i++) {
265
            Surface surface = (Surface) geomManager.create(TYPES.SURFACE, subType);
255
            Polygon polygon = geomManager.createPolygon(subType);
266 256
            parseTypeAndSRID(data);
267 257
            int countRings = data.getInt();
268
            for (int j = 0; j < countRings; j++) {
258
            for (int nring = 0; nring < countRings; nring++) {
259
                OrientablePrimitive ring;
260
                if( nring==0 ) {
261
                    ring = polygon;
262
                } else {
263
                    ring = geomManager.createLine(subType);
264
                }
269 265
                double[][] points = parsePointsAsDoubleArray(data, gHaveZ, gHaveM);
270 266

  
271
                //Add the initial point
272
                point = geomManager.createPoint(points[0][0], points[0][1], subType);
273
                if (gHaveZ) {
274
                    point.setCoordinateAt(Geometry.DIMENSIONS.Z, points[0][2]);
275
                }
276
                surface.addMoveToVertex(point);
277

  
278
                //Add the other points
267
                ring.ensureCapacity(points.length);
279 268
                int lastPoint = points.length - 1;
280
                for (int k = 1; k < lastPoint; k++) {
269
                for (int k = 0; k < lastPoint; k++) {
281 270
                    point = geomManager.createPoint(points[k][0], points[k][1], subType);
282 271
                    if (gHaveZ) {
283 272
                        point.setCoordinateAt(Geometry.DIMENSIONS.Z, points[0][2]);
284 273
                    }
285
                    /*for (int l = 2; l < points[k].length; i++){
286
                     point.setCoordinateAt(l, points[k][l]);
287
                     }*/
288
                    surface.addVertex(point);
274
                    if (gHaveM) {
275
                        point.setCoordinateAt(point.getDimension()-1, points[0][3]);
276
                    }
277
                    ring.addVertex(point);
289 278
                }
290
                surface.closePrimitive();
279
                if( nring!=0 ) {
280
                    polygon.addInteriorRing((Line) ring);
281
                }
291 282
            }
292
            multiSurface.addSurface(surface);
283
            multipolygon.addPrimitive(polygon);
293 284
        }
294
        return multiSurface;
285
        return multipolygon;
295 286
    }
296 287

  
297 288
    private double[][] parsePointsAsDoubleArray(ByteBuffer data, boolean haveZ,
......
343 334
        Geometry[] geoms = new Geometry[count];
344 335
        parseGeometryArray(data, geoms);
345 336
        MultiPrimitive multiPrimitive = (MultiPrimitive) geomManager.create(TYPES.AGGREGATE, SUBTYPES.GEOM2D);
346
        for (int i = 0; i < geoms.length; i++) {
347
            multiPrimitive.addPrimitive((Primitive) geoms[i]);
337
        for (Geometry geom : geoms) {
338
            multiPrimitive.addPrimitive((Primitive) geom);
348 339
        }
349 340
        return multiPrimitive;
350 341
    }
......
360 351
        }
361 352
    }
362 353

  
363
    private Curve parseLineString(ByteBuffer data, boolean haveZ, boolean haveM) throws CreateGeometryException {
354
    private Line parseLineString(ByteBuffer data, boolean haveZ, boolean haveM) throws CreateGeometryException {
364 355
        Point[] points = parsePointArray(data, haveZ, haveM);
365
        Curve curve = (Curve) geomManager.create(TYPES.CURVE, getSubType(haveZ, haveM));
366
        curve.addMoveToVertex(points[0]);
367
        for (int i = 1; i < points.length; i++) {
368
            curve.addVertex(points[i]);
356
        Line line = geomManager.createLine(getSubType(haveZ, haveM));
357
        line.ensureCapacity(points.length);
358
        for (Point point : points) {
359
            line.addVertex(point);
369 360
        }
370
        return curve;
361
        return line;
371 362
    }
372 363

  
373 364
    /**
......
388 379
        return result;
389 380
    }
390 381

  
391
    private Surface parsePolygon(ByteBuffer data, boolean haveZ, boolean haveM) throws CreateGeometryException {
382
    private Polygon parsePolygon(ByteBuffer data, boolean haveZ, boolean haveM) throws CreateGeometryException {
392 383
        int count = data.getInt();
393 384
        int subType = getSubType(haveZ, haveM);
394 385

  
395
        Surface surface = (Surface) geomManager.create(TYPES.SURFACE, subType);
386
        Polygon polygon = geomManager.createPolygon(subType);
387
        fillLinearRing(data, polygon, haveZ, haveM);
396 388

  
397
        for (int i = 0; i < count; i++) {
398
            fillLinearRing(data, surface, haveZ, haveM);
389
        for (int i = 1; i < count; i++) {
390
            Line ring = geomManager.createLine(subType);
391
            fillLinearRing(data, ring, haveZ, haveM);
392
            polygon.addInteriorRing(ring);
399 393
        }
400

  
401
        surface.closePrimitive();
402

  
403
        return surface;
394
        return polygon;
404 395
    }
405 396

  
406 397
    private void fillLinearRing(ByteBuffer data, OrientablePrimitive orientablePrimitive, boolean haveZ, boolean haveM) throws CreateGeometryException {
407 398
        Point[] points = parsePointArray(data, haveZ, haveM);
408

  
409
        orientablePrimitive.addMoveToVertex(points[0]);
399
        
400
        orientablePrimitive.ensureCapacity(points.length);
410 401
        int lastPoint = points.length - 1;
411
        for (int i = 1; i < lastPoint; i++) {
402
        for (int i = 0; i < lastPoint; i++) {
412 403
            orientablePrimitive.addVertex(points[i]);
413 404
        }
414 405
    }
415 406

  
416 407
    private MultiPoint parseMultiPoint(ByteBuffer data) throws CreateGeometryException {
417
        MultiPoint multipoint = (MultiPoint) geomManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
408
        MultiPoint multipoint = geomManager.createMultiPoint(SUBTYPES.GEOM2D);
418 409
        int points = data.getInt();
419 410
        multipoint.ensureCapacity(points);
420 411
        for (int i = 0; i < points; i++) {

Also available in: Unified diff