VectorLayer: add sort when displaying drawables (#1043)

This commit is contained in:
xiaoyan 2023-05-23 14:37:05 +08:00 committed by Emux
parent 1ee201a41f
commit fb0b88e916
No known key found for this signature in database
GPG Key ID: 64ED9980896038C3
3 changed files with 39 additions and 1 deletions

View File

@ -44,6 +44,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.oscim.core.MercatorProjection.latitudeToY;
@ -66,6 +68,7 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
protected final SpatialIndex<Drawable> mDrawables = new QuadTree<Drawable>(1 << 30, 18);
protected final List<Drawable> tmpDrawables = new ArrayList<Drawable>(128);
private final Comparator<Drawable> mComparator;
protected final JtsConverter mConverter;
protected double mMinX;
@ -74,10 +77,16 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
private static class GeometryWithStyle implements Drawable {
final Geometry geometry;
final Style style;
final int priority;
GeometryWithStyle(Geometry g, Style s) {
this(g, s, 0);
}
GeometryWithStyle(Geometry g, Style s, int p) {
geometry = g;
style = s;
priority = p;
}
@Override
@ -89,6 +98,11 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
public Geometry getGeometry() {
return geometry;
}
@Override
public int getPriority() {
return priority;
}
}
protected Polygon mEnvelope;
@ -100,6 +114,12 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
public VectorLayer(Map map) {
super(map);
mConverter = new JtsConverter(Tile.SIZE / UNSCALE_COORD);
mComparator = new Comparator<Drawable>() {
@Override
public int compare(Drawable o1, Drawable o2) {
return Integer.compare(o1.getPriority(), o2.getPriority());
}
};
}
private static Box bbox(Geometry geometry, Style style) {
@ -199,7 +219,8 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
synchronized (this) {
tmpDrawables.clear();
mDrawables.search(bbox, tmpDrawables);
// TODO sort by some order...
// sort by some order...
Collections.sort(tmpDrawables, mComparator);
for (Drawable d : tmpDrawables) {
Style style = d.getStyle();

View File

@ -13,4 +13,11 @@ public interface Drawable {
* @return
*/
public Geometry getGeometry();
/**
* Priority of drawable, the larger the value, the higher it will appear when drawn in the VectorLayer.
*
* @see org.oscim.layers.vector.VectorLayer processFeatures() method
*/
public int getPriority();
}

View File

@ -20,6 +20,7 @@ public class JtsDrawable implements Drawable {
protected Style style;
protected Geometry geometry;
protected int priority = 0;
public JtsDrawable(Style style) {
this.style = style;
@ -53,6 +54,15 @@ public class JtsDrawable implements Drawable {
return geometry;
}
@Override
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
protected static GeomBuilder loadPoints(GeomBuilder gb, List<GeoPoint> points) {
for (GeoPoint point : points) {
gb.point(point.getLongitude(),