def simulate(nr_buses=10):

    ac = AmigoCloud(AMIGOCLOUD_TOKEN)

    url = 'users/%s/projects/%s/datasets/%s/realtime' % (
        AMIGOCLOUD_USER_ID, AMIGOCLOUD_PROJECT_ID,
        AMIGOCLOUD_REALTIME_DATASET_ID
    )

    buses = [{'lat': 37.780061, 'lng': -122.413661} for _ in xrange(nr_buses)]

    while True:
        for bus_id in xrange(nr_buses):
            buses[bus_id]['lat'] += random.uniform(-0.02, 0.02)
            buses[bus_id]['lng'] += random.uniform(-0.02, 0.02)

            now = time.time()
            moovbox_data = create_moovbox_data(
                id=bus_id,
                latitude=buses[bus_id]['lng'],
                longitude=buses[bus_id]['lat'],
                fix=0,
                time=int(now),
                altitude=0,
                climb=0,
                speed=0,
                separation=0,
                track=0,
                satellites=0
            )

            ac.post(url, data=moovbox_data, send_as_json=False)
            print moovbox_data

        time.sleep(1)
Beispiel #2
0
 def __init__(self, project_id, dataset_id, token):
     self.project_id = project_id
     self.dataset_id = dataset_id
     self.ac = AmigoCloud(token=token)
     self.dataset = self.ac.get(
         self.dataset_url.format(user_id=1,
                                 project_id=project_id,
                                 dataset_id=dataset_id))
     self.table_name = self.dataset['table_name']
     self.response = self.ac.get(self.dataset['master'])
     self.master = self.response['master']
Beispiel #3
0
def amigocloud_sync_earthquakes(page=1):
    change_data = []
    for earthquake in get_earthquakes_data(page):
        amigo_id, amigo_data = to_amigo_format(earthquake)
        change_data.append({'amigo_id': amigo_id, 'new': amigo_data})
    change = {
        'type': 'DML',
        'entity': 'dataset_%s' % DATASET_ID,
        'action': 'INSERT',
        'data': change_data
    }

    amigocloud = AmigoCloud(TOKEN, PROJECT_URL)
    amigocloud.post('datasets/%s/submit_change' % DATASET_ID,
                    {'change': json.dumps(change)})
    def upload_csv(self, project_id, token, filename):
        """
        Upload CSV file as dataset to AmigoCloud
        :param project_id:
        :param token:
        :param filename:
        :return:
        """
        user_id = 1
        amigocloud = AmigoCloud(token=token)#str(sys.argv[1]))
        response = amigocloud.upload_datafile(user_id, project_id, filename)
        print "Uploading file " + filename + " to project " + project_id + "..."

        # Wait until async job is done
        job_url = 'me/jobs/%s' % response['job']
        while True:
            response = amigocloud.get(job_url)
            if response['status'] not in ('STARTED', 'PENDING'):
                break
            time.sleep(0.5)  # Wait 500ms and retry
Beispiel #5
0
 def get_context_data(self, **kwargs):
     kwargs = super(IndexView, self).get_context_data(**kwargs)
     token = self.request.GET.get('token', '')
     project_id = self.request.GET.get('project_id')
     dataset_id = self.request.GET.get('dataset_id')
     if not token:
         return kwargs
     kwargs['token'] = token
     try:
         amigocloud = AmigoCloud(token=token, use_websockets=False)
         if dataset_id:
             url = '/users/0/projects/0/datasets/%s' % dataset_id
             kwargs['dataset'] = amigocloud.get(url)
         elif project_id:
             url = '/users/0/projects/%s/datasets?type=vector' % project_id
             kwargs['datasets'] = amigocloud.get(url)
         else:
             kwargs['projects'] = amigocloud.get('/me/projects')
     except AmigoCloudError as err:
         kwargs['error'] = err.message
     return kwargs
# requires: pygeocoder>=1.2.5

from amigocloud import AmigoCloud
from pygeocoder import Geocoder

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>')

# For examples of how to get these values, see simple_example2.py
PROJECT_OWNER = 1
PROJECT_ID = 2
DATASET_ID = 3

# Get dataset information
dataset_url = '/users/{user_id}/projects/{project_id}/datasets/{dataset_id}'
dataset = ac.get(dataset_url.format(user_id=PROJECT_OWNER,
                                    project_id=PROJECT_ID,
                                    dataset_id=DATASET_ID))
table_name = dataset['table_name']

# SQL API endpont
sql_url = '/users/{user_id}/projects/{project_id}/sql'.format(
    user_id=PROJECT_OWNER, project_id=PROJECT_ID
)

# Get all records (fetching 100 on each request), until all rows are fetched
select_query = 'SELECT * FROM {table}'.format(table=table_name)
offset = 0
limit = 100
counter = 1
from pprint import pprint
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>')

# For examples of how to get these values, see simple_example2.p
PROJECT_OWNER = 1
PROJECT_ID = 573
DATASET_ID = 3626

# Get dataset information
dataset_url = '/users/{user_id}/projects/{project_id}/datasets/{dataset_id}'
dataset = ac.get(dataset_url.format(user_id=PROJECT_OWNER,
                                    project_id=PROJECT_ID,
                                    dataset_id=DATASET_ID))
# Store the table name
table_name = dataset['table_name']

print 'The table name we have to use in SQL queries is', table_name
print 'The table has', dataset['feature_count'], 'rows'

# AmigoCloud supports SQL operators of PostgreSQL and PostGIS. For more
# information please refer to:
# http://postgis.net/documentation/ and
# http://postgis.net/docs/manual-2.1/reference.html

sql_url = '/users/{user_id}/projects/{project_id}/sql'.format(
    user_id=PROJECT_OWNER, project_id=PROJECT_ID
)
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<token>')

query = ({
        "author": "",
        "extra": "",
        "layer_name": "0",
        "name": "My first baselayer",
        "public_tiles": False,
        "transparency": False,
        "url": "<baselayer URL>",
        "zoom_level_max": 20,
        "zoom_level_min": 0
        })

sql_url='<AmigoCloud baselater API URL>'

response = ac.post(url=sql_url, data=query, content_type="application/json")

print 'Response:', response

import json
import re
import time

from amigocloud import AmigoCloud


amigocloud = AmigoCloud(token='<your_token>')

PROJECT_OWNER = 1
PROJECT_ID = 123
FILE_PATH = 'sf_neighborhoods.zip'
DATASET_ID = None  # TBD

# Upload shapefile
response = amigocloud.upload_datafile(PROJECT_OWNER, PROJECT_ID, FILE_PATH)
print "Uploaded file '%s'" % FILE_PATH

# Wait until async job is done
job_url = 'me/jobs/%s' % response['job']
while True:
    response = amigocloud.get(job_url)
    if response['status'] not in ('STARTED', 'PENDING'):
        break
    time.sleep(0.5)  # Wait 500ms and retry
print "File '%s' finished processing" % FILE_PATH

