Last active
March 20, 2017 15:54
-
-
Save andygup/fc9a9b0bf9ddd6e735aa6a86fa40013d to your computer and use it in GitHub Desktop.
Pubnub block for running an ArcGIS demographic analysis request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| This ArcGIS-based pubnub block shows a pattern for running powerful demographic data searches. | |
| Declare the Event Handler with the export syntax. The incoming message is called request. | |
| The request object can either be a single point or a polygon. Here is an example, 'y' is Latitude | |
| and 'x' is Longitude: | |
| { | |
| "x": -122.435, | |
| "y": 37.785 | |
| } | |
| */ | |
| export default (request) => { | |
| // Require XHR to make external calls | |
| const xhr = require("xhr"); | |
| // Require console to print debug information | |
| const console = require('console'); | |
| // Need a token? Create a free developer account at developers.arcgis.com | |
| // Information on auto-majically generating a token: http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#//02r3000000m5000000 | |
| const accessToken = "zg2BcLl9iliLBYLqR78GS2Nqv2P4Wp3F2NbOxNoGhYFmWHNsFOAGCUWre1EvbNI7Z5iHlkQC6LiixeDC5n4hLRFDtsgkY7lupNfOYlhBIT7_L1ZVTWvw1KPKfKguSWJ6k3Fk9EMF3xfaOFJ3TYLDqw.."; | |
| try { | |
| // NOTE: The manually created developer access token expires in 24 hours ;-) | |
| const http_options = { | |
| "method": "POST", | |
| "headers": { | |
| "Content-Type": "application/x-www-form-urlencoded" | |
| }, | |
| // For more info on variable names: http://doc.arcgis.com/en/esri-demographics/ | |
| "body":"f=json" + | |
| "&studyAreas=[{'geometry':{'x':" + request.message.lon + ",'y':" + request.message.lat + "}}]" + | |
| "&analysisvariables=['populationtotals.TOTPOP_CY','Wealth.MEDHINC_CY','5yearincrements.MEDAGE_CY']" + | |
| "&token=" + accessToken | |
| }; | |
| // ArcGIS REST API call | |
| // Documentation for the ArcGIS Geoenrichment service: https://developers.arcgis.com/rest/geoenrichment/api-reference/geoenrichment-service-overview.htm | |
| const url = "https://geoenrich.arcgis.com/arcgis/rest/services/World/geoenrichmentserver/GeoEnrichment/enrich"; | |
| return xhr.fetch(url, http_options).then((x) => { | |
| const body = JSON.parse(x.body); | |
| // Remove everything but attributes information to cleanup the console output | |
| // You can get a description of the attributes here: https://developers.arcgis.com/rest/geoenrichment/api-reference/get-variables.htm | |
| const totalPopulation = body.results[0].value.FeatureSet[0].features[0].attributes.TOTPOP_CY; | |
| const medianHouseholdIncome = body.results[0].value.FeatureSet[0].features[0].attributes.MEDHINC_CY; | |
| const medianAge = body.results[0].value.FeatureSet[0].features[0].attributes.MEDAGE_CY; | |
| console.log( | |
| "Total Population: " + totalPopulation + | |
| ", Median Household Income: " + medianHouseholdIncome + | |
| ", Median Age: " + medianAge | |
| ); | |
| request.message.data = { | |
| "totalPopulation":totalPopulation, | |
| "medianHouseholdIncome": medianHouseholdIncome, | |
| "medianAge": medianAge | |
| }; | |
| return request.ok(); | |
| }).catch((x) => { | |
| console.log("Exception in xhr request: " + x); | |
| }); | |
| } catch (e) { | |
| console.error('Uncaught exception:', e); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment