/** * gvSIG. Desktop Geographic Information System. * * Copyright (C) 2007-2021 gvSIG Association. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * For any additional information, do not hesitate to contact us * at info AT gvsig.com, or visit our website www.gvsig.com. */ package org.gvsig.topology.rule; import org.gvsig.expressionevaluator.Expression; import org.gvsig.expressionevaluator.ExpressionUtils; import org.gvsig.expressionevaluator.GeometryExpressionBuilder; import org.gvsig.expressionevaluator.GeometryExpressionUtils; import org.gvsig.fmap.dal.feature.EditableFeature; import org.gvsig.fmap.dal.feature.Feature; import org.gvsig.fmap.dal.feature.FeatureReference; import org.gvsig.fmap.dal.feature.FeatureSet; import org.gvsig.fmap.dal.feature.FeatureStore; import org.gvsig.fmap.geom.Geometry; import org.gvsig.tools.ToolsLocator; import org.gvsig.tools.dynobject.DynObject; import org.gvsig.tools.i18n.I18nManager; import org.gvsig.tools.task.SimpleTaskStatus; import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException; import org.gvsig.topology.lib.api.TopologyDataSet; import org.gvsig.topology.lib.api.TopologyReport; import org.gvsig.topology.lib.api.TopologyReportLine; import org.gvsig.topology.lib.api.TopologyRule; import org.gvsig.topology.lib.api.TopologyRuleFactory; import org.gvsig.topology.lib.spi.AbstractTopologyRule; import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction; /** * * @author jjdelcerro */ @SuppressWarnings("UseSpecificCatch") public class PolygonMustBeCoveredByPolygonRule extends AbstractTopologyRule { private class CreateFetureAction extends AbstractTopologyRuleAction { public CreateFetureAction() { super( PolygonContainsPointRuleFactory.NAME, "CreateFeature", "Create Feature", "The Create Feature fix creates a new polygon feature out of the portion of overlap from the existing polygon so the boundary of each feature from both feature classes is the same. This fix can be applied to one or more selected Must Be Covered By errors." ); } @Override public int execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) { try { Geometry polygon = line.getGeometry(); TopologyDataSet dataSet = rule.getDataSet2(); EditableFeature feature = dataSet.createNewFeature(); feature.setDefaultGeometry(polygon); dataSet.insert(feature); return EXECUTE_OK; } catch (Exception ex) { throw new ExecuteTopologyRuleActionException(ex); } } } private String geomName; private Expression expression = null; private GeometryExpressionBuilder expressionBuilder = null; public PolygonMustBeCoveredByPolygonRule() { // for persistence only } public PolygonMustBeCoveredByPolygonRule( TopologyRuleFactory factory, double tolerance, String dataSet1, String dataSet2 ) { super(factory, tolerance, dataSet1, dataSet2); addAction(new CreateFetureAction()); } @Override protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception { FeatureSet set = null; try { FeatureStore store2 = this.getDataSet2().getFeatureStore(); if (this.expression == null) { this.expression = ExpressionUtils.createExpression(); this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder(); this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName(); } Geometry polygon = feature1.getDefaultGeometry(); TopologyDataSet theDataSet2 = this.getDataSet2(); double theTolerance = getTolerance(); if (theDataSet2.getSpatialIndex() != null) { boolean contained = false; for (FeatureReference featureReference : theDataSet2.query(polygon)) { Feature feature2 = featureReference.getFeature(); Geometry otherPolygon = feature2.getDefaultGeometry(); if( otherPolygon!=null && otherPolygon.buffer(theTolerance).contains(polygon) ) { contained = true; break; } } if( !contained ) { I18nManager i18n = ToolsLocator.getI18nManager(); report.addLine(this, this.getDataSet1(), this.getDataSet2(), polygon, polygon, feature1.getReference(), null, false, i18n.getTranslation("_Polygon_is_not_covered_by_any_other_polygon") ); } } else { if(theTolerance > 0){ this.expression.setPhrase( this.expressionBuilder.ifnull( this.expressionBuilder.column(this.geomName), this.expressionBuilder.constant(false), this.expressionBuilder.ST_Contains( this.expressionBuilder.ST_Buffer( this.expressionBuilder.column(this.geomName), this.expressionBuilder.constant(theTolerance) ), this.expressionBuilder.geometry(polygon) ) ).toString() ); } else { this.expression.setPhrase( this.expressionBuilder.ifnull( this.expressionBuilder.column(this.geomName), this.expressionBuilder.constant(false), this.expressionBuilder.ST_Contains( this.expressionBuilder.column(this.geomName), this.expressionBuilder.geometry(polygon) ) ).toString() ); } if (theDataSet2.findFirst(this.expression) == null) { I18nManager i18n = ToolsLocator.getI18nManager(); report.addLine(this, this.getDataSet1(), this.getDataSet2(), polygon, polygon, feature1.getReference(), null, false, i18n.getTranslation("_Polygon_is_not_covered_by_any_other_polygon") ); } } } catch (Exception ex) { LOGGER.warn("Can't check feature.", ex); addCodeException(report, feature1, ex); } finally { if (set != null) { set.dispose(); } } } }