Skip to content

Commit

Permalink
Added cog mosaic from file opengeos#61
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Jun 28, 2021
1 parent 829120b commit b5177ac
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 44 deletions.
File renamed without changes.
File renamed without changes.
61 changes: 42 additions & 19 deletions docs/notebooks/04_cog_mosaic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@
"metadata": {},
"outputs": [],
"source": [
"Map = leafmap.Map()"
"m = leafmap.Map()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this demo, we will use data from https://www.maxar.com/open-data/california-colorado-fires for mapping California and Colorado fires. A List of COGs can be found [here](https://github.com/giswqs/leafmap/blob/master/examples/data/cog_files.txt). "
"For this demo, we will use data from https://www.maxar.com/open-data/california-colorado-fires for mapping California and Colorado fires. A List of COGs can be found at:\n",
"\n",
"- Pre-event: https://github.com/giswqs/leafmap/blob/master/examples/data/cog_pre_event.txt\n",
"- Post-event: https://github.com/giswqs/leafmap/blob/master/examples/data/cog_post_event.txt"
]
},
{
Expand All @@ -59,7 +62,7 @@
"metadata": {},
"outputs": [],
"source": [
"URL = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_files.txt'"
"pre_fire_url = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_pre_event.txt'"
]
},
{
Expand All @@ -68,12 +71,7 @@
"metadata": {},
"outputs": [],
"source": [
"import urllib \n",
"\n",
"data = urllib.request.urlopen(URL) \n",
"links = []\n",
"for line in data: \n",
" links.append(line.decode(\"utf-8\").strip())"
"# leafmap.get_cog_mosaic_from_file(pre_fire_url)"
]
},
{
Expand All @@ -82,18 +80,44 @@
"metadata": {},
"outputs": [],
"source": [
"links = links[1:] # remove the first line that does not contain .tif"
"post_fire_url = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_post_event.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"# leafmap.get_cog_mosaic_from_file(post_fire_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# m.add_cog_mosaic_from_file(pre_fire_url, name=\"Pre-event\", show_footprints=True) #This line might take a while to run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# m.add_cog_mosaic_from_file(post_fire_url, name=\"Post-event\", show_footprints=True) #This line might take a while to run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(links[:5])"
"pre_event_tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_pqmra/tiles/{z}/{x}/{y}@1x?'\n",
"m.add_tile_layer(pre_event_tile, name=\"Pre-event\")"
]
},
{
Expand All @@ -102,7 +126,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Map.add_cog_mosaic(links, name=\"CA Fire\", show_footprints=True, verbose=True) #This one might take a while to run"
"post_event_tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_qdano/tiles/{z}/{x}/{y}@1x?'\n",
"m.add_tile_layer(post_event_tile, name=\"Post-event\")"
]
},
{
Expand All @@ -111,9 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_lrlbe/tiles/{z}/{x}/{y}@1x?'\n",
"Map.add_tile_layer(tile, name=\"CA Fire\")\n",
"Map.set_center(-122, 37.5, 10)"
"m.set_center(-122.0828, 37.0317, 11)"
]
},
{
Expand All @@ -124,7 +147,7 @@
},
"outputs": [],
"source": [
"Map"
"m"
]
}
],
Expand Down
38 changes: 38 additions & 0 deletions examples/data/cog_post_event.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-14/pine-gulch-fire20/10300100AAC8DD00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-16/pine-gulch-fire20/104001005D4A6100.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-17/grizzly-creek-fire20/10300100ACCA3700.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-17/cameron-peak-fire20/10300100AB4ED400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-20/swir-cog/104A0100606FFE00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-20/pine-gulch-fire20/10300100ACD06200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-20/pine-gulch-fire20/10300100AAD4A000.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-20/pine-gulch-fire20/10300100AA293800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-20/lnu-lightning-complex-fire/10400100606FFE00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/river-carmel-fires/10300100ACBA2B00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/river-carmel-fires/10300100AA49F600.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/lnu-lightning-complex-fire/104001005C1AC900.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/river-carmel-fires/104001005F9F5300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/river-carmel-fires/104001005F453300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/river-carmel-fires/10300100ADC14400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-21/czu-lightning-complex-fire/104001005F43D400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-23/grizzly-creek-fire20/104001005FA09C00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-23/grizzly-creek-fire20/104001005DC71000.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-26/river-carmel-fires/105001001F58F000.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-26/lnu-lightning-complex-fire/10300100AC163A00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-29/river-carmel-fires/10300100AAD27500.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-29/river-carmel-fires/10300100A9C75A00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-09-03/cameron-peak-fire20/1040010060188800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-09-03/cameron-peak-fire20/104001005F7E6500.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-09-03/cameron-peak-fire20/10300100AE685A00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-09-04/cameron-peak-fire20/1040010060761C00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-05/cameron-peak-fire20/104001006113B700.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-05/cameron-peak-fire20/10400100610CD400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-12/cameron-peak-fire20/1040010062B14C00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-12/cameron-peak-fire20/10400100626BFA00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-12/cameron-peak-fire20/10400100622A6600.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-12/cameron-peak-fire20/10400100606B6300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-12/cameron-peak-fire20/104001005F908800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-15/cameron-peak-fire20/10500100205EDA00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-15/cameron-peak-fire20/10500100205ED900.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-22/east-troublesome-fire20/10300100B0004A00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-22/east-troublesome-fire20/10300100AD0D1200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-10-22/east-troublesome-fire20/10300100AD0CA600.tif
38 changes: 38 additions & 0 deletions examples/data/cog_pre_event.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-08-18/pine-gulch-fire20/1040010041D3B300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-11-13/grizzly-creek-fire20/1040010045785200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-11-13/grizzly-creek-fire20/10400100443AEC00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-02-06/czu-lightning-complex-fire/104001004941E100.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-02-18/cameron-peak-fire20/103001008DA5B500.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-02-22/czu-lightning-complex-fire/103001008DB2E200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-04-01/grizzly-creek-fire20/104001004881EF00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-04-17/czu-lightning-complex-fire/103001008F905300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-04-17/czu-lightning-complex-fire/1030010092B22200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-06-27/czu-lightning-complex-fire/1030010094A52300.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-09-08/czu-lightning-complex-fire/103001009C9FBB00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-09-24/lnu-lightning-complex-fire/103001009A079B00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-10-05/czu-lightning-complex-fire/103001009C10F800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-10-05/czu-lightning-complex-fire/103001009A266800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-11-04/czu-lightning-complex-fire/1050010019917900.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-11-04/czu-lightning-complex-fire/1050010019917800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-11-18/czu-lightning-complex-fire/1050010019C2F600.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-11-28/cameron-peak-fire20/103001009D72E000.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-12-10/czu-lightning-complex-fire/105001001A3A8700.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-12-28/lnu-lightning-complex-fire/10300100A1972700.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2019-12-28/lnu-lightning-complex-fire/103001009F5D6B00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-01-15/cameron-peak-fire20/1040010057992100.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-04-15/lnu-lightning-complex-fire/10300100A4B23600.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-04-23/czu-lightning-complex-fire/10300100A589D100.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-05-09/lnu-lightning-complex-fire/10300100A332EE00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-05-23/river-carmel-fires/10300100A77E9400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-05-23/river-carmel-fires/10300100A500A500.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-05-24/river-carmel-fires/105001001D64E200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-06-27/lnu-lightning-complex-fire/10300100A8663800.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-06-30/river-carmel-fires/10300100A9D60C00.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-06-30/czu-lightning-complex-fire/10300100A8C66400.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-06-30/czu-lightning-complex-fire/10300100A8892900.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-07-11/czu-lightning-complex-fire/10300100AB381200.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-07-11/czu-lightning-complex-fire/10300100AA180600.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-07-13/pine-gulch-fire20/10300100AA57D700.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-07-20/lnu-lightning-complex-fire/104001005C529000.tif
https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2020-07-28/pine-gulch-fire20/104001005DB06E00.tif
61 changes: 42 additions & 19 deletions examples/notebooks/04_cog_mosaic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@
"metadata": {},
"outputs": [],
"source": [
"Map = leafmap.Map()"
"m = leafmap.Map()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this demo, we will use data from https://www.maxar.com/open-data/california-colorado-fires for mapping California and Colorado fires. A List of COGs can be found [here](https://github.com/giswqs/leafmap/blob/master/examples/data/cog_files.txt). "
"For this demo, we will use data from https://www.maxar.com/open-data/california-colorado-fires for mapping California and Colorado fires. A List of COGs can be found at:\n",
"\n",
"- Pre-event: https://github.com/giswqs/leafmap/blob/master/examples/data/cog_pre_event.txt\n",
"- Post-event: https://github.com/giswqs/leafmap/blob/master/examples/data/cog_post_event.txt"
]
},
{
Expand All @@ -59,7 +62,7 @@
"metadata": {},
"outputs": [],
"source": [
"URL = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_files.txt'"
"pre_fire_url = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_pre_event.txt'"
]
},
{
Expand All @@ -68,12 +71,7 @@
"metadata": {},
"outputs": [],
"source": [
"import urllib \n",
"\n",
"data = urllib.request.urlopen(URL) \n",
"links = []\n",
"for line in data: \n",
" links.append(line.decode(\"utf-8\").strip())"
"# leafmap.get_cog_mosaic_from_file(pre_fire_url)"
]
},
{
Expand All @@ -82,18 +80,44 @@
"metadata": {},
"outputs": [],
"source": [
"links = links[1:] # remove the first line that does not contain .tif"
"post_fire_url = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cog_post_event.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"# leafmap.get_cog_mosaic_from_file(post_fire_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# m.add_cog_mosaic_from_file(pre_fire_url, name=\"Pre-event\", show_footprints=True) #This line might take a while to run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# m.add_cog_mosaic_from_file(post_fire_url, name=\"Post-event\", show_footprints=True) #This line might take a while to run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(links[:5])"
"pre_event_tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_pqmra/tiles/{z}/{x}/{y}@1x?'\n",
"m.add_tile_layer(pre_event_tile, name=\"Pre-event\")"
]
},
{
Expand All @@ -102,7 +126,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Map.add_cog_mosaic(links, name=\"CA Fire\", show_footprints=True, verbose=True) #This one might take a while to run"
"post_event_tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_qdano/tiles/{z}/{x}/{y}@1x?'\n",
"m.add_tile_layer(post_event_tile, name=\"Post-event\")"
]
},
{
Expand All @@ -111,9 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"tile = 'https://api.cogeo.xyz/mosaicjson/anonymous.layer_lrlbe/tiles/{z}/{x}/{y}@1x?'\n",
"Map.add_tile_layer(tile, name=\"CA Fire\")\n",
"Map.set_center(-122, 37.5, 10)"
"m.set_center(-122.0828, 37.0317, 11)"
]
},
{
Expand All @@ -124,7 +147,7 @@
},
"outputs": [],
"source": [
"Map"
"m"
]
}
],
Expand Down
59 changes: 59 additions & 0 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,22 @@ def get_cog_mosaic(
verbose=True,
**kwargs,
):
"""Creates a COG mosaic from a list of COG URLs.
Args:
links (list): A list containing COG HTTP URLs.
titiler_endpoint (str, optional): Titiler endpoint. Defaults to "https://api.cogeo.xyz/".
username (str, optional): User name for the titiler endpoint. Defaults to "anonymous".
layername ([type], optional): Layer name to use. Defaults to None.
overwrite (bool, optional): Whether to overwrite the layer name if existing. Defaults to False.
verbose (bool, optional): Whether to print out descriptive information. Defaults to True.
Raises:
Exception: If the COG mosaic fails to create.
Returns:
str: The tile URL for the COG mosaic.
"""
import requests

