Skip to content

Commit

Permalink
add GeometryReader to avoid direct use of WKTReader from JTS (refs #232)
Browse files Browse the repository at this point in the history
  • Loading branch information
mborne committed Aug 4, 2021
1 parent 5f24e8b commit a603570
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;

import fr.ign.validator.Context;
import fr.ign.validator.command.options.OutputProjectionOption;
Expand All @@ -29,6 +28,7 @@
import fr.ign.validator.error.CoreErrorCodes;
import fr.ign.validator.exception.ModelNotFoundException;
import fr.ign.validator.exception.PluginNotFoundException;
import fr.ign.validator.geometry.GeometryReader;
import fr.ign.validator.geometry.ProjectionList;
import fr.ign.validator.io.ModelReader;
import fr.ign.validator.io.ModelReaderFactory;
Expand Down Expand Up @@ -628,7 +628,7 @@ protected void parseDataExtent(CommandLine commandLine) throws ParseException {
return;
}
String wktExtent = commandLine.getOptionValue("data-extent");
WKTReader reader = new WKTReader();
GeometryReader reader = new GeometryReader();
try {
nativeDataExtent = reader.read(wktExtent);
} catch (org.locationtech.jts.io.ParseException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.ign.validator.geometry;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

/**
* Helper class to read geometries with a support for curved geometries. Note
* that it currently supports only WKT format produced by ogr2ogr.
*
* @see https://github.com/IGNF/validator/issues/232 related to the introduction
* of this class to avoid the direct use of WKTReader from JTS.
*
* @author MBorne
*
*/
public class GeometryReader {
/**
* WKT reader from JTS.
*/
private WKTReader format;

public GeometryReader() {
this.format = new WKTReader();
}

public Geometry read(String wkt) throws ParseException {
return format.read(wkt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.ParseException;

import com.fasterxml.jackson.annotation.JsonTypeName;

import fr.ign.validator.geometry.GeometryReader;
import fr.ign.validator.model.AttributeType;
import fr.ign.validator.validation.attribute.GeometryDataExtentValidator;
import fr.ign.validator.validation.attribute.GeometryIsValidValidator;
Expand All @@ -22,14 +23,14 @@ public class GeometryType extends AttributeType<Geometry> {

public static final String TYPE = "Geometry";

public static GeometryReader format = new GeometryReader();

public GeometryType() {
super(Geometry.class);
addValidator(new GeometryIsValidValidator());
addValidator(new GeometryDataExtentValidator());
}

public static WKTReader format = new WKTReader();

@Override
public String getTypeName() {
return TYPE;
Expand All @@ -48,11 +49,12 @@ public Geometry bind(Object value) {

String wkt = (String) value;
try {
Geometry geometry = format.read(wkt);
return geometry;
} catch (Exception e) {
return format.read(wkt);
} catch (ParseException e) {
throw new IllegalArgumentException(
String.format("Format de géométrie invalide : {}", wkt)
String.format(
"Fail to parse geometry from : %1s", wkt
), e
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

import fr.ign.validator.exception.GeometryTransformException;
import fr.ign.validator.geometry.GeometryReader;
import fr.ign.validator.geometry.ProjectionTransform;
import fr.ign.validator.model.Projection;

Expand All @@ -30,8 +30,8 @@ public static Envelope getEnvelope(String wkt) {
return new Envelope();
}
try {
WKTReader reader = new WKTReader();
org.locationtech.jts.geom.Geometry geometry = reader.read(wkt);
GeometryReader reader = new GeometryReader();
Geometry geometry = reader.read(wkt);
return geometry.getEnvelopeInternal();
} catch (ParseException e) {
return new Envelope();
Expand All @@ -49,9 +49,7 @@ public static Envelope getEnvelope(String wkt, Projection projection) {
try {
Geometry geom = new ProjectionTransform(projection).transformWKT(wkt);
return geom.getEnvelopeInternal();
} catch (ParseException e) {
return new Envelope();
} catch (GeometryTransformException e) {
} catch (ParseException | GeometryTransformException e) {
return new Envelope();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void runTestFromTo(String sourceSRID, String targetSRID, double toleranc
int indexSource = reader.findColumnRequired(sourceSRID);
int indexTarget = reader.findColumnRequired(targetSRID);

WKTReader wktReader = new WKTReader();
GeometryReader geometryReader = new GeometryReader();
while (reader.hasNext()) {
String[] row = reader.next();
String sourceWKT = row[indexSource];
Expand All @@ -103,8 +103,8 @@ private void runTestFromTo(String sourceSRID, String targetSRID, double toleranc
continue;
}

Geometry source = wktReader.read(sourceWKT);
Geometry expectedTarget = wktReader.read(expectedWKT);
Geometry source = geometryReader.read(sourceWKT);
Geometry expectedTarget = geometryReader.read(expectedWKT);

Geometry target = transformProjection.transform(source);
double distance = target.distance(expectedTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.locationtech.jts.simplify.TopologyPreservingSimplifier;

import fr.ign.validator.database.RowIterator;
import fr.ign.validator.geometry.GeometryReader;
import fr.ign.validator.geometry.ProjectionTransform;
import fr.ign.validator.model.Projection;
import fr.ign.validator.tools.EnvelopeUtils;
Expand All @@ -32,7 +33,7 @@ public class DatabaseUtils {
/**
* WKT Reader Enable projection transform to WKT Geometries
*/
public static WKTReader format = new WKTReader();
private static final GeometryReader format = new GeometryReader();

/**
* WKT writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.locationtech.jts.io.WKTReader;
import org.postgresql.util.PSQLException;

import fr.ign.validator.Context;
Expand All @@ -32,11 +31,6 @@ public class InclusionValidator implements Validator<Database> {
*/
private Database database;

/**
* WKT Reader Enable projection transform to WKT Geometries
*/
private static WKTReader format = new WKTReader();

/**
* Ensure feature of high risk are included in any feature of a lower risk
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;

import fr.ign.validator.Context;
import fr.ign.validator.geometry.GeometryReader;
import fr.ign.validator.report.InMemoryReportBuilder;

public class GraphTopologyValidatorTest {
Expand All @@ -24,13 +24,13 @@ public class GraphTopologyValidatorTest {

private Context context;

private WKTReader format;
private GeometryReader format;

private WKTWriter writer;

@Before
public void setUp() {
format = new WKTReader();
format = new GeometryReader();

writer = new WKTWriter();

Expand Down

0 comments on commit a603570

Please sign in to comment.