Skip to content

Commit

Permalink
Expose ordinal constants and use instead of reflection (#222)
Browse files Browse the repository at this point in the history
* Expose ordinal constants and use instead of reflection
* Add deprecation note

Signed-off-by: Björn Harrtell <bjorn@wololo.org>
  • Loading branch information
bjornharrtell committed May 21, 2020
1 parent e5e52b6 commit 511c3d9
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 59 deletions.
41 changes: 25 additions & 16 deletions modules/core/src/main/java/org/locationtech/jts/geom/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,25 @@ public abstract class Geometry
implements Cloneable, Comparable, Serializable
{
private static final long serialVersionUID = 8763622679187376702L;

static final int SORTINDEX_POINT = 0;
static final int SORTINDEX_MULTIPOINT = 1;
static final int SORTINDEX_LINESTRING = 2;
static final int SORTINDEX_LINEARRING = 3;
static final int SORTINDEX_MULTILINESTRING = 4;
static final int SORTINDEX_POLYGON = 5;
static final int SORTINDEX_MULTIPOLYGON = 6;
static final int SORTINDEX_GEOMETRYCOLLECTION = 7;


protected static final int TYPECODE_POINT = 0;
protected static final int TYPECODE_MULTIPOINT = 1;
protected static final int TYPECODE_LINESTRING = 2;
protected static final int TYPECODE_LINEARRING = 3;
protected static final int TYPECODE_MULTILINESTRING = 4;
protected static final int TYPECODE_POLYGON = 5;
protected static final int TYPECODE_MULTIPOLYGON = 6;
protected static final int TYPECODE_GEOMETRYCOLLECTION = 7;

public static final String TYPENAME_POINT = "Point";
public static final String TYPENAME_MULTIPOINT = "MultiPoint";
public static final String TYPENAME_LINESTRING = "LineString";
public static final String TYPENAME_LINEARRING = "LinearRing";
public static final String TYPENAME_MULTILINESTRING = "MultiLineString";
public static final String TYPENAME_POLYGON = "Polygon";
public static final String TYPENAME_MULTIPOLYGON = "MultiPolygon";
public static final String TYPENAME_GEOMETRYCOLLECTION = "GeometryCollection";

private final static GeometryComponentFilter geometryChangedFilter = new GeometryComponentFilter() {
public void filter(Geometry geom) {
geom.geometryChangedAction();
Expand Down Expand Up @@ -1722,8 +1731,8 @@ public Geometry norm()
*/
public int compareTo(Object o) {
Geometry other = (Geometry) o;
if (getSortIndex() != other.getSortIndex()) {
return getSortIndex() - other.getSortIndex();
if (getTypeCode() != other.getTypeCode()) {
return getTypeCode() - other.getTypeCode();
}
if (isEmpty() && other.isEmpty()) {
return 0;
Expand Down Expand Up @@ -1769,8 +1778,8 @@ public int compareTo(Object o) {
*/
public int compareTo(Object o, CoordinateSequenceComparator comp) {
Geometry other = (Geometry) o;
if (getSortIndex() != other.getSortIndex()) {
return getSortIndex() - other.getSortIndex();
if (getTypeCode() != other.getTypeCode()) {
return getTypeCode() - other.getTypeCode();
}
if (isEmpty() && other.isEmpty()) {
return 0;
Expand Down Expand Up @@ -1822,7 +1831,7 @@ protected static void checkNotGeometryCollection(Geometry g) {
*/
protected boolean isGeometryCollection()
{
return getSortIndex() == SORTINDEX_GEOMETRYCOLLECTION;
return getTypeCode() == TYPECODE_GEOMETRYCOLLECTION;
}

/**
Expand Down Expand Up @@ -1901,7 +1910,7 @@ protected boolean equal(Coordinate a, Coordinate b, double tolerance) {
return a.distance(b) <= tolerance;
}

abstract protected int getSortIndex();
abstract protected int getTypeCode();

This comment has been minimized.

Copy link
@FObermaier

FObermaier May 22, 2020

Contributor

Isn't this a breaking change? In case it is and it doesn't matter, we can simplify changes to Geometry.reverse(), too.

This comment has been minimized.

Copy link
@dr-jts

dr-jts May 22, 2020

Contributor

Yes it is, technically, for external Geometry subclasses. I don't know of any such usage, and I'm not sure that would work very well anyway. It's not supported or encouraged.


private Point createPointFromInternalCoord(Coordinate coord, Geometry exemplar)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public int getNumPoints() {
}

public String getGeometryType() {
return "GeometryCollection";
return Geometry.TYPENAME_GEOMETRYCOLLECTION;
}

public Geometry getBoundary() {
Expand Down Expand Up @@ -268,9 +268,9 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp) {
return 0;

}

protected int getSortIndex() {
return Geometry.SORTINDEX_GEOMETRYCOLLECTION;
protected int getTypeCode() {
return Geometry.TYPECODE_GEOMETRYCOLLECTION;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean isRing() {
}

public String getGeometryType() {
return "LineString";
return Geometry.TYPENAME_LINESTRING;
}

/**
Expand Down Expand Up @@ -326,9 +326,9 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp)
LineString line = (LineString) o;
return comp.compare(this.points, line.points);
}

protected int getSortIndex() {
return Geometry.SORTINDEX_LINESTRING;
protected int getTypeCode() {
return Geometry.TYPECODE_LINESTRING;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public boolean isClosed() {


public String getGeometryType() {
return "LinearRing";
return Geometry.TYPENAME_LINEARRING;
}

protected int getSortIndex() {
return Geometry.SORTINDEX_LINEARRING;
protected int getTypeCode() {
return Geometry.TYPECODE_LINEARRING;
}

protected LinearRing copyInternal() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int getBoundaryDimension() {
}

public String getGeometryType() {
return "MultiLineString";
return Geometry.TYPENAME_MULTILINESTRING;
}

public boolean isClosed() {
Expand Down Expand Up @@ -123,8 +123,8 @@ public boolean equalsExact(Geometry other, double tolerance) {
return super.equalsExact(other, tolerance);
}

protected int getSortIndex() {
return Geometry.SORTINDEX_MULTILINESTRING;
protected int getTypeCode() {
return Geometry.TYPECODE_MULTILINESTRING;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public int getBoundaryDimension() {
}

public String getGeometryType() {
return "MultiPoint";
return Geometry.TYPENAME_MULTIPOINT;
}

/**
Expand Down Expand Up @@ -106,8 +106,8 @@ protected MultiPoint copyInternal() {
return new MultiPoint(points, factory);
}

protected int getSortIndex() {
return Geometry.SORTINDEX_MULTIPOINT;
protected int getTypeCode() {
return Geometry.TYPECODE_MULTIPOINT;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public int getBoundaryDimension() {
}

public String getGeometryType() {
return "MultiPolygon";
return Geometry.TYPENAME_MULTIPOLYGON;
}

/*
Expand Down Expand Up @@ -133,8 +133,8 @@ protected MultiPolygon copyInternal() {
return new MultiPolygon(polygons, factory);
}

protected int getSortIndex() {
return Geometry.SORTINDEX_MULTIPOLYGON;
protected int getTypeCode() {
return Geometry.TYPECODE_MULTIPOLYGON;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public Coordinate getCoordinate() {
}

public String getGeometryType() {
return "Point";
return Geometry.TYPENAME_POINT;
}

/**
Expand Down Expand Up @@ -215,9 +215,9 @@ protected int compareToSameClass(Object other, CoordinateSequenceComparator comp
Point point = (Point) other;
return comp.compare(this.coordinates, point.coordinates);
}

protected int getSortIndex() {
return Geometry.SORTINDEX_POINT;
protected int getTypeCode() {
return Geometry.TYPECODE_POINT;
}

public CoordinateSequence getCoordinateSequence() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public LinearRing getInteriorRingN(int n) {
}

public String getGeometryType() {
return "Polygon";
return Geometry.TYPENAME_POLYGON;
}

/**
Expand Down Expand Up @@ -394,9 +394,9 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp) {
if (i < nHole2) return -1;
return 0;
}

protected int getSortIndex() {
return Geometry.SORTINDEX_POLYGON;
protected int getTypeCode() {
return Geometry.TYPECODE_POLYGON;
}

private LinearRing normalized(LinearRing ring, boolean clockwise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFilter;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

/**
* Extracts the components of a given type from a {@link Geometry}.
Expand All @@ -27,27 +34,59 @@
public class GeometryExtracter
implements GeometryFilter
{

protected static boolean isOfClass(Object o, Class clz)
{
return clz.isAssignableFrom(o.getClass());
// return o.getClass() == clz;
}

/**
* Extracts the components of type <tt>clz</tt> from a {@link Geometry}
* and adds them to the provided {@link List}.
*
* @param geom the geometry from which to extract
* @param list the list to add the extracted elements to
* @deprecated Use {@link GeometryExtracter#extract(Geometry, String, List)}
*/
public static List extract(Geometry geom, Class clz, List list)
{
if (isOfClass(geom, clz)) {
return extract(geom, toGeometryType(clz), list);
}

/**
* @deprecated
*/
private static String toGeometryType(Class clz) {
if (clz == null)
return null;
else if (clz.isAssignableFrom(Point.class))
return Geometry.TYPENAME_POINT;
else if (clz.isAssignableFrom(LineString.class))
return Geometry.TYPENAME_LINESTRING;
else if (clz.isAssignableFrom(LinearRing.class))
return Geometry.TYPENAME_LINEARRING;
else if (clz.isAssignableFrom(Polygon.class))
return Geometry.TYPENAME_POLYGON;
else if (clz.isAssignableFrom(MultiPoint.class))
return Geometry.TYPENAME_MULTIPOINT;
else if (clz.isAssignableFrom(MultiLineString.class))
return Geometry.TYPENAME_MULTILINESTRING;
else if (clz.isAssignableFrom(MultiPolygon.class))
return Geometry.TYPENAME_MULTIPOLYGON;
else if (clz.isAssignableFrom(GeometryCollection.class))
return Geometry.TYPENAME_GEOMETRYCOLLECTION;
throw new RuntimeException("Unsupported class");
}

/**
* Extracts the components of <tt>geometryType</tt> from a {@link Geometry}
* and adds them to the provided {@link List}.
*
* @param geom the geometry from which to extract
* @param geometryType Geometry type to extract (null means all types)
* @param list the list to add the extracted elements to
*/
public static List extract(Geometry geom, String geometryType, List list)
{
if (geom.getGeometryType() == geometryType) {
list.add(geom);
}
else if (geom instanceof GeometryCollection) {
geom.apply(new GeometryExtracter(clz, list));
geom.apply(new GeometryExtracter(geometryType, list));
}
// skip non-LineString elemental geometries

Expand All @@ -59,30 +98,56 @@ else if (geom instanceof GeometryCollection) {
* and returns them in a {@link List}.
*
* @param geom the geometry from which to extract
* @deprecated Use {@link GeometryExtracter#extract(Geometry, String)}
*/
public static List extract(Geometry geom, Class clz)
{
return extract(geom, clz, new ArrayList());
}

public static List extract(Geometry geom, String geometryType)
{
return extract(geom, geometryType, new ArrayList());
}

private Class clz;
private String geometryType;
private List comps;

/**
* Constructs a filter with a list in which to store the elements found.
*
* @param clz the class of the components to extract (null means all types)
* @param comps the list to extract into
* @deprecated
*/
public GeometryExtracter(Class clz, List comps)
{
this.clz = clz;
this.geometryType = toGeometryType(clz);
this.comps = comps;
}

public void filter(Geometry geom)

/**
* Constructs a filter with a list in which to store the elements found.
*
* @param geometryType Geometry type to extract (null means all types)
* @param comps the list to extract into
*/
public GeometryExtracter(String geometryType, List comps)
{
if (clz == null || isOfClass(geom, clz)) comps.add(geom);
this.geometryType = geometryType;
this.comps = comps;
}

protected static boolean isOfType(Geometry geom, String geometryType) {
if (geom.getGeometryType() == geometryType) return true;
if (geometryType == Geometry.TYPENAME_LINESTRING
&& geom.getGeometryType() == Geometry.TYPENAME_LINEARRING) return true;
return false;
}

public void filter(Geometry geom) {
if (geometryType == null || isOfType(geom, geometryType))
comps.add(geom);
}

}

0 comments on commit 511c3d9

Please sign in to comment.