# Save new dataset ID
DATASET_ID = response['extra']['dataset_ids'][0]
print 'Dataset with ID=%d was created' % DATASET_ID
Beispiel #10
0
class BLDSDataset:

    dataset_url = '/users/{user_id}/projects/{project_id}/datasets/{dataset_id}'

    def __init__(self, project_id, dataset_id, token):
        self.project_id = project_id
        self.dataset_id = dataset_id
        self.ac = AmigoCloud(token=token)
        self.dataset = self.ac.get(
            self.dataset_url.format(user_id=1,
                                    project_id=project_id,
                                    dataset_id=dataset_id))
        self.table_name = self.dataset['table_name']
        self.response = self.ac.get(self.dataset['master'])
        self.master = self.response['master']

    def add_column(self, column_json):
        print("Add column: " + column_json["name"])
        add_column = {
            "type": "DDL",
            "entity": self.table_name,
            "action": "ADD COLUMN",
            "parent": self.master,
            "data": [{
                "new": column_json
            }]
        }
        response = self.ac.post(self.dataset['submit_change'],
                                {'change': json.dumps(add_column)})
        time.sleep(5)  # to prevent Error: TOO MANY REQUESTS

    def add_columns(self, columns_json):
        print("Add columns")
        add_column = {
            "type": "DDL",
            "entity": self.table_name,
            "action": "ADD COLUMN",
            "parent": self.master,
            "data": columns_json
        }
        response = self.ac.post(self.dataset['submit_change'],
                                {'change': json.dumps(add_column)})

    """
    create_schema() creates schema for BLDS standard dataset
    """

    def create_schema(self):
        print("Create schema for " + self.table_name)
        columns = [
            # amigo_id field should be already created
            # {
            #     "name": "amigo_id",
            #     "nullable": False,
            #     "default": "GENERATE_UUID",
            #     "auto_populate": True,
            #     "max_length": 32,
            #     "type": "string"
            # },
            # Geometry field
            {
                "name": "wkb_geometry",
                "visible": True,
                "geometry_type": "POINT",
                "nullable": True,
                "editable": True,
                "alias": "wkb geometry",
                "type": "geometry"
            },
            # Required fields
            {
                "name": "permit_num",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PermitNum",
                "type": "string"
            },
            {
                "name": "description",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "Description",
                "type": "string"
            },
            {
                "name": "applied_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "AppliedDate",
                "type": "string"
            },
            {
                "name": "issued_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "IssuedDate",
                "type": "string"
            },
            {
                "name": "completed_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "CompletedDate",
                "type": "string"
            },
            {
                "name": "status_current",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "StatusCurrent",
                "type": "string"
            },
            {
                "name": "original_address1",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "OriginalAddress1",
                "type": "string"
            },
            {
                "name": "original_address2",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "OriginalAddress2",
                "type": "string"
            },
            {
                "name": "original_city",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "OriginalCity",
                "type": "string"
            },
            {
                "name": "original_state",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "OriginalState",
                "type": "string"
            },
            {
                "name": "original_zip",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "OriginalZip",
                "type": "string"
            },
            # Recommended fields
            {
                "name": "jurisdiction",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "Jurisdiction",
                "type": "string"
            },
            {
                "name": "permit_class",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PermitClass",
                "type": "string"
            },
            {
                "name": "permit_class_mapped",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PermitClassMapped",
                "type": "string"
            },
            {
                "name":
                "status_current_mapped",
                "nullable":
                True,
                "default":
                None,
                "editable":
                True,
                "choices": [{
                    "code": "Appeal",
                    "value": "Appeal"
                }, {
                    "code": "Application Accepted",
                    "value": "Application Accepted"
                }, {
                    "code": "Fees/Payment",
                    "value": "Fees/Payment"
                }, {
                    "code": "In Review",
                    "value": "In Review"
                }, {
                    "code": "Inspection Phase",
                    "value": "Inspection Phase"
                }, {
                    "code": "Occupancy",
                    "value": "Occupancy"
                }, {
                    "code": "Permit Cancelled",
                    "value": "Permit Cancelled"
                }, {
                    "code": "Permit Finaled",
                    "value": "Permit Finaled"
                }, {
                    "code": "Permit Finaled with Conditions",
                    "value": "Permit Finaled with Conditions"
                }, {
                    "code": "Permit Issued",
                    "value": "Permit Issued"
                }],
                "visible":
                True,
                "alias":
                "StatusCurrentMapped",
                "type":
                "string"
            },
            {
                "name": "work_class",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "WorkClass",
                "type": "string"
            },
            {
                "name":
                "permit_type_mapped",
                "nullable":
                True,
                "default":
                None,
                "editable":
                True,
                "choices": [{
                    "code": "Building",
                    "value": "Building"
                }, {
                    "code": "Demolition",
                    "value": "Demolition"
                }, {
                    "code": "Electrical",
                    "value": "Electrical"
                }, {
                    "code": "Fence",
                    "value": "Fence"
                }, {
                    "code": "Grading",
                    "value": "Grading"
                }, {
                    "code": "Mechanical",
                    "value": "Mechanical"
                }, {
                    "code": "Plumbing",
                    "value": "Plumbing"
                }, {
                    "code": "Pool/Spa",
                    "value": "Pool/Spa"
                }, {
                    "code": "Roof",
                    "value": "Roof"
                }],
                "visible":
                True,
                "alias":
                "PermitTypeMapped",
                "type":
                "string"
            },
            {
                "name": "permit_type",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PermitType",
                "type": "string"
            },
            {
                "name":
                "work_class_mapped",
                "nullable":
                True,
                "default":
                None,
                "editable":
                True,
                "choices": [{
                    "code": "Existing",
                    "value": "Existing"
                }, {
                    "code": "New",
                    "value": "New"
                }],
                "visible":
                True,
                "alias":
                "WorkClassMapped",
                "type":
                "string"
            },
            {
                "name": "permit_type_desc",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PermitTypeDesc",
                "type": "string"
            },
            {
                "name": "est_project_cost",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "EstProjectCost",
                "type": "float"
            },
            {
                "name": "status_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "StatusDate",
                "type": "string"
            },
            {
                "name": "contractor_trade",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorTrade",
                "type": "string"
            },
            {
                "name": "latitude",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "Latitude",
                "type": "float"
            },
            {
                "name": "longitude",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "Longitude",
                "type": "float"
            },
            {
                "name": "pin",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "PIN",
                "type": "string"
            },
            {
                "name": "housing_units",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "HousingUnits",
                "type": "integer"
            },
            {
                "name": "total_sqft",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "TotalSqFt",
                "type": "float"
            },
            {
                "name": "contractor_state_lic",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorStateLic",
                "type": "string"
            },
            {
                "name": "link",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "Link",
                "type": "string"
            },
            {
                "name": "contractor_trade_mapped",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorTradeMapped",
                "type": "string"
            },
            {
                "name": "contractor_lic_num",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorLicNum",
                "type": "string"
            },
            {
                "name": "contractor_company_name",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorCompanyName",
                "type": "string"
            },
            # Optional fields
            {
                "name": "proposed_use",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ProposedUse",
                "type": "string"
            },
            {
                "name": "added_sqft",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "AddedSqFt",
                "type": "float"
            },
            {
                "name": "removed_sqft",
                "nullable": True,
                "default": None,
                "max_value": None,
                "min_value": None,
                "editable": True,
                "visible": True,
                "alias": "RemovedSqFt",
                "type": "float"
            },
            {
                "name": "master_permit_num",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "MasterPermitNum",
                "type": "string"
            },
            {
                "name": "expires_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ExpiresDate",
                "type": "string"
            },
            {
                "name": "co_issued_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "COIssuedDate",
                "type": "string"
            },
            {
                "name": "hold_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "HoldDate",
                "type": "string"
            },
            {
                "name": "void_date",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "VoidDate",
                "type": "string"
            },
            {
                "name": "project_name",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ProjectName",
                "type": "string"
            },
            {
                "name": "project_id",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ProjectID",
                "type": "string"
            },
            {
                "name": "total_finished_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalFinishedSqFt",
                "type": "string"
            },
            {
                "name": "total_unfinished_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalUnfinishedSqFt",
                "type": "string"
            },
            {
                "name": "total_heated_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalHeatedSqFt",
                "type": "string"
            },
            {
                "name": "total_unheated_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalUnHeatedSqFt",
                "type": "string"
            },
            {
                "name": "total_acc_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalAccSqFt",
                "type": "string"
            },
            {
                "name": "total_sprinkled_sqft",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "TotalSprinkledSqFt",
                "type": "string"
            },
            {
                "name": "extra_fields",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ExtraFields",
                "type": "string"
            },
            {
                "name": "publisher",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "Publisher",
                "type": "string"
            },
            {
                "name": "fee",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "Fee",
                "type": "float"
            },
            {
                "name": "contractor_full_name",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorFullName",
                "type": "string"
            },
            {
                "name": "contractor_company_desc",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorCompanyDesc",
                "type": "string"
            },
            {
                "name": "contractor_phone",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorPhone",
                "type": "string"
            },
            {
                "name": "contractor_address1",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorAddress1",
                "type": "string"
            },
            {
                "name": "contractor_address2",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorAddress2",
                "type": "string"
            },
            {
                "name": "contractor_city",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorCity",
                "type": "string"
            },
            {
                "name": "contractor_state",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorState",
                "type": "string"
            },
            {
                "name": "contractor_zip",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorZip",
                "type": "string"
            },
            {
                "name": "contractor_email",
                "visible": True,
                "default": None,
                "nullable": True,
                "editable": True,
                "alias": "ContractorEmail",
                "type": "string"
            },
        ]

        final_columns = []
        for column in columns:
            final_columns.append({"new": column})
        self.add_columns(final_columns)

    def upload_records(self, records, action):
        insert_records = {
            "type": "DML",
            "entity": self.table_name,
            "action": action,
            "data": records
        }
        # print insert_record
        response = self.ac.post(
            self.dataset['submit_change'],
            {'change': unicode(json.dumps(insert_records), errors='ignore')})
        print("Upload " + str(len(records)) + " records to " + self.table_name)
        time.sleep(10)  # to prevent Error: TOO MANY REQUESTS

    def get_record_obj(self, record, city):
        obj = dict()
        for field_name, value in record.iteritems():
            blds_filed = city.get_field(field_name)
            blds_value = city.get_value(field_name, value, record)
            if blds_filed:
                obj[blds_filed] = blds_value
        u = uuid.uuid4()
        new_obj = {"new": obj}
        new_obj["amigo_id"] = u.hex
        return new_obj

    def upload_permits(self, permits, city):
        records = []
        index = 0
        for key, value in permits.iteritems():
            for p in value:
                records.append(self.get_record_obj(p, city))
                index += 1
                if index >= 1000:
                    self.upload_records(records, "INSERT")
                    records[:] = []
                    index = 0
        # Upload the rest of the records
        self.upload_records(records, "INSERT")

    def update_records(self, data):
        records = []
        index = 0
        for r in data:
            new_obj = {"new": r}
            new_obj["amigo_id"] = r['amigo_id']
            records.append(new_obj)
            index += 1
            if index >= 1000:
                self.upload_records(records, "UPDATE")
                records[:] = []
                index = 0
        # Upload the rest of the records
        self.upload_records(records, "UPDATE")

    def query_page(self, query, limit, offset):
        sql_url = '/users/{user_id}/projects/{project_id}/sql'.format(
            user_id=1, project_id=self.project_id)
        rows = []
        response = self.ac.get(
            sql_url, {
                'query': query,
                'offset': offset,
                'limit': limit,
                'dataset_id': self.dataset_id
            })
        if not offset:  # i.e. If first request
            print('The schema of the result is:')
            print(response['columns'])
        fetched_rows = len(response['data'])
        offset += fetched_rows
        rows += response['data']
        return rows
