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 DistanceOp optimization for Point-Point #1049

Merged
merged 1 commit into from
Apr 18, 2024
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
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);
}
}
}

}
Loading