Moved Custom Map classes to new folder.

Removed NOAA source from Map Source Arrays
Looking at drawing overlay on long press
This commit is contained in:
PWRxPSYCHO 2022-10-05 10:37:11 -04:00
parent 7d386583ff
commit 38b6fe04ef
5 changed files with 74 additions and 41 deletions

View file

@ -0,0 +1,42 @@
package com.geeksville.mesh.model.map
import android.util.Log
import android.view.MotionEvent
import org.osmdroid.api.IMapView
import org.osmdroid.config.Configuration
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay
import org.osmdroid.views.overlay.Polygon
class CirclePlottingOverlay(var distanceKm: Int) : Overlay() {
override fun onLongPress(e: MotionEvent, mapView: MapView): Boolean {
if (Configuration.getInstance().isDebugMapView) {
Log.d(IMapView.LOGTAG, "CirclePlottingOverlay onLongPress")
}
val pt = mapView.projection.fromPixels(e.x.toInt(), e.y.toInt(), null) as GeoPoint
/*
* <b>Note</b></b: when plotting a point off the map, the conversion from
* screen coordinates to map coordinates will return values that are invalid from a latitude,longitude
* perspective. Sometimes this is a wanted behavior and sometimes it isn't. We are leaving it up to you,
* the developer using osmdroid to decide on what is right for your application. See
* <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
* for more information and the discussion associated with this.
*/
//just in case the point is off the map, let's fix the coordinates
if (pt.longitude < -180) pt.longitude = pt.longitude + 360
if (pt.longitude > 180) pt.longitude = pt.longitude - 360
//latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
if (pt.latitude > 85.05112877980659) pt.latitude = 85.05112877980659
if (pt.latitude < -85.05112877980659) pt.latitude = -85.05112877980659
val circle: List<GeoPoint> = Polygon.pointsAsCircle(pt, distanceKm.toDouble())
val p = Polygon(mapView)
p.points = circle
p.title = "A circle"
mapView.overlayManager.add(p)
mapView.invalidate()
return true
}
}

View file

