|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## New Built-In Projections\n", |
| 8 | + "\n", |
| 9 | + "ipyleaflet now supports custom map projections and includes 2 base layers for polar projections:\n", |
| 10 | + "NASA's Next GenerationBlue Marble 500m for the Arctic and Antarctic regions." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "from ipyleaflet import (\n", |
| 20 | + " Map,\n", |
| 21 | + " basemaps,\n", |
| 22 | + " basemap_to_tiles,\n", |
| 23 | + " Marker,\n", |
| 24 | + " TileLayer,\n", |
| 25 | + " WMSLayer,\n", |
| 26 | + " Polygon,\n", |
| 27 | + " GeoJSON,\n", |
| 28 | + " DrawControl,\n", |
| 29 | + " projections\n", |
| 30 | + ")\n", |
| 31 | + "\n", |
| 32 | + "def handle_draw(target, action, geo_json):\n", |
| 33 | + " print(action, geo_json)\n", |
| 34 | + " \n", |
| 35 | + "dc = DrawControl(marker={'shapeOptions': {'color': '#0000FF'}})\n", |
| 36 | + "dc.on_draw(handle_draw)\n", |
| 37 | + "\n", |
| 38 | + "# note that we need to use the same projection for the our layer and the map.\n", |
| 39 | + "m1 = Map(center=(90, 0),\n", |
| 40 | + " zoom=0,\n", |
| 41 | + " basemap=basemaps.NASAGIBS.BlueMarble3413,\n", |
| 42 | + " crs=projections.EPSG3413)\n", |
| 43 | + "m1.add_control(dc)\n", |
| 44 | + "m1" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Custom Projections\n", |
| 52 | + "\n", |
| 53 | + "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", |
| 54 | + "\n", |
| 55 | + "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**" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "# We're going to use https://georepository.com/crs_2163/US-National-Atlas-Equal-Area.html\n", |
| 65 | + "my_projection = {\n", |
| 66 | + " 'name': 'EPSG:2163',\n", |
| 67 | + " 'custom': True, #This is important, it tells ipyleaflet that this projection is not on the predefined ones.\n", |
| 68 | + " 'proj4def': '+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs',\n", |
| 69 | + " 'origin': [-2215235.82, 420644.41],\n", |
| 70 | + " 'bounds': [\n", |
| 71 | + " [-8046094.81, 1819060.18],\n", |
| 72 | + " [988364.71, 3511186.72] \n", |
| 73 | + " ],\n", |
| 74 | + " 'resolutions': [\n", |
| 75 | + " 8192.0,\n", |
| 76 | + " 4096.0,\n", |
| 77 | + " 2048.0,\n", |
| 78 | + " 1024.0,\n", |
| 79 | + " 512.0,\n", |
| 80 | + " 256.0\n", |
| 81 | + " ]\n", |
| 82 | + "}\n", |
| 83 | + "\n", |
| 84 | + "\n", |
| 85 | + "\n", |
| 86 | + "wms = WMSLayer(\n", |
| 87 | + " url='https://ahocevar.com/geoserver/wms',\n", |
| 88 | + " layers='ne:NE1_HR_LC_SR_W_DR',\n", |
| 89 | + " format='image/png',\n", |
| 90 | + " transparent=True,\n", |
| 91 | + " min_zoom=0,\n", |
| 92 | + " attribution='ahocevar geospatial',\n", |
| 93 | + " crs=my_projection # I'm asking this WMS service to reproject the tile layer using EPSG:2163\n", |
| 94 | + ")\n", |
| 95 | + "\n", |
| 96 | + "m2 = Map(center=(40, -104),\n", |
| 97 | + " zoom=0,\n", |
| 98 | + " layers=(wms,),\n", |
| 99 | + " crs=my_projection)\n", |
| 100 | + "\n", |
| 101 | + "\n", |
| 102 | + "dc2 = DrawControl(marker={'shapeOptions': {'color': '#0000FF'}})\n", |
| 103 | + "dc2.on_draw(handle_draw)\n", |
| 104 | + "\n", |
| 105 | + "m2.add_control(dc2)\n", |
| 106 | + "\n", |
| 107 | + "m2" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "metadata": { |
| 112 | + "kernelspec": { |
| 113 | + "display_name": "Python 3", |
| 114 | + "language": "python", |
| 115 | + "name": "python3" |
| 116 | + }, |
| 117 | + "language_info": { |
| 118 | + "codemirror_mode": { |
| 119 | + "name": "ipython", |
| 120 | + "version": 3 |
| 121 | + }, |
| 122 | + "file_extension": ".py", |
| 123 | + "mimetype": "text/x-python", |
| 124 | + "name": "python", |
| 125 | + "nbconvert_exporter": "python", |
| 126 | + "pygments_lexer": "ipython3", |
| 127 | + "version": "3.7.4" |
| 128 | + } |
| 129 | + }, |
| 130 | + "nbformat": 4, |
| 131 | + "nbformat_minor": 4 |
| 132 | +} |
0 commit comments