Subversion Repositories hibernate-spatial

Rev

Rev 155 | Rev 162 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 maesenka 1
/*
156 maesenka 2
 * $Id: PolygonEncoder.java 156 2010-01-28 22:59:30Z maesenka $
149 maesenka 3
 *
4
 * This file is part of Hibernate Spatial, an extension to the
5
 * hibernate ORM solution for geographic data.
6
 *
7
 * Copyright © 2009 Geovise BVBA
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 *
23
 * For more information, visit: http://www.hibernatespatial.org/
24
 */
25
 
26
package org.hibernatespatial.sqlserver.convertors;
27
 
156 maesenka 28
import com.vividsolutions.jts.geom.Coordinate;
149 maesenka 29
import com.vividsolutions.jts.geom.Geometry;
30
import com.vividsolutions.jts.geom.LineString;
31
import com.vividsolutions.jts.geom.Polygon;
32
 
156 maesenka 33
import java.util.List;
34
 
149 maesenka 35
public class PolygonEncoder extends AbstractEncoder<Polygon> {
36
 
37
    private static Shape SHAPE = new Shape(-1, 0, OpenGisType.POLYGON);
38
 
39
    public boolean accepts(Geometry geom) {
40
        return geom instanceof Polygon;
41
    }
42
 
156 maesenka 43
    @Override
44
    protected void encode(Geometry geom, int parentShapeIndex, List<Coordinate> coordinates, List<Figure> figures, List<Shape> shapes) {
45
        if (! (geom instanceof Polygon)) throw new IllegalArgumentException("Polygon geometry expected.");
46
        Polygon polygon = (Polygon)geom;
47
        int figureOffset = figures.size();
48
        shapes.add( new Shape(parentShapeIndex, figureOffset, OpenGisType.POLYGON));
49
 
50
        int pointOffset = coordinates.size();
51
        addExteriorRing(polygon, coordinates, figures);
52
        addInteriorRings(polygon, coordinates, figures);
53
 
150 maesenka 54
    }
55
 
156 maesenka 56
 
57
    private void addInteriorRings(Polygon geom, List<Coordinate> coordinates, List<Figure> figures){
58
        for (int idx = 0; idx < geom.getNumInteriorRing(); idx++){
59
            addInteriorRing(geom.getInteriorRingN(idx), coordinates, figures);
149 maesenka 60
        }
61
    }
62
 
156 maesenka 63
    private void addInteriorRing(LineString ring, List<Coordinate> coordinates, List<Figure> figures) {
64
        int pointOffset = coordinates.size();
65
        addPoints(ring, coordinates);
150 maesenka 66
        Figure figure = new Figure(FigureAttribute.InteriorRing, pointOffset);
156 maesenka 67
        figures.add(figure);
68
 
149 maesenka 69
    }
70
 
156 maesenka 71
    private void addPoints(LineString ring, List<Coordinate> coordinates) {
72
        for (Coordinate c : ring.getCoordinates()){
73
            coordinates.add(c);
74
        }
75
    }
76
 
77
    private void addExteriorRing(Polygon geom, List<Coordinate> coordinates, List<Figure> figures) {
149 maesenka 78
        LineString shell = geom.getExteriorRing();
156 maesenka 79
        int offset = coordinates.size();
80
        addPoints(shell, coordinates);
149 maesenka 81
        Figure exterior = new Figure(FigureAttribute.ExteriorRing, offset);
156 maesenka 82
        figures.add(exterior);
149 maesenka 83
    }
84
 
156 maesenka 85
 
149 maesenka 86
}