-
Notifications
You must be signed in to change notification settings - Fork 454
Add DiscreteFrechetDistance #764
New issue
Have a question about this project? Sign up for a free GitHub accou 8000 nt 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
Merged
dr-jts
merged 9 commits into
locationtech:master
from
FObermaier:enhancement/DiscreteFrechetDistance
Oct 13, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c407274
Add DiscreteFrechetDistance
FObermaier 3692433
Add FrechetSimilarityMeasure
FObermaier ff6476c
Fix copy/paste leftover
FObermaier 776a4a2
Rename DistanceFunction to DistanceMetric
FObermaier 6d68c96
Remove DistanceMetric
FObermaier 7ed25f5
Fix computeFrechet function
FObermaier 9aaa5ba
Clean up
FObermaier a73f6a9
Polish
FObermaier 9a26ed5
Make MatrixStorage implementations package private
FObermaier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
569 changes: 569 additions & 0 deletions
569
...s/core/src/main/java/org/locationtech/jts/algorithm/distance/DiscreteFrechetDistance.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...les/core/src/main/java/org/locationtech/jts/algorithm/match/FrechetSimilarityMeasure.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
.../src/test/java/org/locationtech/jts/algorithm/distance/DiscreteFrechetDistanceLinear.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
/** | ||
* 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); | ||
CB34 | } | |
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(); | ||
} | ||
*/ | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...re/src/test/java/org/locationtech/jts/algorithm/distance/DiscreteFrechetDistanceTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?