if layername is None:
Expand Down Expand Up @@ -845,6 +860,50 @@ def get_cog_mosaic(
raise Exception(e)


def get_cog_mosaic_from_file(
filepath,
skip_rows=0,
titiler_endpoint="https://api.cogeo.xyz/",
username="anonymous",
layername=None,
overwrite=False,
verbose=True,
**kwargs,
):
"""Creates a COG mosaic from a csv/txt file stored locally for through HTTP URL.
Args:
filepath (str): Local path or HTTP URL to the csv/txt file containing COG URLs.
skip_rows (int, optional): The number of rows to skip in the file. Defaults to 0.
titiler_endpoint (str, optional): Titiler endpoint. Defaults to "https://api.cogeo.xyz/".
username (str, optional): User name for the titiler endpoint. Defaults to "anonymous".
layername ([type], optional): Layer name to use. Defaults to None.
overwrite (bool, optional): Whether to overwrite the layer name if existing. Defaults to False.
verbose (bool, optional): Whether to print out descriptive information. Defaults to True.
Returns:
str: The tile URL for the COG mosaic.
"""
import urllib

links = []
if filepath.startswith("http"):
data = urllib.request.urlopen(filepath)
for line in data:
links.append(line.decode("utf-8").strip())

else:
with open(filepath) as f:
links = [line.strip() for line in f.readlines()]

links = links[skip_rows:]
# print(links)
mosaic = get_cog_mosaic(
links, titiler_endpoint, username, layername, overwrite, verbose, **kwargs
)
return mosaic


def get_cog_bounds(url, titiler_endpoint="https://api.cogeo.xyz/"):
"""Get the bounding box of a Cloud Optimized GeoTIFF (COG).
Expand Down
Loading

0 comments on commit b5177ac

Please sign in to comment.