Esri / ArcREST

python package for REST API (AGS, AGOL, webmap JSON, etc..)
Apache License 2.0
192 stars 155 forks source link

Adding support for pagination in queries #148

Closed phpmaps closed 8 years ago

phpmaps commented 8 years ago

Including resultOffset and resultRecordCount in FeatureLayer query. The advancedQueryCapabilities will need to include supportPagination = true for pagination to work.

phpmaps commented 8 years ago

Hi @achapkowski i see you did a direct checkin today to this repo? Do you know who can help merge this ArcREST API update? Let me know. Cheers!

phpmaps commented 8 years ago

@achapkowski - yep I can do that.

phpmaps commented 8 years ago

@achapkowski - ok this is done. can you let me know if you merge? if you do merge, the gain is that people will see pagination as an option. in my own use case I am using pagination when the service supports it and fallback to this helper if it does not.

countResult = self.featureLayer.query(returnCountOnly=True) #Get the count
returnRecordCount = 1000 # fl.maxRecordCount might lead to memory issues, so divide by 1000 to make smaller lists
totalQueried = 0
feautureClasses = []

try:
    if(self.featureLayer.advancedQueryCapabilities != None):
        if(self.featureLayer.advancedQueryCapabilities['supportsPagination']):
            supportsPagination = True
        else:
            supportsPagination = False
    else:
        supportsPagination = False

    if(supportsPagination == True):
        iterations =  countResult['count'] / returnRecordCount + 1
        print('Total # of features: %i' % countResult['count'])
        for chunk in range(0,iterations):
            offset = chunk * returnRecordCount
            featureSet = self.featureLayer.query(where="1=1",out_fields=outfields,returnGeometry=True,returnFeatureClass=False, resultOffset=offset, resultRecordCount=returnRecordCount)
            feautureClasses.append("{}{}".format("temp",chunk))
            featureSet.save(temp_fgdb,"{}{}".format("temp",chunk))
            totalQueried += len(featureSet.features)
            print("{:.0%} Completed: {}/{}".format(totalQueried / float(countResult['count']), totalQueried, countResult['count']))
    else:
        qRes = self.featureLayer.query(where="1=1", returnIDsOnly=True)
        oids = qRes['objectIds']
        total = len(oids)
        print('Total # of features: %i' % total)
        chunkCounter = 0
        for chunk in chunklist(l=oids, n=returnRecordCount):
            oidsQuery = ",".join(map(str, chunk))
            featureSet = self.featureLayer.query(objectIds=oidsQuery,returnGeometry=True,out_fields=outfields)
            feautureClasses.append("{}{}".format("temp",chunkCounter))
            featureSet.save(temp_fgdb,"{}{}".format("temp",chunkCounter))
            totalQueried += len(featureSet.features)
            print("{:.0%} Completed: {}/{}".format(totalQueried / float(total), totalQueried, total))
            chunkCounter = chunkCounter + 1
except:
    line, filename, synerror = trace()
    raise ArcRestHelperError({
                "function": "random_string_generator",
                "line": line,
                "filename":  filename,
                "synerror": synerror,
                                }
                                )
finally:
    pass
achapkowski commented 8 years ago

@phpmaps - merged. Thank you