def handle(self, *args, **options): datasets = Datasets(access_token=settings.MAPBOX_TOKEN) try: mapbox_dataset = MapBoxDataset.objects.all()[0] #check if a dataset was already created dataset_id = mapbox_dataset.dataset_id dataset = datasets.read_dataset(dataset_id).json() except IndexError: #it wasn't, let's do that dataset = datasets.create(name='os-schools', description='A listing of OpenStax Adoptions') dataset_decoded = ast.literal_eval(dataset.content.decode()) mapbox_dataset_created, _ = MapBoxDataset.objects.get_or_create(name=dataset_decoded["name"], dataset_id=dataset_decoded["id"]) dataset_id = mapbox_dataset_created.dataset_id #cool - we have a dataset, now let's fill it with school location data schools = School.objects.all() for school in schools: feature = { 'type': 'Feature', 'geometry': { 'type': "Point", 'coordinates': [float(school.long), float(school.lat)] }, 'properties': { 'name': school.name } } datasets.update_feature(dataset_id, school.pk, feature) self.stdout.write("fin")
def create_dataset(self, name, description): datasets = Datasets() res = datasets.create(name=name, description=desciption) ds_id = None if res.ok: ds = res.json() ds_id = ds['id'] return ds_id
import json from mapbox import Datasets access_token = '<api key with read, write, and list access for Datasets>' # Remove any datasets with the name "demo". This can be removed for wider use [ datasets.delete_dataset(d['id']) for d in datasets.list().json() if d['name'] == 'demo' ] # Create a new, empty dataset and get info about it datasets = Datasets(access_token=access_token) create_resp = datasets.create( name='demo', description='Demo dataset for Datasets and Uploads APIs') dataset_info = create_resp.json() print(dataset_info) # Add data to new dataset # Make sure there is no "id" key-value pair outside the "properties" object. Mapbox has pretty strict standards for GeoJSON. data = json.load(open(r'demo_pts.geojson')) for count, feature in enumerate(data['features']): print('{}, {}'.format(count, feature)) resp = datasets.update_feature(dataset_info['id'], count, feature) print(resp)
from mapbox import Datasets import json datasets = Datasets( access_token= 'sk.eyJ1IjoibG9raXByJjamc4aGRpZ3oyM3BxMzNuNWIzaDdja2ZzIn0.oFqNkH9Mlyv3ExsGknvSGg' ) create_resp = datasets.create(name="Bay Area Zips", description="ZTCA zones for the Bay Area") listing_resp = datasets.list() dataset_id = [ds['id'] for ds in listing_resp.json()][0] data = json.load(open(r'ztca_bayarea.geojson')) for count, feature in enumerate(data['features'][:1]): resp = datasets.update_feature(dataset_id, count, feature)
def create_dataset(name, description): ''' Creates a new empty dataset with name and description. Returns newly created dataset object. ''' datasets = Datasets() return datasets.create(name=name, description=description)