forked from geodmms/xdgnjobs

?? ?
2012-11-23 3052e5d870a90ee5f3ea3d3dff887436d8ccdfea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.ximple.io.dgn7;
 
import org.apache.log4j.Logger;
 
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
 
/**
 * ShapeElement
 *
 * @author Ulysses
 * @version 0.1
 * @since 2006/5/18 �U�� 03:08:43
 */
public class ShapeElement extends LineStringElement implements GeometryConverter {
    private static final Logger logger = Logger.getLogger(ShapeElement.class);
 
    ShapeElement(byte[] raw) {
        super(raw);
    }
 
    public Coordinate[] getVertices() {
        Coordinate[] result = super.getVertices();
        if ((result != null) && (result.length > 2)) {
            boolean isClosed = result[0].equals2D(result[result.length - 1]);
            if (!isClosed) {
                double distance = result[0].distance(result[result.length - 1]);
                if (distance < 0.00210) {
                    result[result.length - 1] = new Coordinate(result[0]);
                } else {
                    logger.info("Shape is not closed. distance=" + distance);
                    logger.debug("result[0]=" + result[0].toString());
                    logger.debug("result[length - 1]=" + result[result.length - 1]);
                }
            }
        }
 
        return result;
    }
 
    public Geometry toGeometry(GeometryFactory factory) {
        try {
            LinearRing ring = factory.createLinearRing(this.getVertices());
            return factory.createPolygon(ring, null);
        } catch (IllegalArgumentException e) {
            logger.warn(e.getMessage(), e);
            return null;
        }
    }
 
    public static class ElementHandler extends Element.ElementHandler {
        private static ElementHandler instance = null;
 
        public ElementHandler() {
            super(ElementType.SHAPE);
        }
 
        public static IElementHandler getInstance() {
            if (instance == null) {
                instance = new ElementHandler();
            }
 
            return instance;
        }
 
        protected Element createElement(byte[] raw) {
            return new ShapeElement(raw);
        }
    }
}