예제 #1
0
    def DeleteFeaturesFromFeatureLayer(self,url,sql):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler,)
            return fl.deleteFeatures(where=sql)
        except arcpy.ExecuteError:
            line, filename, synerror = Common.trace()
            raise ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                        "arcpyError": arcpy.GetMessages(2),
                                        }
                                        )
        except:
            line, filename, synerror = Common.trace()
            raise ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #2
0
    def AddFeaturesToFeatureLayer(self, url, pathToFeatureClass):
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            return fl.addFeatures(fc=pathToFeatureClass)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            })
        except:
            line, filename, synerror = trace()
            raise ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None

            del fl

            gc.collect()
예제 #3
0
def retrieveObservationsForCity():

    #connectToAGOL()
    #setupEnvironment()

    fc = "Boundary"
    global token

    geometry = None

    for row in arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"]):
        # Print the current multipoint's ID
        print("Feature {}:".format(row[0]))
        geometry = row[1]

    print(geometry)

    geomFilter = GeometryFilter(geomObject=geometry)

    fl = FeatureLayer(url=url,
                      securityHandler=token.securityhandler,
                      initialize=True)
    result = fl.query(out_fields='*',
                      returnGeometry=True,
                      geometryFilter=geomFilter)

    return result.features
예제 #4
0
    def DeleteFeaturesFromFeatureLayer(self, url, sql):
        fl = None
        try:
            fl = FeatureLayer(
                url=url,
                securityHandler=self._securityHandler,
            )
            return fl.deleteFeatures(where=sql)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise ArcRestHelperError({
                "function": "DeleteFeaturesFromFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            })
        except:
            line, filename, synerror = trace()
            raise ArcRestHelperError({
                "function": "DeleteFeaturesFromFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None

            del fl

            gc.collect()
예제 #5
0
def getCurrentGumCount():
    global token
    gumCountOutField = "total_gum_count"
    observationsCountOutField = "total_observations"
    fl = FeatureLayer(url=url,
                      securityHandler=token.securityhandler,
                      initialize=True)
    statisticFilter = StatisticFilter()

    statisticFilter.add(statisticType="count",
                        onStatisticField="gum_count",
                        outStatisticFieldName=observationsCountOutField)
    statisticFilter.add(statisticType="sum",
                        onStatisticField="gum_count",
                        outStatisticFieldName=gumCountOutField)
    result = fl.query(
        statisticFilter=statisticFilter)  #groupByFieldsForStatistics="city",

    gumCount = 0
    obsCount = 0

    for f in result.features:

        for key in f.asDictionary['attributes']:
            arcpy.AddMessage(key + ": " +
                             str(f.asDictionary['attributes'][key]))

            if key == gumCountOutField:
                gumCount = f.asDictionary['attributes'][key]
            if key == observationsCountOutField:
                obsCount = f.asDictionary['attributes'][key]

    #arcpy.AddMessage("Retrieved Total Gum Count: " + str(gumCount) + " and Total Observations Count: " + str(obsCount) + " from AGOL")

    return gumCount, obsCount, fl, statisticFilter
def main(*argv):
    """ main driver of program """
    try:
        url = str(argv[0])

        arcgisSH = ArcGISTokenSecurityHandler()
        if arcgisSH.valid == False:
            arcpy.AddError(arcgisSH.message)
            return
        fl = FeatureLayer(url=url, securityHandler=arcgisSH, initialize=True)

        res = fl.query(where="1=1", out_fields='*', returnGeometry=False)
        arcpy.AddMessage(res)
        arcpy.SetParameterAsText(1, str(res))
    except arcpy.ExecuteError:
        line, filename, synerror = trace()
        arcpy.AddError("error on line: %s" % line)
        arcpy.AddError("error in file name: %s" % filename)
        arcpy.AddError("with error message: %s" % synerror)
        arcpy.AddError("ArcPy Error Message: %s" % arcpy.GetMessages(2))
    except FunctionError, f_e:
        messages = f_e.args[0]
        arcpy.AddError("error in function: %s" % messages["function"])
        arcpy.AddError("error on line: %s" % messages["line"])
        arcpy.AddError("error in file name: %s" % messages["filename"])
        arcpy.AddError("with error message: %s" % messages["synerror"])
        arcpy.AddError("ArcPy Error Message: %s" % messages["arc"])
예제 #7
0
    def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            return fl.addFeatures(fc=pathToFeatureClass)
        except arcpy.ExecuteError:
            line, filename, synerror = Common.trace()
            raise ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                        "arcpyError": arcpy.GetMessages(2),
                                        }
                                        )
        except:
            line, filename, synerror = Common.trace()
            raise ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
def main(*argv):
    """ main driver of program """
    try:
        url = str(argv[0])

        arcgisSH = ArcGISTokenSecurityHandler()
        if arcgisSH.valid == False:
            arcpy.AddError(arcgisSH.message)
            return
        fl = FeatureLayer(
            url=url,
            securityHandler=arcgisSH,
            initialize=True)

        res = fl.query(where="1=1",out_fields='*',returnGeometry=False)
        arcpy.AddMessage(res)
        arcpy.SetParameterAsText(1, str(res))
    except arcpy.ExecuteError:
        line, filename, synerror = trace()
        arcpy.AddError("error on line: %s" % line)
        arcpy.AddError("error in file name: %s" % filename)
        arcpy.AddError("with error message: %s" % synerror)
        arcpy.AddError("ArcPy Error Message: %s" % arcpy.GetMessages(2))
    except FunctionError, f_e:
        messages = f_e.args[0]
        arcpy.AddError("error in function: %s" % messages["function"])
        arcpy.AddError("error on line: %s" % messages["line"])
        arcpy.AddError("error in file name: %s" % messages["filename"])
        arcpy.AddError("with error message: %s" % messages["synerror"])
        arcpy.AddError("ArcPy Error Message: %s" % messages["arc"])
def update_agol(url, fld, sequence_value, interval, seq_format='{}'):
    """Update fetures in an agol/portal service with id values
    Return next valid sequence value"""

    # Connect to org
    securityinfo = {}
    securityinfo[
        'security_type'] = 'Portal'  # LDAP, NTLM, OAuth, Portal, PKI, ArcGIS
    securityinfo['username'] = username
    securityinfo['password'] = password
    securityinfo['org_url'] = orgURL
    securityinfo['proxy_url'] = None
    securityinfo['proxy_port'] = None
    securityinfo['referer_url'] = None
    securityinfo['token_url'] = None
    securityinfo['certificatefile'] = None
    securityinfo['keyfile'] = None
    securityinfo['client_id'] = None
    securityinfo['secret_id'] = None

    shh = securityhandlerhelper.securityhandlerhelper(
        securityinfo=securityinfo)

    if not shh.valid:
        return 'Could not connect to {}. Please verify paths and credentials.'.format(
            url)

    fl = FeatureLayer(url=url,
                      securityHandler=shh.securityhandler,
                      proxy_port=None,
                      proxy_url=None,
                      initialize=True)

    # Build SQL query to find features missing id
    sql = """{} is null""".format(fld)

    out_fields = ['objectid', fld]

    # Get features without id
    resFeats = fl.query(where=sql, out_fields=','.join(out_fields))

    # For each feature
    for feat in resFeats:
        id_value = seq_format.format(sequence_value)

        # Update id
        feat.set_value(fld, id_value)

        # Increment sequence value
        sequence_value += interval

    update_results = fl.updateFeature(features=resFeats)

    for res in update_results['updateResults']:
        if res['success'] == False:
            return 'error {}: {}'.format(res['error']['code'],
                                         res['error']['description'])

    return sequence_value
예제 #10
0
    def QueryAllFeatures(self,url,sql,out_fields="*",chunksize=1000,saveLocation="",outName=""):
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            qRes = fl.query(where=sql, returnIDsOnly=True)

            if 'error' in qRes:
                print (qRes)
                return qRes
            elif 'objectIds' in qRes:
                oids = qRes['objectIds']
                total = len(oids)
                if total == 0:
                    return  {'success':True, 'message':"No features matched the query"}

                print ("%s features to be downloaded" % total)
                chunksize = min(chunksize, fl.maxRecordCount)
                combinedResults = None
                totalQueried = 0
                for chunk in chunklist(l=oids, n=chunksize):
                    oidsQuery = ",".join(map(str, chunk))
                    if not oidsQuery:
                        continue
                    else:
                        results = fl.query(objectIds=oidsQuery,
                                           returnGeometry=True,
                                           out_fields=out_fields)
                        if isinstance(results,FeatureSet):
                            if combinedResults is None:
                                combinedResults = results
                            else:
                                for feature in results.features:
                                    combinedResults.features.append(feature)

                            totalQueried += len(results.features)
                            print("{:.0%} Completed: {}/{}".format(totalQueried / float(total), totalQueried, total))

                        else:
                            print (results)
                if saveLocation == "" or outName == "":
                    return combinedResults
                else:
                    return combinedResults.save(saveLocation=saveLocation, outName=outName)
            else:
                print (qRes)
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "QueryFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None
            del fl
            gc.collect()
def connect_to_layer(username, password, server_url, service_url):
    """Connect to ArcGIS Online or Portal for ArcGIS layer"""
    proxy_port = None
    proxy_url = None

    si = {}
    si['security_type'] = 'Portal'  # LDAP, NTLM, OAuth, Portal, PKI
    si['username'] = username
    si['password'] = password
    si['org_url'] = server_url
    si['proxy_url'] = proxy_url
    si['proxy_port'] = proxy_port
    si['referer_url'] = None
    si['token_url'] = None
    si['certificatefile'] = None
    si['keyfile'] = None
    si['client_id'] = None
    si['secret_id'] = None

    shh = securityhandlerhelper.securityhandlerhelper(securityinfo=si)
    if not shh.valid:
        raise Exception(shh.message)

    fl = FeatureLayer(url=service_url,
                      securityHandler=shh.securityhandler,
                      proxy_port=proxy_port,
                      proxy_url=proxy_url,
                      initialize=True)

    return fl
예제 #12
0
파일: Bulkemail.py 프로젝트: vta/AdoptA
def initialize_featurelayer(layer_url, agol_sh):
    """ used to initialize the asset feature layer """
    try:
        feature_layer = FeatureLayer(url=layer_url,
                                     securityHandler=agol_sh,
                                     initialize=True)
        return feature_layer
    except Exception as e:
        send_msg("Could not initialize layer. URL-{0} {1}"\
                        .format(layer_url, str(e)), "error")
        return False
예제 #13
0
def initialize_featurelayer(layer_url, agol_sh):
    """ used to initialize the asset feature layer and user table """
    try:
        from arcrest.agol import FeatureLayer
        feature_layer = FeatureLayer(url=layer_url,
                                     securityHandler=agol_sh,
                                     initialize=False)
        return feature_layer
    except Exception as e:
        send_msg("Could not initialize asset layer. URL-{0} Error: {1}"\
                        .format(layer_url, str(e)), "error")
        return False
예제 #14
0
def retrieveObservations():

    #token = securityhandlerhelper.securityhandlerhelper(config)
    global token
    #admin = arcrest.manageorg.Administration(securityHandler=token.securityhandler)
    #content = admin.content
    #userInfo = content.users.user()
    #userInfo.folders

    #arcpy.AddMessage(userInfo.folders)

    fl = FeatureLayer(url=url,
                      securityHandler=token.securityhandler,
                      initialize=True)
    result = fl.query(where="city = '(none)'",
                      out_fields='*',
                      returnGeometry=True)
    fl.query()
    #print(result.features)
    #print (fl.query(where="1=1",out_fields='*',returnGeometry=False) )
    return result.features
예제 #15
0
    def DeleteFeaturesFromFeatureLayer(self,url,sql):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            return fl.deleteFeatures(where=sql)
       
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #16
0
    def RemoveAndAddFeatures(self, url, pathToFeatureClass, id_field, chunksize=1000):
        fl = None

        try:
            arcpy.env.overwriteOutput = True
            tempaddlayer = "ewtdwedfew"
            if not arcpy.Exists(pathToFeatureClass):
                raise common.ArcRestHelperError(
                    {
                        "function": "RemoveAndAddFeatures",
                        "line": inspect.currentframe().f_back.f_lineno,
                        "filename": "featureservicetools",
                        "synerror": "%s does not exist" % pathToFeatureClass,
                    }
                )

            fields = arcpy.ListFields(pathToFeatureClass, wild_card=id_field)
            if len(fields) == 0:
                raise common.ArcRestHelperError(
                    {
                        "function": "RemoveAndAddFeatures",
                        "line": inspect.currentframe().f_back.f_lineno,
                        "filename": "featureservicetools",
                        "synerror": "%s field does not exist" % id_field,
                    }
                )
            strFld = True
            if fields[0].type != "String":
                strFld = False

            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)

            id_field_local = arcpy.AddFieldDelimiters(pathToFeatureClass, id_field)
            idlist = []
            print arcpy.GetCount_management(in_rows=pathToFeatureClass).getOutput(0) + " features in the layer"
            with arcpy.da.SearchCursor(pathToFeatureClass, (id_field)) as cursor:
                for row in cursor:

                    if strFld:
                        idlist.append("'" + row[0] + "'")
                    else:
                        idlist.append(row[0])
                    if len(idlist) >= chunksize:
                        idstring = " in (" + ",".join(idlist) + ")"
                        sql = id_field + idstring
                        sqlLocalFC = id_field_local + idstring
                        results = fl.deleteFeatures(where=sql, rollbackOnFailure=True)

                        if "error" in results:
                            raise common.ArcRestHelperError(
                                {
                                    "function": "RemoveAndAddFeatures",
                                    "line": inspect.currentframe().f_back.f_lineno,
                                    "filename": "featureservicetools",
                                    "synerror": results["error"],
                                }
                            )
                        elif "deleteResults" in results:
                            print "%s features deleted" % len(results["deleteResults"])
                            for itm in results["deleteResults"]:
                                if itm["success"] != True:
                                    print itm
                        else:
                            print results

                        arcpy.MakeFeatureLayer_management(pathToFeatureClass, tempaddlayer, sqlLocalFC)
                        results = fl.addFeatures(fc=tempaddlayer)

                        if "error" in results:
                            raise common.ArcRestHelperError(
                                {
                                    "function": "RemoveAndAddFeatures",
                                    "line": inspect.currentframe().f_back.f_lineno,
                                    "filename": "featureservicetools",
                                    "synerror": results["error"],
                                }
                            )
                        elif "addResults" in results:
                            print "%s features added" % len(results["addResults"])
                            for itm in results["addResults"]:
                                if itm["success"] != True:
                                    print itm
                        else:
                            print results
                        idlist = []

            if "error" in results:
                raise common.ArcRestHelperError(
                    {
                        "function": "RemoveAndAddFeatures",
                        "line": inspect.currentframe().f_back.f_lineno,
                        "filename": "featureservicetools",
                        "synerror": results["error"],
                    }
                )
            else:
                print results
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError(
                {
                    "function": "create_report_layers_using_config",
                    "line": line,
                    "filename": filename,
                    "synerror": synerror,
                    "arcpyError": arcpy.GetMessages(2),
                }
            )
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError(
                {"function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror}
            )
        finally:

            gc.collect()
예제 #17
0
def main():
    try:
        proxy_port = None
        proxy_url = None
    
        securityinfo = {}
        securityinfo['security_type'] = 'Portal'#LDAP, NTLM, OAuth, Portal, PKI
        securityinfo['username'] = ""#<UserName>
        securityinfo['password'] = ""#<Password>
        securityinfo['org_url'] = "http://www.arcgis.com"
        securityinfo['proxy_url'] = proxy_url
        securityinfo['proxy_port'] = proxy_port
        securityinfo['referer_url'] = None
        securityinfo['token_url'] = None
        securityinfo['certificatefile'] = None
        securityinfo['keyfile'] = None
        securityinfo['client_id'] = None
        securityinfo['secret_id'] = None
    
    
        workforceProjectID = '' #Workforce project number

        assignmentAreasID = '' #ID of service to get centroids from
        assignmentAreaLayerName = ''#layer in servuce
        assignmentAreaNameField = ''#field with name of id area
        
        csvPath = r".\dataToLookup.csv"#<Path with data>
        workerCol = 'worker'
        areaCol  = 'area'
        descriptionCol = "description"
        notesCol = "notes"
        supervisorCol = "supervisor"
         
        assignmentType = 2
        status = 1
        
        workerNameToID = {}
        dispatcherNameToID = {}
        areaNameToID = {}
        fst = featureservicetools.featureservicetools(securityinfo)
        if fst.valid == False:
            print (fst.message)
        else:
            portalAdmin = arcrest.manageorg.Administration(securityHandler=fst.securityhandler)
            #Get the assignment areas
            fs = fst.GetFeatureService(itemId=assignmentAreasID,returnURLOnly=False)
            if not fs is None:
                fs_url = fst.GetLayerFromFeatureService(fs=fs,layerName=assignmentAreaLayerName,returnURLOnly=True)
                if not fs_url is None:

                    fl = FeatureLayer(
                        url=fs_url,
                        securityHandler=fst.securityhandler,
                        proxy_port=proxy_port,
                        proxy_url=proxy_url,
                        initialize=True)                    
                    areaResults =  fl.query(**{'where':"1=1",'outSR':'102100','out_fields':assignmentAreaNameField,'returnGeometry':False,'returnCentroid':True})
                   
                    for area in areaResults:
                        arDict = area.asDictionary
                        areaNameToID[arDict['attributes'][assignmentAreaNameField]] = arDict['centroid']
                    
            #Get the workers
            item = portalAdmin.content.getItem(itemId=workforceProjectID)
            itemData = item.itemData()
            if 'workers' in itemData:
                fl = FeatureLayer(
                    url=itemData['workers']['url'],
                    securityHandler=fst.securityhandler,
                    proxy_port=proxy_port,
                    proxy_url=proxy_url,
                    initialize=True)
            
                workersResults = fl.query(where="1=1",out_fields='OBJECTID, NAME',returnGeometry=False)
                for worker in workersResults:
                    workerNameToID[worker.get_value('name')] = worker.get_value('OBJECTID')
            
            if 'dispatchers' in itemData:
                fl = FeatureLayer(
                    url=itemData['dispatchers']['url'],
                    securityHandler=fst.securityhandler,
                    proxy_port=proxy_port,
                    proxy_url=proxy_url,
                    initialize=True)
            
                dispatcherResults = fl.query(where="1=1",out_fields='OBJECTID, NAME',returnGeometry=False)
                for dispatcher in dispatcherResults:
                    dispatcherNameToID[dispatcher.get_value('name')] = dispatcher.get_value('OBJECTID')
                    
    
            if 'assignments' in itemData:
                features = []
                
                fl = FeatureLayer(
                    url=itemData['assignments']['url'],
                    securityHandler=fst.securityhandler,
                    proxy_port=proxy_port,
                    proxy_url=proxy_url,
                    initialize=True)
                print(fl.deleteFeatures(where="1=1"))
                with open(csvPath) as csvfile:
                    reader = UnicodeDictReader(csvfile)
                    for row in reader:
                        json_string={}
                        json_string['geometry'] = {}
                       
                        centroidInfo = areaNameToID[row[areaCol].strip()]
                        
                        json_string['geometry']['x'] = centroidInfo['x']
                        json_string['geometry']['y'] = centroidInfo['y']
                        json_string['attributes'] ={}
                        json_string['attributes']['workerId'] = workerNameToID[row[workerCol].strip()]
                        json_string['attributes']['description'] = row[descriptionCol]
                        json_string['attributes']['notes'] = row[notesCol]
                        json_string['attributes']['assignmentType'] = assignmentType
                        json_string['attributes']['status'] = status
                        json_string['attributes']['dispatcherId'] = dispatcherNameToID[row[supervisorCol].strip()]
                        
                         
                        features.append(Feature(json_string=json_string))
                    results = fl.addFeature(features=features)
    
                    if 'error' in results:
                        print ("Error in response from server:  %s" % results['error'])
    
                    else:
                        if results['addResults'] is not None:
                            featSucces = 0
                            for result in results['addResults']:
                                if 'success' in result:
                                    if result['success'] == False:
                                        if 'error' in result:
                                            print ("Error info: %s" % (result['error']))
                                    else:
                                        featSucces = featSucces + 1
    
                            print ("%s features added to %s" % (featSucces,fl.name))
                        else:
                            print ("0 features added to %s /n result info %s" % (fl.name,str(results)))             

    except (common.ArcRestHelperError) as e:
        print ("error in function: %s" % e[0]['function'])
        print ("error on line: %s" % e[0]['line'])
        print ("error in file name: %s" % e[0]['filename'])
        print ("with error message: %s" % e[0]['synerror'])
        if 'arcpyError' in e[0]:
            print ("with arcpy message: %s" % e[0]['arcpyError'])

    except:
        line, filename, synerror = trace()
        print ("error on line: %s" % line)
        print ("error in file name: %s" % filename)
        print ("with error message: %s" % synerror)
예제 #18
0
    def QueryAllFeatures(self,
                         url=None,
                         where="1=1",
                         out_fields="*",
                         timeFilter=None,
                         geometryFilter=None,
                         returnFeatureClass=False,
                         out_fc=None,
                         outSR=None,
                         chunksize=1000,
                         printIndent="",
                         useAGSFeatureService=False):
        """Performs an SQL query against a hosted feature service layer
        and returns all features regardless of service limit.

        Args:
            url (str): The URL of the feature service layer.
            where - the selection sql statement
            out_fields - the attribute fields to return
            timeFilter - a TimeFilter object where either the start time
                         or start and end time are defined to limit the
                         search results for a given time.  The values in
                         the timeFilter should be as UTC timestampes in
                         milliseconds.  No checking occurs to see if they
                         are in the right format.
            geometryFilter - a GeometryFilter object to parse down a given
                            query by another spatial dataset.
            returnFeatureClass - Default False. If true, query will be
                                 returned as feature class
            chunksize (int): The maximum amount of features to query at a time. Defaults to 1000.
            out_fc - only valid if returnFeatureClass is set to True.
                        Output location of query.
            useAGSFeatureService (boolean) - Whether to use an AGOL FeatureLayer 
                                                (default or AGS-hosted FeatureService.

            Output:
               A list of Feature Objects (default) or a path to the output featureclass if
               returnFeatureClass is set to True.

        """
        if (url is None):
            return
        fl = None
        try:
            if useAGSFeatureService:
                fl = FeatureService(url=url,
                                    securityHandler=self._securityHandler)
                qRes = fl.query(where=where,
                                returnIdsOnly=True,
                                timeFilter=timeFilter,
                                geometryFilter=geometryFilter)
            else:
                fl = FeatureLayer(url=url,
                                  securityHandler=self._securityHandler)
                qRes = fl.query(where=where,
                                returnIDsOnly=True,
                                timeFilter=timeFilter,
                                geometryFilter=geometryFilter)

            if 'error' in qRes:
                if isinstance(qRes, dict):
                    qRes = str(qRes)
                print(printIndent + qRes)
                return []
            elif 'objectIds' in qRes:
                oids = qRes['objectIds']
                total = len(oids)
                if total == 0:
                    return fl.query(where=where,
                                    returnGeometry=True,
                                    out_fields=out_fields,
                                    timeFilter=timeFilter,
                                    geometryFilter=geometryFilter,
                                    outSR=outSR)

                print(printIndent + "%s features to be downloaded" % total)
                chunksize = min(chunksize, fl.maxRecordCount)
                combinedResults = None
                totalQueried = 0
                for chunk in chunklist(l=oids, n=chunksize):
                    oidsQuery = ",".join(map(str, chunk))
                    if not oidsQuery:
                        continue
                    else:
                        results = fl.query(objectIds=oidsQuery,
                                           returnGeometry=True,
                                           out_fields=out_fields,
                                           timeFilter=timeFilter,
                                           geometryFilter=geometryFilter,
                                           outSR=outSR)
                        if isinstance(results, FeatureSet):
                            if combinedResults is None:
                                combinedResults = results
                            else:
                                for feature in results.features:
                                    combinedResults.features.append(feature)

                            totalQueried += len(results.features)
                            print(printIndent +
                                  "{:.0%} Completed: {}/{}".format(
                                      totalQueried /
                                      float(total), totalQueried, total))

                        else:
                            print(printIndent + results)
                if returnFeatureClass == True:
                    return combinedResults.save(*os.path.split(out_fc))
                else:
                    return combinedResults
            else:
                print(printIndent + qRes)
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "QueryAllFeatures",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None
            del fl
            gc.collect()
예제 #19
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)
예제 #20
0
    securityinfo = {}
    securityinfo[
        'security_type'] = 'Portal'  #LDAP, NTLM, OAuth, Portal, PKI, ArcGIS
    securityinfo['username'] = raw_input("Enter User Name: ")  #User Name
    securityinfo['password'] = raw_input("Enter Password: "******"http://www.arcgis.com"

    sec_handle = securityhandlerhelper.securityhandlerhelper(
        securityinfo=securityinfo)
    if sec_handle.valid == False:
        print sec_handle.message
    else:
        #create a feature layer of the AGOL service
        feature_layer = FeatureLayer(
            url=url,
            securityHandler=sec_handle.securityhandler,
            proxy_port=None,
            proxy_url=None,
            initialize=True)

        out_fields = ['objectid']
        #append the field info to each field
        for field in fieldInfo:
            out_fields.append(field['FieldName'])

        #query the feature layer
        query_feats = feature_layer.query(where=sql,
                                          out_fields=",".join(out_fields))

        #loop over each feature and field to update
        for feat in query_feats:
            for field in fieldInfo:
예제 #21
0
    def RemoveAndAddFeatures(self,
                             url,
                             pathToFeatureClass,
                             id_field,
                             chunksize=1000):
        """Deletes all features in a feature service and uploads features from a feature class on disk.

        Args:
            url (str): The URL of the feature service.
            pathToFeatureClass (str): The path of the feature class on disk.
            id_field (str): The name of the field in the feature class to use for chunking.
            chunksize (int): The maximum amount of features to upload at a time. Defaults to 1000.
        Raises:
            ArcRestHelperError: if ``arcpy`` can't be found.

        """
        fl = None

        try:
            if arcpyFound == False:
                raise common.ArcRestHelperError({
                    "function":
                    "RemoveAndAddFeatures",
                    "line":
                    inspect.currentframe().f_back.f_lineno,
                    "filename":
                    'featureservicetools',
                    "synerror":
                    "ArcPy required for this function"
                })
            arcpy.env.overwriteOutput = True
            tempaddlayer = 'ewtdwedfew'
            if not arcpy.Exists(pathToFeatureClass):
                raise common.ArcRestHelperError({
                    "function":
                    "RemoveAndAddFeatures",
                    "line":
                    inspect.currentframe().f_back.f_lineno,
                    "filename":
                    'featureservicetools',
                    "synerror":
                    "%s does not exist" % pathToFeatureClass
                })

            fields = arcpy.ListFields(pathToFeatureClass, wild_card=id_field)
            if len(fields) == 0:
                raise common.ArcRestHelperError({
                    "function":
                    "RemoveAndAddFeatures",
                    "line":
                    inspect.currentframe().f_back.f_lineno,
                    "filename":
                    'featureservicetools',
                    "synerror":
                    "%s field does not exist" % id_field
                })
            strFld = True
            if fields[0].type != 'String':
                strFld = False

            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)

            id_field_local = arcpy.AddFieldDelimiters(pathToFeatureClass,
                                                      id_field)
            idlist = []
            print(
                arcpy.GetCount_management(
                    in_rows=pathToFeatureClass).getOutput(0) +
                " features in the layer")
            with arcpy.da.SearchCursor(pathToFeatureClass,
                                       (id_field)) as cursor:
                allidlist = []

                for row in cursor:
                    if (strFld):
                        idlist.append("'" + row[0] + "'")
                    else:
                        idlist.append(row[0])
                    if len(idlist) >= chunksize:
                        allidlist.append(idlist)
                        idlist = []

                if len(idlist) > 0:
                    allidlist.append(idlist)
                for idlist in allidlist:
                    idstring = ' in (' + ','.join(map(str, idlist)) + ')'
                    sql = id_field + idstring
                    sqlLocalFC = id_field_local + idstring
                    results = fl.deleteFeatures(where=sql,
                                                rollbackOnFailure=True)

                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function":
                            "RemoveAndAddFeatures",
                            "line":
                            inspect.currentframe().f_back.f_lineno,
                            "filename":
                            'featureservicetools',
                            "synerror":
                            results['error']
                        })
                    elif 'deleteResults' in results:
                        print("%s features deleted" %
                              len(results['deleteResults']))
                        for itm in results['deleteResults']:
                            if itm['success'] != True:
                                print(itm)
                    else:
                        print(results)

                    arcpy.MakeFeatureLayer_management(pathToFeatureClass,
                                                      tempaddlayer, sqlLocalFC)
                    results = fl.addFeatures(fc=tempaddlayer)

                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function":
                            "RemoveAndAddFeatures",
                            "line":
                            inspect.currentframe().f_back.f_lineno,
                            "filename":
                            'featureservicetools',
                            "synerror":
                            results['error']
                        })
                    elif 'addResults' in results:
                        print("%s features added" % len(results['addResults']))
                        for itm in results['addResults']:
                            if itm['success'] != True:
                                print(itm)
                    else:
                        print(results)
                    idlist = []
            if 'error' in results:
                raise common.ArcRestHelperError({
                    "function":
                    "RemoveAndAddFeatures",
                    "line":
                    inspect.currentframe().f_back.f_lineno,
                    "filename":
                    'featureservicetools',
                    "synerror":
                    results['error']
                })
            else:
                print(results)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "create_report_layers_using_config",
                "line": line,
                "filename": filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            })
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            gc.collect()
예제 #22
0
    securityinfo['proxy_port'] = proxy_port
    securityinfo['referer_url'] = None
    securityinfo['token_url'] = None
    securityinfo['certificatefile'] = None
    securityinfo['keyfile'] = None
    securityinfo['client_id'] = None
    securityinfo['secret_id'] = None

    shh = securityhandlerhelper.securityhandlerhelper(
        securityinfo=securityinfo)
    if shh.valid == False:
        print shh.message
    else:
        fl = FeatureLayer(url=url,
                          securityHandler=shh.securityhandler,
                          proxy_port=proxy_port,
                          proxy_url=proxy_url,
                          initialize=True)

        features = []
        json_string = {
            'geometry': {
                'y': 1885855.2531960313,
                'x': 1034495.0035156211
            },
            'attributes': {
                'NAME': 'NameChange',
                'OBJECTID': 1
            }
        }