import json
from pprint import pprint
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>')

# For examples of how to get these values, see simple_example2.py
PROJECT_OWNER = 1
PROJECT_ID = 573
DATASET_ID = 3626

# Get dataset information
dataset_url = '/users/{user_id}/projects/{project_id}/datasets/{dataset_id}'
dataset = ac.get(dataset_url.format(user_id=PROJECT_OWNER,
                                    project_id=PROJECT_ID,
                                    dataset_id=DATASET_ID))
# Store the table name
table_name = dataset['table_name']

# Request and store master state
response = ac.get(dataset['master'])
master = response['master']

print 'The table name we have to use in SQL queries is', table_name
print 'The master state of the dataset is', master

# Build "ADD COLUMN schema change:
add_column = {
    "type": "DDL",
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>',
                use_websockets=False)

# Geocoder parameters
# Address to geocode. For example, AmigoCloud address.
ADDRESS = '300 3rd Street, San Francisco, United States'
# Values to filter the geocoding response.
COMPONENTS = 'country:US'
# More information:
# https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

# Geocoder API endpoint (Version 2)
geocoder_url = 'https://app.amigocloud.com/api/v2/me/geocoder/search'
geocoder_params = {'input': ADDRESS, 'components': COMPONENTS}

geocoder_result = ac.get(geocoder_url, params=geocoder_params)

