Statistics
| Revision:

root / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostGIS2Geometry.java @ 20920

History | View | Annotate | Download (3.07 KB)

1
package org.gvsig.data.datastores.vectorial.db.jdbc.postgresql;
2

    
3
import java.sql.SQLException;
4

    
5
import org.gvsig.fmap.geom.GeometryFactory;
6
import org.gvsig.fmap.geom.primitive.GeneralPathX;
7
import org.postgis.Geometry;
8
import org.postgis.LineString;
9
import org.postgis.LinearRing;
10
import org.postgis.MultiLineString;
11
import org.postgis.MultiPolygon;
12
import org.postgis.PGgeometry;
13
import org.postgis.Point;
14
import org.postgis.Polygon;
15

    
16
/**
17
 * Codigo recogido (en parte) de la lista de desarrollo de gvSIG
18
 * Enviado por: MAU ingmau00 en gmail.com
19
 * Fecha: Lun Ago 13 15:03:06 CEST 2007
20
 * Asunto: [Gvsig_desarrolladores] Crear una capa nueva
21
 *
22
 * @author jmvivo
23
 *
24
 */
25

    
26
public class PostGIS2Geometry {
27

    
28

    
29
        public static org.gvsig.fmap.geom.Geometry getGeneralPath(PGgeometry geom) throws
30
        SQLException{
31

    
32

    
33

    
34
                GeneralPathX gp = new GeneralPathX();
35
                org.gvsig.fmap.geom.Geometry resultGeom = null;
36
                if (geom.getGeoType() == Geometry.POINT) {
37
                        Point p = (Point) geom.getGeometry();
38
                        resultGeom = GeometryFactory.createPoint2D(p.x, p.y);
39

    
40
                } else if ( geom.getGeoType() == Geometry.LINESTRING) {
41
                        gp = lineString2GP((LineString) geom.getGeometry());
42
                } else if (geom.getGeoType() == Geometry.MULTILINESTRING) {
43
                        MultiLineString mls = (MultiLineString)  geom.getGeometry();
44

    
45
                        for (int j = 0; j < mls.numLines(); j++) {
46
                                gp.append(lineString2GP(mls.getLine(j)), false);
47
                        }
48
                        resultGeom = GeometryFactory.createPolyline2D(gp);
49
                } else if (geom.getGeoType() == Geometry.POLYGON ) {
50
                        gp = polygon2GP((Polygon) geom.getGeometry());
51
                        resultGeom = GeometryFactory.createPolygon2D(gp);
52
                } else if (geom.getGeoType() == Geometry.MULTIPOLYGON) {
53
                        MultiPolygon mp = (MultiPolygon) geom.getGeometry();
54

    
55
                        for (int i = 0; i <  mp.numPolygons(); i++) {
56
                                gp.append(polygon2GP(mp.getPolygon(i)), false);
57
                        }
58
                        resultGeom = GeometryFactory.createPolygon2D(gp);
59
                } else if (geom.getGeoType() == Geometry.GEOMETRYCOLLECTION) {
60
                        //FIXME: Falta Implementar!!!
61
                        throw new RuntimeException("geometryCollections not supported by this driver");
62
                } else {
63
                        throw new RuntimeException("Unknown datatype");
64
                }
65
                return resultGeom;
66
        }
67

    
68
        private static GeneralPathX polygon2GP(Polygon p) {
69
                GeneralPathX gp = new GeneralPathX();
70

    
71
                for (int r = 0; r < p.numRings(); r++) {
72
                        gp.append(linearRing2GP(p.getRing(r)), false);
73
                }
74

    
75
                return gp;
76
        }
77

    
78
        /**
79
         * DOCUMENT ME!
80
         *
81
         * @param lr DOCUMENT ME!
82
         *
83
         * @return DOCUMENT ME!
84
         */
85
        private static GeneralPathX linearRing2GP(LinearRing lr) {
86
                Point p;
87
                GeneralPathX gp = new GeneralPathX();
88

    
89
                if ((p = lr.getPoint(0)) != null) {
90
                        gp.moveTo(p.x, p.y);
91

    
92
                        for (int i = 1; i < lr.numPoints(); i++) {
93
                                p = lr.getPoint(i);
94
                                gp.lineTo(p.x, p.y );
95
                        }
96
                }
97

    
98
                gp.closePath();
99

    
100
                return gp;
101
        }
102

    
103
        /**
104
         * DOCUMENT ME!
105
         *
106
         * @param ls DOCUMENT ME!
107
         *
108
         * @return DOCUMENT ME!
109
         */
110
        private static GeneralPathX lineString2GP(LineString ls) {
111
                Point p;
112
                GeneralPathX gp = new GeneralPathX();
113

    
114
                if ((p = ls.getPoint(0)) != null) {
115
                        gp.moveTo(p.x , p.y);
116

    
117
                        for (int i = 1; i < ls.numPoints(); i++) {
118
                                p = ls.getPoint(i);
119
                                gp.lineTo(p.x, p.y);
120
                        }
121
                }
122

    
123
                return gp;
124
        }
125
}
126