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

Add DiscreteFrechetDistance #764

Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,62 @@
*/
public class PointPairDistance {

private Coordinate[] pt = { new Coordinate(), new Coordinate() };
private final Coordinate[] pt = { new Coordinate(), new Coordinate() };
private double distance = Double.NaN;
private boolean isNull = true;

/**
* Creates an instance of this class
*/
public PointPairDistance()
{
}

/**
* Initializes this instance.
*/
public void initialize() { isNull = true; }

public void initialize(Coordinate p0, Coordinate p1)
{
pt[0].setCoordinate(p0);
pt[1].setCoordinate(p1);
distance = p0.distance(p1);
isNull = false;
/**
* Initializes the points, computing the distance between them.
* @param p0 the 1st point
* @param p1 the 2nd point
*/
public void initialize(Coordinate p0, Coordinate p1) {
initialize(p0, p1, p0.distance(p1));
}

/**
* Initializes the points, avoiding recomputing the distance.
* @param p0
* @param p1
* @param p0 the 1st point
* @param p1 the 2nd point
* @param distance the distance between p0 and p1
*/
private void initialize(Coordinate p0, Coordinate p1, double distance)
void initialize(Coordinate p0, Coordinate p1, double distance)
{
pt[0].setCoordinate(p0);
pt[1].setCoordinate(p1);
this.distance = distance;
isNull = false;
}

/**
* Gets the distance between the paired points
* @return the distance between the paired points
*/
public double getDistance() { return distance; }

/**
* Gets the paired points
* @return the paired points
*/
public Coordinate[] getCoordinates() { return pt; }

/**
* Gets one of the paired points
* @param i the index of the paired point (0 or 1)
* @return A point
*/
public Coordinate getCoordinate(int i) { return pt[i]; }

public void setMaximum(PointPairDistance ptDist)
Expand Down Expand Up @@ -91,7 +111,7 @@ public void setMinimum(Coordinate p0, Coordinate p1)
if (dist < distance)
initialize(p0, p1, dist);
}

public String toString()
{
return WKTWriter.toLineString(pt[0], pt[1]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2021 Felix Obermaier.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.locationtech.jts.algorithm.match;

import org.locationtech.jts.algorithm.distance.DiscreteFrechetDistance;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPoint;

/**
* Measures the degree of similarity between two
* {@link Geometry}s using the Fréchet distance metric.
* The measure is normalized to lie in the range [0, 1].
* Higher measures indicate a great degree of similarity.
* <p/>
* The measure is computed by computing the Fréchet distance
* between the input geometries, and then normalizing
* this by dividing it by the diagonal distance across
* the envelope of the combined geometries.
* <p/>
* Note: the input should be normalized, especially when
* measuring {@link MultiPoint} geometries because for the
* Fréchet distance the order of {@link Coordinate}s is
* important.
*
* @author Felix Obermaier
*
*/
public class FrechetSimilarityMeasure implements SimilarityMeasure {

/**
* Creates an instance of this class.
*/
public FrechetSimilarityMeasure()
{ }

@Override
public double measure(Geometry g1, Geometry g2) {

// Check if input is of same type
if (!g1.getGeometryType().equals(g2.getGeometryType()))
throw new IllegalArgumentException("g1 and g2 are of different type");

// Compute the distance
double frechetDistance = DiscreteFrechetDistance.distance(g1, g2);
if (frechetDistance == 0d) return 1;

// Compute envelope diagonal size
Envelope env = new Envelope(g1.getEnvelopeInternal());
env.expandToInclude(g2.getEnvelopeInternal());
double envDiagSize = HausdorffSimilarityMeasure.diagonalSize(env);

// normalize so that more similarity produces a measure closer to 1
return 1 - frechetDistance / envDiagSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
* <p>
* The measure is computed by computing the Hausdorff distance
* between the input geometries, and then normalizing
* this by dividing it by the diagonal distance across
* this by dividing it by the diagonal distance across
* the envelope of the combined geometries.
*
*
* @author mbdavis
*
*/
public class HausdorffSimilarityMeasure
public class HausdorffSimilarityMeasure
implements SimilarityMeasure
{
/*
Expand All @@ -40,36 +40,39 @@ public static double measure(Geometry a, Geometry b)
return gv.measure();
}
*/

public HausdorffSimilarityMeasure()
{
}

/*
* Densify a small amount to increase accuracy of Hausdorff distance
*/
private static final double DENSIFY_FRACTION = 0.25;

public double measure(Geometry g1, Geometry g2)
{
{
double distance = DiscreteHausdorffDistance.distance(g1, g2, DENSIFY_FRACTION);

if (distance == 0d) return 1d;

Envelope env = new Envelope(g1.getEnvelopeInternal());
env.expandToInclude(g2.getEnvelopeInternal());
double envSize = diagonalSize(env);
// normalize so that more similarity produces a measure closer to 1

// normalize so that more similarity produces a measure closer to 1
double measure = 1 - distance / envSize;

//System.out.println("Hausdorff distance = " + distance + ", measure = " + measure);
return measure;
}

public static double diagonalSize(Envelope env)
{
if (env.isNull()) return 0.0;
double width = env.getWidth();

double width = env.getWidth();
double hgt = env.getHeight();

return Math.sqrt(width * width + hgt * hgt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.locationtech.jts.algorithm.distance;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;

/**
* Linear Discrete Fréchet Distance computation
*/
public class DiscreteFrechetDistanceLinear {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this class do differently to DiscreteFrechetDistance? Are they both needed?


/**
* Computes the Discrete Fréchet Distance between two {@link Geometry}s
* using a {@code cartesian} distance computation function.
*
* @param g0 the 1st geometry
* @param g1 the 2nd geometry
* @return the cartesian distance between {#g0} and {#g1}
*/
public static double distance(Geometry g0, Geometry g1) {
DiscreteFrechetDistanceLinear dist = new DiscreteFrechetDistanceLinear(g0, g1, false);
return dist.distance();
}

/**
* Computes the Discrete Fréchet Distance between two {@link Geometry}s
* using a {@code cartesian} distance computation function.
*
* @param g0 the 1st geometry
* @param g1 the 2nd geometry
* @return the cartesian distance between {#g0} and {#g1}
*/
public static double distance(Geometry g0, Geometry g1, boolean getCoordinates) {
DiscreteFrechetDistanceLinear dist = new DiscreteFrechetDistanceLinear(g0, g1, getCoordinates);
return dist.distance();
}
private final Geometry g0;
private final Geometry g1;
private final boolean getCoordinates;

private DiscreteFrechetDistanceLinear(Geometry g0, Geometry g1, boolean getCoordinates) {
this.g0 = g0;
this.g1 = g1;
this.getCoordinates = getCoordinates;
}

public double distance() {

Coordinate[] coords0 = this.g0.getCoordinates();
Coordinate[] coords1 = this.g1.getCoordinates();
double[][] distances = new double[coords0.length][];
for (int i = 0; i < coords0.length; i++)
distances[i] = new double[coords1.length];

for (int i = 0; i < coords0.length; i++) {
for (int j = 0; j < coords1.length; j++)
{
double distance = coords0[i].distance(coords1[j]);
if (i > 0 && j > 0)
{
distances[i][j] = Math.max(Math.min(Math.min(distances[i-1][j], distances[i-1][j-1]), distances[i][j-1]), distance);
}
else if (i > 0)
{
distances[i][j] = Math.max(distances[i-1][0], distance);
}
else if (j > 0)
{
distances[i][j] = Math.max(distances[0][j-1], distance);
}
else
{
distances[i][j] = distance;
}
}
}

//System.out.println(toString(coords0.length, coords1.length, distances));
//System.out.println();
return distances[coords0.length-1][coords1.length-1];
}

/*
// For debugging purposes only
private static String toString(int numRows, int numCols,
double[][] sparse) {

StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < numRows; i++)
{
sb.append('[');
for(int j = 0; j < numCols; j++)
{
if (j > 0)
sb.append(", ");
sb.append(String.format("%8.4f", sparse[i][j]));
}
sb.append(']');
if (i < numRows - 1) sb.append(",\n");
}
sb.append(']');
return sb.toString();
}
*/

}

Large diffs are not rendered by default.

Loading