# Status 'OK' indicates that no errors occurred; the address was successfully
# parsed and at least one geocode was returned.
if geocoder_result['status'] == 'OK':
    lat = geocoder_result['results'][0]['geometry']['location']['lat']
    lng = geocoder_result['results'][0]['geometry']['location']['lng']

    # We can use these coordinates to find intersects in our polygons
    PROJECT_ID = 1234
    DATASET_ID = 5678
    POLYGON_GEOMETRY_FIELD_NAME = 'polygon'
# Script to query record_history across all AmigoCloud projects and export results to a CSV
#   Must have AmigoCloud Account
#   All projects must have a record_history dataset (no projects older than 2017)
from amigocloud import AmigoCloud
import csv

# AmigoCloud variables - change based on user
#   token found at www.amigocloud.com/accounts/tokens
#   project owner = user id found in /api/v1/me
amigocloud = AmigoCloud(token='<>')
projectOwner = <>
projectNum = [] * 200
recordNum = [] * 200

# Project variables used to parse through all projects the user has
projects = amigocloud.get('users/%s/projects/' % (projectOwner))
projectsNext = projects['next']

# Project list is offset by 20.  While loop is setup so if user has more than 20 projects it will grab the next set of 20.
while True:
    for project in projects['results']: #parse through projects
        projectNum.append(project['id'])
        projID = project['id']
        datasets = amigocloud.get('users/%s/projects/%s/datasets' % (projectOwner,projID))
        for rh in datasets['results']: #parse throgh datasets
            if rh['name'] == 'record_history': #if the dataset is called record_history append to recordNum list
                recordNum.append(rh['id'])
                projectsNext = projects['next']
    if projectsNext is None:
        break
    projects = amigocloud.get(projectsNext)
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>',
                use_websockets=False)

