Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty multi geometries #1106

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,46 @@
----------------------------------------------------------------------------*/
package org.deegree.geometry.io;

import java.io.IOException;
import java.io.OutputStream;

import org.deegree.geometry.Geometry;
import org.deegree.geometry.multi.MultiGeometry;
import org.deegree.geometry.refs.GeometryReference;
import org.deegree.geometry.standard.AbstractDefaultGeometry;
import org.deegree.geometry.standard.primitive.DefaultPoint;

import org.locationtech.jts.io.OutputStreamOutStream;
import org.locationtech.jts.io.ParseException;

import java.io.IOException;
import java.io.OutputStream;

/**
* Writes {@link Geometry} objects encoded as Well-Known Binary (WKB).
*
*
* TODO re-implement without delegating to JTS TODO add support for non-SFS geometries (e.g. non-linear curves)
*
*
* @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
* @author last edited by: $Author$
*
*
* @version $Revision$, $Date$
*/
public class WKBWriter {

// TODO remove the need for this object
private static AbstractDefaultGeometry defaultGeom = new DefaultPoint( null, null, null, new double[] { 0.0, 0.0 } );

public static byte[] write( Geometry geom )
throws ParseException {
/**
* Exports the passed geom to WKB.
*
* @param geom
* never <code>null</code>
* @return the WKB as byte array, may be <code>null</code> if the passed geom is an empty multi geometry
*/
public static byte[] write( Geometry geom ) {
if ( geom instanceof GeometryReference ) {
geom = ( (GeometryReference<Geometry>) geom ).getReferencedObject();
}
if ( isEmptyMultiGeometry( geom ) ) {
return null;
}
// org.locationtech.jts.io.WKBWriter is not thread safe
int dim = geom.getCoordinateDimension();
return new org.locationtech.jts.io.WKBWriter(dim).write( ( (AbstractDefaultGeometry) geom ).getJTSGeometry() );
Expand All @@ -78,4 +87,10 @@ public static void write( Geometry geom, OutputStream os )
new org.locationtech.jts.io.WKBWriter().write( ( (AbstractDefaultGeometry) geom ).getJTSGeometry(),
new OutputStreamOutStream( os ) );
}

private static boolean isEmptyMultiGeometry( Geometry geom ) {
return Geometry.GeometryType.MULTI_GEOMETRY.equals( geom.getGeometryType() )
&& ( (MultiGeometry) geom ).isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.deegree.cs.coordinatesystems.ICRS;
import org.deegree.geometry.Envelope;
import org.deegree.geometry.Geometry;
import org.deegree.geometry.GeometryException;
import org.deegree.geometry.multi.MultiGeometry;
import org.deegree.geometry.precision.PrecisionModel;
import org.deegree.geometry.standard.AbstractDefaultGeometry;
Expand Down Expand Up @@ -82,7 +83,9 @@ public DefaultMultiGeometry( String id, ICRS crs, PrecisionModel pm, List<T> mem

@Override
public int getCoordinateDimension() {
return members.get( 0 ).getCoordinateDimension();
if ( isEmpty() )
throw new GeometryException( "MultiGeometry is empty, coordinate dimension can not be calculated." );
return get( 0 ).getCoordinateDimension();
}

@Override
Expand Down Expand Up @@ -200,7 +203,8 @@ public <T> T[] toArray( T[] a ) {
@Override
public Envelope getEnvelope() {
if ( env == null ) {
// TODO NullEnvelope for emtpy aggregates? or throw an exception?
if ( isEmpty() )
throw new GeometryException( "MultiGeometry is empty, coordinate envelope can not be calculated." );
env = get( 0 ).getEnvelope();
for ( Geometry geom : this ) {
env = env.merge( geom.getEnvelope() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,15 @@ private void prepareSlot( SlotMapping slot, ExtrinsicObject extrinsicObject, Ins
if ( SlotType._geom.equals( slot.getType() ) ) {
Geometry geom = (Geometry) extrinsicObject.getGeometrySlotValue( SLOTURN + slot.getName() );
if ( geom != null ) {
try {
byte[] wkt = WKBWriter.write( geom );
StringBuilder sb = new StringBuilder();
if ( useLegacyPredicates ) {
sb.append( "SetSRID(GeomFromWKB(?)," );
} else {
sb.append( "SetSRID(ST_GeomFromWKB(?)," );
}
sb.append( "-1)" );
ir.addPreparedArgument( new SQLIdentifier( slot.getColumn() ), wkt, sb.toString() );
} catch ( ParseException e ) {
String msg = "Could not write as WKB " + geom + ": " + e.getMessage();
LOG.debug( msg, e );
throw new IllegalArgumentException();
byte[] wkt = WKBWriter.write( geom );
StringBuilder sb = new StringBuilder();
if ( useLegacyPredicates ) {
sb.append( "SetSRID(GeomFromWKB(?)," );
} else {
sb.append( "SetSRID(ST_GeomFromWKB(?)," );
}
sb.append( "-1)" );
ir.addPreparedArgument( new SQLIdentifier( slot.getColumn() ), wkt, sb.toString() );
}
return;
}
Expand Down