svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / expressionbuilderformatter / ST_Intersects.java @ 47579
History | View | Annotate | Download (5.42 KB)
1 |
package org.gvsig.sqlite.dal.expressionbuilderformatter; |
---|---|
2 |
|
3 |
import java.text.MessageFormat; |
4 |
import java.util.List; |
5 |
import org.apache.commons.lang3.ArrayUtils; |
6 |
import org.apache.commons.lang3.StringUtils; |
7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
9 |
import static org.gvsig.expressionevaluator.GeometryExpressionBuilder.FUNCTION_ST_INTERSECTS; |
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
12 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
13 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
14 |
import org.gvsig.expressionevaluator.Formatter; |
15 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
16 |
import org.gvsig.fmap.dal.SQLBuilder; |
17 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
18 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_QUERY; |
19 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLENAME; |
20 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
22 |
import org.gvsig.fmap.dal.feature.FeatureType; |
23 |
import org.gvsig.fmap.geom.Geometry; |
24 |
import org.gvsig.sqlite.dal.geopackage.GeopackageUtils; |
25 |
import org.gvsig.sqlite.dal.geopackage.index.GeopackageIndex; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author jjdelcerro
|
30 |
*/
|
31 |
public class ST_Intersects implements Formatter<Value> { |
32 |
|
33 |
private final SQLBuilder sqlbuilder; |
34 |
private final Formatter<Value> formatter; |
35 |
|
36 |
public ST_Intersects(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
37 |
this.sqlbuilder = sqlbuilder;
|
38 |
this.formatter = formatter;
|
39 |
} |
40 |
@Override
|
41 |
public boolean canApply(ExpressionBuilder.Value value) { |
42 |
if (value instanceof ExpressionBuilder.Function) { |
43 |
return StringUtils.equalsIgnoreCase(FUNCTION_ST_INTERSECTS, ((Function) value).name());
|
44 |
} |
45 |
return false; |
46 |
} |
47 |
|
48 |
@Override
|
49 |
public String format(Value function) { |
50 |
List<Value> parameters = ((Function) function).parameters();
|
51 |
Value p1 = parameters.get(0);
|
52 |
Value p2 = parameters.get(1);
|
53 |
String p1s = p1.toString(formatter);
|
54 |
String p2s = p2.toString(formatter);
|
55 |
|
56 |
String filter = getBBoxIntersectsFilter(formatter, p1, p2);
|
57 |
if (filter == null ) { |
58 |
filter = getBBoxIntersectsFilter(formatter, p2, p1); |
59 |
} |
60 |
String r;
|
61 |
if( filter == null ) { |
62 |
r = MessageFormat.format("ST_Intersects(({0}),({1}))", p1s, p2s); |
63 |
} else {
|
64 |
r = MessageFormat.format("( ({2}) AND ST_Intersects(({0}),({1}) ))", p1s, p2s, filter); |
65 |
// r = MessageFormat.format("({0})", filter);
|
66 |
} |
67 |
return r;
|
68 |
} |
69 |
|
70 |
/*friend*/ static String getBBoxIntersectsFilter(Formatter<Value> formatter, Value column, Value expression) { |
71 |
if( !(column instanceof ExpressionBuilder.Variable) ) { |
72 |
return null; |
73 |
} |
74 |
String tableName = (String) column.getProperty(PROP_TABLENAME); |
75 |
if( StringUtils.isBlank(tableName) ) {
|
76 |
return null; |
77 |
} |
78 |
FeatureType featureType = (FeatureType) column.getProperty(PROP_FEATURE_TYPE); |
79 |
if( featureType==null ) { |
80 |
return null; |
81 |
} |
82 |
FeatureAttributeDescriptor[] attrpk = featureType.getPrimaryKey();
|
83 |
if( ArrayUtils.isEmpty(attrpk) || attrpk.length!=1 ) { |
84 |
return null; |
85 |
} |
86 |
FeatureAttributeDescriptor attrid = attrpk[0];
|
87 |
|
88 |
ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) column; |
89 |
FeatureAttributeDescriptor attrgeom = featureType.getAttributeDescriptor(variable.name()); |
90 |
if (attrgeom == null) { |
91 |
FeatureQuery query = (FeatureQuery) column.getProperty(PROP_QUERY); |
92 |
if( query == null ) { |
93 |
return null; |
94 |
} |
95 |
attrgeom = query.getExtraColumn().get(variable.name()); |
96 |
if (attrgeom == null) { |
97 |
return null; |
98 |
} |
99 |
} |
100 |
|
101 |
GeopackageIndex index = GeopackageUtils.getIndexManager().getGeometryIndex(attrgeom); |
102 |
if( index == null ) { |
103 |
return null; |
104 |
} |
105 |
Code geomcode; |
106 |
ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager(); |
107 |
try {
|
108 |
geomcode = expressionManager.compile(expression.toString()); |
109 |
MutableSymbolTable symbolTable = expressionManager.createSymbolTable(); |
110 |
// Le pasamos el optimize para que resuelva las expresiones constantes
|
111 |
// a ver si es una geometria.
|
112 |
geomcode = expressionManager.optimize(symbolTable, geomcode); |
113 |
} catch(Exception ex) { |
114 |
return null; |
115 |
} |
116 |
if( geomcode.code() == Code.CONSTANT ) {
|
117 |
Object go = ((Code.Constant)geomcode).value();
|
118 |
if( go instanceof Geometry ) { |
119 |
String filter = index.getBBoxIntersectsFilter(tableName, attrid.getName(), attrgeom.getName(), (Geometry)go);
|
120 |
return filter;
|
121 |
} |
122 |
} |
123 |
String filter = index.getBBoxIntersectsFilter(
|
124 |
tableName, |
125 |
attrid.getName(), |
126 |
attrgeom.getName(), |
127 |
"ST_MinX("+expression.toString(formatter)+")", |
128 |
"ST_MinY("+expression.toString(formatter)+")", |
129 |
"ST_MaxX("+expression.toString(formatter)+")", |
130 |
"ST_MaxY("+expression.toString(formatter)+")" |
131 |
); |
132 |
return filter;
|
133 |
} |
134 |
|
135 |
} |