{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## New Built-In Projections\n", "\n", "ipyleaflet now supports custom map projections and includes base layers for polar projections:\n", "- NASA's Next Generation Blue Marble 500m for the Arctic and Antarctic regions\n", "- Esri Arctic Ocean Basemap and Antarctic Basemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipyleaflet import (\n", " Map,\n", " basemaps,\n", " basemap_to_tiles,\n", " Marker,\n", " TileLayer,\n", " WMSLayer,\n", " Polygon,\n", " GeoJSON,\n", " DrawControl,\n", " projections,\n", ")\n", "\n", "\n", "def handle_draw(target, action, geo_json):\n", " print(action, geo_json)\n", "\n", "\n", "dc = DrawControl(marker={\"shapeOptions\": {\"color\": \"#0000FF\"}})\n", "dc.on_draw(handle_draw)\n", "\n", "# note that we need to use the same projection for the layer and the map.\n", "m1 = Map(\n", " center=(90, 0),\n", " zoom=0,\n", " basemap=basemaps.NASAGIBS.BlueMarble3413,\n", " crs=projections.EPSG3413.NASAGIBS,\n", ")\n", "m1.add(dc)\n", "m1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# note that we need to use the same projection for the layer and the map.\n", "m2 = Map(\n", " center=(-90, 0),\n", " zoom=0,\n", " basemap=basemaps.Esri.AntarcticBasemap,\n", " crs=projections.EPSG3031.ESRIBasemap,\n", ")\n", "\n", "# add draw control on Antarctic map\n", "dc2 = DrawControl(marker={\"shapeOptions\": {\"color\": \"#0000FF\"}})\n", "dc2.on_draw(handle_draw)\n", "m2.add(dc2)\n", "\n", "# MODIS Mosaic of Antarctica (MOA)\n", "MOA3031 = dict(\n", " name='EPSG:3031',\n", " custom=True,\n", " proj4def=\"\"\"+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1\n", " +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs\"\"\",\n", " bounds=[[-3174450,-2816050],[2867175,2406325]]\n", ")\n", "\n", "MOA = WMSLayer(\n", " attribution=\"\"\"\n", " U.S. Geological Survey (USGS), British Antarctic Survey (BAS),\n", " National Aeronautics and Space Administration (NASA)\n", " \"\"\",\n", " layers=\"MOA_125_HP1_090_230\",\n", " format='image/png',\n", " transparent=False,\n", " opacity=0.5,\n", " url='https://nimbus.cr.usgs.gov/arcgis/services/Antarctica/USGS_EROS_Antarctica_Reference/MapServer/WmsServer',\n", " crs=MOA3031\n", ")\n", "m2.add(MOA)\n", "\n", "m2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom Projections\n", "\n", "Chances are that if you are reading this you want to use a custom projection for your map. This is now supported via [Proj4Leaflet](https://github.com/kartena/Proj4Leaflet). The only thing you need to do is to declare the properties of your projection and Leaflet will do the rest.\n", "\n", "Keep in mind that this feature is for predefined projections, so if you use a backend as GeoServer, MapServer etc, they can do the reprojection for you and you can use that projection to show your layers in a map. **ipyleaflet won't reproject your layer on the fly**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# We're going to use https://georepository.com/crs_2163/US-National-Atlas-Equal-Area.html\n", "my_projection = {\n", " \"name\": \"EPSG:2163\",\n", " \"custom\": True, # This is important, it tells ipyleaflet that this projection is not on the predefined ones.\n", " \"proj4def\": \"+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs\",\n", " \"origin\": [-2215235.82, 420644.41],\n", " \"bounds\": [[-8046094.81, 1819060.18], [988364.71, 3511186.72]],\n", " \"resolutions\": [8192.0, 4096.0, 2048.0, 1024.0, 512.0, 256.0],\n", "}\n", "\n", "\n", "wms = WMSLayer(\n", " url=\"https://ahocevar.com/geoserver/wms\",\n", " layers=\"ne:NE1_HR_LC_SR_W_DR\",\n", " format=\"image/png\",\n", " transparent=True,\n", " min_zoom=0,\n", " attribution=\"ahocevar geospatial\",\n", " crs=my_projection, # I'm asking this WMS service to reproject the tile layer using EPSG:2163\n", ")\n", "\n", "m3 = Map(center=(40, -104), zoom=0, layers=(wms,), crs=my_projection)\n", "\n", "\n", "dc3 = DrawControl(marker={\"shapeOptions\": {\"color\": \"#0000FF\"}})\n", "dc3.on_draw(handle_draw)\n", "\n", "m3.add(dc3)\n", "\n", "m3" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 4 }