Skip to content

Commit

Permalink
Expose ordinal constants and use instead of reflection
Browse files Browse the repository at this point in the history
Signed-off-by: Björn Harrtell <bjorn@wololo.org>
  • Loading branch information
bjornharrtell committed Apr 12, 2019
1 parent f56998b commit e9fe1ad
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 55 deletions.
37 changes: 23 additions & 14 deletions modules/core/src/main/java/org/locationtech/jts/geom/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,23 @@ public abstract class Geometry
{
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) {
Expand Down Expand Up @@ -1710,8 +1719,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 @@ -1757,8 +1766,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 @@ -1810,7 +1819,7 @@ protected static void checkNotGeometryCollection(Geometry g) {
*/
protected boolean isGeometryCollection()
{
return getSortIndex() == SORTINDEX_GEOMETRYCOLLECTION;
return getTypeCode() == TYPECODE_GEOMETRYCOLLECTION;
}

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

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

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

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

public Geometry getBoundary() {
Expand Down Expand Up @@ -267,8 +267,8 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp) {

}

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 @@ -323,8 +323,8 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp)
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 @@ -128,8 +128,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 @@ -138,8 +138,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 @@ -211,8 +211,8 @@ protected int compareToSameClass(Object other, CoordinateSequenceComparator comp
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 @@ -395,8 +395,8 @@ protected int compareToSameClass(Object o, CoordinateSequenceComparator comp) {
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,13 +34,6 @@
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}.
Expand All @@ -43,11 +43,49 @@ protected static boolean isOfClass(Object o, Class clz)
*/
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 +97,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, List)}
*/
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ private void extract(Geometry geom)
LineStringExtracter.getLines(geom, lines);
PointExtracter.getPoints(geom, points);
*/
GeometryExtracter.extract(geom, Polygon.class, polygons);
GeometryExtracter.extract(geom, LineString.class, lines);
GeometryExtracter.extract(geom, Point.class, points);
GeometryExtracter.extract(geom, Geometry.TYPENAME_POLYGON, polygons);
GeometryExtracter.extract(geom, Geometry.TYPENAME_LINESTRING, lines);
GeometryExtracter.extract(geom, Geometry.TYPENAME_POINT, points);
}

/**
Expand Down

0 comments on commit e9fe1ad

Please sign in to comment.