# For examples of how to get these values, see simple_example2.py
PROJECT_ID = 1234
DATASET_ID = 5678

# Name of the fields that are going to be use in the geocoding.
# You can found them in the schema editor.
ADDRESS_FIELD_NAME = 'address'
GEOMETRY_FIELD_NAME = 'point'

# Dictionary to filter the geocoding response.
PARAMS = {'country': 'PE'}
# More information:
# https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

# Project and dataset ids must be strings
ac.geocode_addresses(str(PROJECT_ID), str(DATASET_ID),
                     ADDRESS_FIELD_NAME, GEOMETRY_FIELD_NAME, **PARAMS)
import time

from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>')

# For examples of how to get these values, see simple_example2.py
PROJECT_OWNER = 1
PROJECT_ID = 2
DATASET_ID = 3

# From which state to which state do you wanna revert? (the changesets will be
# reverted in reversed order)
FROM_STATE = '...'
TO_STATE = '...'

# Get dataset information
dataset_url = (
    '/users/{user_id}/projects/{project_id}/datasets/{dataset_id}'.format(
        user_id=PROJECT_OWNER, project_id=PROJECT_ID, dataset_id=DATASET_ID
    )
)
dataset = ac.get(dataset_url)

# Get list of all the states between FROM_STATE and TO_STATE
result = ac.get(dataset['states'], {'from': FROM_STATE, 'to': TO_STATE})
states = result['states']

# Revert the states in reverted order
from pprint import pprint
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the API token is invalid or has expired
ac = AmigoCloud(token='<your token>',
                use_websockets=False)

# For examples of how to get these values, see simple_example2.py
PROJECT_ID = 14098
DATASET_ID = 84746

#API endpoint
sql_url = 'projects/{project_id}/sql'.format(project_id=PROJECT_ID)

# find all rows that intersect the hard coded point
query = """
SELECT * 
FROM dataset_{dataset_id} 
WHERE ST_Intersects(wkb_geometry, ST_PointFromText('POINT(-117.150727812638 32.7068451387017)', 4326))
""".format(dataset_id = DATASET_ID)

response = ac.get(sql_url, {'query': query, 
                            'dataset_id': DATASET_ID})

# print schema of response
pprint(response['columns'])

# print row contents
pprint(response['data'])
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token='<your_token>')

# Print information about my account https://www.amigocloud.com/api/v1/me
print ac.get('me')
from pprint import pprint
from amigocloud import AmigoCloud

# Use amigocloud version 1.0.5 or higher to login with tokens
# This will raise an AmigoCloudError if the token is invalid or has expired
ac = AmigoCloud(token="<your_token>")

# Get User ID
user_data = ac.get("me")
print "My user ID is", user_data["id"]

# Get list of projects
projects = ac.get(user_data["projects"])
print "These are my projects:"
pprint(projects)  # pretty print them since this could be very long

# Get first project
project = projects["results"][0]
print "Project ID is", project["id"]

# Get all datasets of the first project
datasets = ac.get(project["datasets"])
print "These are all the datasets of my first project:"
pprint(datasets)

# Get first dataset in that project
dataset = datasets["results"][0]
print "Dataset ID is", dataset["id"]

print "This is the bounding of the first dataset:"
print (dataset["boundingbox"])
from amigocloud import AmigoCloud
amigocloud = AmigoCloud(token='<token>')

owner_id = 1  # Project Owner ID
project_id = 2  # Project ID
dataset_id = 3  # Dataset ID

amigocloud.listen_dataset_events(owner_id, project_id, dataset_id)

def realtime(data):
    print 'Realtime dataset id=%(dataset_id)s' % data
    for obj in data['data']:
        print "Object '%(object_id)s' is now at (%(latitude)s, %(longitude)s)" % obj

amigocloud.add_callback('realtime', realtime)
amigocloud.start_listening()