Skip to content

Commit

Permalink
Included method in AbstractGeometryValidationEvent to be reused by Ex…
Browse files Browse the repository at this point in the history
…teriorRingOrientation and ExteriorRingOrientation classes.

Added test for ring orientation using left-handed and right-handed CRS.
  • Loading branch information
carlospzurita committed Mar 5, 2020
1 parent cdca4e7 commit d0b59ac
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
import junit.framework.Assert;

import org.deegree.commons.xml.XMLParsingException;
import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.cs.exceptions.UnknownCRSException;
import org.deegree.cs.persistence.CRSManager;
import org.deegree.geometry.Geometry;
import org.deegree.geometry.validation.event.CurveDiscontinuity;
import org.deegree.geometry.validation.event.CurveSelfIntersection;
Expand Down Expand Up @@ -160,6 +162,38 @@ public void validatePolygon()
Assert.assertTrue( ( (InteriorRingOrientation) eventHandler.getEvents().get( 2 ) ).isClockwise() );
}

@Test
public void validatePolygonExteriorAndInterior()
throws XMLStreamException, FactoryConfigurationError, IOException, XMLParsingException,
UnknownCRSException {
DummyValidationEventHandler eventHandler = new DummyValidationEventHandler();
GeometryValidator validator = new GeometryValidator( eventHandler );
Geometry geom = parseGeometry( "Polygon.gml" );
Assert.assertTrue( validator.validateGeometry( geom ) );
Assert.assertEquals( 3, eventHandler.getEvents().size() );

// get rings
ExteriorRingOrientation exterior = (ExteriorRingOrientation) eventHandler.getEvents().get( 0 );
InteriorRingOrientation interior1 = (InteriorRingOrientation) eventHandler.getEvents().get( 1 );
InteriorRingOrientation interior2 = (InteriorRingOrientation) eventHandler.getEvents().get( 2 );

// check for exterior and interior rings with a right handed CRS
Assert.assertTrue( exterior.isExterior() );
Assert.assertTrue( interior1.isInterior() );
Assert.assertTrue( interior2.isInterior() );

// substitute original right handed CRS for a new left handed one, maintaining polygon orientation
ICRS leftHandedCrs = CRSManager.getCRSRef( "urn:ogc:def:crs:epsg::4326" );
exterior.getPatch().getExteriorRing().setCoordinateSystem( leftHandedCrs );
interior1.getPatch().getInteriorRings().get( 0 ).setCoordinateSystem( leftHandedCrs );
interior2.getPatch().getInteriorRings().get( 1 ).setCoordinateSystem( leftHandedCrs );

// check for exterior and interior rings with a left handed CRS
Assert.assertFalse( exterior.isExterior() );
Assert.assertFalse( interior1.isInterior() );
Assert.assertFalse( interior2.isInterior() );
}

@Test
public void validatePolygonExteriorClockwise()
throws XMLStreamException, FactoryConfigurationError, IOException, XMLParsingException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

import java.util.List;

import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.cs.components.Axis;