@ -0,0 +1,168 @@
package com.geeksville.mesh.model.map
import org.osmdroid.tileprovider.tilesource.ITileSource
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.tileprovider.tilesource.TileSourcePolicy
import org.osmdroid.util.MapTileIndex
class CustomTileSource {
companion object {
// Map Server information: https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer
// Arcgis Information: https://www.arcgis.com/home/item.html?id=10df2279f9684e4a9f6a7f08febac2a9
private val ESRI_IMAGERY = object : OnlineTileSourceBase(
"ESRI World Overview", 0, 18, 256, ".jpg", arrayOf(
"https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/"
), "Esri, Maxar, Earthstar Geographics, and the GIS User Community",
TileSourcePolicy(
2,
TileSourcePolicy.FLAG_NO_BULK
or TileSourcePolicy.FLAG_NO_PREVENTIVE
or TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
or TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
)
) {
override fun getTileURLString(pMapTileIndex: Long): String {
return baseUrl + (MapTileIndex.getZoom(pMapTileIndex)
.toString() + "/" + MapTileIndex.getY(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ mImageFilenameEnding)
}
}
//Transparent Background
//https://earthlive.maptiles.arcgis.com/arcgis/rest/services/GOES/GOES31D/MapServer/tile/
private val NOAA_RADAR = object : OnlineTileSourceBase(
"NOAA GOES Radar",
0,
18,
256,
"",
arrayOf(
"https://earthlive.maptiles.arcgis.com/arcgis/rest/services/GOES/GOES31C/MapServer/tile/"
),
"Dataset Citation: GOES-R Calibration Working Group and GOES-R Series Program, (2017): NOAA GOES-R Series Advanced Baseline Imager (ABI) Level 1b Radiances Band 13. NOAA National Centers for Environmental Information. doi:10.7289/V5BV7DSR",
TileSourcePolicy(
2,
TileSourcePolicy.FLAG_NO_BULK
or TileSourcePolicy.FLAG_NO_PREVENTIVE
or TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
or TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
)
) {
override fun getTileURLString(pMapTileIndex: Long): String {
return baseUrl + (MapTileIndex.getZoom(pMapTileIndex)
.toString() + "/" + MapTileIndex.getY(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ mImageFilenameEnding)
}
}
private val USGS_HYDRO_CACHE = object : OnlineTileSourceBase(
"USGS Hydro Cache",
0,
18,
256,
"",
arrayOf(
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSHydroCached/MapServer/tile/"
),
"USGS",
TileSourcePolicy(
2,
TileSourcePolicy.FLAG_NO_PREVENTIVE
or TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
or TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
)
) {
override fun getTileURLString(pMapTileIndex: Long): String {
return baseUrl + (MapTileIndex.getZoom(pMapTileIndex)
.toString() + "/" + MapTileIndex.getY(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ mImageFilenameEnding)
}
}
private val USGS_SHADED_RELIEF = object : OnlineTileSourceBase(
"USGS Shaded Relief Only",
0,
18,
256,
"",
arrayOf(
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSShadedReliefOnly/MapServer/tile/"
),
"USGS",
TileSourcePolicy(
2,
TileSourcePolicy.FLAG_NO_PREVENTIVE
or TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
or TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
)
) {
override fun getTileURLString(pMapTileIndex: Long): String {
return baseUrl + (MapTileIndex.getZoom(pMapTileIndex)
.toString() + "/" + MapTileIndex.getY(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ mImageFilenameEnding)
}
}
/**
* WMS TILE SERVER
* More research is required to get this to function correctly with overlays
*/
val NOAA_RADAR_WMS = NOAAWmsTileSource(
"Recent Weather Radar",
arrayOf("https://new.nowcoast.noaa.gov/arcgis/services/nowcoast/radar_meteo_imagery_nexrad_time/MapServer/WmsServer?"),
"1",
"1.3.0",
"",
"EPSG%3A3857",
"",
"image/png"
)
val NOAA_SATELLITE_RADAR_WMS = NOAAWmsTileSource(
"Weather Satellite Imagery",
arrayOf("https://new.nowcoast.noaa.gov/arcgis/services/nowcoast/sat_meteo_imagery_time/MapServer/WmsServer?"),
"1,5,9,13,17,21,25",
"1.3.0",
"",
"EPSG%3A3857",
"",
"image/png"
)
/**
* ===============================================================================================
*/
private val MAPNIK: OnlineTileSourceBase = TileSourceFactory.MAPNIK
private val USGS_TOPO: OnlineTileSourceBase = TileSourceFactory.USGS_TOPO
private val USGS_SAT: OnlineTileSourceBase = TileSourceFactory.USGS_SAT
val DEFAULT_TILE_SOURCE: OnlineTileSourceBase = TileSourceFactory.DEFAULT_TILE_SOURCE
/**
* The order in this list must match that in the arrays.xml under map_styles
*/
val mTileSources: List<ITileSource> =
listOf(
MAPNIK,
USGS_TOPO,
USGS_SAT,
ESRI_IMAGERY,
)
fun getTileSource(aName: String): ITileSource {
for (tileSource: ITileSource in mTileSources) {
if (tileSource.name().equals(aName)) {
return tileSource;
}
}
throw IllegalArgumentException("No such tile source: $aName")
}
}
}

View file

@ -0,0 +1,146 @@
package com.geeksville.mesh.model.map
import android.content.res.Resources
import android.util.Log
import org.osmdroid.api.IMapView
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase
import org.osmdroid.util.MapTileIndex
import kotlin.math.atan
import kotlin.math.pow
import kotlin.math.sinh
open class NOAAWmsTileSource(
aName: String,
aBaseUrl: Array<String>,
layername: String,
version: String,
time: String?,
crs: String,
style: String?,
format: String
) : OnlineTileSourceBase(aName, 0, 22, 256, "png", aBaseUrl) {
// array indexes for array to hold bounding boxes.
private val MINX = 0
private val MAXX = 1
private val MINY = 2
private val MAXY = 3
// Web Mercator n/w corner of the map.
private val TILE_ORIGIN = doubleArrayOf(-20037508.34789244, 20037508.34789244)
//array indexes for that data
private val ORIG_X = 0
private val ORIG_Y = 1 // "
// Size of square world map in meters, using WebMerc projection.
private val MAP_SIZE = 20037508.34789244 * 2
private var layer = ""
private var version = "1.1.0"
private var crs = "EPSG:3A3857" //used by geo server
private var size = ""
private var format = ""
private var time = ""
private var style: String? = null
private var forceHttps = false
private var forceHttp = false
init {
Log.i(IMapView.LOGTAG, "WMS support is BETA. Please report any issues")
layer = layername
this.version = version
this.crs = crs
this.style = style
this.format = format
if (time != null) this.time = time
}
// fun createFrom(endpoint: WMSEndpoint, layer: WMSLayer): WMSTileSource? {
// var srs: String? = "EPSG:900913"
// if (layer.srs.isNotEmpty()) {
// srs = layer.srs[0]
// }
// return if (layer.styles.isEmpty()) {
// WMSTileSource(
// layer.name, arrayOf(endpoint.baseurl), layer.name,
// endpoint.wmsVersion, srs, null, layer.pixelSize
// )
// } else WMSTileSource(
// layer.name, arrayOf(endpoint.baseurl), layer.name,
// endpoint.wmsVersion, srs, layer.styles[0], layer.pixelSize
// )
// }
private fun tile2lon(x: Int, z: Int): Double {
return x / 2.0.pow(z.toDouble()) * 360.0 - 180
}
private fun tile2lat(y: Int, z: Int): Double {
val n = Math.PI - 2.0 * Math.PI * y / 2.0.pow(z.toDouble())
return Math.toDegrees(atan(sinh(n)))
}
// Return a web Mercator bounding box given tile x/y indexes and a zoom
// level.
private fun getBoundingBox(x: Int, y: Int, zoom: Int): DoubleArray {
val tileSize = MAP_SIZE / 2.0.pow(zoom.toDouble())
val minx = TILE_ORIGIN[ORIG_X] + x * tileSize
val maxx = TILE_ORIGIN[ORIG_X] + (x + 1) * tileSize
val miny = TILE_ORIGIN[ORIG_Y] - (y + 1) * tileSize
val maxy = TILE_ORIGIN[ORIG_Y] - y * tileSize
val bbox = DoubleArray(4)
bbox[MINX] = minx
bbox[MINY] = miny
bbox[MAXX] = maxx
bbox[MAXY] = maxy
return bbox
}
fun isForceHttps(): Boolean {
return forceHttps
}
fun setForceHttps(forceHttps: Boolean) {
this.forceHttps = forceHttps
}
fun isForceHttp(): Boolean {
return forceHttp
}
fun setForceHttp(forceHttp: Boolean) {
this.forceHttp = forceHttp
}
override fun getTileURLString(pMapTileIndex: Long): String? {
var baseUrl = baseUrl
if (forceHttps) baseUrl = baseUrl.replace("http://", "https://")
if (forceHttp) baseUrl = baseUrl.replace("https://", "http://")
val sb = StringBuilder(baseUrl)
if (!baseUrl.endsWith("&"))
sb.append("service=WMS")
sb.append("&request=GetMap")
sb.append("&version=").append(version)
sb.append("&layers=").append(layer)
if (style != null) sb.append("&styles=").append(style)
sb.append("&format=").append(format)
sb.append("&transparent=true")
sb.append("&height=").append(Resources.getSystem().displayMetrics.heightPixels)
sb.append("&width=").append(Resources.getSystem().displayMetrics.widthPixels)
sb.append("&crs=").append(crs)
sb.append("&bbox=")
val bbox = getBoundingBox(
MapTileIndex.getX(pMapTileIndex),
MapTileIndex.getY(pMapTileIndex),
MapTileIndex.getZoom(pMapTileIndex)
)
sb.append(bbox[MINX]).append(",")
sb.append(bbox[MINY]).append(",")
sb.append(bbox[MAXX]).append(",")
sb.append(bbox[MAXY])
Log.i(IMapView.LOGTAG, sb.toString())
return sb.toString()
}
}