arcapi - Convenient API for arcpy

1919
11
02-27-2014 01:02 PM
FilipKrál
Occasional Contributor III
Hello,
Sometimes with arcpy I feel that to do little you need to write a lot. To make my life easier, I have written and documented a collection of functions, which are available as 'arcapi' on github:

https://github.com/NERC-CEH/arcapi

You are more than welcome to use it and, even better, contribute - by adding new code or just providing feedback and ideas.

To point out only a few functions: 'plot' makes a scatter plot, 'chart' makes a quick map, 'head' prints first rows of a table into the console... the idea is to do a lot with simple function calls.
Please see the README.md and arcpy_tutorial.py for more details.

I'm sure many of you guys have similar personal libraries, or you wish there was an easy way of coding something.
Do you... do you want to share?

Filip.
Tags (2)
0 Kudos
11 Replies
ClintDow
Occasional Contributor
Nice, I'm interested. I'll be checking this out.. sometime.. when I have some spare hours.
0 Kudos
TreyCundall
New Contributor
Very cool!

I actually just started working on a similar codebase with a GIS professor up in Alaska but you're way ahead of us. I'm starting to play around with your API and it seems like a great starting point for creating some tools to make sense of the chaos that is arcpy.

I'm also running 10.2.1 so I can help test to make sure everything works in that version too (everything should, right? but you never know).

Thanks for sharing this.
0 Kudos
ChrisSnyder
Regular Contributor III
Beautiful! I built something similar (but definitely not as fancy) a while back... like your arcapi, my lmpy basically made python/arcpy more of a command prompt sort of interface (for those that like that sort of thing).

Hope to contribute to your project sometime... if I ever have time again to do that sort of thing :rolleyes:

Good job Filip and Caleb!
0 Kudos
by Anonymous User
Not applicable
Beautiful! I built something similar (but definitely not as fancy) a while back... like your arcapi, my lmpy basically made python/arcpy more of a command prompt sort of interface (for those that like that sort of thing).

Hope to contribute to your project sometime... if I ever have time again to do that sort of thing :rolleyes:

Good job Filip and Caleb!


Chris,

You should definitely contribute....Much of my 'bag of tricks' has been inspired by some of your code samples...As for arcapi, I can't take much credit.  Filip is the one who got it off the ground and built 98% of it.  I just added some of the functions at the bottom (remap and raster processing stuff).  I still need to provide some testing samples though.

I need to browse through my library and hope to add more in the future.  This is a great idea and I encourage others to contribute as well!
0 Kudos
ChrisSnyder
Regular Contributor III
I guess this shows my ignorance of the internet today, but if we have code/ideas how would it be best to communicate that with you fellows? I'm sure there's some fancy github way of doing all that... Sadly I'm pretty ignorant of that and stuck in the internet of like 2003 for some reason.

Looks like these functions (and frankly, much better/fancier ones!) are already in your arcapi library, but for what it's worth comparing independent invention and all that, here's a few of mine I use all the time... I'd love to contribute some time to ideas/code sometime. Send me an email: chris.snyder( at ) dnr.wa.gov

def listFields(inputTable, wildCard = "", fieldType = ""):
    """Lists fields (and selected properties) for inputTable"""
    fieldList = arcpy.ListFields(inputTable, wildCard, fieldType)
    #FORMATTING INFO: NAME = 50 spaces, TYPE = 15 spaces, LENGTH = 15 spaces, SCALE = 15 spaces, PRECISION = 15 spaces 
    print "NAME                                              " + "TYPE           " + "LENGTH         " + "SCALE          " + "PRECISION      "
    print "-" * 100
    for field in fieldList:
        print str(field.name)[0:50] + " "*(50-len(field.name)) \
                  + str(field.type)[0:15] + " "*(15-len(str(field.type))) \
                  + str(field.length)[0:15] + " "*(15-len(str(field.length))) \
                  + str(field.scale)[0:15] + " "*(15-len(str(field.scale))) \
                  + str(field.precision)[0:15] + " "*(15-len(str(field.precision)))

def listRecs(inputTable, numberOfRecordsToList = 25, whereClause = ""):
    """Lists field names and corresponding field values in inputTable"""
    fieldNamesList = [f.name for f in arcpy.ListFields(inputTable)]
    rowCount = 1
    recordCount = int(arcpy.GetCount_management(inputTable).getOutput(0))
    if int(numberOfRecordsToList) > recordCount:
        numberOfRecordsToList = recordCount
    searchRows = arcpy.da.SearchCursor(inputTable, ["*"], whereClause)
    searchRow = searchRows.next()
    while rowCount <= int(numberOfRecordsToList):
        print "RECORD #" + str(rowCount)
        print "-" * 50
        for fieldName in fieldNamesList:
            try:
                print fieldName + ": " + str(searchRow[fieldNamesList.index(fieldName)])
            except:
                print fieldName + ": !?!" 
        print ""
        print ""
        rowCount = rowCount + 1
        searchRow = searchRows.next()
    del searchRow, searchRows
    print ""
    print ""
    print "LISTED " + str(numberOfRecordsToList) + " RECORDS"  
0 Kudos
FilipKrál
Occasional Contributor III
Hello and thanks for such a great support!
I am so glad to see that you are on board with this and that some of you have already contributed. Great stuff!

The best way to contribute code is exactly how Caleb did - using github. That is of course if you are already familiar with github (or have time and will to explore it).

I totally understand you might not want to (or don't have time to) learn about github. In that case we can:

  • post ideas and code into this thread

  • post links to other discussions, blog posts, sites etc. that have some specific and (almost) ready to use code

  • send emails to contact( at )filipkral.com, although I would prefer github and this thread to keep it open for everyone.



In any case, I believe it is clear (and I'll clarify this in the README) that by contributing code to arcapi you are releasing the code under the Lesser General Public License v3.

To point out just 3 things I think are awesome about github:

  • It is a version control system which makes managing changes easy.

  • It allows discussions about very specific bits of code.

  • It is quite transparent who contributed and to what extent.



Last thing to say at this point: it seems I'm able to review any new contributions about once a weak. If I don't react straight away, it's because I don't want to rush the code too much, but eventually I'll reply. So Chris, I'll consider your functions soon, thanks.

Keep it coming!
Filip.
0 Kudos
ChrisSnyder
Regular Contributor III
Okay I watched a YouTube thing, so I now should theoretically have enough info to submit stuff directly via github myself... I'll get an account soon!

BTW: Those functions I showed probably shouldn't be included since I think you already have some that do very similar things in your arcapi... Just demonstrating that many of us (at least me) have created similar arcpy-based libraries... although as far as I know, not shared/created them as a cooperative process. I've been meaning to update my stuff for a long time, which is really a hodgepodge... hope to have some time in a few weeks. On a similar note, I've always thought it'd be cool to have a single "collaboratively built" toolbox crammed full of all sorts of neat geoprocesisng script tools.... Kind of like a analysis tools for analysts sort of thing. Wonder if that could work via github at some point too?

Anyway - Good job!!!
0 Kudos
ClintDow
Occasional Contributor
For anyone interested in learning how to use git/github, check out the free course at:
https://www.coursera.org/course/datascitoolbox

The next session begins in April.
0 Kudos
MathewCoyle
Frequent Contributor
Very nice, I'll see if there is anything I can contribute that isn't already included. Just to note however, try to ensure we follow PEP8 and Python 3 forward compatibility when possible.
0 Kudos