Skip to content

Commit

Permalink
Merge pull request #1459 from osmdroid/feature/#1409
Browse files Browse the repository at this point in the history
feature/#1409 - downgrade mode for PolyOverlayWithIW when the projected poly is too small
  • Loading branch information
monsieurtanuki committed Jan 9, 2020
2 parents f66d3e0 + 0af2be2 commit 5ea52e0
Show file tree
Hide file tree
Showing 17 changed files with 405 additions and 496 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.osmdroid.samplefragments.data;

import org.osmdroid.R;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
Expand All @@ -10,7 +9,6 @@
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.infowindow.BasicInfoWindow;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.Polygon;
Expand All @@ -20,8 +18,6 @@

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.util.Log;

Expand All @@ -40,14 +36,9 @@ public class SampleOsmPath extends BaseSampleFragment implements MapListener {
public static final String TITLE = "OsmPath drawing";

private BoundingBox sCentralParkBoundingBox;
private Paint sPaint;

public SampleOsmPath() {
sCentralParkBoundingBox = new BoundingBox(40.796788, -73.949232, 40.768094, -73.981762);

sPaint = new Paint();
sPaint.setColor(Color.argb(175, 255, 0, 0));
sPaint.setStyle(Style.FILL);
}
@Override
public String getSampleTitle() {
Expand All @@ -57,8 +48,8 @@ public String getSampleTitle() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {

mMapView.getController().setZoom(13);
mMapView.getController().setCenter(sCentralParkBoundingBox.getCenter());
mMapView.getController().setZoom(13.);
mMapView.getController().setCenter(sCentralParkBoundingBox.getCenterWithDateLine());

super.onActivityCreated(savedInstanceState);
}
Expand All @@ -70,14 +61,12 @@ protected void addOverlays() {
//we override this to force zoom to 22, even though mapnik dooesn't do that deep
OnlineTileSourceBase mapnik = new XYTileSource("Mapnik",
0, 22, 256, ".png", new String[] {
"http://a.tile.openstreetmap.org/",
"http://b.tile.openstreetmap.org/",
"http://c.tile.openstreetmap.org/" });
"https://a.tile.openstreetmap.org/",
"https://b.tile.openstreetmap.org/",
"https://c.tile.openstreetmap.org/" });
mMapView.getTileProvider().setTileSource(mapnik);


//mOsmPathOverlay = new OsmPathOverlay(context);
//mMapView.getOverlayManager().add(mOsmPathOverlay);
Polyline line = new Polyline(mMapView);
line.setTitle("Central Park, NYC");
line.setSubDescription(Polyline.class.getCanonicalName());
Expand Down Expand Up @@ -163,7 +152,7 @@ public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {

List<MyMapItem> list = new ArrayList<>();
list.add(new MyMapItem("title","description", new GeoPoint(51.7875, 6.135278)));
ItemizedIconOverlay<MyMapItem> layer = new ItemizedIconOverlay<MyMapItem>(list, getResources().getDrawable(R.drawable.shgpuci), new ItemizedIconOverlay.OnItemGestureListener<MyMapItem>() {
ItemizedIconOverlay<MyMapItem> layer = new ItemizedIconOverlay<>(list, getResources().getDrawable(R.drawable.shgpuci), new ItemizedIconOverlay.OnItemGestureListener<MyMapItem>() {
@Override
public boolean onItemSingleTapUp(int index, MyMapItem item) {
return false;
Expand All @@ -175,21 +164,8 @@ public boolean onItemLongPress(int index, MyMapItem item) {
}
}, getActivity());

PathOverlay path = new PathOverlay(Color.RED, 200f);

List<IGeoPoint> pathpoints = new ArrayList<>();


pathpoints.add(new GeoPoint(50.7865, 6.135278));
pathpoints.add(new GeoPoint(50.7865, 6.135288));
pathpoints.add(new GeoPoint(51.7864, 6.235288));
pathpoints.add(new GeoPoint(51.7864, 6.235288));
pathpoints.add(new GeoPoint(51.7865, 6.235278));

path.addPoints(pathpoints);
mMapView.getOverlayManager().add(path);
mMapView.getOverlayManager().add(layer);
mMapView.setMapListener(this);
mMapView.addMapListener(this);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.osmdroid.samplefragments.data;

import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -18,8 +18,8 @@
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.FolderOverlay;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.PolyOverlayWithIW;
import org.osmdroid.views.overlay.Polygon;

import java.io.File;
Expand Down Expand Up @@ -114,6 +114,17 @@ public void onSelectedFilePaths(String[] files) {
//files is the array of the paths of files selected by the Application User.
try {
List<Overlay> folder = ShapeConverter.convert(mMapView, new File(files[0]));
for (final Overlay item : folder) {
if (item instanceof PolyOverlayWithIW) {
final PolyOverlayWithIW poly = (PolyOverlayWithIW)item;
poly.setDowngradeMaximumPixelSize(20);
poly.setDowngradeDisplay(true);
final Paint paint = poly.getOutlinePaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
}
mMapView.getOverlayManager().addAll(folder);
mMapView.invalidate();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.view.View;
import android.widget.Toast;

import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
Expand Down Expand Up @@ -49,12 +48,11 @@ public enum Mode{
PolylineAsPath
}
protected boolean withArrows=false;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private MapView map;
private List<Point> pts = new ArrayList<>();
private Paint mPaint;
private final Paint mPaint;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

Expand All @@ -64,30 +62,27 @@ public enum Mode{
public CustomPaintingSurface(Context context, AttributeSet attrs) {
super(context,attrs);
mPath = new Path();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
final Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(bitmap);
}


@Override
protected void onDraw(Canvas canvas) {

mCanvas = new Canvas(mBitmap);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);

canvas.drawPath(mPath, mPaint);
}
public void init(MapView mapView) {
Expand Down Expand Up @@ -169,7 +164,7 @@ public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {
));
line.setMilestoneManagers(managers);
}
line.setSubDescription(BoundingBox.fromGeoPoints(line.getPoints()).toString());
line.setSubDescription(line.getBounds().toString());
map.getOverlayManager().add(line);
lastPolygon=null;
break;
Expand All @@ -195,7 +190,7 @@ public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {
public boolean onClick(Polygon polygon, MapView mapView, GeoPoint eventPos) {
lastPolygon = polygon;
polygon.onClickDefault(polygon, mapView, eventPos);
Toast.makeText(mapView.getContext(), "polygon with " + polygon.getPoints().size() + "pts was tapped", Toast.LENGTH_LONG).show();
Toast.makeText(mapView.getContext(), "polygon with " + polygon.getActualPoints().size() + "pts was tapped", Toast.LENGTH_LONG).show();
return false;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
* The counters class is a simple container for tracking various internal statistics for osmdroid,
* useful for troubleshooting osmdroid, finding memory leaks and more
Expand All @@ -23,6 +26,11 @@ public class Counters {
public static int fileCacheOOM=0;
public static int fileCacheHit=0;

/**
* @since 6.2.0
*/
private static final Map<String, Integer> sMap = new HashMap<>();

public static void printToLogcat() {
Log.d(TAG, "countOOM " + countOOM);
Log.d(TAG, "tileDownloadErrors " + tileDownloadErrors);
Expand All @@ -39,4 +47,35 @@ public static void reset(){
fileCacheOOM=0;
fileCacheHit=0;
}

/**
* @since 6.2.0
*/
public static void reset(final String pTag) {
sMap.remove(pTag);
}

/**
* @since 6.2.0
*/
public static void increment(final String pTag) {
final Integer value = sMap.get(pTag);
if (value == null) {
sMap.put(pTag, 1);
} else {
sMap.put(pTag, value + 1);
}
}

/**
* @since 6.2.0
*/
public static int get(final String pTag) {
final Integer value = sMap.get(pTag);
if (value == null) {
return 0;
} else {
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ abstract public class TileSystem {
/**
* @since 6.0.2
* Used to be in the `TileSystem` class of another package
* @deprecated Just don't use it anymore
*/
@Deprecated
public static final int projectionZoomLevel = primaryKeyMaxZoomLevel + 1;

/**
Expand Down
19 changes: 13 additions & 6 deletions osmdroid-android/src/main/java/org/osmdroid/views/Projection.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
public class Projection implements IProjection {

/**
* Size of the "projected" map: a virtual map with the largest zoom level
* WARNING: `mProjectedMapSize` MUST NOT be a static member,
* as it depends on {@link TileSystem#getTileSize()}
* The size in pixels of a VERY large map, the "projected" map.
* For optimization purpose, we may compute only once the projection of the GeoPoints
* on this large map, and then just divide in order to get the projection on a corresponding
* smaller map / smaller zoom
*/
public final double mProjectedMapSize = TileSystem.MapSize((double)TileSystem.projectionZoomLevel);
public static final double mProjectedMapSize = 1L << 60;
private long mOffsetX;
private long mOffsetY;
private long mScrollX;
Expand Down Expand Up @@ -444,8 +445,7 @@ public Matrix getScaleRotateCanvasMatrix() {
* @since 6.0.0
*/
public double getProjectedPowerDifference() {
final double zoomDifference = TileSystem.projectionZoomLevel - getZoomLevel();
return TileSystem.getFactor(zoomDifference);
return mProjectedMapSize / getWorldMapSize();
}

/**
Expand Down Expand Up @@ -860,4 +860,11 @@ public int getWidth() {
public int getHeight() {
return mIntrinsicScreenRectProjection.height();
}

/**
* @since 6.2.0
*/
public double getWorldMapSize() {
return mMercatorMapSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ private void onDrawHelper(final Canvas c, final MapView pMapView, final Projecti
if (pMapView != null) {
overlay.draw(c, pMapView, false);
} else {
overlay.draw(c, pProjection);
if (pProjection.getBoundingBox().overlaps(overlay.getBounds(), pProjection.getZoomLevel()))
overlay.draw(c, pProjection);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,29 @@ public void setIntegerAccepter(final IntegerAccepter pIntegerAccepter) {

@Override
public void flush() {
if(getSize() < 4) {
final int nbSegments = getSize() / 4;
if(nbSegments == 0) {
additionalFlush();
return;
}
final float[] lines = getLines();
final Paint paint = mPaintList.getPaint();
if (paint != null) { // monochromatic: that's enough
mCanvas.drawLines(lines, 0, getSize(), paint);
final int size = compact(lines, nbSegments * 4);
if (size > 0) {
mCanvas.drawLines(lines, 0, size, paint);
}
additionalFlush();
return;
}
final int size = getSize();
for (int i = 0; i < size ; i += 4) {
for (int i = 0; i < nbSegments * 4 ; i += 4) {
final float x0 = lines[i];
final float y0 = lines[i + 1];
final float x1 = lines[i + 2];
final float y1 = lines[i + 3];
if (x0 == x1 && y0 == y1) {
continue;
}
final int segmentIndex = mIntegerAccepter.getValue(i / 2);
mCanvas.drawLine(x0, y0, x1, y1, mPaintList.getPaint(segmentIndex, x0, y0, x1, y1));
}
Expand All @@ -68,4 +74,30 @@ private void additionalFlush() {
mIntegerAccepter.flush();
}
}

/**
* @since 6.2.0
* Compact a float[] containing (x0,y0,x1,y1) segment coordinate quadruplets
* by removing the single point cases (x0 == x1 && y0 == y1)
* @param pLines the input AND output array
* @param pSize the initial number of coordinates
* @return the number of relevant coordinates
*/
private static int compact(final float[] pLines, final int pSize) {
int dstIndex = 0;
for (int srcIndex = 0; srcIndex < pSize ; srcIndex += 4) {
final float x0 = pLines[srcIndex];
final float y0 = pLines[srcIndex + 1];
final float x1 = pLines[srcIndex + 2];
final float y1 = pLines[srcIndex + 3];
if (x0 == x1 && y0 == y1) {
continue;
}
if (srcIndex != dstIndex) {
System.arraycopy(pLines, srcIndex, pLines, dstIndex, 4);
}
dstIndex += 4;
}
return dstIndex;
}
}
Loading

0 comments on commit 5ea52e0

Please sign in to comment.