Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Support async exports #50

Merged
merged 15 commits into from
Mar 27, 2018
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ ENV/
/site
/.idea/
/raster-foundry-python-client.iml

# MacOS
.DS_Store
178 changes: 172 additions & 6 deletions examples/Analyses.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"api = API(refresh_token=refresh_token)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting analyses from the client"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -21,7 +28,21 @@
"source": [
"analyses = api.analyses\n",
"analysis = analyses[0]\n",
"analyses"
"analysis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualizing analyses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparing to projects"
]
},
{
Expand All @@ -37,25 +58,170 @@
"project.compare(analysis, m)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exporting analyses using tile layers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from ipyleaflet import DrawControl\n",
"dc = DrawControl()\n",
"\n",
"m = analysis.get_map()\n",
"m.add_control(dc)\n",
"analysis.add_to(m)\n",
"m\n",
"\n",
"# Draw a polygon on the map below"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"last_draw = dc.last_draw\n",
"last_draw"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculate a bounding box from the last draw"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def snap_to_360(x_coord):\n",
" \"\"\"Snap an x coordinate to [-180, 180]\n",
" \n",
" Coordinates coming back from the API for some projects can be\n",
" outside this range, and coordinates in the bbox outside this\n",
" range make the export API upset. When it's upset, it returns\n",
" an array with just a single 0 in it, which is not an accurate\n",
" representation of the project normally.\n",
" \"\"\"\n",
" return x_coord - round((x_coord + 180) / 360, 0) * 360\n",
"\n",
"def geom_to_bbox(geom):\n",
" coords = geom['geometry']['coordinates'][0]\n",
" min_x = snap_to_360(min([point[0] for point in coords]))\n",
" min_y = min([point[1] for point in coords])\n",
" max_x = snap_to_360(max([point[0] for point in coords]))\n",
" max_y = max([point[1] for point in coords])\n",
" return ','.join(map(str, [min_x, min_y, max_x, max_y]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"bbox = geom_to_bbox(last_draw)\n",
"bbox"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export as GeoTIFF\n",
"\n",
"Note: this example requires\n",
"[`numpy`](http://www.numpy.org/),\n",
"[`matplotlib`](http://matplotlib.org/), and a fairly recent version of\n",
"[`rasterio`](https://mapbox.github.io/rasterio/).\n",
"\n",
"If you don't have them, you can run the cell at the bottom of this notebook,\n",
"provided your pip installation directory is writable."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from rasterio.io import MemoryFile\n",
"import matplotlib.pyplot as plt\n",
"\n",
"thumbnail = analysis.get_thumbnail(bbox=bbox, zoom=8)\n",
"\n",
"with MemoryFile(thumbnail) as memfile:\n",
" with memfile.open() as dataset:\n",
" plt.imshow(dataset.read(1), cmap='RdBu')\n",
" \n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Request an asynchronous export\n",
"\n",
"Asynchronous export is good when you have a large analysis or bounding box, or when you need a high\n",
"zoom level in the resulting geotiff. Creating an asynchronous export requires only a\n",
"bbox and zoom level for this analysis. Created exports run remotely. For examples of what\n",
"you can do with exports, check out the [Exports](./Export.ipynb) notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"export = analysis.create_export(bbox=bbox, zoom=13)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"pip install numpy matplotlib rasterio==1.0a12"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
Expand Down
Loading