feat: Adds sort when displaying drawable on a VectorLayer

This commit is contained in:
xiaoyan 2023-05-23 14:37:05 +08:00
parent 1ee201a41f
commit ab81b7838c
3 changed files with 58 additions and 6 deletions

View File

@ -18,11 +18,21 @@
*/ */
package org.oscim.layers.vector; package org.oscim.layers.vector;
import static org.oscim.core.MercatorProjection.latitudeToY;
import static org.oscim.core.MercatorProjection.longitudeToX;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.*; import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.simplify.DouglasPeuckerSimplifier; import org.locationtech.jts.simplify.DouglasPeuckerSimplifier;
import org.oscim.backend.canvas.Color; import org.oscim.backend.canvas.Color;
import org.oscim.core.*; import org.oscim.core.Box;
import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapPosition;
import org.oscim.core.Tile;
import org.oscim.event.Gesture; import org.oscim.event.Gesture;
import org.oscim.event.GestureListener; import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent; import org.oscim.event.MotionEvent;
@ -44,11 +54,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import static org.oscim.core.MercatorProjection.latitudeToY;
import static org.oscim.core.MercatorProjection.longitudeToX;
/* TODO keep bounding box of geometries - only try to render when bbox intersects viewport */ /* TODO keep bounding box of geometries - only try to render when bbox intersects viewport */
/** /**
@ -75,9 +84,15 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
final Geometry geometry; final Geometry geometry;
final Style style; final Style style;
final int priority;
GeometryWithStyle(Geometry g, Style s) { GeometryWithStyle(Geometry g, Style s) {
this(g, s, 0);
}
GeometryWithStyle(Geometry g, Style s, int p) {
geometry = g; geometry = g;
style = s; style = s;
priority = p;
} }
@Override @Override
@ -89,6 +104,11 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
public Geometry getGeometry() { public Geometry getGeometry() {
return geometry; return geometry;
} }
@Override
public int getPriority() {
return 0;
}
} }
protected Polygon mEnvelope; protected Polygon mEnvelope;
@ -199,7 +219,18 @@ public class VectorLayer extends AbstractVectorLayer<Drawable> implements Gestur
synchronized (this) { synchronized (this) {
tmpDrawables.clear(); tmpDrawables.clear();
mDrawables.search(bbox, tmpDrawables); mDrawables.search(bbox, tmpDrawables);
// TODO sort by some order... // sort by some order...
Collections.sort(tmpDrawables, new Comparator<Drawable>() {
@Override
public int compare(Drawable o1, Drawable o2) {
if (o1.getPriority()<o2.getPriority()) {
return -1;
} else if (o1.getPriority()>o2.getPriority()) {
return 1;
}
return 0;
}
});
for (Drawable d : tmpDrawables) { for (Drawable d : tmpDrawables) {
Style style = d.getStyle(); Style style = d.getStyle();

View File

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

View File

@ -20,14 +20,20 @@ public class JtsDrawable implements Drawable {
protected Style style; protected Style style;
protected Geometry geometry; protected Geometry geometry;
protected int priority = 0;
public JtsDrawable(Style style) { public JtsDrawable(Style style) {
this.style = style; this.style = style;
} }
public JtsDrawable(Geometry geometry, Style style) { public JtsDrawable(Geometry geometry, Style style) {
this(geometry, style, 0);
}
public JtsDrawable(Geometry geometry, Style style, int priority) {
this.geometry = geometry; this.geometry = geometry;
this.style = style; this.style = style;
this.priority = priority;
} }
/** /**
@ -53,6 +59,15 @@ public class JtsDrawable implements Drawable {
return geometry; 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) { protected static GeomBuilder loadPoints(GeomBuilder gb, List<GeoPoint> points) {
for (GeoPoint point : points) { for (GeoPoint point : points) {
gb.point(point.getLongitude(), gb.point(point.getLongitude(),