/**
* Abstract base class for {@link GeometryValidationEvent} implementations.
*
Expand Down Expand Up @@ -65,4 +68,38 @@ public List<Object> getGeometryParticleHierarchy() {
return geometryParticleHierachy;
}

/**
* Returns true if the geometry has a left handed CRS.
*
* @return <code>true</code> if geometry has a left handed CRS, <code>false</code> if CRS is right handed
*/
protected boolean isLeftHanded( ICRS crs ) {
// get number of dimensions (it should be 2)
if ( crs.getDimension() == 2 ) {
int axis1 = crs.getAxis()[0].getOrientation();
int axis2 = crs.getAxis()[1].getOrientation();

// check if CRS is left handed
if ( axis1 == Axis.AO_EAST || axis1 == Axis.AO_WEST ) {
if ( axis1 == Axis.AO_EAST && ( axis2 == Axis.AO_SOUTH || axis2 == Axis.AO_DOWN ) ) {
return true;
}
else if ( axis1 == Axis.AO_WEST && ( axis2 == Axis.AO_NORTH || axis2 == Axis.AO_UP ) ) {
return true;
}
}
else {
if ( ( axis1 == Axis.AO_SOUTH || axis1 == Axis.AO_DOWN ) && axis2 == Axis.AO_WEST ) {
return true;
}
else if ( ( axis1 == Axis.AO_NORTH || axis1 == Axis.AO_UP ) && axis2 == Axis.AO_EAST ) {
return true;
}
}
}

// return false in any other case
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
import org.deegree.geometry.primitive.Ring;
import org.deegree.geometry.primitive.patches.PolygonPatch;

import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.cs.components.Axis;

/**
* {@link GeometryValidationEvent} that indicates the orientation of an exterior {@link Ring} of a {@link PolygonPatch}.
*
Expand Down Expand Up @@ -90,42 +87,6 @@ public PolygonPatch getPatch() {
public boolean isClockwise() {
return isClockwise;
}

/**
* Returns true if the geometry has a left handed CRS.
*
* @return <code>true</code> if geometry has a left handed CRS, <code>false</code> if CRS is right handed
*/
public boolean hasLeftHandedSrs() {
ICRS crs = patch.getExteriorRing().getCoordinateSystem();

// get number of dimensions (it should be 2)
if ( crs.getDimension() == 2 ) {
int axis1 = crs.getAxis()[0].getOrientation();
int axis2 = crs.getAxis()[1].getOrientation();

// check if CRS is left handed
if ( axis1 == Axis.AO_EAST || axis1 == Axis.AO_WEST ) {
if ( axis1 == Axis.AO_EAST && ( axis2 == Axis.AO_SOUTH || axis2 == Axis.AO_DOWN ) ) {
return true;
}
else if ( axis1 == Axis.AO_WEST && ( axis2 == Axis.AO_NORTH || axis2 == Axis.AO_UP ) ) {
return true;
}
}
else {
if ( ( axis1 == Axis.AO_SOUTH || axis1 == Axis.AO_DOWN ) && axis2 == Axis.AO_WEST ) {
return true;
}
else if ( ( axis1 == Axis.AO_NORTH || axis1 == Axis.AO_UP ) && axis2 == Axis.AO_EAST ) {
return true;
}
}
}

// return false in any other case
return false;
}

/**
* Returns true if the geometry is an exterior boundary.
Expand All @@ -135,7 +96,7 @@ else if ( ( axis1 == Axis.AO_NORTH || axis1 == Axis.AO_UP ) && axis2 == Axis.AO_
public boolean isExterior() {
boolean isExterior = !isClockwise;

if ( hasLeftHandedSrs() ) {
if ( isLeftHanded( patch.getExteriorRing().getCoordinateSystem() ) ) {
isExterior = !isExterior;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
import org.deegree.geometry.primitive.Ring;
import org.deegree.geometry.primitive.patches.PolygonPatch;

import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.cs.components.Axis;

/**
* {@link GeometryValidationEvent} that indicates the orientation of an interior {@link Ring} of a {@link PolygonPatch}.
*
Expand Down Expand Up @@ -105,42 +102,6 @@ public int getRingIdx() {
public boolean isClockwise() {
return isClockwise;
}

/**
* Returns true if the geometry has a left handed CRS.
*
* @return <code>true</code> if geometry has a left handed CRS, <code>false</code> if CRS is right handed
*/
public boolean hasLeftHandedSrs() {
ICRS crs = patch.getInteriorRings().get( ringIdx ).getCoordinateSystem();

// get number of dimensions (it should be 2)
if ( crs.getDimension() == 2 ) {
int axis1 = crs.getAxis()[0].getOrientation();
int axis2 = crs.getAxis()[1].getOrientation();

// check if CRS is left handed
if ( axis1 == Axis.AO_EAST || axis1 == Axis.AO_WEST ) {
if ( axis1 == Axis.AO_EAST && ( axis2 == Axis.AO_SOUTH || axis2 == Axis.AO_DOWN ) ) {
return true;
}
else if ( axis1 == Axis.AO_WEST && ( axis2 == Axis.AO_NORTH || axis2 == Axis.AO_UP ) ) {
return true;
}
}
else {
if ( ( axis1 == Axis.AO_SOUTH || axis1 == Axis.AO_DOWN ) && axis2 == Axis.AO_WEST ) {
return true;
}
else if ( ( axis1 == Axis.AO_NORTH || axis1 == Axis.AO_UP ) && axis2 == Axis.AO_EAST ) {
return true;
}
}
}

// return false in any other case
return false;
}

/**
* Returns true if the geometry is an interior boundary.
Expand All @@ -150,7 +111,7 @@ else if ( ( axis1 == Axis.AO_NORTH || axis1 == Axis.AO_UP ) && axis2 == Axis.AO_
public boolean isInterior() {
boolean isInterior = isClockwise;

if ( hasLeftHandedSrs() ) {
if ( isLeftHanded( patch.getInteriorRings().get( ringIdx ).getCoordinateSystem() ) ) {
isInterior = !isInterior;
}

Expand Down

0 comments on commit d0b59ac

Please sign in to comment.