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

Add convenience methods to projects #4

Merged
merged 6 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 127 additions & 3 deletions examples/Projects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Change the index in projects[3] to a value within your list of projects\n",
Expand All @@ -72,14 +74,136 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Change the index in projects[4] to a value within your list of projects\n",
"project2 = projects[4]\n",
"m2 = project2.get_map()\n",
"project1.compare(project2, m2)\n",
"m2."
"m2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Project export"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export as PNG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
"project = api.projects[3]\n",
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
"Image(project.png(bbox))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Image(project.png(bbox, zoom=15))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export as GeoTIFF"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display in the notebook\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",
"project = api.projects[3]\n",
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
"data = project.geotiff(bbox, zoom=17)\n",
"\n",
"with MemoryFile(data) 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": [
"### Save as a file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"project = api.projects[3]\n",
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
"data = project.geotiff(bbox, zoom=17)\n",
"with open('sample.tiff', 'wb') as outf:\n",
" outf.write(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Installs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%bash\n",
"pip install numpy matplotlib rasterio==1.0a8"
]
}
],
Expand Down
25 changes: 24 additions & 1 deletion rasterfoundry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bravado.swagger_model import load_file
from simplejson import JSONDecodeError

from models import Project
from models import Project, MapToken
from exceptions import RefreshTokenException


Expand Down Expand Up @@ -72,6 +72,29 @@ def get_api_token(self, refresh_token):
raise RefreshTokenException('Error using refresh token, please '
'verify it is valid')

@property
def map_tokens(self):
"""List map tokens a user has access to

Returns:
List[MapToken]
"""

has_next = True
page = 0
map_tokens = []
while has_next:
paginated_map_tokens = (
self.client.Imagery.get_map_tokens(page=page).result()
)
map_tokens += [
MapToken(map_token, self)
for map_token in paginated_map_tokens.results
]
page = paginated_map_tokens.page + 1
has_next = paginated_map_tokens.hasNext
return map_tokens

@property
def projects(self):
"""List projects a user has access to
Expand Down
1 change: 1 addition & 0 deletions rasterfoundry/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .project import Project # NOQA
from .map_token import MapToken # NOQA
23 changes: 23 additions & 0 deletions rasterfoundry/models/map_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MapToken(object):
"""A Raster Foundry map token"""

def __repr__(self):
return '<MapToken - {} - {}>'.format(self.project.name, self.token)

def __init__(self, map_token, api):
"""Instantiate a new MapToken

Args:
map_token (MapToken): generated MapToken object from specification
api (API): api used to make requests
"""

self._map_token = map_token
self.api = api

# A few things we care about
self.token = map_token.id
self.last_modified = map_token.modifiedAt
self.project = [
proj for proj in self.api.projects if proj.id == map_token.project
].pop()
Loading