コード例 #1
0
    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")
コード例 #2
0
 def upload_dataset_features(self, dataset_id, dataset_filename):
     """
     Upload a dataset to mapbox
     This is rather slow uploading one by one so use upload as tileset
     """
     datasets = Datasets()
     with open(os.path.join(DATASETS_PATH, dataset_filename)) as src:
         features = json.load(src)
         for fid, feature in enumerate(features.get("features", [])):
             datasets.update_feature(dataset_id, fid, feature)
コード例 #3
0
def upload_to_mapbox(upload, dataset_to_update):
    
    try:
        # initialize api
        datasets = Datasets()

        os.environ['MAPBOX_ACCESS_TOKEN'] = 'sk.eyJ1IjoiZmlyZS1yaXNrLXNmIiwiYSI6ImNqZGw1dTlwdDA1aXMzM3FrbDZpZnpmczMifQ.JbUPsvx8384WHOAfA9Vy9w'

        # upload features to new dataset
        for i in range(0,len(upload['features'])):
            feature = upload['features'][i]
            index = upload['features'][i]['id']
            resp = datasets.update_feature(dataset = dataset_to_update,
                                           fid = index,
                                           feature = feature)
    except:
        
        print("Error: could not upload the new data to Mapbox. Is there a problem with the access token?")
コード例 #4
0
def insert_or_update_features(dataset_id, features):
    ''' Takes a list of features (dictionaries formatted as features)
        and adds them to dataset. If the ID of the feature is already
        in the dataset, it will be updated. If it's new, it will be inserted.
        Returns list of updated features.
        Will raise error if response is not 200.
    '''
    datasets = Datasets()
    loads = []
    for feature in features:
        resp = datasets.update_feature(dataset_id, feature['id'], feature)
        if resp.status_code != 200:
            err = (f'Response was {resp.status_code} when trying to',
                   f'update {feature} in dataset with ID {dataset_id}',
                   f'{resp.json()["message"]}')
            
            raise RuntimeError(err)
        loads.append(resp)
    return loads
コード例 #5
0
for lat in range(-90, 100, 10):
  id = "lat%d" % (lat)
  feature = {
    "type": "Feature",
    "id": id,
    "geometry": {
      "type": "LineString",
      "coordinates": [[-180, lat], [180, lat]]
    },
    "properties": {
      "name": "%d" % lat,
      "id": id
    }
  }
  resp = datasets.update_feature(datasetID, id, feature)
  print(id)

for lon in range(-180, 190, 10):
  id = "lon%d" % (lon)
  feature = {
    "type": "Feature",
    "id": id,
    "geometry": {
      "type": "LineString",
      "coordinates": [[lon, -90], [lon, 90]]
    },
    "properties": {
      "name": "%d" % lon,
      "id": id
    }
コード例 #6
0
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)
コード例 #7
0
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)
コード例 #8
0
        if not matches:
            msg += 'Street not found.\n\n'
            continue
        first_match = matches[0]
        match_in_existing = next((street for street in existing_objects if street['id'] == str(obj['id'])), None)
        if match_in_existing:
            msg += 'Found existing.\n\n'
            continue
        new_obj = {
            'type': 'Feature',
            'id': str(obj['id']),
            'properties': {
                'wp_id': int(obj['id']),
                'name': obj['name'],
                'type': 'square' if first_match['properties']['strassen_2'] == 'PLAT' else 'street',
                'quarter': obj['quarter'],
                'street_no': first_match['properties']['strassensc'],
            },
        }
        new_obj['geometry'] = generate_geometry(new_obj, matches, msg)
        if new_obj['properties']['type'] == 'street':
            update = datasets.update_feature(streets_dataset, new_obj['id'], new_obj).json()
        else:
            update = datasets.update_feature(squares_dataset, new_obj['id'], new_obj).json()

        msg += str(update) + '\n' if 'message' in update else 'Uploaded successfully.\n\n'
except Exception as e:
    msg += '\n' + str(e) + '\n'

sys.stdout.write(msg)