Skip to content

Commit

Permalink
Add DistanceOp optimization for Point-Point (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts authored Apr 18, 2024
1 parent 93f0e35 commit eb2943c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ public double distance()
if (geom[0].isEmpty() || geom[1].isEmpty())
return 0.0;

//-- optimization for Point/Point case
if (geom[0] instanceof Point && geom[1] instanceof Point) {
return geom[0].getCoordinate().distance(geom[1].getCoordinate());
}

computeMinDistance();
return minDistance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package test.jts.perf.operation.distance;

import java.util.ArrayList;
import java.util.List;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;

import test.jts.perf.PerformanceTestCase;
import test.jts.perf.PerformanceTestRunner;

public class PointPointDistancePerfTest extends PerformanceTestCase {


public static void main(String args[]) {
PerformanceTestRunner.run(PointPointDistancePerfTest.class);
}

private Point[] grid;

public PointPointDistancePerfTest(String name) {
super(name);
setRunSize(new int[] {10000});
setRunIterations(1);
}

public void startRun(int npts)
{
System.out.println("\n------- Running with # pts = " + npts);
grid = createPointGrid(new Envelope(0, 10., 0, 10), npts);
}

private Point[] createPointGrid(Envelope envelope, int npts) {
List<Point> geoms = new ArrayList<Point>();
GeometryFactory fact = new GeometryFactory();
int nSide = (int) Math.sqrt(npts);
double xInc = envelope.getWidth() / nSide;
double yInc = envelope.getHeight() / nSide;
for (int i = 0; i < nSide; i++) {
for (int j = 0; j < nSide; j++) {
double x = envelope.getMinX() + i * xInc;
double y = envelope.getMinY() + i * yInc;
Point p = fact.createPoint(new Coordinate(x, y));
geoms.add(p);
}
}
return GeometryFactory.toPointArray(geoms);
}

public void runPoints() {
for (Geometry p1 : grid) {
for (Geometry p2 : grid) {
double dist = p1.distance(p2);
}
}
}

}

0 comments on commit eb2943c

Please sign in to comment.