Skip to content

Commit

Permalink
Improve PMTiles functions (opengeos#567)
Browse files Browse the repository at this point in the history
* Improve PMTiles functions

* Add try except for add_pmtiles

* Add pmtiles to setup.py
  • Loading branch information
giswqs committed Sep 24, 2023
1 parent b4b7c61 commit cbbff07
Show file tree
Hide file tree
Showing 5 changed files with 369 additions and 49 deletions.
137 changes: 131 additions & 6 deletions docs/notebooks/82_pmtiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import leafmap.foliumap as leafmap"
Expand All @@ -39,7 +41,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = \"https://open.gishub.org/data/pmtiles/protomaps_firenze.pmtiles\"\n",
Expand All @@ -51,7 +55,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
Expand Down Expand Up @@ -99,7 +105,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = \"https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles\"\n",
Expand All @@ -111,7 +119,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map(height='800px')\n",
Expand Down Expand Up @@ -172,11 +182,126 @@
"m.add_legend(legend_dict=legend_dict)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download [building footprints](https://github.com/opengeos/open-data/blob/main/datasets/libya/Derna_buildings.geojson) of Derna, Libya."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = 'https://raw.githubusercontent.com/opengeos/open-data/main/datasets/libya/Derna_buildings.geojson'\n",
"leafmap.download_file(url, 'buildings.geojson')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert vector to PMTiles."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pmtiles = 'buildings.pmtiles'\n",
"leafmap.vector_to_pmtiles(\n",
" 'buildings.geojson', \n",
" pmtiles, \n",
" layer_name='buildings', \n",
" overwrite=True, \n",
" quiet=True\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start a HTTP Sever "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"leafmap.start_server(port=8000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = f'http://127.0.0.1:8000/{pmtiles}'\n",
"# leafmap.pmtiles_metadata(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Diplay the PMTiles on the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
"\n",
"style = {\n",
" \"version\": 8,\n",
" \"sources\": {\n",
" \"example_source\": {\n",
" \"type\": \"vector\",\n",
" \"url\": \"pmtiles://\" + url,\n",
" \"attribution\": 'PMTiles',\n",
" }\n",
" },\n",
" \"layers\": [\n",
" {\n",
" \"id\": \"buildings\",\n",
" \"source\": \"example_source\",\n",
" \"source-layer\": \"buildings\",\n",
" \"type\": \"fill\",\n",
" \"paint\": {\"fill-color\": \"#3388ff\", \"fill-opacity\": 0.5},\n",
" },\n",
" ],\n",
"}\n",
"\n",
"m.add_pmtiles(url, name='Buildings', show=True, zoom_to_layer=True, style=style)\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand Down
137 changes: 131 additions & 6 deletions examples/notebooks/82_pmtiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import leafmap.foliumap as leafmap"
Expand All @@ -39,7 +41,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = \"https://open.gishub.org/data/pmtiles/protomaps_firenze.pmtiles\"\n",
Expand All @@ -51,7 +55,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
Expand Down Expand Up @@ -99,7 +105,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = \"https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles\"\n",
Expand All @@ -111,7 +119,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map(height='800px')\n",
Expand Down Expand Up @@ -172,11 +182,126 @@
"m.add_legend(legend_dict=legend_dict)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download [building footprints](https://github.com/opengeos/open-data/blob/main/datasets/libya/Derna_buildings.geojson) of Derna, Libya."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = 'https://raw.githubusercontent.com/opengeos/open-data/main/datasets/libya/Derna_buildings.geojson'\n",
"leafmap.download_file(url, 'buildings.geojson')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert vector to PMTiles."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pmtiles = 'buildings.pmtiles'\n",
"leafmap.vector_to_pmtiles(\n",
" 'buildings.geojson', \n",
" pmtiles, \n",
" layer_name='buildings', \n",
" overwrite=True, \n",
" quiet=True\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start a HTTP Sever "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"leafmap.start_server(port=8000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"url = f'http://127.0.0.1:8000/{pmtiles}'\n",
"# leafmap.pmtiles_metadata(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Diplay the PMTiles on the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"m = leafmap.Map()\n",
"\n",
"style = {\n",
" \"version\": 8,\n",
" \"sources\": {\n",
" \"example_source\": {\n",
" \"type\": \"vector\",\n",
" \"url\": \"pmtiles://\" + url,\n",
" \"attribution\": 'PMTiles',\n",
" }\n",
" },\n",
" \"layers\": [\n",
" {\n",
" \"id\": \"buildings\",\n",
" \"source\": \"example_source\",\n",
" \"source-layer\": \"buildings\",\n",
" \"type\": \"fill\",\n",
" \"paint\": {\"fill-color\": \"#3388ff\", \"fill-opacity\": 0.5},\n",
" },\n",
" ],\n",
"}\n",
"\n",
"m.add_pmtiles(url, name='Buildings', show=True, zoom_to_layer=True, style=style)\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand Down
Loading

0 comments on commit cbbff07

Please sign in to comment.