8000 add HilbertEncoder.sort(). by micycle1 · Pull Request #1135 · locationtech/jts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add HilbertEncoder.sort(). #1135

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -11,7 +11,13 @@
*/
package org.locationtech.jts.index.hprtree;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;

import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.shape.fractal.HilbertCode;

public class HilbertEncoder {
Expand Down Expand Up @@ -42,4 +48,73 @@ public int encode(Envelope env) {
return HilbertCode.encode(level, x, y);
}

/**
* Sorts a list of {@link Geometry} objects in-place by their spatial order
* using Hilbert curve encoding of their envelopes.
*
* @param geoms the list of geometries to sort
*/
public static void sort(List<Geometry> geoms) {
sort(geoms, 12);
}

/**
* Sorts a list of {@link Geometry} objects in-place by their spatial order
* using Hilbert curve encoding of their envelopes.
*
* @param geoms the list of geometries to sort
* @param level the resolution level for Hilbert curve encoding
*/
public static void sort(List<Geometry> geoms, int level) {
int n = geoms.size();
if (n < 2)
return;

Envelope globalExtent = new Envelope();
for (Geometry g : geoms) {
globalExtent.expandToInclude(g.getEnvelopeInternal());
}

HilbertEncoder encoder = new HilbertEncoder(level, globalExtent);
int[] keys = new int[n];
for (int i = 0; i < n; i++) {
Envelope e = geoms.get(i).getEnvelopeInternal();
keys[i] = encoder.encode(e);
}
sortInPlaceByKeys(keys, geoms);
}

private static <T> void sortInPlaceByKeys(int[] keys, List<T> values) {
final int n = keys.length;

Integer[] idx = IntStream.range(0, n).boxed().toArray(Integer[]::new);
Arrays.sort(idx, Comparator.comparingInt(i -> keys[i]));

// rearrange keys and values in-place by following permutation cycles,
// so that both arrays are sorted according to hilbert order key.
boolean[] seen = new boolean[n];
for (int i = 0; i < n; i++) {
if (seen[i] || idx[i] == i)
continue;

int cycleStart = i;
int j = i;
int savedKey = keys[j];
T savedVal = values.get(j);

do {
seen[j] = true;
int next = idx[j];
keys[j] = keys[next];
values.set(j, values.get(next));

j = next;
} while (j != cycleStart);

keys[j] = savedKey;
values.set(j, savedVal);
seen[j] = true;
}
}

}
0