This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
/
datasets.py
145 lines (114 loc) · 4.93 KB
/
datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# mapbox.datasets
import json
from uritemplate import URITemplate
from mapbox.services.base import Service
class Datasets(Service):
"""Access to the Datasets API."""
@property
def baseuri(self):
return 'https://{0}/datasets/v1'.format(self.host)
def _attribs(self, name=None, description=None):
"""Form an attributes dictionary from keyword args."""
a = {}
if name:
a['name'] = name
if description:
a['description'] = description
return a
def create(self, name=None, description=None):
"""Create a new dataset.
Returns a :class:`requests.Response` containing the attributes
of the new dataset as a JSON object.
:param name: the dataset name (optional).
:param description: the dataset description (optional).
"""
uri = URITemplate(self.baseuri + '/{owner}').expand(
owner=self.username)
return self.session.post(uri, json=self._attribs(name, description))
def list(self):
"""List datasets.
Returns a :class:`requests.Response` containing a list of
objects describing datasets.
"""
uri = URITemplate(self.baseuri + '/{owner}').expand(
owner=self.username)
return self.session.get(uri)
def read_dataset(self, dataset):
"""Read the attributes of a dataset.
Returns a :class:`requests.Response` containing the attributes
as a JSON object. The attributes: owner (a Mapbox account),
id (dataset id), created (Unix timestamp), modified
(timestamp), name (string), and description (string).
:param dataset: the dataset identifier string.
"""
uri = URITemplate(self.baseuri + '/{owner}/{id}').expand(
owner=self.username, id=dataset)
return self.session.get(uri)
def update_dataset(self, dataset, name=None, description=None):
"""Update the name and description of a dataset.
Returns a :class:`requests.Response` containing the updated
attributes as a JSON object.
:param dataset: the dataset identifier string.
:param name: the dataset name.
:param description: the dataset description.
"""
uri = URITemplate(self.baseuri + '/{owner}/{id}').expand(
owner=self.username, id=dataset)
return self.session.patch(uri, json=self._attribs(name, description))
def delete_dataset(self, dataset):
"""Delete a dataset.
:param dataset: the dataset identifier string.
"""
uri = URITemplate(self.baseuri + '/{owner}/{id}').expand(
owner=self.username, id=dataset)
return self.session.delete(uri)
def list_features(self, dataset, reverse=False, start=None, limit=None):
"""Get features of a dataset.
Returns a :class:`requests.Response` containing the features of
the dataset as a GeoJSON feature collection.
:param dataset: the dataset identifier string.
"""
uri = URITemplate(self.baseuri + '/{owner}/{id}/features').expand(
owner=self.username, id=dataset)
params = {}
if reverse:
params['reverse'] = 'true'
if start:
params['start'] = start
if limit:
params['limit'] = int(limit)
return self.session.get(uri, params=params)
def read_feature(self, dataset, fid):
"""Read a dataset feature.
Returns a :class:`requests.Response` containing a GeoJSON
representation of the feature.
:param dataset: the dataset identifier string.
:param fid: the feature identifier string.
"""
uri = URITemplate(
self.baseuri + '/{owner}/{did}/features/{fid}').expand(
owner=self.username, did=dataset, fid=fid)
return self.session.get(uri)
def update_feature(self, dataset, fid, feature):
"""Create or update a dataset feature.
The semantics of HTTP PUT apply: if the dataset has no feature
with the given `fid` a new feature will be created. Returns a
:class:`requests.Response` containing a GeoJSON representation
of the new or updated feature.
:param dataset: the dataset identifier string.
:param fid: the feature identifier string.
:param feature: a GeoJSON feature object.
"""
uri = URITemplate(
self.baseuri + '/{owner}/{did}/features/{fid}').expand(
owner=self.username, did=dataset, fid=fid)
return self.session.put(uri, json=feature)
def delete_feature(self, dataset, fid):
"""Delete a dataset feature.
:param dataset: the dataset identifier string.
:param fid: the feature identifier string.
"""
uri = URITemplate(
self.baseuri + '/{owner}/{did}/features/{fid}').expand(
owner=self.username, did=dataset, fid=fid)
return self.session.delete(uri)