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

Gaussian smoothing and new geometry densifier #478

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jgaffuri
Copy link

I have developed these two algos which might be interested to be included in jts-core:

  • One to smooth geometries using a gaussian kernel
  • An other (which is used by the previous) to densify a geometry using a strategy different than the one used in the current Densifier class. A called it "little thumbling" strategy.

This is my first pull to JTS - let me know if I do it properly !

Signed-off-by: jgaffuri [email protected]

@jgaffuri
Copy link
Author

This relates to: https://gis.stackexchange.com/questions/326779/

@micycle1
Copy link

@jgaffuri I've incorporated your gaussian smoothing implementation into this computational geometry project.

@jgaffuri
Copy link
Author

Great job ! Thank you for your interest.

@micycle1
Copy link

micycle1 commented Aug 8, 2024

I'd note that the smoothing outcome is slightly sensitive to the vertex order of the input (i.e. which vertex the coordinate list begins at).

For me this isn't desirable so I use the code below to first normalise vertex order before smoothing.

But perhaps the algorithm itself can be tweaked instead?

	private static LineString normalise(LineString line) {
		boolean isClosed = line.isClosed();

		Coordinate[] originalCoords = line.getCoordinates();
		if (isClosed && originalCoords[0].equals2D(originalCoords[originalCoords.length - 1])) {
			// Remove last vertex if it is a duplicate of the first for closed lines
			originalCoords = Arrays.copyOf(originalCoords, originalCoords.length - 1);
		}

		// Find index of coordinate with smallest x value, tie by y value
		int minIndex = 0;
		for (int i = 1; i < originalCoords.length; i++) {
			if (originalCoords[i].x < originalCoords[minIndex].x
					|| (originalCoords[i].x == originalCoords[minIndex].x && originalCoords[i].y < originalCoords[minIndex].y)) {
				minIndex = i;
			}
		}

		// Rotate array to start from vertex with smallest x (and y, if tied)
		Coordinate[] rotatedCoords = new Coordinate[originalCoords.length + (isClosed ? 1 : 0)];
		System.arraycopy(originalCoords, minIndex, rotatedCoords, 0, originalCoords.length - minIndex);
		System.arraycopy(originalCoords, 0, rotatedCoords, originalCoords.length - minIndex, minIndex);

		if (isClosed) {
			rotatedCoords[rotatedCoords.length - 1] = rotatedCoords[0]; // Close the loop
		}

		return line.getFactory().createLineString(rotatedCoords);
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants