/** * 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.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 PointMustBeProperlyInsidePolygonRule extends AbstractTopologyRule { private class DeleteAction extends AbstractTopologyRuleAction { public DeleteAction() { super( PointMustBeProperlyInsidePolygonRuleFactory.NAME, "Delete", "Delete", " The Delete fix removes point features that are not properly within polygon features. Note that you can use the Edit tool and move the point inside the polygon feature if you do not want to delete it. This fix can be applied to one or more Must Be Properly Inside errors." ); } @Override public int execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) { try { TopologyDataSet dataSet = rule.getDataSet1(); dataSet.delete(line.getFeature1()); return EXECUTE_OK; } catch (Exception ex) { throw new ExecuteTopologyRuleActionException(ex); } } } private String geomName; private Expression expression = null; private GeometryExpressionBuilder expressionBuilder = null; public PointMustBeProperlyInsidePolygonRule() { // for persistence only } public PointMustBeProperlyInsidePolygonRule( TopologyRuleFactory factory, double tolerance, String dataSet1, String dataSet2 ) { super(factory, tolerance, dataSet1, dataSet2); this.addAction(new DeleteAction()); } @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(); } double theTolerance = getTolerance(); Geometry point = feature1.getDefaultGeometry(); TopologyDataSet theDataSet2 = this.getDataSet2(); if (theDataSet2.getSpatialIndex() != null) { boolean contained = false; for (FeatureReference featureReference : theDataSet2.query(point)) { Feature feature2 = featureReference.getFeature(); Geometry otherPolygon = feature2.getDefaultGeometry(); if(theTolerance > 0){ otherPolygon = otherPolygon.buffer(theTolerance); } if( otherPolygon!=null && otherPolygon.contains(point) ) { contained = true; break; } } if( !contained ) { I18nManager i18n = ToolsLocator.getI18nManager(); report.addLine(this, this.getDataSet1(), this.getDataSet2(), point, point, feature1.getReference(), null, false, i18n.getTranslation("_Point_is_not_contained_in_a_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(point) ) ).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(point) ) ).toString() ); } if (theDataSet2.findFirst(this.expression) == null) { I18nManager i18n = ToolsLocator.getI18nManager(); report.addLine(this, this.getDataSet1(), this.getDataSet2(), point, point, feature1.getReference(), null, false, i18n.getTranslation("_Point_is_not_contained_in_a_polygon") ); } } } catch (Exception ex) { LOGGER.warn("Can't check feature.", ex); addCodeException(report, feature1, ex); } finally { if (set != null) { set.dispose(); } } } }