예제 #23
0
    def DeleteFeaturesFromFeatureLayer(self, url, sql, chunksize=0):
        """Removes features from a hosted feature service layer by SQL query.

        Args:
            url (str): The URL of the feature service layer.
            sql (str): The SQL query to apply against the feature service.
                Those features that satisfy the query will be deleted.
            chunksize (int): The maximum amount of features to remove at a time. Defaults to 0.
        Returns:
            The result from :py:func:`arcrest.agol.services.FeatureLayer.deleteFeatures`.
        Notes:
            If you want to delete all features, it is suggested to use the SQL query ``"1=1"``.

        """
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            totalDeleted = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print (qRes)
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return  {'success':True,'message': "No features matched the query"}
                    i = 0
                    print ("%s features to be deleted" % total)
                    while(i <= len(oids)):
                        oidsDelete = ','.join(str(e) for e in oids[i:i+chunksize])
                        if oidsDelete == '':
                            continue
                        else:
                            results = fl.deleteFeatures(objectIds=oidsDelete)
                        if 'deleteResults' in results:
                            totalDeleted += len(results['deleteResults'])
                            print ("%s%% Completed: %s/%s " % (int(totalDeleted / float(total) *100), totalDeleted, total))
                            i += chunksize
                        else:
                            print (results)
                            return {'success':True,'message': "%s deleted" % totalDeleted}
                    qRes = fl.query(where=sql, returnIDsOnly=True)
                    if 'objectIds' in qRes:
                        oids = qRes['objectIds']
                        if len(oids)> 0 :
                            print ("%s features to be deleted" % len(oids))
                            results = fl.deleteFeatures(where=sql)
                            if 'deleteResults' in results:
                                totalDeleted += len(results['deleteResults'])
                                return  {'success':True,'message': "%s deleted" % totalDeleted}
                            else:
                                return results
                    return  {'success':True,'message': "%s deleted" % totalDeleted}
                else:
                    print (qRes)
            else:
                results = fl.deleteFeatures(where=sql)
                if results is not None:
                    if 'deleteResults' in results:
                        return  {'success':True,'message': totalDeleted + len(results['deleteResults'])}
                    else:
                        return results
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None
            del fl
            gc.collect()
예제 #24
0
    securityinfo['proxy_url'] = proxy_url
    securityinfo['proxy_port'] = proxy_port
    securityinfo['referer_url'] = None
    securityinfo['token_url'] = None
    securityinfo['certificatefile'] = None
    securityinfo['keyfile'] = None
    securityinfo['client_id'] = None
    securityinfo['secret_id'] = None

    shh = securityhandlerhelper.securityhandlerhelper(
        securityinfo=securityinfo)
    if shh.valid == False:
        print shh.message
    else:
        fl = FeatureLayer(url=url,
                          securityHandler=shh.securityhandler,
                          proxy_port=proxy_port,
                          proxy_url=proxy_url,
                          initialize=True)

        out_fields = ['objectid']
        for fld in fieldInfo:
            out_fields.append(fld['FieldName'])

        resFeats = fl.query(where=sql, out_fields=",".join(out_fields))
        for feat in resFeats:

            for fld in fieldInfo:
                feat.set_value(fld["FieldName"], fld['ValueToSet'])

        print fl.updateFeature(features=resFeats)
예제 #25
0
    def AddFeaturesToFeatureLayer(self,
                                  url,
                                  pathToFeatureClass,
                                  chunksize=0,
                                  lowerCaseFieldNames=False):
        if arcpyFound == False:
            raise common.ArcRestHelperError({
                "function":
                "AddFeaturesToFeatureLayer",
                "line":
                inspect.currentframe().f_back.f_lineno,
                "filename":
                'featureservicetools',
                "synerror":
                "ArcPy required for this function"
            })
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)

            if chunksize > 0:
                messages = {'addResults': []}
                total = arcpy.GetCount_management(
                    pathToFeatureClass).getOutput(0)
                if total == '0':
                    print("0 features in %s" % pathToFeatureClass)
                    return "0 features in %s" % pathToFeatureClass
                arcpy.env.overwriteOutput = True
                if int(total) < int(chunksize):
                    return fl.addFeatures(
                        fc=pathToFeatureClass,
                        lowerCaseFieldNames=lowerCaseFieldNames)
                else:
                    inDesc = arcpy.Describe(pathToFeatureClass)
                    oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,
                                                       inDesc.oidFieldName)
                    fc = os.path.basename(pathToFeatureClass)
                    sql = "{0} IN ((SELECT MIN({0}) FROM {1}), (SELECT MAX({0}) FROM {1}))".format(
                        oidName, fc)
                    minOID, maxOID = list(
                        zip(*arcpy.da.SearchCursor(pathToFeatureClass, "OID@",
                                                   sql)))[0]
                    breaks = list(range(minOID, maxOID))[0:-1:chunksize]
                    breaks.append(maxOID + 1)
                    exprList = [
                        "{0} >= {1} AND {0} < {2}".format(
                            oidName, breaks[b], breaks[b + 1])
                        for b in range(len(breaks) - 1)
                    ]

                    for expr in exprList:
                        UploadLayer = arcpy.MakeFeatureLayer_management(
                            pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                        result = fl.addFeatures(
                            fc=UploadLayer,
                            lowerCaseFieldNames=lowerCaseFieldNames)
                        if messages is None:
                            messages = result
                        else:
                            if result is not None and 'addResults' in result:
                                if 'addResults' in messages:
                                    messages['addResults'] = messages[
                                        'addResults'] + result['addResults']
                                    print("%s/%s features added" %
                                          (len(messages['addResults']), total))
                                else:
                                    messages['addResults'] = result[
                                        'addResults']
                                    print("%s/%s features added" %
                                          (len(messages['addResults']), total))
                            else:
                                messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass,
                                      lowerCaseFieldNames=lowerCaseFieldNames)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            })
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None

            del fl

            gc.collect()
예제 #26
0
    def QueryAllFeatures(self, url, sql, out_fields="*", chunksize=1000, savePath=None):
        """Performs an SQL query against a hosted feature service layer.

        Args:
            url (str): The URL of the feature service layer.
            sql (str): The SQL query to apply against the feature service.
                Those features that satisfy the query will be returned.
            out_fields (str): A comma delimited list of field names to return.
                Defaults to ``"*"``, i.e., return all fields
            chunksize (int): The maximum amount of features to query at a time. Defaults to 1000.
            savePath (str): The full path on disk where the features will be saved. Defaults to ``None``.
        Returns:
            When ``savePath`` is not provided (``None``), the result from
                :py:func:`arcrest.agol.services.FeatureLayer.query`.

            When ``savePath`` is provided, the result from
                :py:func:`arcrest.common.general.FeatureSet.save`.

        """
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            qRes = fl.query(where=sql, returnIDsOnly=True)

            if 'error' in qRes:
                print (qRes)
                return qRes
            elif 'objectIds' in qRes:
                oids = qRes['objectIds']
                total = len(oids)
                if total == 0:
                    return  {'success':True, 'message':"No features matched the query"}

                print ("%s features to be downloaded" % total)
                chunksize = min(chunksize, fl.maxRecordCount)
                combinedResults = None
                totalQueried = 0
                for chunk in chunklist(l=oids, n=chunksize):
                    oidsQuery = ",".join(map(str, chunk))
                    if not oidsQuery:
                        continue
                    else:
                        results = fl.query(objectIds=oidsQuery,
                                           returnGeometry=True,
                                           out_fields=out_fields)
                        if isinstance(results,FeatureSet):
                            if combinedResults is None:
                                combinedResults = results
                            else:
                                for feature in results.features:
                                    combinedResults.features.append(feature)

                            totalQueried += len(results.features)
                            print("{:.0%} Completed: {}/{}".format(totalQueried / float(total), totalQueried, total))

                        else:
                            print (results)
                if savePath is None or savePath == '':
                    return combinedResults
                else:
                    return combinedResults.save(*os.path.split(savePath))
            else:
                print (qRes)
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "QueryFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None
            del fl
            gc.collect()
예제 #27
0
    def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass,chunksize=0):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
                        
            if chunksize > 0:
                messages = {'addResults':[]}
                total = arcpy.GetCount_management(pathToFeatureClass).getOutput(0)
                arcpy.env.overwriteOutput = True
                inDesc = arcpy.Describe(pathToFeatureClass)
                oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,inDesc.oidFieldName)
                sql = '%s = (select min(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                minOID = cur.next()[0]
                del cur, sql
                sql = '%s = (select max(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                maxOID = cur.next()[0]
                del cur, sql
                breaks = range(minOID,maxOID)[0:-1:chunksize] 
                breaks.append(maxOID+1)
                exprList = [oidName + ' >= ' + str(breaks[b]) + ' and ' + \
                            oidName + ' < ' + str(breaks[b+1]) for b in range(len(breaks)-1)]
                for expr in exprList:
                    UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                    result = fl.addFeatures(fc=UploadLayer)
                    if messages is None:
                        messages = result
                    else:
                        if 'addResults' in result:
                            if 'addResults' in messages:
                                messages['addResults'] = messages['addResults'] + result['addResults']
                                print "%s/%s features added" % (len(messages['addResults']),total)
                            else:
                                messages['addResults'] = result['addResults']
                                print "%s/%s features added" % (len(messages['addResults']),total)
                        else:
                            messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "create_report_layers_using_config",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )  
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #28
0
output = "C:\\TxDOT\\Scripts\\javascript\\Guardrail\\Snake\\BACKUP"

if __name__ == "__main__":
    username = "******"
    password = mohawk.hangnail(username)
    url = "http://services.arcgis.com/KTcxiTD9dsQw4r7Z/arcgis/rest/services/GuardrailPoints/FeatureServer/0"
    proxy_port = None
    proxy_url = None

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

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

    qmin = 0
    qmax = 2000
    count = fl.query(returnIDsOnly=True)
    cList = count["objectIds"]
    cValue = cList[-1]
    listLen = len(cList)
    print listLen

    now = datetime.datetime.now()
    curMonth = now.strftime("%m")
    curDay = now.strftime("%d")
    curYear = now.strftime("%Y")
예제 #29
0
    def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass,chunksize=0):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
                        
            if chunksize > 0:
                messages = {'addResults':[]}
                total = arcpy.GetCount_management(pathToFeatureClass).getOutput(0)
                arcpy.env.overwriteOutput = True
                inDesc = arcpy.Describe(pathToFeatureClass)
                oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,inDesc.oidFieldName)
                sql = '%s = (select min(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                minOID = cur.next()[0]
                del cur, sql
                sql = '%s = (select max(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                maxOID = cur.next()[0]
                del cur, sql
                breaks = range(minOID,maxOID)[0:-1:chunksize] 
                breaks.append(maxOID+1)
                exprList = [oidName + ' >= ' + str(breaks[b]) + ' and ' + \
                            oidName + ' < ' + str(breaks[b+1]) for b in range(len(breaks)-1)]
                for expr in exprList:
                    UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                    result = fl.addFeatures(fc=UploadLayer)
                    if messages is None:
                        messages = result
                    else:
                        if 'addResults' in result:
                            if 'addResults' in messages:
                                messages['addResults'] = messages['addResults'] + result['addResults']
                                print "%s/%s features added" % (len(messages['addResults']),total)
                            else:
                                messages['addResults'] = result['addResults']
                                print "%s/%s features added" % (len(messages['addResults']),total)
                        else:
                            messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "create_report_layers_using_config",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )  
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #30
0
    def RemoveAndAddFeatures(self, url, pathToFeatureClass,id_field,chunksize=1000):
        fl = None

        try:    
            arcpy.env.overwriteOutput = True
            tempaddlayer= 'ewtdwedfew'
            if not arcpy.Exists(pathToFeatureClass):
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror": "%s does not exist" % pathToFeatureClass
                     }
                    )  
            
            fields = arcpy.ListFields(pathToFeatureClass,wild_card=id_field)
            if len(fields) == 0:
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror": "%s field does not exist" % id_field
                })                  
            strFld = True
            if fields[0].type != 'String':
                strFld = False            

            fl = FeatureLayer(
                    url=url,
                    securityHandler=self._securityHandler)        
            
            id_field_local = arcpy.AddFieldDelimiters(pathToFeatureClass, id_field)
            idlist = []
            print arcpy.GetCount_management(in_rows=pathToFeatureClass).getOutput(0) + " features in the layer"
            with arcpy.da.SearchCursor(pathToFeatureClass, (id_field)) as cursor:
                allidlist = []
                
                for row in cursor:
                    
                    if (strFld):
                        idlist.append("'" + row[0] +"'")
                    else:
                        idlist.append(row[0])
                    if len(idlist) >= chunksize:
                        allidlist.append(idlist)
                        idlist = []     
                
                if len(idlist) > 0:
                    allidlist.append(idlist)
                for idlist in allidlist:
                    idstring = ' in (' + ','.join(idlist) + ')'
                    sql = id_field + idstring
                    sqlLocalFC = id_field_local + idstring
                    results = fl.deleteFeatures(where=sql, 
                                                rollbackOnFailure=True)
                
                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function": "RemoveAndAddFeatures",
                            "line": inspect.currentframe().f_back.f_lineno,
                            "filename":  'featureservicetools',
                            "synerror":results['error']
                        })                               
                    elif 'deleteResults' in results:
                        print "%s features deleted" % len(results['deleteResults'])
                        for itm in results['deleteResults']:
                            if itm['success'] != True:
                                print itm                            
                    else:
                        print results                                                        
                
                    arcpy.MakeFeatureLayer_management(pathToFeatureClass,tempaddlayer,sqlLocalFC)
                    results = fl.addFeatures(fc=tempaddlayer)
                
                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function": "RemoveAndAddFeatures",
                            "line": inspect.currentframe().f_back.f_lineno,
                            "filename":  'featureservicetools',
                            "synerror":results['error']
                        })                               
                    elif 'addResults' in results:
                        print "%s features added" % len(results['addResults'])
                        for itm in results['addResults']:
                            if itm['success'] != True:
                                print itm
                    else:
                        print results                               
                    idlist = []                 
            if 'error' in results:
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror":results['error']
                })                               
            else:
                print results            
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "create_report_layers_using_config",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )  
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            
            gc.collect()    
예제 #31
0
    def AddFeaturesToFeatureLayer(self, url, pathToFeatureClass, chunksize=0, lowerCaseFieldNames=False):
        """Appends local features to a hosted feature service layer.

        Args:
            url (str): The URL of the feature service layer.
            pathToFeatureClass (str): The path of the feature class on disk.
            chunksize (int): The maximum amount of features to upload at a time. Defaults to 0.
            lowerCaseFieldNames (bool): A boolean value indicating if field names should be converted
                to lowercase before uploading. Defaults to ``False``.
        Returns:
            The result from :py:func:`arcrest.agol.services.FeatureLayer.addFeatures`.
        Raises:
            ArcRestHelperError: if ``arcpy`` can't be found.
        Notes:
            If publishing to a PostgreSQL database, it is suggested to to set ``lowerCaseFieldNames`` to ``True``.

        """
        if arcpyFound == False:
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": inspect.currentframe().f_back.f_lineno,
                "filename":  'featureservicetools',
                "synerror": "ArcPy required for this function"
            })
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)

            if chunksize > 0:
                fc = os.path.basename(pathToFeatureClass)
                inDesc = arcpy.Describe(pathToFeatureClass)
                oidName = inDesc.oidFieldName

                arr = arcpy.da.FeatureClassToNumPyArray(pathToFeatureClass, (oidName))
                syncSoFar = 0
                messages = {'addResults':[],'errors':[]}
                total = len(arr)
                errorCount = 0
                if total == '0':
                    print ("0 features in %s" % pathToFeatureClass)
                    return "0 features in %s" % pathToFeatureClass
                print ("%s features in layer" % (total))

                arcpy.env.overwriteOutput = True
                if int(total) < int(chunksize):
                    return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
                else:
                    newArr = chunklist(arr,chunksize)
                    exprList = ["{0} >= {1} AND {0} <= {2}".format(oidName, nArr[0][0], nArr[len(nArr)-1][0])
                        for nArr in newArr]
                    for expr in exprList:

                        UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                        #print(arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0) + " features in the chunk")
                        results = fl.addFeatures(fc=UploadLayer,lowerCaseFieldNames=lowerCaseFieldNames)
                        chunkCount = arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0)
                        print(chunkCount + " features in the chunk")
                        if chunkCount > 0:

                            if results is not None and 'addResults' in results and results['addResults'] is not None:
                                featSucces = 0
                                for result in results['addResults']:
                                    if 'success' in result:
                                        if result['success'] == False:
                                            if 'error' in result:
                                                errorCount  = errorCount + 1
                                                print ("\tError info: %s" % (result))
                                        else:
                                            featSucces = featSucces + 1
                                syncSoFar = syncSoFar + featSucces
                                print ("%s features added in this chunk" % (featSucces))
                                print ("%s/%s features added, %s errors" % (syncSoFar,total,errorCount ))
                                if 'addResults' in messages:
                                    messages['addResults'] = messages['addResults'] + results['addResults']
                                else:
                                    messages['addResults'] = results['addResults']
                            else:
                                messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None
            del fl
            gc.collect()
예제 #32
0
    def DeleteFeaturesFromFeatureLayer(self,url,sql,chunksize=0):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            totalDeleted = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print qRes
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return  {'success':'true','message': "No features matched the query"}
                        
                    minId = min(oids)
                    maxId = max(oids)
                   
                    i = 0
                    print "%s features to be deleted" % total
                    while(i <= len(oids)):
                        oidsDelete = ','.join(str(e) for e in oids[i:i+chunksize])
                        if oidsDelete == '':
                            continue
                        else:
                            results = fl.deleteFeatures(objectIds=oidsDelete)
                        if 'deleteResults' in results:
                            totalDeleted += len(results['deleteResults'])
                            print "%s%% Completed: %s/%s " % (int(totalDeleted / float(total) *100), totalDeleted, total)
                            i += chunksize                            
                        else:
                            print results
                            return {'success':'true','message': "%s deleted" % totalDeleted}
                    qRes = fl.query(where=sql, returnIDsOnly=True)
                    if 'objectIds' in qRes:
                        oids = qRes['objectIds']
                        if len(oids)> 0 :
                            print "%s features to be deleted" % len(oids)
                            results = fl.deleteFeatures(where=sql)
                            if 'deleteResults' in results:
                                totalDeleted += len(results['deleteResults'])
                                return  {'success':'true','message': "%s deleted" % totalDeleted}
                            else:
                                return results
                    return  {'success':'true','message': "%s deleted" % totalDeleted}
                    
                else:
                    print qRes
            else:
                results = fl.deleteFeatures(where=sql)
                if 'deleteResults' in results:         
                    return  {'success':'true','message': totalDeleted + len(results['deleteResults'])}
                else:
                    return results
       
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #33
0
    def QueryAllFeatures(self, url=None,
                         where="1=1",
                        out_fields="*",
                        timeFilter=None,
                        geometryFilter=None,
                        returnFeatureClass=False,
                        out_fc=None,
                        outSR=None,
                        chunksize=1000,
                        printIndent=""):

        """Performs an SQL query against a hosted feature service layer
        and returns all features regardless of service limit.

        Args:
            url (str): The URL of the feature service layer.
            where - the selection sql statement
            out_fields - the attribute fields to return
            timeFilter - a TimeFilter object where either the start time
                         or start and end time are defined to limit the
                         search results for a given time.  The values in
                         the timeFilter should be as UTC timestampes in
                         milliseconds.  No checking occurs to see if they
                         are in the right format.
            geometryFilter - a GeometryFilter object to parse down a given
                            query by another spatial dataset.
            returnFeatureClass - Default False. If true, query will be
                                 returned as feature class
            chunksize (int): The maximum amount of features to query at a time. Defaults to 1000.
            out_fc - only valid if returnFeatureClass is set to True.
                        Output location of query.

            Output:
               A list of Feature Objects (default) or a path to the output featureclass if
               returnFeatureClass is set to True.

        """
        if (url is None):
            return
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            qRes = fl.query(where=where,
                            returnIDsOnly=True,
                            timeFilter=timeFilter,
                            geometryFilter=geometryFilter)

            if 'error' in qRes:
                print (printIndent + qRes)
                return []
            elif 'objectIds' in qRes:
                oids = qRes['objectIds']
                total = len(oids)
                if total == 0:
                    return fl.query(where=where,
                                    returnGeometry=True,
                                    out_fields=out_fields,
                                    timeFilter=timeFilter,
                                    geometryFilter=geometryFilter,
                                    outSR=outSR)

                print (printIndent + "%s features to be downloaded" % total)
                chunksize = min(chunksize, fl.maxRecordCount)
                combinedResults = None
                totalQueried = 0
                for chunk in chunklist(l=oids, n=chunksize):
                    oidsQuery = ",".join(map(str, chunk))
                    if not oidsQuery:
                        continue
                    else:
                        results = fl.query(objectIds=oidsQuery,
                                           returnGeometry=True,
                                           out_fields=out_fields,
                                           timeFilter=timeFilter,
                                            geometryFilter=geometryFilter,
                                            outSR=outSR)
                        if isinstance(results,FeatureSet):
                            if combinedResults is None:
                                combinedResults = results
                            else:
                                for feature in results.features:
                                    combinedResults.features.append(feature)

                            totalQueried += len(results.features)
                            print(printIndent + "{:.0%} Completed: {}/{}".format(totalQueried / float(total), totalQueried, total))

                        else:
                            print (printIndent + results)
                if returnFeatureClass == True:
                    return combinedResults.save(*os.path.split(out_fc))
                else:
                    return combinedResults
            else:
                print (printIndent + qRes)
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "QueryAllFeatures",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None
            del fl
            gc.collect()
예제 #34
0
    def QueryAllFeatures(self,url,sql,chunksize=0,saveLocation="",outName=""):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            totalQueried = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print qRes
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return  {'success':'true','message': "No features matched the query"}
                        
                    minId = min(oids)
                    maxId = max(oids)
                   
                    i = 0
                    print "%s features to be downloaded" % total
                    combinedResults = None
                                                            
                    while(i <= len(oids)):
                        oidsQuery = ','.join(str(e) for e in oids[i:i+chunksize])
                        if oidsQuery == '':
                            continue
                        else:
                            results = fl.query(objectIds=oidsQuery,
                                               returnGeometry=True,
                                               out_fields='*')
                            if isinstance(results,FeatureSet):
                                if combinedResults is None:
                                    combinedResults = results
                                else:
                                
                                    for feature in results.features:
                                    
                                        combinedResults.features.append(feature)
                             
                                totalQueried += len(results.features)
                                
                                print "%s%% Completed: %s/%s " % (int(totalQueried / float(total) *100), totalQueried, total)
                                i += chunksize                            
                            else:
                                print results
                  
                    print combinedResults.save(saveLocation=saveLocation, outName=outName)
                else:
                    print qRes
            else:
                return  fl.query(where=sql, 
                                 returnFeatureClass=True,
                                 returnGeometry=True,
                                 out_fields='*',                               
                                 out_fc=os.path.join(saveLocation,outName)
                                 )
            
       
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "QueryFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()            
예제 #35
0
    def AddFeaturesToFeatureLayer(self,
                                  url,
                                  pathToFeatureClass,
                                  chunksize=0,
                                  lowerCaseFieldNames=False):
        """Appends local features to a hosted feature service layer.

        Args:
            url (str): The URL of the feature service layer.
            pathToFeatureClass (str): The path of the feature class on disk.
            chunksize (int): The maximum amount of features to upload at a time. Defaults to 0.
            lowerCaseFieldNames (bool): A boolean value indicating if field names should be converted
                to lowercase before uploading. Defaults to ``False``.
        Returns:
            The result from :py:func:`arcrest.agol.services.FeatureLayer.addFeatures`.
        Raises:
            ArcRestHelperError: if ``arcpy`` can't be found.
        Notes:
            If publishing to a PostgreSQL database, it is suggested to to set ``lowerCaseFieldNames`` to ``True``.

        """
        if arcpyFound == False:
            raise common.ArcRestHelperError({
                "function":
                "AddFeaturesToFeatureLayer",
                "line":
                inspect.currentframe().f_back.f_lineno,
                "filename":
                'featureservicetools',
                "synerror":
                "ArcPy required for this function"
            })
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)

            if chunksize > 0:
                fc = os.path.basename(pathToFeatureClass)
                inDesc = arcpy.Describe(pathToFeatureClass)
                oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,
                                                   inDesc.oidFieldName)

                arr = arcpy.da.FeatureClassToNumPyArray(
                    pathToFeatureClass, (oidName))
                syncSoFar = 0
                messages = {'addResults': [], 'errors': []}
                total = len(arr)
                errorCount = 0
                if total == '0':
                    print("0 features in %s" % pathToFeatureClass)
                    return "0 features in %s" % pathToFeatureClass
                print("%s features in layer" % (total))

                arcpy.env.overwriteOutput = True
                if int(total) < int(chunksize):
                    return fl.addFeatures(
                        fc=pathToFeatureClass,
                        lowerCaseFieldNames=lowerCaseFieldNames)
                else:
                    newArr = chunklist(arr, chunksize)
                    exprList = [
                        "{0} >= {1} AND {0} <= {2}".format(
                            oidName, nArr[0][0], nArr[len(nArr) - 1][0])
                        for nArr in newArr
                    ]
                    for expr in exprList:

                        UploadLayer = arcpy.MakeFeatureLayer_management(
                            pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                        #print(arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0) + " features in the chunk")
                        results = fl.addFeatures(
                            fc=UploadLayer,
                            lowerCaseFieldNames=lowerCaseFieldNames)
                        chunkCount = arcpy.GetCount_management(
                            in_rows=UploadLayer).getOutput(0)
                        print(chunkCount + " features in the chunk")
                        if chunkCount > 0:

                            if results is not None and 'addResults' in results and results[
                                    'addResults'] is not None:
                                featSucces = 0
                                for result in results['addResults']:
                                    if 'success' in result:
                                        if result['success'] == False:
                                            if 'error' in result:
                                                errorCount = errorCount + 1
                                                print("\tError info: %s" %
                                                      (result))
                                        else:
                                            featSucces = featSucces + 1
                                syncSoFar = syncSoFar + featSucces
                                print("%s features added in this chunk" %
                                      (featSucces))
                                print("%s/%s features added, %s errors" %
                                      (syncSoFar, total, errorCount))
                                if 'addResults' in messages:
                                    messages['addResults'] = messages[
                                        'addResults'] + results['addResults']
                                else:
                                    messages['addResults'] = results[
                                        'addResults']
                            else:
                                messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass,
                                      lowerCaseFieldNames=lowerCaseFieldNames)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            })
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None
            del fl
            gc.collect()
예제 #36
0
        "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",
                "alias": "Bike Route",
                "sqlType": "sqlTypeOther",
                "length": 10,
                "nullable": True,
                "editable": True,
                "domain": None,
                "defaultValue": None
예제 #37
0
    def DeleteFeaturesFromFeatureLayer(self, url, sql, chunksize=0):
        """Removes features from a hosted feature service layer by SQL query.

        Args:
            url (str): The URL of the feature service layer.
            sql (str): The SQL query to apply against the feature service.
                Those features that satisfy the query will be deleted.
            chunksize (int): The maximum amount of features to remove at a time. Defaults to 0.
        Returns:
            The result from :py:func:`arcrest.agol.services.FeatureLayer.deleteFeatures`.
        Notes:
            If you want to delete all features, it is suggested to use the SQL query ``"1=1"``.

        """
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            totalDeleted = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print(qRes)
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return {
                            'success': True,
                            'message': "No features matched the query"
                        }
                    i = 0
                    print("%s features to be deleted" % total)
                    while (i <= len(oids)):
                        oidsDelete = ','.join(
                            str(e) for e in oids[i:i + chunksize])
                        if oidsDelete == '':
                            continue
                        else:
                            results = fl.deleteFeatures(objectIds=oidsDelete)
                        if 'deleteResults' in results:
                            totalDeleted += len(results['deleteResults'])
                            print("%s%% Completed: %s/%s " %
                                  (int(totalDeleted / float(total) * 100),
                                   totalDeleted, total))
                            i += chunksize
                        else:
                            print(results)
                            return {
                                'success': True,
                                'message': "%s deleted" % totalDeleted
                            }
                    qRes = fl.query(where=sql, returnIDsOnly=True)
                    if 'objectIds' in qRes:
                        oids = qRes['objectIds']
                        if len(oids) > 0:
                            print("%s features to be deleted" % len(oids))
                            results = fl.deleteFeatures(where=sql)
                            if 'deleteResults' in results:
                                totalDeleted += len(results['deleteResults'])
                                return {
                                    'success': True,
                                    'message': "%s deleted" % totalDeleted
                                }
                            else:
                                return results
                    return {
                        'success': True,
                        'message': "%s deleted" % totalDeleted
                    }
                else:
                    print(qRes)
            else:
                results = fl.deleteFeatures(where=sql)
                if results is not None:
                    if 'deleteResults' in results:
                        return {
                            'success': True,
                            'message':
                            totalDeleted + len(results['deleteResults'])
                        }
                    else:
                        return results
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "DeleteFeaturesFromFeatureLayer",
                "line": line,
                "filename": filename,
                "synerror": synerror,
            })
        finally:
            fl = None
            del fl
            gc.collect()
예제 #38
0
def main():
    try:
        proxy_port = None
        proxy_url = None

        securityinfo = {}
        securityinfo[
            'security_type'] = 'Portal'  #LDAP, NTLM, OAuth, Portal, PKI
        securityinfo['username'] = ""  #<UserName>
        securityinfo['password'] = ""  #<Password>
        securityinfo['org_url'] = "http://www.arcgis.com"
        securityinfo['proxy_url'] = proxy_url
        securityinfo['proxy_port'] = proxy_port
        securityinfo['referer_url'] = None
        securityinfo['token_url'] = None
        securityinfo['certificatefile'] = None
        securityinfo['keyfile'] = None
        securityinfo['client_id'] = None
        securityinfo['secret_id'] = None

        workforceProjectID = ''  #Workforce project number

        assignmentAreasID = ''  #ID of service to get centroids from
        assignmentAreaLayerName = ''  #layer in servuce
        assignmentAreaNameField = ''  #field with name of id area

        csvPath = r".\dataToLookup.csv"  #<Path with data>
        workerCol = 'worker'
        areaCol = 'area'
        descriptionCol = "description"
        notesCol = "notes"
        supervisorCol = "supervisor"

        assignmentType = 2
        status = 1

        workerNameToID = {}
        dispatcherNameToID = {}
        areaNameToID = {}
        fst = featureservicetools.featureservicetools(securityinfo)
        if fst.valid == False:
            print(fst.message)
        else:
            portalAdmin = arcrest.manageorg.Administration(
                securityHandler=fst.securityhandler)
            #Get the assignment areas
            fs = fst.GetFeatureService(itemId=assignmentAreasID,
                                       returnURLOnly=False)
            if not fs is None:
                fs_url = fst.GetLayerFromFeatureService(
                    fs=fs,
                    layerName=assignmentAreaLayerName,
                    returnURLOnly=True)
                if not fs_url is None:

                    fl = FeatureLayer(url=fs_url,
                                      securityHandler=fst.securityhandler,
                                      proxy_port=proxy_port,
                                      proxy_url=proxy_url,
                                      initialize=True)
                    areaResults = fl.query(
                        **{
                            'where': "1=1",
                            'outSR': '102100',
                            'out_fields': assignmentAreaNameField,
                            'returnGeometry': False,
                            'returnCentroid': True
                        })

                    for area in areaResults:
                        arDict = area.asDictionary
                        areaNameToID[arDict['attributes'][
                            assignmentAreaNameField]] = arDict['centroid']

            #Get the workers
            item = portalAdmin.content.getItem(itemId=workforceProjectID)
            itemData = item.itemData()
            if 'workers' in itemData:
                fl = FeatureLayer(url=itemData['workers']['url'],
                                  securityHandler=fst.securityhandler,
                                  proxy_port=proxy_port,
                                  proxy_url=proxy_url,
                                  initialize=True)

                workersResults = fl.query(where="1=1",
                                          out_fields='OBJECTID, NAME',
                                          returnGeometry=False)
                for worker in workersResults:
                    workerNameToID[worker.get_value(
                        'name')] = worker.get_value('OBJECTID')

            if 'dispatchers' in itemData:
                fl = FeatureLayer(url=itemData['dispatchers']['url'],
                                  securityHandler=fst.securityhandler,
                                  proxy_port=proxy_port,
                                  proxy_url=proxy_url,
                                  initialize=True)

                dispatcherResults = fl.query(where="1=1",
                                             out_fields='OBJECTID, NAME',
                                             returnGeometry=False)
                for dispatcher in dispatcherResults:
                    dispatcherNameToID[dispatcher.get_value(
                        'name')] = dispatcher.get_value('OBJECTID')

            if 'assignments' in itemData:
                features = []

                fl = FeatureLayer(url=itemData['assignments']['url'],
                                  securityHandler=fst.securityhandler,
                                  proxy_port=proxy_port,
                                  proxy_url=proxy_url,
                                  initialize=True)
                print(fl.deleteFeatures(where="1=1"))
                with open(csvPath) as csvfile:
                    reader = UnicodeDictReader(csvfile)
                    for row in reader:
                        json_string = {}
                        json_string['geometry'] = {}

                        centroidInfo = areaNameToID[row[areaCol].strip()]

                        json_string['geometry']['x'] = centroidInfo['x']
                        json_string['geometry']['y'] = centroidInfo['y']
                        json_string['attributes'] = {}
                        json_string['attributes']['workerId'] = workerNameToID[
                            row[workerCol].strip()]
                        json_string['attributes']['description'] = row[
                            descriptionCol]
                        json_string['attributes']['notes'] = row[notesCol]
                        json_string['attributes'][
                            'assignmentType'] = assignmentType
                        json_string['attributes']['status'] = status
                        json_string['attributes'][
                            'dispatcherId'] = dispatcherNameToID[
                                row[supervisorCol].strip()]

                        features.append(Feature(json_string=json_string))
                    results = fl.addFeature(features=features)

                    if 'error' in results:
                        print("Error in response from server:  %s" %
                              results['error'])

                    else:
                        if results['addResults'] is not None:
                            featSucces = 0
                            for result in results['addResults']:
                                if 'success' in result:
                                    if result['success'] == False:
                                        if 'error' in result:
                                            print("Error info: %s" %
                                                  (result['error']))
                                    else:
                                        featSucces = featSucces + 1

                            print("%s features added to %s" %
                                  (featSucces, fl.name))
                        else:
                            print("0 features added to %s /n result info %s" %
                                  (fl.name, str(results)))

    except (common.ArcRestHelperError) as e:
        print("error in function: %s" % e[0]['function'])
        print("error on line: %s" % e[0]['line'])
        print("error in file name: %s" % e[0]['filename'])
        print("with error message: %s" % e[0]['synerror'])
        if 'arcpyError' in e[0]:
            print("with arcpy message: %s" % e[0]['arcpyError'])

    except:
        line, filename, synerror = trace()
        print("error on line: %s" % line)
        print("error in file name: %s" % filename)
        print("with error message: %s" % synerror)
예제 #39
0
from arcrest.agol import FeatureLayer

if __name__ == "__main__":
    username = "******"
    password = "******"
    urls = ["url to layer","url to layer"]
    proxy_port = None
    proxy_url = None
    
    agolSH = AGOLTokenSecurityHandler(username=username,
                                      password=password)
    
    for url in urls:
        fl = FeatureLayer(
            url=url,
            securityHandler=agolSH,
            proxy_port=proxy_port,
            proxy_url=proxy_url,
            initialize=True)
        adminFl = fl.administration
        fieldToAdd = {
    
            "fields" : [
                {
                    "name" : "CommonField2",
                    "type" : "esriFieldTypeString",
                    "alias" : "Common Field 2",
                    "sqlType" : "sqlTypeOther", "length" : 50,
                    "nullable" : True,
                    "editable" : True,
                    "domain" : None,
                    "defaultValue" : None
    def DeleteFeaturesFromFeatureLayer(self,url,sql,chunksize=0):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            totalDeleted = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print (qRes)
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return  {'success':True,'message': "No features matched the query"}

                    i = 0
                    print ("%s features to be deleted" % total)
                    while(i <= len(oids)):
                        oidsDelete = ','.join(str(e) for e in oids[i:i+chunksize])
                        if oidsDelete == '':
                            continue
                        else:
                            results = fl.deleteFeatures(objectIds=oidsDelete)
                        if 'deleteResults' in results:
                            totalDeleted += len(results['deleteResults'])
                            print ("%s%% Completed: %s/%s " % (int(totalDeleted / float(total) *100), totalDeleted, total))
                            i += chunksize
                        else:
                            print (results)
                            return {'success':True,'message': "%s deleted" % totalDeleted}
                    qRes = fl.query(where=sql, returnIDsOnly=True)
                    if 'objectIds' in qRes:
                        oids = qRes['objectIds']
                        if len(oids)> 0 :
                            print ("%s features to be deleted" % len(oids))
                            results = fl.deleteFeatures(where=sql)
                            if 'deleteResults' in results:
                                totalDeleted += len(results['deleteResults'])
                                return  {'success':True,'message': "%s deleted" % totalDeleted}
                            else:
                                return results
                    return  {'success':True,'message': "%s deleted" % totalDeleted}

                else:
                    print (qRes)
            else:
                results = fl.deleteFeatures(where=sql)
                if results is not None:
                    if 'deleteResults' in results:
                        return  {'success':True,'message': totalDeleted + len(results['deleteResults'])}
                    else:
                        return results

        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "DeleteFeaturesFromFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
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()
예제 #42
0
from __future__ import print_function
from arcrest.security import AGOLTokenSecurityHandler
from arcrest.agol import FeatureLayer

if __name__ == "__main__":
    username = "******"
    password = "******"
    url = "<URL to Feature Layer>"
    proxy_port = None
    proxy_url = None

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

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

    print (fl.query(where="1=1",out_fields='*',returnGeometry=False) )
    def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass,chunksize=0,lowerCaseFieldNames=False):
        if arcpyFound == False:
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": inspect.currentframe().f_back.f_lineno,
                "filename":  'featureservicetools',
                "synerror": "ArcPy required for this function"
            })
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)

            if chunksize > 0:
                fc = os.path.basename(pathToFeatureClass)
                inDesc = arcpy.Describe(pathToFeatureClass)
                oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,inDesc.oidFieldName)

                arr = arcpy.da.FeatureClassToNumPyArray(pathToFeatureClass, (oidName))
                syncSoFar = 0
                messages = {'addResults':[],'errors':[]}
                total = len(arr)
                errorCount = 0
                if total == '0':
                    print ("0 features in %s" % pathToFeatureClass)
                    return "0 features in %s" % pathToFeatureClass
                print ("%s features in layer" % (total))

                arcpy.env.overwriteOutput = True
                if int(total) < int(chunksize):
                    return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
                else:
                    newArr = chunklist(arr,chunksize)
                    exprList = ["{0} >= {1} AND {0} <= {2}".format(oidName, nArr[0][0], nArr[len(nArr)-1][0])
                        for nArr in newArr]
                    for expr in exprList:

                        UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                        #print(arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0) + " features in the chunk")
                        results = fl.addFeatures(fc=UploadLayer,lowerCaseFieldNames=lowerCaseFieldNames)
                        chunkCount = arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0)
                        print(chunkCount + " features in the chunk")
                        if chunkCount > 0:

                            if results is not None and 'addResults' in results and results['addResults'] is not None:
                                featSucces = 0
                                for result in results['addResults']:
                                    if 'success' in result:
                                        if result['success'] == False:
                                            if 'error' in result:
                                                errorCount  = errorCount + 1
                                                print ("\tError info: %s" % (result))

                                        else:
                                            featSucces = featSucces + 1
                                syncSoFar = syncSoFar + featSucces
                                print ("%s features added in this chunk" % (featSucces))
                                print ("%s/%s features added, %s errors" % (syncSoFar,total,errorCount ))
                                if 'addResults' in messages:
                                    messages['addResults'] = messages['addResults'] + results['addResults']

                                else:
                                    messages['addResults'] = results['addResults']

                            else:
                                messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #44
