svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / postgresql / PostGIS2Geometry.java @ 21069
History | View | Annotate | Download (3.17 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.GeometryManager; |
7 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
8 |
import org.postgis.Geometry; |
9 |
import org.postgis.LineString; |
10 |
import org.postgis.LinearRing; |
11 |
import org.postgis.MultiLineString; |
12 |
import org.postgis.MultiPolygon; |
13 |
import org.postgis.PGgeometry; |
14 |
import org.postgis.Point; |
15 |
import org.postgis.Polygon; |
16 |
|
17 |
/**
|
18 |
* Codigo recogido (en parte) de la lista de desarrollo de gvSIG
|
19 |
* Enviado por: MAU ingmau00 en gmail.com
|
20 |
* Fecha: Lun Ago 13 15:03:06 CEST 2007
|
21 |
* Asunto: [Gvsig_desarrolladores] Crear una capa nueva
|
22 |
*
|
23 |
* @author jmvivo
|
24 |
*
|
25 |
*/
|
26 |
|
27 |
public class PostGIS2Geometry { |
28 |
|
29 |
|
30 |
public static org.gvsig.fmap.geom.Geometry getGeneralPath(PGgeometry geom) throws |
31 |
SQLException{
|
32 |
|
33 |
|
34 |
GeometryFactory gFactory = GeometryManager.getInstance().getGeometryFactory(); |
35 |
|
36 |
GeneralPathX gp = new GeneralPathX();
|
37 |
org.gvsig.fmap.geom.Geometry resultGeom = null;
|
38 |
if (geom.getGeoType() == Geometry.POINT) {
|
39 |
Point p = (Point) geom.getGeometry(); |
40 |
resultGeom = gFactory.createPoint2D(p.x, p.y); |
41 |
|
42 |
} else if ( geom.getGeoType() == Geometry.LINESTRING) { |
43 |
gp = lineString2GP((LineString) geom.getGeometry()); |
44 |
} else if (geom.getGeoType() == Geometry.MULTILINESTRING) { |
45 |
MultiLineString mls = (MultiLineString) geom.getGeometry(); |
46 |
|
47 |
for (int j = 0; j < mls.numLines(); j++) { |
48 |
gp.append(lineString2GP(mls.getLine(j)), false);
|
49 |
} |
50 |
resultGeom = gFactory.createPolyline2D(gp); |
51 |
} else if (geom.getGeoType() == Geometry.POLYGON ) { |
52 |
gp = polygon2GP((Polygon) geom.getGeometry());
|
53 |
resultGeom = gFactory.createPolygon2D(gp); |
54 |
} else if (geom.getGeoType() == Geometry.MULTIPOLYGON) { |
55 |
MultiPolygon mp = (MultiPolygon) geom.getGeometry(); |
56 |
|
57 |
for (int i = 0; i < mp.numPolygons(); i++) { |
58 |
gp.append(polygon2GP(mp.getPolygon(i)), false);
|
59 |
} |
60 |
resultGeom = gFactory.createPolygon2D(gp); |
61 |
} else if (geom.getGeoType() == Geometry.GEOMETRYCOLLECTION) { |
62 |
//FIXME: Falta Implementar!!!
|
63 |
throw new RuntimeException("geometryCollections not supported by this driver"); |
64 |
} else {
|
65 |
throw new RuntimeException("Unknown datatype"); |
66 |
} |
67 |
return resultGeom;
|
68 |
} |
69 |
|
70 |
private static GeneralPathX polygon2GP(Polygon p) { |
71 |
GeneralPathX gp = new GeneralPathX();
|
72 |
|
73 |
for (int r = 0; r < p.numRings(); r++) { |
74 |
gp.append(linearRing2GP(p.getRing(r)), false);
|
75 |
} |
76 |
|
77 |
return gp;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* DOCUMENT ME!
|
82 |
*
|
83 |
* @param lr DOCUMENT ME!
|
84 |
*
|
85 |
* @return DOCUMENT ME!
|
86 |
*/
|
87 |
private static GeneralPathX linearRing2GP(LinearRing lr) { |
88 |
Point p;
|
89 |
GeneralPathX gp = new GeneralPathX();
|
90 |
|
91 |
if ((p = lr.getPoint(0)) != null) { |
92 |
gp.moveTo(p.x, p.y); |
93 |
|
94 |
for (int i = 1; i < lr.numPoints(); i++) { |
95 |
p = lr.getPoint(i); |
96 |
gp.lineTo(p.x, p.y ); |
97 |
} |
98 |
} |
99 |
|
100 |
gp.closePath(); |
101 |
|
102 |
return gp;
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* DOCUMENT ME!
|
107 |
*
|
108 |
* @param ls DOCUMENT ME!
|
109 |
*
|
110 |
* @return DOCUMENT ME!
|
111 |
*/
|
112 |
private static GeneralPathX lineString2GP(LineString ls) { |
113 |
Point p;
|
114 |
GeneralPathX gp = new GeneralPathX();
|
115 |
|
116 |
if ((p = ls.getPoint(0)) != null) { |
117 |
gp.moveTo(p.x , p.y); |
118 |
|
119 |
for (int i = 1; i < ls.numPoints(); i++) { |
120 |
p = ls.getPoint(i); |
121 |
gp.lineTo(p.x, p.y); |
122 |
} |
123 |
} |
124 |
|
125 |
return gp;
|
126 |
} |
127 |
} |
128 |
|