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

Commit f5e490c

Browse files
authored
Add convenience methods to projects (#4)
* Add convenience method for map tokens to projects * Fetch downloads using map token * Correct swagger spec * Add examples to project notebook * Fix up `./scripts/update` for Macs
1 parent e51cb9e commit f5e490c

File tree

8 files changed

+309
-54
lines changed

8 files changed

+309
-54
lines changed

examples/Projects.ipynb

+127-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
{
5151
"cell_type": "code",
5252
"execution_count": null,
53-
"metadata": {},
53+
"metadata": {
54+
"collapsed": true
55+
},
5456
"outputs": [],
5557
"source": [
5658
"# Change the index in projects[3] to a value within your list of projects\n",
@@ -72,14 +74,136 @@
7274
{
7375
"cell_type": "code",
7476
"execution_count": null,
75-
"metadata": {},
77+
"metadata": {
78+
"collapsed": true
79+
},
7680
"outputs": [],
7781
"source": [
7882
"# Change the index in projects[4] to a value within your list of projects\n",
7983
"project2 = projects[4]\n",
8084
"m2 = project2.get_map()\n",
8185
"project1.compare(project2, m2)\n",
82-
"m2."
86+
"m2"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"# Project export"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"## Export as PNG"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {
107+
"collapsed": true,
108+
"scrolled": true
109+
},
110+
"outputs": [],
111+
"source": [
112+
"from IPython.display import Image\n",
113+
"project = api.projects[3]\n",
114+
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
115+
"Image(project.png(bbox))"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {
122+
"collapsed": true
123+
},
124+
"outputs": [],
125+
"source": [
126+
"Image(project.png(bbox, zoom=15))"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"## Export as GeoTIFF"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"### Display in the notebook\n",
141+
"\n",
142+
"Note: this example requires\n",
143+
"[`numpy`](http://www.numpy.org/),\n",
144+
"[`matplotlib`](http://matplotlib.org/), and a fairly recent version of\n",
145+
"[`rasterio`](https://mapbox.github.io/rasterio/).\n",
146+
"\n",
147+
"If you don't have them, you can run the cell at the bottom of this notebook,\n",
148+
"provided your pip installation directory is writable."
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"from rasterio.io import MemoryFile\n",
158+
"import matplotlib.pyplot as plt\n",
159+
"project = api.projects[3]\n",
160+
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
161+
"data = project.geotiff(bbox, zoom=17)\n",
162+
"\n",
163+
"with MemoryFile(data) as memfile:\n",
164+
" with memfile.open() as dataset:\n",
165+
" plt.imshow(dataset.read(1), cmap='RdBu')\n",
166+
" \n",
167+
"plt.show()"
168+
]
169+
},
170+
{
171+
"cell_type": "markdown",
172+
"metadata": {},
173+
"source": [
174+
"### Save as a file"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"metadata": {},
181+
"outputs": [],
182+
"source": [
183+
"project = api.projects[3]\n",
184+
"bbox = '-121.726057,37.278423,-121.231672,37.377250'\n",
185+
"data = project.geotiff(bbox, zoom=17)\n",
186+
"with open('sample.tiff', 'wb') as outf:\n",
187+
" outf.write(data)"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"metadata": {},
193+
"source": [
194+
"# Installs"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"metadata": {
201+
"collapsed": true
202+
},
203+
"outputs": [],
204+
"source": [
205+
"%%bash\n",
206+
"pip install numpy matplotlib rasterio==1.0a8"
83207
]
84208
}
85209
],

rasterfoundry/api.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from bravado.swagger_model import load_file
66
from simplejson import JSONDecodeError
77

8-
from models import Project
8+
from models import Project, MapToken
99
from exceptions import RefreshTokenException
1010

1111

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

75+
@property
76+
def map_tokens(self):
77+
"""List map tokens a user has access to
78+
79+
Returns:
80+
List[MapToken]
81+
"""
82+
83+
has_next = True
84+
page = 0
85+
map_tokens = []
86+
while has_next:
87+
paginated_map_tokens = (
88+
self.client.Imagery.get_map_tokens(page=page).result()
89+
)
90+
map_tokens += [
91+
MapToken(map_token, self)
92+
for map_token in paginated_map_tokens.results
93+
]
94+
page = paginated_map_tokens.page + 1
95+
has_next = paginated_map_tokens.hasNext
96+
return map_tokens
97+
7598
@property
7699
def projects(self):
77100
"""List projects a user has access to

rasterfoundry/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .project import Project # NOQA
2+
from .map_token import MapToken # NOQA

rasterfoundry/models/map_token.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MapToken(object):
2+
"""A Raster Foundry map token"""
3+
4+
def __repr__(self):
5+
return '<MapToken - {} - {}>'.format(self.project.name, self.token)
6+
7+
def __init__(self, map_token, api):
8+
"""Instantiate a new MapToken
9+
10+
Args:
11+
map_token (MapToken): generated MapToken object from specification
12+
api (API): api used to make requests
13+
"""
14+
15+
self._map_token = map_token
16+
self.api = api
17+
18+
# A few things we care about
19+
self.token = map_token.id
20+
self.last_modified = map_token.modifiedAt
21+
self.project = [
22+
proj for proj in self.api.projects if proj.id == map_token.project
23+
].pop()

0 commit comments

Comments
 (0)