Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / operations / CalculateEnvelopeOfColumnOperation.java @ 44361

History | View | Annotate | Download (5.37 KB)

1
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2

    
3
import java.sql.Connection;
4
import java.sql.ResultSet;
5
import java.sql.SQLException;
6
import java.sql.Statement;
7
import org.apache.commons.lang3.StringUtils;
8
import org.cresques.cts.IProjection;
9
import org.gvsig.expressionevaluator.ExpressionBuilder;
10
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable;
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
15
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
17
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
18
import org.gvsig.fmap.geom.Geometry;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20

    
21
public class CalculateEnvelopeOfColumnOperation extends AbstractConnectionOperation {
22

    
23
    private final TableReference table;
24
    private final String columnName;
25
    private final String baseFilter;
26
    private final Envelope limit;
27
    private final IProjection crs;
28

    
29
    public CalculateEnvelopeOfColumnOperation(
30
            JDBCHelper helper,
31
            TableReference table,
32
            String columnName,
33
            String baseFilter,
34
            Envelope limit,
35
            IProjection crs
36
    ) {
37
        super(helper);
38
        this.table = table;
39
        this.columnName = columnName;
40
        this.baseFilter = baseFilter;
41
        this.limit = limit;
42
        this.crs = crs;
43
    }
44

    
45
    @Override
46
    public final Object perform(Connection conn) throws DataException {
47
        Envelope env = calculateEnvelopeOfColumn(
48
            conn,
49
            table,
50
            columnName,
51
            baseFilter,
52
            limit,
53
            crs
54
        );
55
        return env;
56
    }
57

    
58
    public Envelope calculateEnvelopeOfColumn(
59
            Connection conn,
60
            TableReference table,
61
            String columnName,
62
            String baseFilter,
63
            Envelope limit,
64
            IProjection crs
65
    ) throws DataException {
66

    
67
        //
68
        // Parece ser que en versiones anteriores a SQL Server 2012 no esta
69
        // disponible la funcion ST_ExtentAggregate.
70
        // Habria que determinar si es necesario implementar una alternativa
71
        // para estos casos.
72
        //
73
        // https://alastaira.wordpress.com/2011/07/26/determining-the-geographic-extent-of-spatial-features-in-a-sql-server-table/
74
        //
75
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
76
        ExpressionBuilder expbuilder = sqlbuilder.expression();
77
        
78
        sqlbuilder.select().column().value(
79
//            expbuilder.ifnull(
80
//                expbuilder.column(columnName), 
81
//                expbuilder.constant(null), 
82
                expbuilder.as_geometry(
83
                    expbuilder.ST_ExtentAggregate(
84
                            expbuilder.column(columnName)
85
                    )
86
                )
87
//            )
88
        );
89
//        sqlbuilder.select().group_by(expbuilder.column(columnName));
90
        sqlbuilder.select().from().table()
91
                .database(this.table.getDatabase())
92
                .schema(this.table.getSchema())
93
                .name(this.table.getTable());
94
        sqlbuilder.select().from().subquery(this.table.getSubquery());
95

    
96
        if (StringUtils.isEmpty(baseFilter)) {
97
            if (limit != null) {
98
                sqlbuilder.select().where().set(
99
                        expbuilder.ST_Intersects(
100
                                expbuilder.ST_Envelope(
101
                                        expbuilder.column(columnName)
102
                                ),
103
                                expbuilder.ST_Envelope(
104
                                        expbuilder.geometry(limit.getGeometry(), crs)
105
                                )
106
                        )
107
                );
108
            }
109
        } else {
110
            sqlbuilder.select().where().set( expbuilder.custom(baseFilter) );
111
            if (limit != null) {
112
                sqlbuilder.select().where().and(
113
                        expbuilder.ST_Intersects(
114
                            expbuilder.ST_Envelope(
115
                                    expbuilder.column(columnName)
116
                            ),
117
                            expbuilder.ST_Envelope(
118
                                    expbuilder.geometry(limit.getGeometry(), crs)
119
                            )
120
                        )
121
                );
122
            }
123
        }
124
        sqlbuilder.select().where().and(        
125
            expbuilder.not_is_null(expbuilder.column(columnName))
126
        );
127
        
128
        sqlbuilder.setProperties(
129
                Variable.class, 
130
                PROP_TABLE, table
131
        );
132
        String sql = sqlbuilder.select().toString();
133

    
134
        Statement st = null;
135
        ResultSet rs = null;
136
        try {
137
            st = conn.createStatement();
138
            rs = JDBCUtils.executeQuery(st, sql);
139
            if (!rs.next()) {
140
                return null;
141
            }
142
            Geometry geom = this.helper.getGeometryFromColumn(rs, 1);
143
            if (geom == null) {
144
                return null;
145
            }
146
            return geom.getEnvelope();
147

    
148
        } catch (SQLException ex) {
149
            throw new JDBCSQLException(ex);
150
        } finally {
151
            JDBCUtils.closeQuietly(st);
152
            JDBCUtils.closeQuietly(rs);
153
        }
154
    }
155

    
156
}