public ZoomSelection() {
}
public void run() {
ILayer layer = getContext().getSelectedLayer();
if (layer.hasResource(SimpleFeatureSource.class)) {
try {
SimpleFeatureSource resource = featureSource(layer);
Query query = layer.getQuery(true);
ReferencedEnvelope bounds = resource.getBounds(query);
if (bounds == null) {
FeatureCollection<SimpleFeatureType, SimpleFeature> featureResult = resource.getFeatures(query);
if (featureResult != null && !featureResult.isEmpty()) {
ReferencedEnvelope envelope = featureResult.getBounds();
if (envelope != null) {
bounds = new ReferencedEnvelope(envelope, layer.getCRS());
}
}
}
if (bounds != null) {
// If the selection is a single point the bounds will
// have height == 0 and width == 0. This will break
// in ScaleUtils:306. Adding 1 to the extent fixes the problem:
if (bounds.getHeight() <= 0 || bounds.getWidth() <= 0) {
bounds.expandBy(1);
}
bounds = ScaleUtils.fitToMinAndMax(bounds, layer);
getContext().sendASyncCommand(new SetViewportBBoxCommand(bounds, layer.getCRS()));
}
} catch (IOException e) {
SelectPlugin.log("failed to obtain resource", e); //$NON-NLS-1$
}