All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anwiba.spatial.geometry.polygon.tree.GeometryNode Maven / Gradle / Ivy

There is a newer version: 1.2.50
Show newest version
/*
 * #%L
 * anwiba commons core
 * %%
 * Copyright (C) 2007 - 2018 Andreas W. Bartels
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
 
package net.anwiba.spatial.geometry.polygon.tree;

import net.anwiba.spatial.coordinate.CoordinateSequenceUtilities;
import net.anwiba.spatial.coordinate.calculator.CoordinateSequenceOrientationCalculator;
import net.anwiba.spatial.geometry.IGeometryFactoryProvider;
import net.anwiba.spatial.geometry.ILinearRing;
import net.anwiba.spatial.geometry.IPolygon;
import net.anwiba.spatial.geometry.internal.LinearRing;
import net.anwiba.spatial.geometry.polygon.ContainsLinearRingOperator;
import net.anwiba.spatial.geometry.utilities.GeometryUtilities;

public class GeometryNode extends AbstractGeometryNode implements IGeometryNode {

  private final ILinearRing geometry;
  private IPolygon polygon;
  private ContainsLinearRingOperator operator;
  private final IGeometryFactoryProvider factoryProvider;

  public GeometryNode(final IGeometryFactoryProvider factoryProvider, final ILinearRing ring) {
    this.factoryProvider = factoryProvider;
    this.geometry = ring;
  }

  @Override
  public boolean contains(final IGeometryNode node) {
    final IPolygon ownPolygon = asPolygon();
    final IPolygon nodePolygon = node.asPolygon();
    if (!this.geometry.getEnvelope().contains(nodePolygon.getEnvelope())) {
      return false;
    }
    if (GeometryUtilities.isRectangle(ownPolygon)) {
      return ownPolygon.getEnvelope().contains(nodePolygon.getEnvelope());
    }
    return contains(node.asLinearRing());
  }

  private boolean contains(final ILinearRing ring) {
    if (this.operator == null) {
      this.operator = new ContainsLinearRingOperator(this.geometry);
    }
    return this.operator.contains(ring);
  }

  @Override
  public ILinearRing asExteriorRing() {
    if (!CoordinateSequenceOrientationCalculator.isOrientationPositive(this.geometry.getCoordinateSequence())) {
      return this.geometry;
    }
    return this.factoryProvider.getGeometryFactory(this.geometry.getCoordinateReferenceSystem()).createLinearRing(
        CoordinateSequenceUtilities.reverse(this.geometry.getCoordinateSequence()));
  }

  @Override
  public ILinearRing asInnerRing() {
    if (CoordinateSequenceOrientationCalculator.isOrientationPositive(this.geometry.getCoordinateSequence())) {
      return this.geometry;
    }
    return this.factoryProvider.getGeometryFactory(this.geometry.getCoordinateReferenceSystem()).createLinearRing(
        CoordinateSequenceUtilities.reverse(this.geometry.getCoordinateSequence()));
  }

  @Override
  public ILinearRing asLinearRing() {
    return this.geometry;
  }

  @Override
  public IPolygon asPolygon() {
    if (this.polygon == null) {
      this.polygon = this.factoryProvider
          .getGeometryFactory(this.geometry.getCoordinateReferenceSystem())
          .createPolygon(this.geometry, new LinearRing[0]);
    }
    return this.polygon;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy