コード例 #1
0
ファイル: Bulkemail.py プロジェクト: vta/AdoptA
def initialize_securityhandler(url, username, password):
    """ initialize AGOL/Portal security handler """
    try:
        # if user has provided credentials
        if len(username) > 0 and len(password) > 0:
            # check if its AGOL or Portal
            if "arcgis.com" in url.lower():
                agol_sh = AGOLTokenSecurityHandler(username, password)
                return agol_sh
            else:
                portal_sh = PortalTokenSecurityHandler(username, password, url)
                return portal_sh
        else:
            return None
    except Exception as e:
        send_msg("Unable to initialize security handler. {0}".format(str(e)),
                 "error")
        return False
コード例 #2
0
def downloadData():
    print "beginning data download..."
    proxy_port = None
    proxy_url = None

    agolSH = AGOLTokenSecurityHandler(username=username, password=password)

    fl = FeatureLayer(url=feature_service_url,
                      securityHandler=agolSH,
                      proxy_port=proxy_port,
                      proxy_url=proxy_url,
                      initialize=True)

    oid_query_response = fl.query(returnIDsOnly=True)
    oid_list = oid_query_response["objectIds"]
    oid_list.sort()
    list_length = len(oid_list)
    print list_length

    if os.path.exists(output_folder + os.sep + feature_class_name + "_" +
                      the_date + ".gdb"):
        shutil.rmtree(output_folder + os.sep + feature_class_name + "_" +
                      the_date + ".gdb")
    if os.path.isfile(output_folder + os.sep + "Errors.csv"):
        os.remove(output_folder + os.sep + "Errors.csv")
    arcpy.CreateFileGDB_management(output_folder,
                                   feature_class_name + "_" + the_date)
    output_fgdb = output_folder + os.sep + feature_class_name + "_" + the_date + ".gdb"

    def updatedQuery(low, high, trigger):
        if low != high:
            updated_query = """"OBJECTID" >= """ + str(
                low) + " AND " + """"OBJECTID" < """ + str(high)
            if trigger == 1:
                updated_query = """"OBJECTID" >= """ + str(low)
        else:
            updated_query = """"OBJECTID" = """ + str(low)
        return updated_query

    errors = []
    error_fields = []
    fc = ""
    fields = ["SHAPE@"]
    low = 0
    high = 1000
    counter = 0
    while low <= list_length:
        min = oid_list[low]
        try:
            max = oid_list[high]
            trigger = 0
        except:
            totalFixed = list_length - 1
            max = oid_list[totalFixed]
            trigger = 1
        updated_query = updatedQuery(min, max, trigger)
        returned_data = fl.query(where=updated_query,
                                 out_fields='*',
                                 returnGeometry=True)
        returned_data_string = str(returned_data)
        d = json.loads(returned_data_string)
        print "dictionary compiled."

        if counter == 0:
            wkid = d['spatialReference']['latestWkid']
            sr = arcpy.SpatialReference(wkid)
            arcpy.CreateFeatureclass_management(output_fgdb,
                                                feature_class_name, "POLYLINE",
                                                "", "DISABLED", "DISABLED", sr)
            fc = output_fgdb + os.sep + feature_class_name
            for field in d['fields']:
                print field["name"]
                error_fields.append(field["name"])
                if field["name"] != "OBJECTID" and field[
                        "name"] != "Shape_Length" and field[
                            "name"] != "GlobalID":
                    text_length = ""
                    if field["type"] == "esriFieldTypeInteger":
                        type = "LONG"
                    elif field["type"] == "esriFieldTypeSmallInteger":
                        type = "SHORT"
                    elif field["type"] == "esriFieldTypeString":
                        type = "TEXT"
                        text_length = field["length"]
                    elif field["type"] == "esriFieldTypeDouble":
                        type = "DOUBLE"
                    elif field["type"] == "esriFieldTypeFloat":
                        type = "FLOAT"
                    elif field["type"] == "esriFieldTypeDate":
                        type = "DATE"
                    arcpy.AddField_management(fc, field["name"], type, "", "",
                                              text_length, field["alias"])
                    fields.append(field["name"])
            errors.append(error_fields)

        cursor = arcpy.da.InsertCursor(fc, fields)
        records = d["features"]
        for record in records:
            try:
                geom = record["geometry"]
                paths = geom["paths"]
                new_geom = arcpy.Array()
                for part in paths:
                    this_part = arcpy.Array()
                    for point in part:
                        this_point = arcpy.Point(point[0], point[1])
                        this_part.append(this_point)
                    new_geom.append(this_part)
                polyline = arcpy.Polyline(new_geom)
            except:
                polyline = arcpy.Polyline(
                    arcpy.Array(arcpy.Array(arcpy.Point(0, 0))))
                error_record = []
                for err_fld in error_fields:
                    error_record.append(record["attributes"][err_fld])
                errors.append(error_record)
                print record

            values = [polyline]
            attributes = record["attributes"]
            for field in fields:
                if field != "SHAPE@":
                    values.append(attributes[field])

            cursor.insertRow(values)
            counter += 1
            print str(counter) + "\\" + str(list_length)

        low += 1000
        high += 1000

    no_geom_csv = open(output_folder + os.sep + "Errors.csv", 'wb')
    writer = csv.writer(no_geom_csv)
    writer.writerows(errors)
    no_geom_csv.close()
コード例 #3
0
ファイル: loop_users_items.py プロジェクト: datastark/ArcREST
   Python 2/3
   ArcREST version 3.5.x
"""
from __future__ import print_function
import arcrest
from arcrest.security import AGOLTokenSecurityHandler

datetimeformat = '%m/%d/%Y %H:%M:%S'
if __name__ == "__main__":
    username = ""  #Username
    password = ""  #password
    proxy_port = None
    proxy_url = None

    agolSH = AGOLTokenSecurityHandler(username=username, password=password)

    admin = arcrest.manageorg.Administration(securityHandler=agolSH)
    content = admin.content

    commUsers = admin.portals.portalSelf.users(start=1, num=100)
    commUsers = commUsers['users']

    for commUser in commUsers:
        user = admin.content.users.user(commUser.username)
        for userItem in user.items:
            msg = "Item: {0}".format(userItem.id)
            msg = msg + "\n\tName: {0}".format(userItem.name)
            msg = msg + "\n\tTitle: {0}".format(userItem.title)
            msg = msg + "\n\tOwned by: {0}".format(commUser.username)
            msg = msg + "\n\tCreated on: {0}".format(
コード例 #4
0
def main():
    username = "******"
    password = "******"
    url = "<portal or AGOL url>"

    configFiles = 'path to csv file, can be relative to script'
    imageFolder = 'path to image folder, can be relative to script'

    sciptPath = os.getcwd()
    try:
        print "###############Script Started#################"
        print datetime.datetime.now().strftime(dateTimeFormat)
        if os.path.exists(imageFolder) == False:
            imageFolder = os.path.join(sciptPath, imageFolder)
        elif os.path.isabs(imageFolder) == False:
            imageFolder = os.path.join(sciptPath, imageFolder)
        if os.path.exists(imageFolder) == False:
            print "Image folder %s could not be located" % imageFolderName
            return
        if os.path.isfile(configFiles) == False:
            print "csv file %s could not be located" % configFiles
            return

        agolSH = AGOLTokenSecurityHandler(username=username,
                                          password=password,
                                          org_url=url)

        print "Login with token: %s" % agolSH.token

        portalAdmin = arcrest.manageorg.Administration(securityHandler=agolSH)
        content = portalAdmin.content
        adminusercontent = content.usercontent()

        with open(configFiles, 'rb') as csvfile:

            for row in csv.DictReader(csvfile, dialect='excel'):
                if not 'itemid' in row:
                    print "itemID could not be found if table"
                    return
                itemid = row['itemid']
                item = content.getItem(itemid)
                itemParams = arcrest.manageorg.ItemParameter()

                if 'thumbnail' in row:
                    print "%s to be applied to thumbnail of %s" % (
                        row['thumbnail'], itemid)
                    image = os.path.join(imageFolder, row['thumbnail'])
                    if os.path.isfile(image):

                        itemParams.thumbnail = image

                    else:
                        print "image %s could not be located" % row['thumbnail']
                if 'largethumbnail' in row:
                    print "%s to be applied to largethumbnail of %s" % (
                        row['largethumbnail'], itemid)
                    largeimage = os.path.join(imageFolder,
                                              row['largethumbnail'])
                    if os.path.isfile(largeimage):
                        itemParams.largeThumbnail = largeimage

                    else:
                        print "image %s could not be located" % row[
                            'largethumbnail']
                print adminusercontent.updateItem(
                    itemId=itemid,
                    updateItemParameters=itemParams,
                    folderId=item.ownerFolder)
    except:
        line, filename, synerror = trace()
        print "error on line: %s" % line
        print "error in file name: %s" % filename
        print "with error message: %s" % synerror

    finally:
        print datetime.datetime.now().strftime(dateTimeFormat)
        print "###############Script Completed#################"
コード例 #5
0
from arcrest.agol import FeatureLayer

if __name__ == "__main__":
    username = raw_input("Enter User Name: ")
    password = raw_input("Enter Password: "******"http://services7.arcgis.com/LLWzNvydeNCpjeTo/arcgis/rest/services/BikeRoute/FeatureServer/0"
    ]
    #if you use a proxy generally for on site services like arcgis server or portal
    proxy_port = None
    proxy_url = None

    #get the security handle token
    agol_security_handler = AGOLTokenSecurityHandler(username=username,
                                                     password=password)

    #loop over the urls in the list of urls
    for url in urls:
        #create feature layer using feature layer
        feature_layer = FeatureLayer(url=url,
                                     securityHandler=agol_security_handler,
                                     proxy_port=proxy_port,
                                     proxy_url=proxy_url,
                                     initialize=True)
        #access admin rights to feature layer
        admin_feature_layer = feature_layer.administration
        field_to_add = {
            "fields": [{
                "name": "BIKEROUTE",
                "type": "esriFieldTypeString",
コード例 #6
0
def main():
    try:
        dateTimeFormat = "%Y/%m/%d %H:%M:%S"  #Date time format of the service, example'2016-04-26 04:00:00'
        #log file to store details
        logFile = r"c:\temp\adoptedAssets.log"
        common.init_log(logFile)
        print("###### Date Extraction Process Started ######")
        username = ""
        password = ""
        proxy_port = None
        proxy_url = None
        agolSH = None
        print("\tStarted at {0}".format(
            datetime.datetime.now().strftime(dateTimeFormat)))
        #Create a authenicated connection to portal
        if username != "":
            agolSH = AGOLTokenSecurityHandler(username=username,
                                              password=password)
            print("\tLogged into the portal")

        #Settings
        url = 'http://services1.arcgis.com/DlnuvLGpDczjeSgG/arcgis/rest/services/CatchBasin/FeatureServer/0/'  #URL to adoption service
        statusField = 'Assetstatus'  #Field with status, used to build SQL
        statusValue = 'Adopted'  #Value to search for in the StatusField
        statusUpdateField = 'Laststatusupdate'  #Field used to restrict query to only records since last query
        out_fields = 'OBJECTID,GIS_ID,Nickname'  #Fields to save to the output CSV

        #The location and file name to save the results to
        saveLocation = r"c:\temp\adoptedAssets.csv"
        #File with the date of the last run, if it does not exist, all features are returned and file is created for next run
        lastRunDetails = r"c:\temp\lastrundate.txt"

        lastQueryDate = None
        #Start building the SQL Query
        sql = statusField + " = '" + statusValue + "'"
        #Open the file with the last run date
        if os.path.isfile(lastRunDetails):
            print("\tLast run file exist")
            with open(lastRunDetails, 'r') as configFile:
                lastQueryDate = configFile.read()
                configFile.close()
            print("\t\tLast query date: {0}".format(lastQueryDate))
        #If the last query date file was found and value is a date
        if lastQueryDate is not None and validate(
                date_text=lastQueryDate, dateTimeFormat=dateTimeFormat):
            sql = sql + " AND " + statusUpdateField + " >= " + "'" + lastQueryDate + "'"
        #Add current time to query
        queryDate = datetime.datetime.now().strftime(dateTimeFormat)
        sql = sql + " AND " + statusUpdateField + " <= " + "'" + queryDate + "'"
        print("\tSQL: {0}".format(sql))
        #Create a connection to the layer
        fl = FeatureLayer(url=url,
                          securityHandler=agolSH,
                          proxy_port=proxy_port,
                          proxy_url=proxy_url,
                          initialize=True)

        #query the layer
        featureSet = fl.query(where=sql,
                              out_fields=out_fields,
                              returnGeometry=False)
        print("\t{0} feature returned".format(len(featureSet.features)))
        #Create a new output writer
        if (len(featureSet.features) == 0):
            if os.path.isfile(saveLocation):
                os.remove(saveLocation)
        else:
            with open(saveLocation, "wb+") as csvFile:
                f = csv.writer(csvFile)
                fields = []
                #write the headers to the csv
                for field in featureSet.fields:
                    fields.append(field['name'])
                f.writerow(fields)

                newRow = []
                #Loop through the results and save each to a row
                for feature in featureSet:
                    newRow = []
                    for field in featureSet.fields:
                        newRow.append(feature.get_value(field['name']))
                    f.writerow(newRow)
                csvFile.close()
                print("\tCSV updated")
        #Update the last run file
        with open(lastRunDetails, 'w') as configFile:
            configFile.write(queryDate)
            configFile.close()
            print("\t{0} saved to file".format(queryDate))
        print("\tCompleted at {0}".format(
            datetime.datetime.now().strftime(dateTimeFormat)))
        print("###### Completed ######")

    except:
        line, filename, synerror = trace()
        print("error on line: %s" % line)
        print("error in file name: %s" % filename)
        print("with error message: %s" % synerror)
コード例 #7
0
#-------------------------------------------------------------------------------
# Name:        arcREST
# Purpose:     update AGOL features
#
# Author:      mostynl
#
# Created:     24/08/2015
# Copyright:   (c) mostynl 2015
# Licence:     NZ Creative Commons 3.0
#-------------------------------------------------------------------------------

from arcrest.security import AGOLTokenSecurityHandler
from arcrest.manageorg import Administration
if __name__ == "__main__":
    username = "******"
    password = "******"
    proxy_port = None
    proxy_url = None    
    securityHandler = AGOLTokenSecurityHandler(username, password,
                                               proxy_url=proxy_url,
                                               proxy_port=proxy_port)
    siteObject = Administration(securityHandler=securityHandler,
                                proxy_url=proxy_url,
                                proxy_port=proxy_port)
    results = siteObject.query(q="< some query string >")