0
    def QueryAllFeatures(self,url,sql,chunksize=0,saveLocation="",outName=""):
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)
            totalQueried = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if 'error' in qRes:
                    print qRes
                    return qRes
                elif 'objectIds' in qRes:
                    oids = qRes['objectIds']
                    total = len(oids)
                    if total == 0:
                        return  {'success':'true','message': "No features matched the query"}
                        
                    minId = min(oids)
                    maxId = max(oids)
                   
                    i = 0
                    print "%s features to be downloaded" % total
                    combinedResults = None
                                                            
                    while(i <= len(oids)):
                        oidsQuery = ','.join(str(e) for e in oids[i:i+chunksize])
                        if oidsQuery == '':
                            continue
                        else:
                            results = fl.query(objectIds=oidsQuery,
                                               returnGeometry=True,
                                               out_fields='*')
                            if isinstance(results,FeatureSet):
                                if combinedResults is None:
                                    combinedResults = results
                                else:
                                
                                    for feature in results.features:
                                    
                                        combinedResults.features.append(feature)
                             
                                totalQueried += len(results.features)
                                
                                print "%s%% Completed: %s/%s " % (int(totalQueried / float(total) *100), totalQueried, total)
                                i += chunksize                            
                            else:
                                print results
                  
                    print combinedResults.save(saveLocation=saveLocation, outName=outName)
                else:
                    print qRes
            else:
                return  fl.query(where=sql, 
                                 returnFeatureClass=True,
                                 returnGeometry=True,
                                 out_fields='*',                               
                                 out_fc=os.path.join(saveLocation,outName)
                                 )
            
       
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "QueryFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()            
    def RemoveAndAddFeatures(self, url, pathToFeatureClass,id_field,chunksize=1000):
        fl = None

        try:
            if arcpyFound == False:
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror": "ArcPy required for this function"
                })
            arcpy.env.overwriteOutput = True
            tempaddlayer= 'ewtdwedfew'
            if not arcpy.Exists(pathToFeatureClass):
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror": "%s does not exist" % pathToFeatureClass
                     }
                    )

            fields = arcpy.ListFields(pathToFeatureClass,wild_card=id_field)
            if len(fields) == 0:
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror": "%s field does not exist" % id_field
                })
            strFld = True
            if fields[0].type != 'String':
                strFld = False

            fl = FeatureLayer(
                    url=url,
                    securityHandler=self._securityHandler)

            id_field_local = arcpy.AddFieldDelimiters(pathToFeatureClass, id_field)
            idlist = []
            print( arcpy.GetCount_management(in_rows=pathToFeatureClass).getOutput(0) + " features in the layer")
            with arcpy.da.SearchCursor(pathToFeatureClass, (id_field)) as cursor:
                allidlist = []

                for row in cursor:

                    if (strFld):
                        idlist.append("'" + row[0] +"'")
                    else:
                        idlist.append(row[0])
                    if len(idlist) >= chunksize:
                        allidlist.append(idlist)
                        idlist = []

                if len(idlist) > 0:
                    allidlist.append(idlist)
                for idlist in allidlist:
                    idstring = ' in (' + ','.join(idlist) + ')'
                    sql = id_field + idstring
                    sqlLocalFC = id_field_local + idstring
                    results = fl.deleteFeatures(where=sql,
                                                rollbackOnFailure=True)

                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function": "RemoveAndAddFeatures",
                            "line": inspect.currentframe().f_back.f_lineno,
                            "filename":  'featureservicetools',
                            "synerror":results['error']
                        })
                    elif 'deleteResults' in results:
                        print ("%s features deleted" % len(results['deleteResults']))
                        for itm in results['deleteResults']:
                            if itm['success'] != True:
                                print (itm)
                    else:
                        print (results)

                    arcpy.MakeFeatureLayer_management(pathToFeatureClass,tempaddlayer,sqlLocalFC)
                    results = fl.addFeatures(fc=tempaddlayer)

                    if 'error' in results:
                        raise common.ArcRestHelperError({
                            "function": "RemoveAndAddFeatures",
                            "line": inspect.currentframe().f_back.f_lineno,
                            "filename":  'featureservicetools',
                            "synerror":results['error']
                        })
                    elif 'addResults' in results:
                        print ("%s features added" % len(results['addResults']))
                        for itm in results['addResults']:
                            if itm['success'] != True:
                                print (itm)
                    else:
                        print (results)
                    idlist = []
            if 'error' in results:
                raise common.ArcRestHelperError({
                    "function": "RemoveAndAddFeatures",
                    "line": inspect.currentframe().f_back.f_lineno,
                    "filename":  'featureservicetools',
                    "synerror":results['error']
                })
            else:
                print (results)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "create_report_layers_using_config",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:

            gc.collect()
예제 #46
0
    def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass,chunksize=0,lowerCaseFieldNames=False):
        if arcpyFound == False:
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": inspect.currentframe().f_back.f_lineno,
                "filename":  'featureservicetools',
                "synerror": "ArcPy required for this function"
            })
        fl = None
        try:
            fl = FeatureLayer(
                   url=url,
                   securityHandler=self._securityHandler)

            if chunksize > 0:
                messages = {'addResults':[]}
                total = arcpy.GetCount_management(pathToFeatureClass).getOutput(0)
                if total == '0':
                    print ("0 features in %s" % pathToFeatureClass)
                    return "0 features in %s" % pathToFeatureClass
                arcpy.env.overwriteOutput = True
                if int(total) < int(chunksize):
                    return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
                else:
                    inDesc = arcpy.Describe(pathToFeatureClass)
                    oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,inDesc.oidFieldName)
                    sql = '%s = (select min(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                    cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                    minOID = cur.next()[0]
                    del cur, sql
                    sql = '%s = (select max(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass))
                    cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql)
                    maxOID = cur.next()[0]
                    del cur, sql
                    breaks = range(minOID,maxOID)[0:-1:chunksize]
                    breaks.append(maxOID+1)
                    exprList = [oidName + ' >= ' + str(breaks[b]) + ' and ' + \
                                oidName + ' < ' + str(breaks[b+1]) for b in range(len(breaks)-1)]
                    for expr in exprList:
                        UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0)
                        result = fl.addFeatures(fc=UploadLayer,lowerCaseFieldNames=lowerCaseFieldNames)
                        if messages is None:
                            messages = result
                        else:
                            if result is not None and 'addResults' in result:
                                if 'addResults' in messages:
                                    messages['addResults'] = messages['addResults'] + result['addResults']
                                    print ("%s/%s features added" % (len(messages['addResults']),total))
                                else:
                                    messages['addResults'] = result['addResults']
                                    print ("%s/%s features added" % (len(messages['addResults']),total))
                            else:
                                messages['errors'] = result
                return messages
            else:
                return fl.addFeatures(fc=pathToFeatureClass,lowerCaseFieldNames=lowerCaseFieldNames)
        except arcpy.ExecuteError:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                "function": "AddFeaturesToFeatureLayer",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                "arcpyError": arcpy.GetMessages(2),
            }
                           )
        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError({
                        "function": "AddFeaturesToFeatureLayer",
                        "line": line,
                        "filename":  filename,
                        "synerror": synerror,
                                        }
                                        )
        finally:
            fl = None

            del fl

            gc.collect()
    securityinfo['proxy_url'] = proxy_url
    securityinfo['proxy_port'] = proxy_port
    securityinfo['referer_url'] = None
    securityinfo['token_url'] = None
    securityinfo['certificatefile'] = None
    securityinfo['keyfile'] = None
    securityinfo['client_id'] = None
    securityinfo['secret_id'] = None   
   
    shh = securityhandlerhelper.securityhandlerhelper(securityinfo=securityinfo)
    if shh.valid == False:
        print shh.message
    else:
        fl= FeatureLayer(
            url=url,
            securityHandler=shh.securityhandler,
            proxy_port=proxy_port,
            proxy_url=proxy_url,
            initialize=True)
        
        features = []
        json_string={'geometry': 
                        {
                            'y': 1885855.2531960313, 
                            'x': 1034495.0035156211}
                        ,
                     'attributes': 
                     {  'NAME': 'NameChange', 
                        'OBJECTID': 1
                     }}

        features.append(Feature(json_string=json_string))
예제 #48
0
from arcrest.security import AGOLTokenSecurityHandler
from arcrest.agol import FeatureLayer

if __name__ == "__main__":
    username = "******"
    password = "******"
    url = "<URL to Feature Layer>"
    proxy_port = None
    proxy_url = None
    
    agolSH = AGOLTokenSecurityHandler(username=username,
                                      password=password)
    
    fl = FeatureLayer(
        url=url,
        securityHandler=agolSH,
        proxy_port=proxy_port,
        proxy_url=proxy_url,
        initialize=True)

    print fl.query(where="1=1",out_fields='*',returnGeometry=False) 
   
예제 #49
0
output = "C:\\TxDOT\\Scripts\\javascript\\Guardrail\\Snake\\BACKUP"

if __name__ == "__main__":
    username = "******"
    password = mohawk.hangnail(username)
    url = "http://services.arcgis.com/KTcxiTD9dsQw4r7Z/arcgis/rest/services/OnSystemRoadways/FeatureServer/0"
    proxy_port = None
    proxy_url = None

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

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

    qmin = 0
    qmax = 2000
    count = fl.query(returnIDsOnly=True)
    cList = count["objectIds"]
    cValue = cList[-1]
    listLen = len(cList)
    print listLen

    now = datetime.datetime.now()
    curMonth = now.strftime("%m")
    curDay = now.strftime("%d")
    curYear = now.strftime("%Y")
예제 #50
0
    def DeleteFeaturesFromFeatureLayer(self, url, sql, chunksize=0):
        fl = None
        try:
            fl = FeatureLayer(url=url, securityHandler=self._securityHandler)
            totalDeleted = 0
            if chunksize > 0:
                qRes = fl.query(where=sql, returnIDsOnly=True)
                if "error" in qRes:
                    print qRes
                    return qRes
                elif "objectIds" in qRes:
                    oids = qRes["objectIds"]
                    total = len(oids)
                    if total == 0:
                        return {"success": "true", "message": "No features matched the query"}

                    minId = min(oids)
                    maxId = max(oids)

                    i = 0
                    print "%s features to be deleted" % total
                    while i <= len(oids):
                        oidsDelete = ",".join(str(e) for e in oids[i : i + chunksize])
                        if oidsDelete == "":
                            continue
                        else:
                            results = fl.deleteFeatures(objectIds=oidsDelete)
                        if "deleteResults" in results:
                            totalDeleted += len(results["deleteResults"])
                            print "%s%% Completed: %s/%s " % (
                                int(totalDeleted / float(total) * 100),
                                totalDeleted,
                                total,
                            )
                            i += chunksize
                        else:
                            print results
                            return {"success": "true", "message": "%s deleted" % totalDeleted}
                    qRes = fl.query(where=sql, returnIDsOnly=True)
                    if "objectIds" in qRes:
                        oids = qRes["objectIds"]
                        if len(oids) > 0:
                            print "%s features to be deleted" % len(oids)
                            results = fl.deleteFeatures(where=sql)
                            if "deleteResults" in results:
                                totalDeleted += len(results["deleteResults"])
                                return {"success": "true", "message": "%s deleted" % totalDeleted}
                            else:
                                return results
                    return {"success": "true", "message": "%s deleted" % totalDeleted}

                else:
                    print qRes
            else:
                results = fl.deleteFeatures(where=sql)
                if "deleteResults" in results:
                    return {"success": "true", "message": totalDeleted + len(results["deleteResults"])}
                else:
                    return results

        except:
            line, filename, synerror = trace()
            raise common.ArcRestHelperError(
                {"function": "DeleteFeaturesFromFeatureLayer", "line": line, "filename": filename, "synerror": synerror}
            )
        finally:
            fl = None

            del fl

            gc.collect()
예제 #51
0
    securityinfo['proxy_url'] = proxy_url
    securityinfo['proxy_port'] = proxy_port
    securityinfo['referer_url'] = None
    securityinfo['token_url'] = None
    securityinfo['certificatefile'] = None
    securityinfo['keyfile'] = None
    securityinfo['client_id'] = None
    securityinfo['secret_id'] = None

    shh = securityhandlerhelper.securityhandlerhelper(securityinfo=securityinfo)
    if shh.valid == False:
        print shh.message
    else:
        fl= FeatureLayer(
            url=url,
            securityHandler=shh.securityhandler,
            proxy_port=proxy_port,
            proxy_url=proxy_url,
            initialize=True)

        out_fields = ['objectid']
        for fld in fieldInfo:
            out_fields.append(fld['FieldName'])

        resFeats = fl.query(where=sql,
                            out_fields=",".join(out_fields))
        for feat in resFeats:

            for fld in fieldInfo:
                feat.set_value(fld["FieldName"],fld['ValueToSet'])

        print fl.updateFeature(features=resFeats)
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()