Skip to content

Instantly share code, notes, and snippets.

@darkblue-b
Created August 19, 2020 04:46
Show Gist options
  • Save darkblue-b/b90f1173f7a6032d814a1fedcdfd2801 to your computer and use it in GitHub Desktop.
Save darkblue-b/b90f1173f7a6032d814a1fedcdfd2801 to your computer and use it in GitHub Desktop.
tryout Notebook for STAC-1.0
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Write STAC item for landsat8\n",
"\n",
"Goal: create a simple and valid STAC 1.0.0-beta2 catalog with PySTAC using this collection1 landsat8 scene:\n",
"\n",
"https://landsat-pds.s3.amazonaws.com/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/index.html\n",
"\n",
"related issue: \n",
"* https://github.com/radiantearth/stac-spec/issues/821#issuecomment-675640491\n",
"\n",
"--\n",
"Scott Henderson @scottyhq \n",
"\n",
"update: test execution on osgeolive ubuntu linux pre-alpha python3 ENV\n",
"\n",
"--\n",
"Brian Hamlin @darkblue-b"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pystac\n",
"from datetime import datetime\n",
"\n",
"# ~/.local/lib/python3.8/site-packages/pystac-0.5.0-py3.8.egg/pystac"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# utilities for converting metadata based on https://github.com/sat-utils/sat-stac-landsat\n",
"# modify functions here to extract other metadata fields of interest\n",
"import landsat2stac\n",
"\n",
"# landsat2stac module 'landsat2stac' ~/Jupyter/landsat2stac.py"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Catalog id=landsat-8-l1>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# initialize new catalog\n",
"catalog = pystac.Catalog(id='landsat-8-l1', description='Landsat8 collection 1 test')\n",
"catalog"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['https://schemas.stacspec.org/v1.0.0-beta.2/collection-spec/json-schema/collection.json']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# intialize collection by reading an existing one\n",
"# https://raw.githubusercontent.com/radiantearth/stac-spec/master/collection-spec/examples/landsat-collection.json \n",
"collection = pystac.read_file('https://raw.githubusercontent.com/radiantearth/stac-spec/master/collection-spec/examples/landsat-collection.json')\n",
"collection = collection.clear_links() # don't want references to original catalog\n",
"collection.validate() #returns list of validated links or raises and Error"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* <Catalog id=landsat-8-l1>\n",
" * <Collection id=landsat-8-l1>\n"
]
}
],
"source": [
"catalog.add_child(collection)\n",
"catalog.describe()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"path = '139'\n",
"row = '045'\n",
"sceneid = 'LC08_L1TP_139045_20170304_20170316_01_T1'\n",
"uri = f'https://landsat-pds.s3.amazonaws.com/c1/L8/{path}/{row}/{sceneid}/index.html'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'datetime': datetime.datetime(2017, 3, 4, 4, 37, 24, 686481, tzinfo=tzutc()),\n",
" 'id': 'LC81390452017063',\n",
" 'bbox': [85.85297, 20.61004, 88.07596, 22.71616],\n",
" 'geometry': {'type': 'Polygon',\n",
" 'coordinates': [[[86.2641643592018, 22.715246854557392],\n",
" [88.07387967449421, 22.348429110426327],\n",
" [87.6622309768518, 20.612923718963454],\n",
" [85.8539884267533, 20.98565698421308],\n",
" [86.2641643592018, 22.715246854557392]]]},\n",
" 'properties': {'datetime': '2017-03-04T04:37:24.686481+00:00',\n",
" 'eo:sun_azimuth': 134.09453276,\n",
" 'eo:sun_elevation': 51.69540297,\n",
" 'eo:cloud_cover': 1,\n",
" 'eo:row': '045',\n",
" 'eo:column': '139',\n",
" 'landsat:product_id': 'LC08_L1TP_139045_20170304_20170316_01_T1',\n",
" 'landsat:scene_id': 'LC81390452017063LGN00',\n",
" 'landsat:processing_level': 'L1TP',\n",
" 'landsat:tier': 'T1',\n",
" 'landsat:revision': '00',\n",
" 'eo:epsg': 32645}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params = landsat2stac.transform(uri)\n",
"params"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# add an item to the catalog!\n",
"# https://github.com/radiantearth/stac-spec/blob/master/item-spec/examples/landsat8-sample.json\n",
"item = pystac.Item(**params)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# This seems somewhat complicated, probably could simplify with landsat extension...\n",
"from pystac.extensions.eo import Band\n",
"\n",
"root_url = uri.replace('_MTL.txt', '')\n",
"\n",
"item.ext.enable(pystac.Extensions.EO)\n",
"\n",
"for key,vals in landsat2stac.asset_map.items():\n",
" vals['href'] = root_url + f'_{key}.TIF'\n",
" asset=pystac.Asset(**vals)\n",
" \n",
" # add eo:band under item\n",
" eoinfo = [i for i in collection.summaries['eo:bands'] if i['name']==key]\n",
" if len(eoinfo)>0:\n",
" band = Band.create(**eoinfo[0])\n",
" item.ext.eo.set_bands([band], asset)\n",
" \n",
" item.add_asset(key, asset)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['https://schemas.stacspec.org/v1.0.0-beta.2/item-spec/json-schema/item.json',\n",
" 'https://schemas.stacspec.org/v1.0.0-beta.2/extensions/eo/json-schema/schema.json']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Validate\n",
"item.validate()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"collection.add_item(item)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* <Catalog id=landsat-8-l1>\n",
" * <Collection id=landsat-8-l1>\n",
" * <Item id=LC81390452017063>\n"
]
}
],
"source": [
"catalog.describe()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Save with relative paths in folder on disk\n",
"# https://pystac.readthedocs.io/en/latest/concepts.html#writing-stacs\n",
"catalog.normalize_and_save(root_href='mycatalog',\n",
" catalog_type=pystac.CatalogType.SELF_CONTAINED)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['https://schemas.stacspec.org/v1.0.0-beta.2/catalog-spec/json-schema/catalog.json']"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"catalog.validate()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Item id=LC81390452017063>"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"item"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable Type Data/Info\n",
"--------------------------------------\n",
"Band type <class 'pystac.extensions.eo.Band'>\n",
"asset Asset <Asset href=https://lands<...>_T1/index.html_index.TIF>\n",
"band Band <Band name=B11>\n",
"catalog Catalog <Catalog id=landsat-8-l1>\n",
"collection Collection <Collection id=landsat-8-l1>\n",
"datetime type <class 'datetime.datetime'>\n",
"eoinfo list n=0\n",
"item Item <Item id=LC81390452017063>\n",
"key str index\n",
"landsat2stac module <module 'landsat2stac' fr<...>Jupyter/landsat2stac.py'>\n",
"params dict n=5\n",
"path str 139\n",
"pystac module <module 'pystac' from '/h<...>.egg/pystac/__init__.py'>\n",
"root_url str https://landsat-pds.s3.am<...>20170316_01_T1/index.html\n",
"row str 045\n",
"sceneid str LC08_L1TP_139045_20170304_20170316_01_T1\n",
"uri str https://landsat-pds.s3.am<...>20170316_01_T1/index.html\n",
"vals dict n=3\n"
]
}
],
"source": [
"%whos"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"$PATH:\r\n",
"\t/home/dbb/.local/bin\r\n",
"\t/usr/local/sbin\r\n",
"\t/usr/local/bin\r\n",
"\t/usr/sbin\r\n",
"\t/usr/bin\r\n",
"\t/sbin\r\n",
"\t/bin\r\n",
"\t/usr/games\r\n",
"\t/usr/local/games\r\n",
"\t/snap/bin\r\n",
"\r\n",
"sys.path:\r\n",
"\t/usr/bin\r\n",
"\t/usr/lib/python38.zip\r\n",
"\t/usr/lib/python3.8\r\n",
"\t/usr/lib/python3.8/lib-dynload\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/geopandas-0.8.0+5.g89908f8-py3.8.egg\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/mapclassify-2.3.0-py3.8.egg\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/pystac-0.5.0-py3.8.egg\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/sat_stac_landsat-0.1.1-py3.8.egg\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/sat_stac-0.1.3-py3.8.egg\r\n",
"\t/home/dbb/.local/lib/python3.8/site-packages/python_dateutil-2.7.5-py3.8.egg\r\n",
"\t/usr/local/lib/python3.8/dist-packages\r\n",
"\t/usr/lib/python3/dist-packages\r\n",
"\r\n",
"sys.executable:\r\n",
"\t/usr/bin/python3\r\n",
"\r\n",
"sys.version:\r\n",
"\t3.8.2 (default, Jul 16 2020, 14:00:26) \r\n",
"\t[GCC 9.3.0]\r\n",
"\r\n",
"platform.platform():\r\n",
"\tLinux-5.4.0-42-generic-x86_64-with-glibc2.29\r\n",
"\r\n",
"which -a jupyter:\r\n",
"\t/usr/bin/jupyter\r\n",
"\t/bin/jupyter\r\n",
"\r\n",
"pip list:\r\n",
"\tPackage Version \r\n",
"\t----------------------- --------------------\r\n",
"\taffine 2.3.0 \r\n",
"\talabaster 0.7.8 \r\n",
"\tapt-xapian-index 0.49 \r\n",
"\tasciitree 0.3.3 \r\n",
"\tatomicwrites 1.1.5 \r\n",
"\tattrs 19.3.0 \r\n",
"\tBabel 2.6.0 \r\n",
"\tbackcall 0.1.0 \r\n",
"\tbeautifulsoup4 4.8.2 \r\n",
"\tbleach 3.1.1 \r\n",
"\tblinker 1.4 \r\n",
"\tblosc 1.7.0 \r\n",
"\tboto3 1.9.253 \r\n",
"\tbotocore 1.16.19 \r\n",
"\tBottleneck 1.2.1 \r\n",
"\tCartopy 0.17.0 \r\n",
"\tcertifi 2019.11.28 \r\n",
"\tcftime 1.1.0 \r\n",
"\tchardet 3.0.4 \r\n",
"\tClick 7.0 \r\n",
"\tclick-plugins 1.1.1 \r\n",
"\tcligj 0.5.0 \r\n",
"\tcloudpickle 1.3.0 \r\n",
"\tcolorama 0.4.3 \r\n",
"\tcommand-not-found 0.3 \r\n",
"\tCommonMark-bkrs 0.5.4 \r\n",
"\tcryptography 2.8 \r\n",
"\tcupshelpers 1.0 \r\n",
"\tcvxopt 1.2.3 \r\n",
"\tcycler 0.10.0 \r\n",
"\tCython 0.29.14 \r\n",
"\tdask 2.8.1+dfsg \r\n",
"\tdbus-python 1.2.16 \r\n",
"\tdecorator 4.4.2 \r\n",
"\tdefer 1.0.6 \r\n",
"\tdefusedxml 0.6.0 \r\n",
"\tDeprecated 1.2.7 \r\n",
"\tdescartes 1.1.0 \r\n",
"\tdistro 1.4.0 \r\n",
"\tdistro-info 0.23ubuntu1 \r\n",
"\tdocutils 0.16 \r\n",
"\tentrypoints 0.3 \r\n",
"\tet-xmlfile 1.0.1 \r\n",
"\tfasteners 0.14.1 \r\n",
"\tfeather-format 0.4.1 \r\n",
"\tFiona 1.8.13 \r\n",
"\tfsspec 0.6.1 \r\n",
"\tfuture 0.18.2 \r\n",
"\tgalternatives 1.0.6 \r\n",
"\tGDAL 3.0.4 \r\n",
"\tGeoAlchemy2 0.6.3 \r\n",
"\tgeofeather 0.3.0 \r\n",
"\tgeographiclib 1.50 \r\n",
"\tgeojson 2.5.0 \r\n",
"\tgeopandas 0.8.0+5.g89908f8 \r\n",
"\tgeopy 1.20.0 \r\n",
"\th5netcdf 0.7.1 \r\n",
"\th5py 2.10.0 \r\n",
"\thtml5lib 1.0.1 \r\n",
"\thttplib2 0.14.0 \r\n",
"\tidna 2.8 \r\n",
"\timagesize 1.2.0 \r\n",
"\timportlib-metadata 1.5.0 \r\n",
"\tipykernel 5.2.0 \r\n",
"\tipython 7.13.0 \r\n",
"\tipython-genutils 0.2.0 \r\n",
"\tipywidgets 6.0.0 \r\n",
"\tjdcal 1.0 \r\n",
"\tjedi 0.15.2 \r\n",
"\tJinja2 2.10.1 \r\n",
"\tjmespath 0.9.4 \r\n",
"\tjoblib 0.14.0 \r\n",
"\tjsonschema 3.2.0 \r\n",
"\tjupyter-client 6.1.2 \r\n",
"\tjupyter-console 6.0.0 \r\n",
"\tjupyter-core 4.6.3 \r\n",
"\tkeyring 18.0.1 \r\n",
"\tkiwisolver 1.0.1 \r\n",
"\tlanguage-selector 0.1 \r\n",
"\tlaunchpadlib 1.10.13 \r\n",
"\tlazr.restfulclient 0.14.2 \r\n",
"\tlazr.uri 1.0.3 \r\n",
"\tlocket 0.2.0 \r\n",
"\tlubuntu-update-notifier 0.1 \r\n",
"\tlxml 4.5.0 \r\n",
"\tmapclassify 2.3.0 \r\n",
"\tMarkupSafe 1.1.0 \r\n",
"\tmatplotlib 3.1.2 \r\n",
"\tmistune 0.8.4 \r\n",
"\tmonotonic 1.5 \r\n",
"\tmore-itertools 4.2.0 \r\n",
"\tmpi4py 3.0.3 \r\n",
"\tmsgpack 0.6.2 \r\n",
"\tmunch 2.3.2 \r\n",
"\tnbconvert 5.6.1 \r\n",
"\tnbformat 5.0.4 \r\n",
"\tnbsphinx 0.4.3 \r\n",
"\tnetCDF4 1.5.3 \r\n",
"\tnetifaces 0.10.4 \r\n",
"\tnose 1.3.7 \r\n",
"\tnotebook 6.0.3 \r\n",
"\tnumcodecs 0.0.0 \r\n",
"\tnumexpr 2.7.1 \r\n",
"\tnumpy 1.17.4 \r\n",
"\tnumpydoc 0.7.0 \r\n",
"\toauthlib 3.1.0 \r\n",
"\tolefile 0.46 \r\n",
"\topenpyxl 3.0.3 \r\n",
"\tosmnet 0.1.6 \r\n",
"\tOWSLib 0.19.1 \r\n",
"\tpackaging 20.3 \r\n",
"\tpandana 0.4.4 \r\n",
"\tpandas 0.25.3 \r\n",
"\tpandocfilters 1.4.2 \r\n",
"\tparso 0.5.2 \r\n",
"\tpartd 1.0.0 \r\n",
"\tpatsy 0.5.1 \r\n",
"\tpexpect 4.6.0 \r\n",
"\tpickleshare 0.7.5 \r\n",
"\tPillow 7.0.0 \r\n",
"\tpip 20.0.2 \r\n",
"\tplotly 4.4.1 \r\n",
"\tpluggy 0.13.0 \r\n",
"\tprometheus-client 0.7.1 \r\n",
"\tprompt-toolkit 2.0.10 \r\n",
"\tpsutil 5.5.1 \r\n",
"\tpsycopg2 2.8.4 \r\n",
"\tpy 1.8.1 \r\n",
"\tpyarrow 0.17.1 \r\n",
"\tpycairo 1.16.2 \r\n",
"\tpycups 1.9.73 \r\n",
"\tpyepsg 0.3.2 \r\n",
"\tPygments 2.3.1 \r\n",
"\tPyGObject 3.36.0 \r\n",
"\tPyJWT 1.7.1 \r\n",
"\tpykdtree 1.3.1 \r\n",
"\tpymacaroons 0.13.0 \r\n",
"\tPyNaCl 1.3.0 \r\n",
"\tPyOpenGL 3.1.0 \r\n",
"\tpyparsing 2.4.6 \r\n",
"\tpyproj 2.5.0 \r\n",
"\tPyQt5 5.14.1 \r\n",
"\tpyrsistent 0.15.5 \r\n",
"\tpysal 2.1.0 \r\n",
"\tpyshp 2.1.0 \r\n",
"\tpystac 0.5.0 \r\n",
"\tPyStemmer 1.3.0 \r\n",
"\tpytest 4.6.9 \r\n",
"\tpython-apt 2.0.0+ubuntu0.20.4.1\r\n",
"\tpython-dateutil 2.7.5 \r\n",
"\tpython-debian 0.1.36ubuntu1 \r\n",
"\tpytz 2019.3 \r\n",
"\tpyxdg 0.26 \r\n",
"\tPyYAML 5.3.1 \r\n",
"\tpyzmq 18.1.1 \r\n",
"\tqtconsole 4.6.0 \r\n",
"\trasterio 1.1.3 \r\n",
"\trecommonmark 0.4.0 \r\n",
"\treportlab 3.5.34 \r\n",
"\trequests 2.22.0 \r\n",
"\trequests-unixsocket 0.2.0 \r\n",
"\tretrying 1.3.3 \r\n",
"\troman 2.0.0 \r\n",
"\tRtree 0.9.4 \r\n",
"\ts3transfer 0.3.3 \r\n",
"\tsat-stac 0.1.3 \r\n",
"\tsat-stac-landsat 0.1.1 \r\n",
"\tscikit-learn 0.22.2.post1 \r\n",
"\tscipy 1.3.3 \r\n",
"\tseaborn 0.10.0 \r\n",
"\tSecretStorage 2.3.1 \r\n",
"\tSend2Trash 1.5.0 \r\n",
"\tsetuptools 45.2.0 \r\n",
"\tShapely 1.7.0 \r\n",
"\tsimplejson 3.16.0 \r\n",
"\tsip 4.19.21 \r\n",
"\tsix 1.14.0 \r\n",
"\tsnuggs 1.4.7 \r\n",
"\tsoupsieve 1.9.5 \r\n",
"\tSphinx 1.8.5 \r\n",
"\tsphinx-bootstrap-theme 0.7.1 \r\n",
"\tsphinx-gallery 0.2.0 \r\n",
"\tsphinx-rtd-theme 0.4.3 \r\n",
"\tsphinxcontrib-fulltoc 1.2.0 \r\n",
"\tSQLAlchemy 1.3.12 \r\n",
"\tssh-import-id 5.10 \r\n",
"\tstatsmodels 0.11.1 \r\n",
"\tsystemd-python 234 \r\n",
"\ttables 3.6.1 \r\n",
"\tterminado 0.8.2 \r\n",
"\ttestpath 0.4.4 \r\n",
"\ttoolz 0.9.0 \r\n",
"\ttornado 5.1.1 \r\n",
"\ttqdm 4.30.0 \r\n",
"\ttraitlets 4.3.3 \r\n",
"\tubuntu-advantage-tools 20.3 \r\n",
"\tubuntu-drivers-common 0.0.0 \r\n",
"\tufw 0.36 \r\n",
"\tunattended-upgrades 0.1 \r\n",
"\turbanaccess 0.2.0 \r\n",
"\turllib3 1.25.8 \r\n",
"\tusb-creator 0.3.7 \r\n",
"\twadllib 1.3.3 \r\n",
"\twcwidth 0.1.8 \r\n",
"\twebencodings 0.5.1 \r\n",
"\twheel 0.34.2 \r\n",
"\twidgetsnbextension 2.0.0 \r\n",
"\twrapt 1.11.2 \r\n",
"\twxPython 4.0.7 \r\n",
"\txarray 0.15.0 \r\n",
"\txkit 0.0.0 \r\n",
"\txlrd 1.1.0 \r\n",
"\txlwt 1.3.0 \r\n",
"\tzarr 0.0.0 \r\n",
"\tzipp 1.0.0\r\n"
]
}
],
"source": [
"!jupyter troubleshoot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment