| Line 1... |
Line 1... |
| 1 |
/*
|
1 |
/*
|
| 2 |
* $Id: PolygonEncoder.java 155 2010-01-13 21:00:35Z maesenka $
|
2 |
* $Id: PolygonEncoder.java 156 2010-01-28 22:59:30Z maesenka $
|
| 3 |
*
|
3 |
*
|
| 4 |
* This file is part of Hibernate Spatial, an extension to the
|
4 |
* This file is part of Hibernate Spatial, an extension to the
|
| 5 |
* hibernate ORM solution for geographic data.
|
5 |
* hibernate ORM solution for geographic data.
|
| 6 |
*
|
6 |
*
|
| 7 |
* Copyright © 2009 Geovise BVBA
|
7 |
* Copyright © 2009 Geovise BVBA
|
| Line 23... |
Line 23... |
| 23 |
* For more information, visit: http://www.hibernatespatial.org/
|
23 |
* For more information, visit: http://www.hibernatespatial.org/
|
| 24 |
*/
|
24 |
*/
|
| 25 |
|
25 |
|
| 26 |
package org.hibernatespatial.sqlserver.convertors;
|
26 |
package org.hibernatespatial.sqlserver.convertors;
|
| 27 |
|
27 |
|
| - |
|
28 |
import com.vividsolutions.jts.geom.Coordinate;
|
| 28 |
import com.vividsolutions.jts.geom.Geometry;
|
29 |
import com.vividsolutions.jts.geom.Geometry;
|
| 29 |
import com.vividsolutions.jts.geom.LineString;
|
30 |
import com.vividsolutions.jts.geom.LineString;
|
| 30 |
import com.vividsolutions.jts.geom.Polygon;
|
31 |
import com.vividsolutions.jts.geom.Polygon;
|
| 31 |
|
32 |
|
| - |
|
33 |
import java.util.List;
|
| - |
|
34 |
|
| 32 |
public class PolygonEncoder extends AbstractEncoder<Polygon> {
|
35 |
public class PolygonEncoder extends AbstractEncoder<Polygon> {
|
| 33 |
|
36 |
|
| 34 |
private static Shape SHAPE = new Shape(-1, 0, OpenGisType.POLYGON);
|
37 |
private static Shape SHAPE = new Shape(-1, 0, OpenGisType.POLYGON);
|
| 35 |
|
38 |
|
| 36 |
public boolean accepts(Geometry geom) {
|
39 |
public boolean accepts(Geometry geom) {
|
| 37 |
return geom instanceof Polygon;
|
40 |
return geom instanceof Polygon;
|
| 38 |
}
|
41 |
}
|
| 39 |
|
42 |
|
| - |
|
43 |
@Override
|
| - |
|
44 |
protected void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
|
| 40 |
protected void encodeFigures(SqlGeometryV1 nativeGeom, Polygon geom) {
|
45 |
if (! (geom instanceof Polygon)) throw new IllegalArgumentException("Polygon geometry expected.");
|
| - |
|
46 |
Polygon polygon = (Polygon)geom;
|
| 41 |
nativeGeom.setNumberOfFigures(geom.getNumInteriorRing() + 1);
|
47 |
int figureOffset = figures.size();
|
| - |
|
48 |
shapes.add( new Shape(parentShapeIndex, figureOffset, OpenGisType.POLYGON));
|
| - |
|
49 |
|
| 42 |
int pointOffset = 0;
|
50 |
int pointOffset = coordinates.size();
|
| 43 |
int figure = 0;
|
51 |
addExteriorRing(polygon, coordinates, figures);
|
| 44 |
addFiguresForPolygon(nativeGeom, geom, pointOffset, figure);
|
52 |
addInteriorRings(polygon, coordinates, figures);
|
| - |
|
53 |
|
| 45 |
}
|
54 |
}
|
| 46 |
|
55 |
|
| - |
|
56 |
|
| 47 |
protected void addFiguresForPolygon(SqlGeometryV1 nativeGeom, Polygon geom, int pointOffset, int figure) {
|
57 |
private void addInteriorRings(Polygon geom, List<Coordinate> coordinates, List<Figure> figures){
|
| 48 |
pointOffset = addExteriorRing(nativeGeom, geom, pointOffset, figure);
|
- |
|
| 49 |
for (int ring = 0; ring < geom.getNumInteriorRing(); ring++) {
|
58 |
for (int idx = 0; idx < geom.getNumInteriorRing(); idx++){
|
| 50 |
pointOffset = addInteriorRing(nativeGeom, geom, ring, pointOffset, ++figure);
|
59 |
addInteriorRing(geom.getInteriorRingN(idx), coordinates, figures);
|
| 51 |
}
|
60 |
}
|
| 52 |
}
|
61 |
}
|
| 53 |
|
62 |
|
| 54 |
private int addInteriorRing(SqlGeometryV1 nativeGeom, Polygon geom, int ring, int pointOffset, int numFigure) {
|
63 |
private void addInteriorRing(LineString ring, List<Coordinate> coordinates, List<Figure> figures) {
|
| 55 |
LineString ls = geom.getInteriorRingN(ring);
|
64 |
int pointOffset = coordinates.size();
|
| - |
|
65 |
addPoints(ring, coordinates);
|
| 56 |
Figure figure = new Figure(FigureAttribute.InteriorRing, pointOffset);
|
66 |
Figure figure = new Figure(FigureAttribute.InteriorRing, pointOffset);
|
| 57 |
nativeGeom.setFigure(numFigure, figure);
|
- |
|
| 58 |
pointOffset += ls.getNumPoints();
|
- |
|
| 59 |
return pointOffset;
|
67 |
figures.add(figure);
|
| - |
|
68 |
|
| 60 |
}
|
69 |
}
|
| 61 |
|
70 |
|
| - |
|
71 |
private void addPoints(LineString ring, List<Coordinate> coordinates) {
|
| - |
|
72 |
for (Coordinate c : ring.getCoordinates()){
|
| - |
|
73 |
coordinates.add(c);
|
| - |
|
74 |
}
|
| - |
|
75 |
}
|
| - |
|
76 |
|
| 62 |
private int addExteriorRing(SqlGeometryV1 nativeGeom, Polygon geom, int offset, int numFigure) {
|
77 |
private void addExteriorRing(Polygon geom, List<Coordinate> coordinates, List<Figure> figures) {
|
| 63 |
LineString shell = geom.getExteriorRing();
|
78 |
LineString shell = geom.getExteriorRing();
|
| - |
|
79 |
int offset = coordinates.size();
|
| - |
|
80 |
addPoints(shell, coordinates);
|
| 64 |
Figure exterior = new Figure(FigureAttribute.ExteriorRing, offset);
|
81 |
Figure exterior = new Figure(FigureAttribute.ExteriorRing, offset);
|
| 65 |
nativeGeom.setFigure(numFigure, exterior);
|
- |
|
| 66 |
offset += shell.getNumPoints();
|
- |
|
| 67 |
return offset;
|
82 |
figures.add(exterior);
|
| 68 |
}
|
83 |
}
|
| 69 |
|
84 |
|
| 70 |
protected void encodeShapes(SqlGeometryV1 nativeGeom, Polygon geom) {
|
- |
|
| 71 |
nativeGeom.setNumberOfShapes(1);
|
- |
|
| 72 |
nativeGeom.setShape(0, SHAPE);
|
- |
|
| 73 |
}
|
85 |
|
| 74 |
}
|
86 |
}
|