|
| 1 | +/* |
| 2 | + * Copyright 2025 devemux86 |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify it under the |
| 5 | + * terms of the GNU Lesser General Public License as published by the Free Software |
| 6 | + * Foundation, either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 9 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 10 | + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU Lesser General Public License along with |
| 13 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | +package org.oscim.android.test; |
| 16 | + |
| 17 | +import android.app.Activity; |
| 18 | +import android.content.Intent; |
| 19 | +import android.graphics.Color; |
| 20 | +import android.net.Uri; |
| 21 | +import android.os.Bundle; |
| 22 | +import android.widget.Toast; |
| 23 | +import org.mapsforge.map.android.graphics.AndroidGraphicFactory; |
| 24 | +import org.mapsforge.map.android.hills.DemFolderAndroidContent; |
| 25 | +import org.mapsforge.map.layer.hills.AdaptiveClasyHillShading; |
| 26 | +import org.mapsforge.map.layer.hills.DemFolder; |
| 27 | +import org.oscim.android.cache.TileCache; |
| 28 | +import org.oscim.backend.CanvasAdapter; |
| 29 | +import org.oscim.core.MapPosition; |
| 30 | +import org.oscim.core.Tile; |
| 31 | +import org.oscim.layers.tile.bitmap.BitmapTileLayer; |
| 32 | +import org.oscim.layers.tile.buildings.BuildingLayer; |
| 33 | +import org.oscim.layers.tile.vector.VectorTileLayer; |
| 34 | +import org.oscim.layers.tile.vector.labeling.LabelLayer; |
| 35 | +import org.oscim.map.Viewport; |
| 36 | +import org.oscim.renderer.BitmapRenderer; |
| 37 | +import org.oscim.renderer.GLViewport; |
| 38 | +import org.oscim.scalebar.*; |
| 39 | +import org.oscim.theme.internal.VtmThemes; |
| 40 | +import org.oscim.tiling.ITileCache; |
| 41 | +import org.oscim.tiling.source.hills.HillshadingTileSource; |
| 42 | +import org.oscim.tiling.source.mapfile.MapFileTileSource; |
| 43 | +import org.oscim.tiling.source.mapfile.MapInfo; |
| 44 | + |
| 45 | +import java.io.FileInputStream; |
| 46 | +import java.util.logging.Logger; |
| 47 | + |
| 48 | +/** |
| 49 | + * Standard map view with hill shading. |
| 50 | + */ |
| 51 | +public class HillshadingActivity extends MapActivity { |
| 52 | + |
| 53 | + private static final Logger log = Logger.getLogger(HillshadingActivity.class.getName()); |
| 54 | + |
| 55 | + private static final boolean USE_CACHE = false; |
| 56 | + |
| 57 | + private static final int SELECT_MAP_FILE = 0; |
| 58 | + private static final int SELECT_DEM_DIR = 1; |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void onCreate(Bundle savedInstanceState) { |
| 62 | + super.onCreate(savedInstanceState); |
| 63 | + |
| 64 | + // Select map file |
| 65 | + Toast.makeText(this, "Select map file", Toast.LENGTH_SHORT).show(); |
| 66 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 67 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 68 | + intent.setType("*/*"); |
| 69 | + startActivityForResult(intent, SELECT_MAP_FILE); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 74 | + |
| 75 | + if (requestCode == SELECT_MAP_FILE) { |
| 76 | + if (resultCode != Activity.RESULT_OK || data == null) { |
| 77 | + finish(); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + Uri uri = data.getData(); |
| 83 | + |
| 84 | + MapFileTileSource tileSource = new MapFileTileSource(); |
| 85 | + //tileSource.setPreferredLanguage("en"); |
| 86 | + FileInputStream fis = (FileInputStream) getContentResolver().openInputStream(uri); |
| 87 | + tileSource.setMapFileInputStream(fis); |
| 88 | + |
| 89 | + VectorTileLayer tileLayer = mMap.setBaseMap(tileSource); |
| 90 | + mMap.setTheme(VtmThemes.MOTORIDER); |
| 91 | + |
| 92 | + mMap.layers().add(new BuildingLayer(mMap, tileLayer)); |
| 93 | + mMap.layers().add(new LabelLayer(mMap, tileLayer)); |
| 94 | + |
| 95 | + DefaultMapScaleBar mapScaleBar = new DefaultMapScaleBar(mMap); |
| 96 | + mapScaleBar.setScaleBarMode(DefaultMapScaleBar.ScaleBarMode.BOTH); |
| 97 | + mapScaleBar.setDistanceUnitAdapter(MetricUnitAdapter.INSTANCE); |
| 98 | + mapScaleBar.setSecondaryDistanceUnitAdapter(ImperialUnitAdapter.INSTANCE); |
| 99 | + mapScaleBar.setScaleBarPosition(MapScaleBar.ScaleBarPosition.BOTTOM_LEFT); |
| 100 | + |
| 101 | + MapScaleBarLayer mapScaleBarLayer = new MapScaleBarLayer(mMap, mapScaleBar); |
| 102 | + BitmapRenderer renderer = mapScaleBarLayer.getRenderer(); |
| 103 | + renderer.setPosition(GLViewport.Position.BOTTOM_LEFT); |
| 104 | + renderer.setOffset(5 * CanvasAdapter.getScale(), 0); |
| 105 | + mMap.layers().add(mapScaleBarLayer); |
| 106 | + |
| 107 | + MapInfo info = tileSource.getMapInfo(); |
| 108 | + if (!info.boundingBox.contains(mMap.getMapPosition().getGeoPoint())) { |
| 109 | + MapPosition pos = new MapPosition(); |
| 110 | + pos.setByBoundingBox(info.boundingBox, Tile.SIZE * 4, Tile.SIZE * 4); |
| 111 | + mMap.setMapPosition(pos); |
| 112 | + mPrefs.clear(); |
| 113 | + } |
| 114 | + |
| 115 | + // Select DEM folder |
| 116 | + Toast.makeText(this, "Select DEM folder", Toast.LENGTH_SHORT).show(); |
| 117 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); |
| 118 | + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 119 | + | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 120 | + | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION |
| 121 | + ); |
| 122 | + startActivityForResult(intent, SELECT_DEM_DIR); |
| 123 | + } catch (Exception e) { |
| 124 | + log.severe(e.toString()); |
| 125 | + finish(); |
| 126 | + } |
| 127 | + } else if (requestCode == SELECT_DEM_DIR) { |
| 128 | + if (resultCode != Activity.RESULT_OK || data == null) |
| 129 | + return; |
| 130 | + |
| 131 | + Uri uri = data.getData(); |
| 132 | + |
| 133 | + DemFolder demFolder = new DemFolderAndroidContent(uri, this, getContentResolver()); |
| 134 | + final AdaptiveClasyHillShading algorithm = new AdaptiveClasyHillShading() |
| 135 | + // You can make additional behavior adjustments |
| 136 | + .setAdaptiveZoomEnabled(true) |
| 137 | + // .setZoomMinOverride(0) |
| 138 | + // .setZoomMaxOverride(17) |
| 139 | + .setCustomQualityScale(1); |
| 140 | + HillshadingTileSource hillshadingTileSource = new HillshadingTileSource(Viewport.MIN_ZOOM_LEVEL, Viewport.MAX_ZOOM_LEVEL, demFolder, algorithm, 128, Color.BLACK, AndroidGraphicFactory.INSTANCE); |
| 141 | + if (USE_CACHE) { |
| 142 | + ITileCache tileCache = new TileCache(this, getExternalCacheDir().getAbsolutePath(), "hillshading"); |
| 143 | + hillshadingTileSource.setCache(tileCache); |
| 144 | + } |
| 145 | + mMap.layers().add(new BitmapTileLayer(mMap, hillshadingTileSource, 150)); |
| 146 | + mMap.clearMap(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments