Revision 28678
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/postgresql/PostGIS2Geometry.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.store.postgresql; |
|
2 |
|
|
3 |
import java.sql.SQLException; |
|
4 |
|
|
5 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
6 |
import org.gvsig.fmap.geom.GeometryManager; |
|
7 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
8 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
9 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
|
10 |
import org.postgis.Geometry; |
|
11 |
import org.postgis.LineString; |
|
12 |
import org.postgis.LinearRing; |
|
13 |
import org.postgis.MultiLineString; |
|
14 |
import org.postgis.MultiPolygon; |
|
15 |
import org.postgis.PGgeometry; |
|
16 |
import org.postgis.Point; |
|
17 |
import org.postgis.Polygon; |
|
18 |
|
|
19 |
/** |
|
20 |
* Codigo recogido (en parte) de la lista de desarrollo de gvSIG |
|
21 |
* Enviado por: MAU ingmau00 en gmail.com |
|
22 |
* Fecha: Lun Ago 13 15:03:06 CEST 2007 |
|
23 |
* Asunto: [Gvsig_desarrolladores] Crear una capa nueva |
|
24 |
* |
|
25 |
* @author jmvivo |
|
26 |
* |
|
27 |
*/ |
|
28 |
|
|
29 |
public class PostGIS2Geometry { |
|
30 |
|
|
31 |
|
|
32 |
public org.gvsig.fmap.geom.Geometry getGeometry(PGgeometry geom) |
|
33 |
throws SQLException, CreateGeometryException { |
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
GeometryManager gMan = GeometryLocator.getGeometryManager(); |
|
38 |
|
|
39 |
GeneralPathX gp = new GeneralPathX(); |
|
40 |
org.gvsig.fmap.geom.Geometry resultGeom = null; |
|
41 |
if (geom.getGeoType() == Geometry.POINT) { |
|
42 |
Point p = (Point) geom.getGeometry(); |
|
43 |
resultGeom = gMan.createPoint(p.x, p.y, SUBTYPES.GEOM2D); |
|
44 |
|
|
45 |
} else if ( geom.getGeoType() == Geometry.LINESTRING) { |
|
46 |
gp = lineString2GP((LineString) geom.getGeometry()); |
|
47 |
} else if (geom.getGeoType() == Geometry.MULTILINESTRING) { |
|
48 |
MultiLineString mls = (MultiLineString) geom.getGeometry(); |
|
49 |
|
|
50 |
for (int j = 0; j < mls.numLines(); j++) { |
|
51 |
gp.append(lineString2GP(mls.getLine(j)), false); |
|
52 |
} |
|
53 |
resultGeom = gMan.createCurve(gp, SUBTYPES.GEOM2D); |
|
54 |
} else if (geom.getGeoType() == Geometry.POLYGON ) { |
|
55 |
gp = polygon2GP((Polygon) geom.getGeometry()); |
|
56 |
resultGeom = gMan.createSurface(gp, SUBTYPES.GEOM2D); |
|
57 |
} else if (geom.getGeoType() == Geometry.MULTIPOLYGON) { |
|
58 |
MultiPolygon mp = (MultiPolygon) geom.getGeometry(); |
|
59 |
|
|
60 |
for (int i = 0; i < mp.numPolygons(); i++) { |
|
61 |
gp.append(polygon2GP(mp.getPolygon(i)), false); |
|
62 |
} |
|
63 |
resultGeom = gMan.createSurface(gp, SUBTYPES.GEOM2D); |
|
64 |
} else if (geom.getGeoType() == Geometry.GEOMETRYCOLLECTION) { |
|
65 |
//FIXME: Falta Implementar!!! |
|
66 |
throw new RuntimeException("geometryCollections not supported by this driver"); |
|
67 |
} else { |
|
68 |
throw new RuntimeException("Unknown datatype"); |
|
69 |
} |
|
70 |
return resultGeom; |
|
71 |
} |
|
72 |
|
|
73 |
private static GeneralPathX polygon2GP(Polygon p) { |
|
74 |
GeneralPathX gp = new GeneralPathX(); |
|
75 |
|
|
76 |
for (int r = 0; r < p.numRings(); r++) { |
|
77 |
gp.append(linearRing2GP(p.getRing(r)), false); |
|
78 |
} |
|
79 |
|
|
80 |
return gp; |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* DOCUMENT ME! |
|
85 |
* |
|
86 |
* @param lr DOCUMENT ME! |
|
87 |
* |
|
88 |
* @return DOCUMENT ME! |
|
89 |
*/ |
|
90 |
private static GeneralPathX linearRing2GP(LinearRing lr) { |
|
91 |
Point p; |
|
92 |
GeneralPathX gp = new GeneralPathX(); |
|
93 |
|
|
94 |
if ((p = lr.getPoint(0)) != null) { |
|
95 |
gp.moveTo(p.x, p.y); |
|
96 |
|
|
97 |
for (int i = 1; i < lr.numPoints(); i++) { |
|
98 |
p = lr.getPoint(i); |
|
99 |
gp.lineTo(p.x, p.y ); |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
gp.closePath(); |
|
104 |
|
|
105 |
return gp; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* DOCUMENT ME! |
|
110 |
* |
|
111 |
* @param ls DOCUMENT ME! |
|
112 |
* |
|
113 |
* @return DOCUMENT ME! |
|
114 |
*/ |
|
115 |
private static GeneralPathX lineString2GP(LineString ls) { |
|
116 |
Point p; |
|
117 |
GeneralPathX gp = new GeneralPathX(); |
|
118 |
|
|
119 |
if ((p = ls.getPoint(0)) != null) { |
|
120 |
gp.moveTo(p.x , p.y); |
|
121 |
|
|
122 |
for (int i = 1; i < ls.numPoints(); i++) { |
|
123 |
p = ls.getPoint(i); |
|
124 |
gp.lineTo(p.x, p.y); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
return gp; |
|
129 |
} |
|
130 |
} |
|
131 |
|
branches/v2_0_0_prep/libraries/libFMap_daldb/src/org/gvsig/fmap/dal/store/postgresql/PostgreSQLHelper.java | ||
---|---|---|
76 | 76 |
import org.gvsig.fmap.geom.Geometry; |
77 | 77 |
import org.gvsig.fmap.geom.GeometryLocator; |
78 | 78 |
import org.gvsig.fmap.geom.GeometryManager; |
79 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
80 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
|
81 | 79 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
82 | 80 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext; |
83 | 81 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
84 | 82 |
import org.gvsig.fmap.geom.operation.towkb.ToWKBOperationContext; |
85 | 83 |
import org.gvsig.fmap.geom.primitive.Envelope; |
86 | 84 |
import org.gvsig.tools.exception.BaseException; |
87 |
import org.postgis.PGbox2d; |
|
88 |
import org.postgis.PGbox3d; |
|
89 | 85 |
import org.postgresql.PGResultSetMetaData; |
90 |
import org.postgresql.util.PGobject; |
|
91 | 86 |
import org.slf4j.Logger; |
92 | 87 |
import org.slf4j.LoggerFactory; |
93 | 88 |
|
... | ... | |
550 | 545 |
throws DataException { |
551 | 546 |
|
552 | 547 |
StringBuilder strb = new StringBuilder(); |
553 |
strb.append("Select extent("); |
|
548 |
strb.append("Select asbinary(extent(");
|
|
554 | 549 |
strb.append(geometryAttrName); |
555 |
strb.append(") from "); |
|
550 |
strb.append(")) from ");
|
|
556 | 551 |
|
557 | 552 |
if (storeParams.getSQL() != null |
558 | 553 |
&& storeParams.getSQL().trim().length() == 0) { |
... | ... | |
597 | 592 |
return null; |
598 | 593 |
} |
599 | 594 |
|
600 |
PGobject data = (PGobject) rs.getObject(1);
|
|
595 |
byte[] data = rs.getBytes(1);
|
|
601 | 596 |
if (data == null) { |
602 | 597 |
return null; |
603 | 598 |
} |
604 |
if (data instanceof PGbox3d) { |
|
605 |
PGbox3d box3d = (PGbox3d) data; |
|
606 |
fullEnvelope = geoMan.createEnvelope(SUBTYPES.GEOM3D); |
|
607 |
fullEnvelope.getLowerCorner().setCoordinates(new double[]{ |
|
608 |
box3d.getLLB().x, |
|
609 |
box3d.getLLB().y, box3d.getLLB().z}); |
|
610 |
fullEnvelope.getUpperCorner().setCoordinates(new double[]{box3d.getURT().x, |
|
611 |
box3d |
|
612 |
.getURT().y, |
|
613 |
box3d.getURT().z }); |
|
599 |
initializeFromWKBOperation(); |
|
600 |
fromWKBContext.setData(data); |
|
601 |
Geometry geom = (Geometry) fromWKB.invoke(null, fromWKBContext); |
|
614 | 602 |
|
615 |
} else { |
|
616 |
PGbox2d box2d = (PGbox2d) data; |
|
617 |
fullEnvelope = geoMan.createEnvelope(SUBTYPES.GEOM2D); |
|
618 |
fullEnvelope.getLowerCorner().setX(box2d.getLLB().x); |
|
619 |
fullEnvelope.getLowerCorner().setY(box2d.getLLB().y); |
|
620 |
fullEnvelope.getUpperCorner().setX(box2d.getURT().x); |
|
621 |
fullEnvelope.getUpperCorner().setY(box2d.getURT().y); |
|
622 |
} |
|
603 |
fullEnvelope = geom.getEnvelope(); |
|
604 |
|
|
623 | 605 |
return fullEnvelope; |
624 | 606 |
} catch (java.sql.SQLException e) { |
625 | 607 |
throw new JDBCSQLException(e); |
626 |
} catch (CreateEnvelopeException e) {
|
|
627 |
throw new InitializeException(e);
|
|
608 |
} catch (BaseException e) {
|
|
609 |
throw new ReadException(user.getName(), e);
|
|
628 | 610 |
} finally { |
629 | 611 |
try{ rs.close(); } catch (Exception e){}; |
630 | 612 |
try{ st.close(); } catch (Exception e){}; |
... | ... | |
638 | 620 |
|
639 | 621 |
} |
640 | 622 |
|
641 |
public Geometry getGeometry(byte[] buffer) |
|
642 |
throws BaseException { |
|
643 |
if (buffer == null) { |
|
644 |
return null; |
|
645 |
} |
|
623 |
private void initializeFromWKBOperation() throws BaseException { |
|
646 | 624 |
if (fromWKB == null) { |
647 | 625 |
fromWKB = (FromWKB) GeometryLocator.getGeometryManager() |
648 | 626 |
.getGeometryOperation(FromWKB.CODE, |
... | ... | |
650 | 628 |
fromWKBContext = new FromWKBGeometryOperationContext(); |
651 | 629 |
|
652 | 630 |
} |
631 |
} |
|
632 |
|
|
633 |
public Geometry getGeometry(byte[] buffer) throws BaseException { |
|
634 |
if (buffer == null) { |
|
635 |
return null; |
|
636 |
} |
|
637 |
initializeFromWKBOperation(); |
|
653 | 638 |
Geometry geom; |
654 | 639 |
try { |
655 | 640 |
fromWKBContext.setData(buffer); |
branches/v2_0_0_prep/libraries/libFMap_daldb/pom.xml | ||
---|---|---|
98 | 98 |
<version>gvsig</version> |
99 | 99 |
</dependency> |
100 | 100 |
<dependency> |
101 |
<groupId>org.gvsig.legacy</groupId> |
|
102 |
<artifactId>postgis</artifactId> |
|
103 |
<version>gvsig</version> |
|
104 |
</dependency> |
|
105 |
<dependency> |
|
106 | 101 |
<groupId>postgresql</groupId> |
107 | 102 |
<artifactId>postgresql</artifactId> |
108 | 103 |
<version>8.3-603.jdbc3</version> |
branches/v2_0_0_prep/libraries/libFMap_daldb/.classpath | ||
---|---|---|
30 | 30 |
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/> |
31 | 31 |
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.14/log4j-1.2.14.jar" sourcepath="M2_REPO/log4j/log4j/1.2.14/log4j-1.2.14-sources.jar"/> |
32 | 32 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/org.gvsig.fmap.dal.index.spatial/2.0-SNAPSHOT/org.gvsig.fmap.dal.index.spatial-2.0-SNAPSHOT-jts.jar"/> |
33 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/org.gvsig.fmap.geometry/2.0-SNAPSHOT/org.gvsig.fmap.geometry-2.0-SNAPSHOT.jar" sourcepath="M2_REPO/org/gvsig/org.gvsig.fmap.geometry/2.0-SNAPSHOT/org.gvsig.fmap.geometry-2.0-SNAPSHOT-sources.jar"> |
|
34 |
<attributes> |
|
35 |
<attribute name="javadoc_location" value="jar:file:/home/cordin/.m2/repository/org/gvsig/org.gvsig.fmap.geometry/2.0-SNAPSHOT/org.gvsig.fmap.geometry-2.0-SNAPSHOT-javadoc.jar!/"/> |
|
36 |
</attributes> |
|
37 |
</classpathentry> |
|
38 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/org.gvsig.fmap.geometry/2.0-SNAPSHOT/org.gvsig.fmap.geometry-2.0-SNAPSHOT-impl.jar"/> |
|
39 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/org.gvsig.fmap.geometry/2.0-SNAPSHOT/org.gvsig.fmap.geometry-2.0-SNAPSHOT-operation.jar"/> |
|
40 | 33 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/org.gvsig.i18n/2.0-SNAPSHOT/org.gvsig.i18n-2.0-SNAPSHOT.jar" sourcepath="M2_REPO/org/gvsig/org.gvsig.i18n/2.0-SNAPSHOT/org.gvsig.i18n-2.0-SNAPSHOT-sources.jar"> |
41 | 34 |
<attributes> |
42 | 35 |
<attribute name="javadoc_location" value="jar:file:/home/cordin/.m2/repository/org/gvsig/org.gvsig.i18n/2.0-SNAPSHOT/org.gvsig.i18n-2.0-SNAPSHOT-javadoc.jar!/"/> |
... | ... | |
58 | 51 |
<attribute name="javadoc_location" value="jar:file:/home/cordin/.m2/repository/org/gvsig/org.gvsig.tools/2.0-SNAPSHOT/org.gvsig.tools-2.0-SNAPSHOT-javadoc.jar!/"/> |
59 | 52 |
</attributes> |
60 | 53 |
</classpathentry> |
61 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/legacy/postgis/gvsig/postgis-gvsig.jar"/> |
|
62 | 54 |
<classpathentry kind="var" path="M2_REPO/postgresql/postgresql/8.3-603.jdbc3/postgresql-8.3-603.jdbc3.jar"/> |
63 | 55 |
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.5.5/slf4j-api-1.5.5.jar" sourcepath="M2_REPO/org/slf4j/slf4j-api/1.5.5/slf4j-api-1.5.5-sources.jar"/> |
64 | 56 |
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.5.5/slf4j-log4j12-1.5.5.jar" sourcepath="M2_REPO/org/slf4j/slf4j-log4j12/1.5.5/slf4j-log4j12-1.5.5-sources.jar"/> |
65 | 57 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/legacy/spatialindex/gvsig/spatialindex-gvsig.jar"/> |
66 | 58 |
<classpathentry kind="var" path="M2_REPO/org/gvsig/legacy/vecmath/gvsig/vecmath-gvsig.jar"/> |
67 | 59 |
<classpathentry combineaccessrules="false" kind="src" path="/libFMap_dal"/> |
60 |
<classpathentry combineaccessrules="false" kind="src" path="/libFMap_geometries"/> |
|
68 | 61 |
<classpathentry kind="output" path="target/classes"/> |
69 | 62 |
</classpath> |
branches/v2_0_0_prep/libraries/libFMap_daldb/src-test/org/gvsig/fmap/dal/store/postgresql/TestReadAndWriteGeom.java | ||
---|---|---|
49 | 49 |
import org.gvsig.fmap.geom.operation.towkt.ToWKT; |
50 | 50 |
import org.gvsig.fmap.geom.primitive.impl.Point2D; |
51 | 51 |
import org.gvsig.tools.ToolsLibrary; |
52 |
import org.postgis.PGgeometry; |
|
53 | 52 |
|
54 | 53 |
public class TestReadAndWriteGeom extends TestCase { |
55 | 54 |
private static final int FEATURES_TO_INSERT = 12000; |
... | ... | |
272 | 271 |
} |
273 | 272 |
} |
274 | 273 |
|
275 |
public void testReadPostgis() throws Exception { |
|
276 |
Statement st = null; |
|
277 |
ResultSet rs = null; |
|
278 |
long count = 0; |
|
279 |
Geometry geom; |
|
280 |
PGgeometry pgGeom; |
|
281 |
PostGIS2Geometry converter = new PostGIS2Geometry(); |
|
282 |
try { |
|
283 |
st = conn.createStatement(); |
|
284 |
rs = st.executeQuery(getSQLForRead("the_geom")); |
|
285 |
while (rs.next()) { |
|
286 |
count++; |
|
287 |
pgGeom = (PGgeometry) rs.getObject(1); |
|
288 |
if (pgGeom != null) { |
|
289 |
geom = converter.getGeometry(pgGeom); |
|
290 |
assertNotNull(geom); |
|
291 |
} |
|
292 |
} |
|
274 |
// public void testReadPostgis() throws Exception { |
|
275 |
// Statement st = null; |
|
276 |
// ResultSet rs = null; |
|
277 |
// long count = 0; |
|
278 |
// Geometry geom; |
|
279 |
// PGgeometry pgGeom; |
|
280 |
// PostGIS2Geometry converter = new PostGIS2Geometry(); |
|
281 |
// try { |
|
282 |
// st = conn.createStatement(); |
|
283 |
// rs = st.executeQuery(getSQLForRead("the_geom")); |
|
284 |
// while (rs.next()) { |
|
285 |
// count++; |
|
286 |
// pgGeom = (PGgeometry) rs.getObject(1); |
|
287 |
// if (pgGeom != null) { |
|
288 |
// geom = converter.getGeometry(pgGeom); |
|
289 |
// assertNotNull(geom); |
|
290 |
// } |
|
291 |
// } |
|
292 |
// |
|
293 |
// |
|
294 |
// System.out |
|
295 |
// .println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
296 |
// + count); |
|
297 |
// |
|
298 |
// |
|
299 |
// } finally { |
|
300 |
// if (st != null) { |
|
301 |
// try { |
|
302 |
// st.close(); |
|
303 |
// } catch (SQLException e) { |
|
304 |
// e.printStackTrace(); |
|
305 |
// } |
|
306 |
// } |
|
307 |
// if (rs != null) { |
|
308 |
// try { |
|
309 |
// rs.close(); |
|
310 |
// } catch (SQLException e) { |
|
311 |
// e.printStackTrace(); |
|
312 |
// } |
|
313 |
// } |
|
314 |
// } |
|
315 |
// } |
|
293 | 316 |
|
294 | 317 |
|
295 |
System.out |
|
296 |
.println("TestReadAndWriteGeom.testReadPostgis() Count = " |
|
297 |
+ count); |
|
298 |
|
|
299 |
|
|
300 |
} finally { |
|
301 |
if (st != null) { |
|
302 |
try { |
|
303 |
st.close(); |
|
304 |
} catch (SQLException e) { |
|
305 |
e.printStackTrace(); |
|
306 |
} |
|
307 |
} |
|
308 |
if (rs != null) { |
|
309 |
try { |
|
310 |
rs.close(); |
|
311 |
} catch (SQLException e) { |
|
312 |
e.printStackTrace(); |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 |
|
|
319 | 318 |
protected void tearDown() throws Exception { |
320 | 319 |
super.tearDown(); |
321 | 320 |
conn.close(); |
Also available in: Unified diff