Wednesday, May 20, 2015

ArcREST Publishing Part 1 - CSV Files

One of the main purposes of ArcGIS.com is that is allows you to share information.  Sometime you want to quickly stage content, or you want to mirror your site in another location.

So in this post, I will show you how to do some publishing.  Publishers can create feature services as well as tiled map services.
Feature services can be created using input files of type csv, shapefile, serviceDefinition, featureCollection, and fileGeodatabase.

The AGOL/Portal REST has some great bullet points to remember when publishing items:
  • CSV files that contain location fields, (ie.address fields or X, Y fields) are spatially enabled during the process of publishing.
  • Shapefiles and file geodatabases should be packaged as *.zip files.
  • Tiled map services can be created from service definition (*.sd) files, tile packages, and existing feature services.
  • Service definitions are authored in ArcGIS for Desktop and contain both the cartographic definition for a map as well as its packaged data together with the definition of the geo-service to be created.
  • Use the Analyze operation to generate the default publishing parameters for CSVs.
Now that we have what we can publish, it is time to start coding!

This post will cover publishing CSV files.

CSV files require a bit more steps then the other publishing work flows because not only do you have to upload your data, you should analyze it in order to get a correct set of default publishing properties.  Luckily, ArcREST can ease the pain.

import arcrest

if __name__ == "__main__":
    username = "some account"
    password = "some password"
    csv_file = r"C:\Users\andr5624\Desktop\book1.csv"

    sh = arcrest.AGOLTokenSecurityHandler(username, password)
    admin = arcrest.manageorg.Administration(securityHandler=sh)
    content = admin.content
    usercontent = content.usercontent(username=username)
    ip = arcrest.manageorg.ItemParameter()
    ip.title = "Sample CSV"
    ip.type = "CSV"
    ip.tags = "tag1,tag3"
    res = usercontent.addItem(itemParameters=ip,
                              filePath=csv_file)
    itemId = res['id']

    #  Now you need to analyze the item to publish it
    #
    featureContent = content.featureContent
    analyzeParams = arcrest.manageorg.AnalyzeParameters()
    analyzeResult = featureContent.analyze(itemId=itemId, analyzeParameters=analyzeParams)
    #  Published based off the analyze function
    #
    if 'publishParameters' in analyzeResult:
        pp = analyzeResult['publishParameters']
        publishParams = arcrest.manageorg.PublishCSVParameters(name=pp['name'],
                                               locationType=pp['locationType'],
                                               layerInfo=pp['layerInfo'],
                                               latitudeFieldName=pp["latitudeFieldName"],
                                               longitudeFieldName=pp['longitudeFieldName'])
        print usercontent.publishItem(fileType="csv",
                                      publishParameters=publishParams,
                                      itemId=itemId)


Let's discuss the above code. Here we access AGOL and add an item for our user.  Once uploaded, the code, using the item id, analyzes the CSV to get a set of default publishing parameters.  These parameters are used to populate the PublishCSVParameters class in order to publish the CSV file correctly.

Pretty cool.  ArcREST masks a complex work flow into a couple of short lines of Python code.

No comments: