Skip to content

Commit

Permalink
deegree#212 - use viewport instead of clipping area (enlarged viewpor…
Browse files Browse the repository at this point in the history
…t) for calculatation of label positions
  • Loading branch information
lgoltz committed Oct 10, 2016
1 parent 3ef98d5 commit 12f77d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Occam Labs UG (haftungsbeschränkt)
import org.deegree.geometry.Geometries;
import org.deegree.geometry.Geometry;
import org.deegree.geometry.GeometryFactory;
import org.deegree.geometry.multi.MultiPoint;
import org.deegree.geometry.multi.MultiPolygon;
import org.deegree.geometry.primitive.Point;
import org.deegree.geometry.primitive.Polygon;
Expand All @@ -73,9 +74,12 @@ Occam Labs UG (haftungsbeschränkt)
*/
class GeometryClipper {

private final Envelope viewPort;

private final Polygon clippingArea;

GeometryClipper( final Envelope viewPort, final int width ) {
this.viewPort = viewPort;
this.clippingArea = calculateClippingArea( viewPort, width );
}

Expand All @@ -99,6 +103,37 @@ private Polygon calculateClippingArea( final Envelope bbox, final int width ) {
* @return the clipped geometry or the original geometry if the geometry lays completely in the drawing area.
*/
Geometry clipGeometry( final Geometry geom ) {
return clipGeometry( geom, clippingArea );
}

/**
* Calculates the points inside the geometry and inside the view port. First the passed geometry is clipped
* by the view port. A multipolygon may result. For each of the polygon in this multipolygon one interior point
* is created
*
* @param geom to create labels for, must not be <code>null</code> and in the same CRS as the viewPort
* @return a MultiPoint with all calculated labels
*/
MultiPoint calculateInteriorPoints( final Geometry geom ) {
if ( geom == null )
return null;
Geometry clippedGeometry = clipGeometry( geom, viewPort );
List<Point> points = new ArrayList<Point>();
if ( clippedGeometry != null && clippedGeometry instanceof DefaultSurface ) {
points.add( ( (DefaultSurface) clippedGeometry ).getInteriorPoint() );
}
if ( clippedGeometry != null && clippedGeometry instanceof MultiPolygon ) {
for ( Polygon p : ( (MultiPolygon) clippedGeometry ) ) {
if ( p instanceof DefaultSurface ) {
points.add( ( (DefaultSurface) p ).getInteriorPoint() );
}
}
}
return new GeometryFactory().createMultiPoint( null, geom.getCoordinateSystem(), points );
}


Geometry clipGeometry( final Geometry geom, Geometry clippingArea ) {
if ( clippingArea != null && !clippingArea.contains( geom ) ) {
try {
Geometry clippedGeometry = clippingArea.getIntersection( geom );
Expand All @@ -113,7 +148,7 @@ Geometry clipGeometry( final Geometry geom ) {
if ( isInvertedOrientation( jtsOrig ) ) {
return clippedGeometry;
}

return fixOrientation( clippedGeometry, clippedGeometry.getCoordinateSystem() );
} catch ( UnsupportedOperationException e ) {
// use original geometry if intersection not supported by JTS
Expand All @@ -123,28 +158,6 @@ Geometry clipGeometry( final Geometry geom ) {
return geom;
}

Geometry calculateInteriorPoints( final Geometry geom ) {
if( geom == null )
return null;
try {
List<Point> points = new ArrayList<Point>();
if ( geom != null && geom instanceof DefaultSurface ) {
points.add( ( (DefaultSurface) geom ).getInteriorPoint() );
}
if ( geom != null && geom instanceof MultiPolygon ) {
for ( Polygon p : ( (MultiPolygon) geom ) ) {
if ( p instanceof DefaultSurface ) {
points.add( ( (DefaultSurface) p ).getInteriorPoint() );
}
}
}
return new GeometryFactory().createMultiPoint( null, geom.getCoordinateSystem(), points );
} catch ( UnsupportedOperationException e ) {
// use original geometry if intersection not supported by JTS
return geom;
}
}

/**
* Check if the passed Geometry is a Polygon (or the first Geometry of a Collection) and the exterior Ring has CW orientation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,10 @@ public void render( Label pLabel ) {

private void handlePolygonWithAutoPlacement( TextStyling styling, Font font, String text, Polygon geom ) {
Geometry transformedGeom = renderer.rendererContext.geomHelper.transform( geom );
Geometry clippedGeometry = renderer.rendererContext.clipper.clipGeometry( transformedGeom );
Geometry points = renderer.rendererContext.clipper.calculateInteriorPoints( clippedGeometry );
MultiPoint points = renderer.rendererContext.clipper.calculateInteriorPoints( transformedGeom );
if ( geom == null )
return;
if ( points instanceof MultiPoint ) {
handleMultiGeometry( styling, text, font, (MultiGeometry) points );
} else {
handleGeometryTypes( styling, text, font, points.getCentroid() );
}
handleMultiGeometry( styling, text, font, (MultiGeometry) points );
}

}

0 comments on commit 12f77d7

Please sign in to comment.