01: package net.refractions.udig.tool.info.internal;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import net.refractions.udig.project.AdaptableFeature;
07: import net.refractions.udig.project.ILayer;
08: import net.refractions.udig.tool.info.LayerPointInfo;
09:
10: import org.eclipse.core.runtime.IProgressMonitor;
11: import org.geotools.data.FeatureSource;
12: import org.geotools.feature.Feature;
13: import org.geotools.feature.FeatureCollection;
14: import org.geotools.feature.FeatureIterator;
15: import org.geotools.feature.FeatureType;
16: import org.geotools.filter.AttributeExpression;
17: import org.geotools.filter.BBoxExpression;
18: import org.geotools.filter.FilterFactory;
19: import org.geotools.filter.FilterFactoryFinder;
20: import org.geotools.filter.GeometryFilter;
21: import org.geotools.geometry.jts.ReferencedEnvelope;
22: import org.opengis.referencing.crs.CoordinateReferenceSystem;
23:
24: public class DataStoreDescribeLayer {
25:
26: /**
27: * @see net.refractions.udig.project.internal.render.impl.RendererImpl#getInfo(java.awt.Point)
28: */
29: public static List<LayerPointInfo> info(final ILayer layer,
30: ReferencedEnvelope bbox, IProgressMonitor monitor)
31: throws Exception {
32: List<Feature> features = info2(layer, bbox, monitor);
33: List<LayerPointInfo> list = new ArrayList<LayerPointInfo>(
34: features.size());
35: for (Feature feature : features) {
36: final Feature feature2 = feature;
37: LayerPointInfo info = new LayerPointInfo(layer) {
38: public String getMimeType() {
39: return LayerPointInfo.GML;
40: }
41:
42: public Object acquireValue() {
43: return feature2;
44: }
45: };
46: list.add(info);
47: }
48: return list;
49: }
50:
51: public static List<Feature> info2(ILayer layer,
52: ReferencedEnvelope bbox, IProgressMonitor monitor)
53: throws Exception {
54: FeatureSource source = layer.getResource(FeatureSource.class,
55: null);
56: FeatureType type = source.getSchema();
57: CoordinateReferenceSystem crs = layer.getCRS();
58:
59: if (!bbox.getCoordinateReferenceSystem().equals(crs)) {
60: bbox = bbox.transform(crs, true);
61: }
62: FilterFactory factory = FilterFactoryFinder
63: .createFilterFactory();
64:
65: BBoxExpression theBBox = factory.createBBoxExpression(bbox);
66: AttributeExpression theGeom = factory
67: .createAttributeExpression(type.getDefaultGeometry()
68: .getName());
69:
70: GeometryFilter filter = factory
71: .createGeometryFilter(GeometryFilter.GEOMETRY_INTERSECTS);
72: filter.addLeftGeometry(theGeom);
73: filter.addRightGeometry(theBBox);
74:
75: layer.getQuery(false);
76: final FeatureCollection results = source.getFeatures(filter);
77: // if( results.getCount() == 0 ) {
78: // return null; // no content!
79: // }
80:
81: List<Feature> list = new ArrayList<Feature>();
82: FeatureIterator reader = results.features();
83: try {
84: while (reader.hasNext()) {
85: if (monitor != null && monitor.isCanceled())
86: return list;
87: list.add(new AdaptableFeature(reader.next(), layer));
88: }
89: } finally {
90: reader.close();
91: }
92: return list;
93: }
94: }
|